[C++-sig] Solved (pickle multiple refs to objects)

2011-09-06 Thread Neal Becker
The solution to my problem is a combination of boost::serialization and 
boost::shared_ptr.

Rather than holding refs to the contained shared objects, if the objects are 
share_ptr, then serialization will do the correct thing.

For example, I have

struct boost_uniform_int_wrap {
  boost::shared_ptr rng_obj;
  boost::uniform_int<> gen;

  boost_uniform_int_wrap (boost::shared_ptr _rng_obj, int min, int max) :
rng_obj (_rng_obj),
gen (min, max) {}
...

struct uniform_int_pickle_suite : bp::pickle_suite {
  static bp::tuple getinitargs (boost_uniform_int_wrap const& gen) {
return bp::make_tuple (gen.rng_obj, gen.gen.min(), gen.gen.max());
  }
};

Also, I have modified mersenne_twister to make it serializable (very simple to 
add).

Then to make mersenne_twister pickleable:

typedef boost::mt19937 rng_t;

struct mt_pickle_suite : bp::pickle_suite {

  static bp::object getstate (const rng_t& rng) {
std::ostringstream os;
boost::archive::binary_oarchive oa(os);
oa << rng;
return bp::str (os.str());
  }

  static void
  setstate(rng_t& rng, bp::object entries) {
bp::str s = bp::extract (entries)();
std::string st = bp::extract (s)();
std::istringstream is (st);

boost::archive::binary_iarchive ia (is);
ia >> rng;
  }
};

Now:
from boost_rand import rng, normal
from sys import getrefcount

rng1 = rng()
print getrefcount(rng1)
n1 = normal (rng1, 1, 0)
print getrefcount(rng1)
n2 = normal (rng1, 1, 0)
print getrefcount(rng1)

from cPickle import dumps, loads
rng2, n3, n4 = loads (dumps ((rng1, n1, n2), -1))

In [10]: ## working on region in file /usr/tmp/python-18983NT.py...
2
3
4

In [11]: rng2
Out[11]: 

In [12]: n3.rng
Out[12]: 

In [13]: n4.rng
Out[13]: 

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


[C++-sig] Build Errors Making the "quickstart" Demo Project

2011-09-06 Thread Paul Kroitor
Hello all,

 

We are adding a python scripting layer on top of an existing largish C++
app, and are hoping to use Boost.Python to accomplish this. So far, it's
hasn't been going very smoothly, but I think we are nearly to the point
of getting the demo program to compile.

 

For the most part, we are following the instructions here:

http://www.boost.org/doc/libs/1_47_0/libs/python/doc/building.html

 

but have also had to use hints from here

http://stackoverflow.com/questions/2629421/how-to-use-boost-in-visual-st
udio-2010

and several other forums to get as far as we have (who / where should we
report manual errors to?).

 

I don't think we are doing anything non-standard - we certainly aren't
trying to - but perhaps some of the issues we are seeing come from using
Python 3.2. To complete the list, we are also using MSVC 10.0 SP1 and
Boost 1.47 on Windows 7 32-bit platform.

 

Currently, we are trying to get the "quickstart" example working and are
stuck on an error in the build of  "embedding.cpp":

 

(note that for clarity I've rerun the build a second time so that it is
only trying to build the failed modules)

 


...

 

C:\Boost\boost_1_47_0\libs\python\example\quickstart>b2

...patience...

...patience...

...found 1603 targets...

...updating 3 targets...

compile-c-c++ bin\msvc-10.0\debug\threading-multi\embedding.obj

embedding.cpp

embedding.cpp(39) : error C2668:
'std::basic_string<_Elem,_Traits,_Ax>::basic_st

ring' : ambiguous call to overloaded function

with

[

_Elem=char,

_Traits=std::char_traits,

_Ax=std::allocator

]

c:\Program Files\Microsoft Visual Studio
10.0\VC\INCLUDE\xstring(700): c

ould be
'std::basic_string<_Elem,_Traits,_Ax>::basic_string(std::basic_string<_E

lem,_Traits,_Ax> &&)'

with

[

_Elem=char,

_Traits=std::char_traits,

_Ax=std::allocator

]

c:\Program Files\Microsoft Visual Studio
10.0\VC\INCLUDE\xstring(590): o

r   'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const _Elem
*)'

with

[

_Elem=char,

_Traits=std::char_traits,

_Ax=std::allocator

]

while trying to match the argument list
'(boost::python::detail::method_

result)'

embedding.cpp(56) : error C2065: 'initembedded_hello' : undeclared
identifier

 

call "c:\Program Files\Microsoft Visual Studio
10.0\VC\vcvarsall.bat" x86 >n

ul

cl /Zm800 -nologo
@"bin\msvc-10.0\debug\threading-multi\embedding.obj.rsp"

 

...failed compile-c-c++
bin\msvc-10.0\debug\threading-multi\embedding.obj...

...skipped embedding.exe for lack
of embedding.obj...

...skipped embedding.pdb for lack
of embedding.obj...

...failed updating 1 target...

...skipped 2 targets...

 


...

 

With all the templates and overloads, this is getting too technical for
to solve here. I am hoping someone here will recognize this error or
understand what's causing it.

 

Thanks in advance for any help or guidance anyone is able to give.

Paul Kroitor 

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