Re: [C++-sig] [python] Function objects in place of member functions

2009-10-11 Thread troy d. straszheim

Ravi wrote:
[snip]


In order to use a function object in place of a free function, one must 
specialize/overload

  boost::python::detail::get_signature
which, for some reason, does not account for function objects. Here's a very 
simple example that works:



[snip]


However, note that the overload of get_signature precedes the inclusion of the 
boost.python headers, which is extremely inconvenient. However, if the headers 
are moved to their proper location as in the following,



[snip]


Why is the overloaded get_signature not picked up when it is declared *after* 
the inclusion of the headers?




I'm not sure why it isn't picked up.  I've been working in this area, 
replacing most of detail/caller.hpp and detail/invoke.hpp with 
boost.fusion, seen here:


http://gitorious.org/~straszheim/boost/straszheim/blobs/python/boost/python/detail/caller.hpp

In the process, I overhauled get_signature to use boost::function_types, 
and to be a metafunction, not a function:


http://gitorious.org/~straszheim/boost/straszheim/blobs/python/boost/python/signature.hpp

The overall effect is a lot less preprocessor stuff to look around.

I have function objects (given a specialization of get_signature), and 
boost::function (only up to 3 arguments ATM) working,

here's the test (which passes):

/

#include 
#include 
#include 
#include 

namespace mpl = boost::mpl;

struct X { int y; };

struct FnObject
{
  typedef int result_type;

  int operator()(X *x, int z) const
  {
return z + x->y;
  }
};

int f1(X* x) { return x->y ; }
int f2(X* x, int i) { return x->y * i; }
int f3(X* x, int i, int j) { return x->y * i + j; }

namespace boost {
  namespace python {
namespace detail {

  template<>
  struct get_signature
  {
typedef mpl::vector type;
  };

}
  }
}

using namespace boost::python;

BOOST_PYTHON_MODULE( function_objects_ext )
{
  FnObject fobj;
  boost::function bf0(fobj);

  boost::function bf1(f1);
  boost::function bf2(f2);
  boost::function bf3(f3);

  boost::python::class_( "X" )
.def( "fobj", fobj)
.def( "bf0",  bf0)
.def( "bf1",  bf1)
.def( "bf2",  bf2)
.def( "bf3",  bf3)
.def_readwrite( "y", &X::y )
;
}

/

>>> from function_objects_ext import *
>>> x = X()
>>> x.y = 13
>>> x.fobj(12)
25
>>> x.bf1()
13
>>> x.bf2(2)
26
>>> x.bf3(2, -27)
-1

/

I'm fairly new to the internals of boost.python, and only just now got 
this working...  Do you see problems with this, specifically the 
conversion of get_signature from function to metafunction?


-t


___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] [python] Function objects in place of member functions

2009-10-11 Thread Ravi
On Sunday 11 October 2009 19:44:29 troy d. straszheim wrote:
> > Why is the overloaded get_signature not picked up when it is declared
> > after  the inclusion of the headers?
> 
> I'm not sure why it isn't picked up.

Does that mean that you can reproduce the problem I pointed out?

> I've been working in this area, 
> replacing most of detail/caller.hpp and detail/invoke.hpp with 
> boost.fusion, seen here:
> 
> http://gitorious.org/~straszheim/boost/straszheim/blobs/python/boost/python
> /detail/caller.hpp
> 
> In the process, I overhauled get_signature to use boost::function_types, 
> and to be a metafunction, not a function:
> 
> http://gitorious.org/~straszheim/boost/straszheim/blobs/python/boost/python
> /signature.hpp

IMHO, this is the right way to do it. This avoids relying on the compiler to 
optimize out all the ugly tag-dispatching. Of course, Dave A & Ralf WGK did 
not have function types when they wrote this originally.

[snip]

>boost::function bf0(fobj);

Why do you need to use boost::function here? Shouldn't the type be deduced 
automatically?

> I'm fairly new to the internals of boost.python, and only just now got 
> this working...  Do you see problems with this, specifically the 
> conversion of get_signature from function to metafunction?

I don't see any problems with the conversion of get_signature to a 
metafunction. Do compile times get any longer?

Regards,
Ravi

___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


[C++-sig] Boost.python: extracting an array

2009-10-11 Thread Randolph Fritz
Is there any way to extract a vector of floats from a python array?

In Python, can I write something like:
  from array import array
  from bpclass import foo

  vec = array('f', (1,2,3))
  foo ([vec])

And, in C++:
  void bpclass::foo (list v) {
... extract< {magic} >(v[0]) ...

Or would it be better to replace the array with a list?
-- 
Randolph Fritz
  design machine group, architecture department, university of washington
[email protected] -or- [email protected]

___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig