[C++-sig] Virtual functions : should I use wrapper::get_override or call_method and boost::ref ?

2010-03-11 Thread Guillaume Seguin
Boost.Python 1.42 documentation shows how to overload virtual functions
using boost::python::wrapper::get_override. I made the example work.

Now I am struggling with a inheritance problem with virtual functions
calling each other, with a mix and match of Python and C++ implementation of
a common base.

I saw that in the distribution's "test" directory, as well as in the paper
"Building Hybrid System with Boost.Python", call_method and boost::ref are
used instead of wrapper::get_override.

What is the difference between the two approach ? Given a class hierarchy
like the one below, wich road should I take ?

//Simplified C++ version
struct Base {
virtual int f() = 0;
virtual void call_f(Base &b) { this->f(); b.f(); } /* this pointer will
always be specialized */
}

struct BaseWrap : Base, /*should I inherit from wrapper here... */
{
 int f() {  /* or use call_method here ??? */
}

struct Derived1 : Base /* or BaseWrap ? */
{
   int f() { return 1; }
}

struct Derived2 : Base /* or BaseWrap ? */
{
   int f() { return 2; }
}

#Extended in Pyhton
class PyDerived1(Derived1):
def f(self):
return -Derived1.f(self); #Calling the ancestor class is a
requirement

class PyDerived2(Derived2):
def f(self):
return -Derived2.f(self); #Calling the ancestor class is a
requirement

#And called like this
py1 = PyDerived1()
py2 = PyDerived2()

py1.call_f(py2)


TIA,

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

[C++-sig] boost.python class constructor error

2010-03-11 Thread hitesh dhiman
This error occurs when i declare the class constructor in the c++ header
file and the definition in the .cpp file. Bjam throws up a LNK2019 error,
unresolved symbol.
But if i declare the class constructor in the header file itself, the code
compiles.
Here's the code:

*Test1.h*
#include 
//using namespace std;

class World
{
public:
World();
~World();
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};

*Test1.cpp*
#include "Test1.h"


World::World() {};
World::~World() {};
int addition(int a, int b)
{
int z = a + b;
return z;
}

*Wrapper file:*
*
#include 
#include "Test1.h"


BOOST_PYTHON_MODULE(hello)
{
using namespace boost::python;
class_("World")
.def("greet", &World::greet)
.def("set", &World::set)
;
}

Is it a compiler bug? I want to define the constructors in the cpp file, and
not the header file.
*
-- 
Regards,
Hitesh
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] boost.python class constructor error

2010-03-11 Thread Jim Bosch
On Fri, 2010-03-12 at 10:36 +0800, hitesh dhiman wrote:
> This error occurs when i declare the class constructor in the c++
> header file and the definition in the .cpp file. Bjam throws up a
> LNK2019 error, unresolved symbol.
> But if i declare the class constructor in the header file itself, the
> code compiles.
> Here's the code:
> 
> 
> Test1.h
> #include 
> //using namespace std;
> 
> 
> class World
> {
> public:
> World();
> ~World();
> void set(std::string msg) { this->msg = msg; }
> std::string greet() { return msg; }
> std::string msg;
> };
> 
> 
> Test1.cpp
> #include "Test1.h"
> 
> 
> 
> 
> World::World() {};
> World::~World() {};
> int addition(int a, int b)
> {
> int z = a + b;
> return z;
> }
> 
> 
> Wrapper file:
> #include 
> #include "Test1.h"
> 
> 
> 
> 
> BOOST_PYTHON_MODULE(hello)
> {
> using namespace boost::python;
> class_("World")
> 
> .def("greet", &World::greet)
> .def("set", &World::set)
> ;
> }
> 
> 
> Is it a compiler bug? I want to define the constructors in the cpp
> file, and not the header file. 
> -- 

I've never had any problems doing exactly that, and I can't say more
about your particular situation without seeing your build system setup
(which probably actually wouldn't help me, since I don't know much about
bjam or Windows).

Are you certain your source file is getting compiled into (or otherwise
linked) to the module's shared library?

Did you compile the source file with the same compiler flags as the
wrapper file?  They probably don't have to be identical, but some may be
important.

My guess that it's not just the constructor; you aren't linking against
anything in your non-wrapper cpp file.


Jim



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


Re: [C++-sig] boost.python class constructor error

2010-03-11 Thread hitesh dhiman
hi Jim
Will it help if i post my jam-root file? Its included below:
import python ;

if ! [ python.configured ]
{
ECHO "notice: no Python configured in user-config.jam" ;
ECHO "notice: will use default configuration" ;
using python ;
}

# Specify the path to the Boost project.  If you move this project,
# adjust this path to refer to the Boost root directory.
use-project boost
  : "C:/Program Files/boost/boost_1_42" ;

# Set up the project-wide requirements that everything uses the
# boost_python library from the project whose global ID is
# /boost/python.
project
  : requirements /boost/python//boost_python ;

# Declare the three extension modules.  You can specify multiple
# source files after the colon separated by spaces.
python-extension hello : TestWrap.cpp ;

# A little "rule" (function) to clean up the syntax of declaring tests
# of these extension modules.
local rule run-test ( test-name : sources + )
{
import testing ;
testing.make-test run-pyd : $(sources) : : $(test-name) ;
}


