473,549 Members | 2,982 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
};

//::::::::::::::: ::::::::::::BOO ST::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()").pt r(),ii);}
};

BOOST_PYTHON_MO DULE(test2py)
{
class_<A>("A",i nit<int>())
.def("fun",&A:: fun);

class_<BaseWrap , boost::noncopya ble>("Base")
.def("f", pure_virtual(&B ase::f))
.def("g", pure_virtual(&B ase::g))
.def("__call__" , pure_virtual(&B ase::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_Document s\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 1753

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

Similar topics

2
2483
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 int xfunc() = 0 ;
4
2360
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
4361
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
1834
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
2882
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 <iostream> using namespace std;
5
11338
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 to pass all the necessary object files to the linker. Neither of those are my problem. Please bear with me as the question I ask is rather long and...
4
4846
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 _Predicate::argument_type argument_type; typedef typename _Predicate::return_type return_type; _Predicate pred_;
1
6225
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 a class (libxml2 is a C interface). I am wondering how people solve this problem in java. Googling reveals a tonne of xml configuration projects...
5
2086
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, OutputIterator first, OutputIterator last) { while (first != last) *first++ = *source++;
0
7446
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7956
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7469
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6040
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3498
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1935
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 we have to send another system
1
1057
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.