How do I wrap a static member function?

View: New views
3 Messages — Rating Filter:   Alert me  

How do I wrap a static member function?

by James Amundson-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I have the following code:

-------------------------------------------------------------------------------
#include <boost/python.hpp>
#include <iostream>

class Foo
{
public:
     Foo() {} ;
     static void doit(const char * message) {
         std::cout << "doing it statically: " << message << "\n";
     };
     void nonstatic_doit(const char *message) {
         std::cout << "doing it nonstatically: " << message << "\n";
     };
};

using namespace boost::python;

BOOST_PYTHON_MODULE(foo)
{
     class_<Foo>("Foo",init<>())
     .def("doit", &Foo::doit)
     .def("nonstatic_doit", &Foo::nonstatic_doit)
     ;
}
-------------------------------------------------------------------------------

When I try out my foo module, I see this:

In [1]: from foo import Foo

In [2]: f = Foo()

In [3]: f.nonstatic_doit("hello")
doing it nonstatically: hello

In [4]: f.doit("hello")
---------------------------------------------------------------------------
ArgumentError                             Traceback (most recent call last)

/home/amundson/work/synergia2-devel_1_0/build/synergia2/components/foundation/<ipython
console> in <module>()

ArgumentError: Python argument types in
     Foo.doit(Foo, str)
did not match C++ signature:
     doit(char const*)


-------------------------------------------------------------------------------

How can I wrap doit?

Thanks,
Jim Amundson

_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@...
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: How do I wrap a static member function?

by Ravi-41 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Use class_.staticmethod(...) as shown below:

On Friday 30 October 2009 17:47:45 James Amundson wrote:
> BOOST_PYTHON_MODULE(foo)
> {
>      class_<Foo>("Foo",init<>())
>      .def("doit", &Foo::doit)

       .staticmethod( "doit" )

>      .def("nonstatic_doit", &Foo::nonstatic_doit)
>      ;
> }
 
Regards,
Ravi

_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@...
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: How do I wrap a static member function?

by James Amundson-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 10/30/2009 10:43 PM, Ravi wrote:
> Use class_.staticmethod(...) as shown below:
>
>    
Thanks. That's what I needed.

Best,
Jim Amundson
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@...
http://mail.python.org/mailman/listinfo/cplusplus-sig