>From what you said, its seems the Test1.cpp file needs to be listed under
the extension modules,  is it?
Its as if bjam is totally ignoring Test1.cpp. Any definition/declaration in
Test1.cpp file doesn't work.

On Fri, Mar 12, 2010 at 11:38 AM, Jim Bosch  wrote:

> On Fri, 2010-03-12 at 10:36 +0800, hitesh dhiman wrote:
> > This error occurs when i declare the class constructor in the c++
> > header file and the definition in the .cpp file. Bjam throws up a
> > LNK2019 error, unresolved symbol.
> > But if i declare the class constructor in the header file itself, the
> > code compiles.
> > Here's the code:
> >
> >
> > Test1.h
> > #include 
> > //using namespace std;
> >
> >
> > class World
> > {
> > public:
> > World();
> > ~World();
> > void set(std::string msg) { this->msg = msg; }
> > std::string greet() { return msg; }
> > std::string msg;
> > };
> >
> >
> > Test1.cpp
> > #include "Test1.h"
> >
> >
> >
> >
> > World::World() {};
> > World::~World() {};
> > int addition(int a, int b)
> > {
> > int z = a + b;
> > return z;
> > }
> >
> >
> > Wrapper file:
> > #include 
> > #include "Test1.h"
> >
> >
> >
> >
> > BOOST_PYTHON_MODULE(hello)
> > {
> > using namespace boost::python;
> > class_("World")
> >
> > .def("greet", &World::greet)
> > .def("set", &World::set)
> > ;
> > }
> >
> >
> > Is it a compiler bug? I want to define the constructors in the cpp
> > file, and not the header file.
> > --
>
> I've never had any problems doing exactly that, and I can't say more
> about your particular situation without seeing your build system setup
> (which probably actually wouldn't help me, since I don't know much about
> bjam or Windows).
>
> Are you certain your source file is getting compiled into (or otherwise
> linked) to the module's shared library?
>
> Did you compile the source file with the same compiler flags as the
> wrapper file?  They probably don't have to be identical, but some may be
> important.
>
> My guess that it's not just the constructor; you aren't linking against
> anything in your non-wrapper cpp file.
>
>
> Jim
>
>
>
> ___
> Cplusplus-sig mailing list
> [email protected]
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Regards,
Hitesh Dhiman
Electrical Engineering
National University of Singapore
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] boost.python class constructor error

2010-03-11 Thread Jim Bosch
On Fri, 2010-03-12 at 12:01 +0800, hitesh dhiman wrote:
> hi Jim
> Will it help if i post my jam-root file? Its included below:
> import python ;
> 
> 
> if ! [ python.configured ]
> {
> ECHO "notice: no Python configured in user-config.jam" ;
> ECHO "notice: will use default configuration" ;
> using python ;
> }
> 
> 
> # Specify the path to the Boost project.  If you move this project,
> # adjust this path to refer to the Boost root directory.
> use-project boost
>   : "C:/Program Files/boost/boost_1_42" ;
> 
> 
> # Set up the project-wide requirements that everything uses the
> # boost_python library from the project whose global ID is
> # /boost/python.
> project
>   : requirements /boost/python//boost_python ;
> 
> 
> # Declare the three extension modules.  You can specify multiple
> # source files after the colon separated by spaces.
> python-extension hello : TestWrap.cpp ;
> 

It's this line right here, I think. Should be:

 python-extension hello : TestWrap.cpp Test1.cpp;


Good luck!

Jim




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


Re: [C++-sig] boost.python class constructor error

2010-03-11 Thread hitesh dhiman
Hi Jim,
The solution was to include the Test1.cpp in the python extension line as
you suggested. After i had to include include , and the code
compiled.
I guess the boost documentation is not too clear on these concepts...
Anyways thanks for the help!

On Fri, Mar 12, 2010 at 12:48 PM, Jim Bosch  wrote:

> On Fri, 2010-03-12 at 12:01 +0800, hitesh dhiman wrote:
> > hi Jim
> > Will it help if i post my jam-root file? Its included below:
> > import python ;
> >
> >
> > if ! [ python.configured ]
> > {
> > ECHO "notice: no Python configured in user-config.jam" ;
> > ECHO "notice: will use default configuration" ;
> > using python ;
> > }
> >
> >
> > # Specify the path to the Boost project.  If you move this project,
> > # adjust this path to refer to the Boost root directory.
> > use-project boost
> >   : "C:/Program Files/boost/boost_1_42" ;
> >
> >
> > # Set up the project-wide requirements that everything uses the
> > # boost_python library from the project whose global ID is
> > # /boost/python.
> > project
> >   : requirements /boost/python//boost_python ;
> >
> >
> > # Declare the three extension modules.  You can specify multiple
> > # source files after the colon separated by spaces.
> > python-extension hello : TestWrap.cpp ;
> >
>
> It's this line right here, I think. Should be:
>
>  python-extension hello : TestWrap.cpp Test1.cpp;
>
>
> Good luck!
>
> Jim
>
>
>
>
> ___
> Cplusplus-sig mailing list
> [email protected]
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Regards,
Hitesh Dhiman
Electrical Engineering
National University of Singapore
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig