473,508 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ to python with boost.python: Exposing virtual bool operator()(int)=0

Hello pythonians! ;-D ,

I have a little problem when I expose (assisted by boost.python) classes
with virtual functions, specially with operator().
In the C++ code below I test two different implementations of a member
function of A that
takes as an argument the abstract class Base (see Option 1 / Option 2):
- Both compile without problems
- Only the second works in python (see python program below).

It seems like if the problem is in the way I expose operator()... but I
do not understand it very well
Any clue about why this is happening?I would appreciate any comment on this.
Thank you very much in advance. :-D

Pedro.
//.................................................. .....
//................ C++ code......................
//.................................................. .....
#include <boost/python.hpp>
#define OPTION1
//#undef OPTION1

struct Base
{
virtual ~Base() {}
virtual int f() = 0; // Like in
Boost.python tutorial
virtual int g(int) = 0; // With 1 argument
virtual bool operator()(int ii) =0; // __call__ - type function
};

class A{
private:
int v_;
public: A(int v): v_(v){}
#ifdef OPTION1
/* Option 1 */ int fun(Base& c) const { return
c(v_)?(c.g(v_)+c.f()):33; }
#else
/* Option 2 */ int fun(Base& c) const { return (c.g(v_)+c.f()); }
#endif
};

//:::::::::::::::::::::::::::BOOST::PYTHON in
action:::::::::::::::::::::::::::
using namespace boost::python;

struct BaseWrap : Base, wrapper<Base>
{
int f(){return call<int>(this->get_override("f").ptr());}
int g(int ii){return call<int>(this->get_override("g").ptr(),ii);}
bool operator()(int ii){ return
call<bool>(this->get_override("operator()").ptr(),ii);}
};

BOOST_PYTHON_MODULE(test2py)
{
class_<A>("A",init<int>())
.def("fun",&A::fun);

class_<BaseWrap, boost::noncopyable>("Base")
.def("f", pure_virtual(&Base::f))
.def("g", pure_virtual(&Base::g))
.def("__call__", pure_virtual(&Base::operator())) ;
};
//.................................................. .....
//................ Python code...................
//.................................................. .....

from test2py import *
## Extension of C++ class in python
class Derived(Base):
def f(self):
return 44
def g(self,n):
return n
def __call__(self, v):
return (v<23)
d=Derived()
print d.f()
print d.g(3)
print "is 20<23 and 24<23", d(20), d(24)
a=A(20)
print a.fun(d)

b=A(24)
print b.fun(d)
//.................................................. ....................
//................ Executing Python code..................
//.................................................. ...................
With Option 1 IS NOT WORKING!!
...
int fun(Base& c) const { return c(v_)?(c.g(v_)+c.f()):33; }
...
44
3
is 20<23 and 24<23 True False
Traceback (most recent call last):
File "d:\My_Documents\src\pysource\test.py", line 29, in ?
print a.fun(d)
TypeError: 'NoneType' object is not callable
With Option 2 IS WORKING!!
...
int fun(Base& c) const { return (c.g(v_)+c.f()); }
...
44
3
is 20<23 and 24<23 True False
64
68

Mar 29 '06 #1
0 1744

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2477
by: Bill Zhao | last post by:
Dear all, Sorry for i did not paste original propreity codes See below the fake codes for what I cannot understand. =============================================== lass A { public: virtual...
4
2352
by: Alex Vinokur | last post by:
Why is it ambiguous? ------ foo.cpp ------ struct Foo { Foo operator* (Foo) { return Foo(); } Foo operator* (int) const { return Foo(); } Foo () {} Foo (int) {} };
15
4353
by: Tim Clacy | last post by:
Please illuminate; what operator of class 'Event' will get matched for these two cases : Event ev1; Event ev2; // Case 1 // if (ev1) ;
10
1828
by: Elementary Penguin | last post by:
Shouldn't I be able to do this: if(1) ++count; it seems really a pain to have to do if(1==1) ++count; -- Texeme
2
2875
by: Claudius | last post by:
Hello, I have written a class A with the access operator(int) overloaded by a A-const version which returns an int by value: ------------------------------------------------ #include...
5
11335
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
4
4840
by: Lycan. Mao.. | last post by:
Hello, I'm trying to write a function adapter object, but it fails with the above information. Can you help me. template <typename _Predicate> struct Unary_negate { typedef typename...
1
6222
by: Andrew Marlow | last post by:
Hello, I am new to java and need to write a program that uses an XML file for its configuration. I have an equivalent C++ program that uses libxml2 from gnome for this task. I wrapped this up in...
5
2079
by: Chris Forone | last post by:
hello group, why i cant make the InputIterator of the following func-temp const? template<typename InputIterator, typename OutputIterator> OutputIterator Types::Copy(InputIterator source,...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.