[C++-sig] pickle the unpickleable

2011-09-03 Thread Neal Becker
I have a pickling problem I suspect is just not possible to solve.

I have some wrappers around boost::random objects.

There is a rng object, which is a single underlying mersenne twister.

There are various distributions.  For example, uniform_real.

Each uniform_real holds a reference to the single, external mt object.

I don't think there's any way to unpickle several instance of uniform_real so 
that their references to the (shared) mt object get correctly restored.

Imagine something like this:

class mt;

struct uniform_real {
  mt & _mt;

  uniform_real (mt &, min, max) ...
};

When unpickled, the internal _mt states are saved and restored correctly, but 2 
instances of uniform_real will not share a reference to a single mt object, 
they 
each have their own copy which starts with a copy of the same state (no good).

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


Re: [C++-sig] copy constructors and multiple instances

2011-09-03 Thread Josh Stratton
Thanks for everyone's help.  I'm posting my updated class that is now working.

class Scene : public boost::enable_shared_from_this
{
public:
  void sendYourselfToPython()
  {
try {
  object ignored = exec(EXAMPLE_PY_FUNCTION, pyMainNamespace);
  object processFileFunc = pyMainModule.attr("Foo").attr("processFile");
  processFileFunc(shared_from_this(), "test.txt");
} catch (boost::python::error_already_set const &) {
  std::string perror = parse_python_exception();
  std::cerr << "Error in Python: " << perror << std::endl;
}
  }
};
typedef boost::shared_ptr SceneP;

On Fri, Sep 2, 2011 at 9:08 PM, Dave Abrahams  wrote:
>
> on Fri Sep 02 2011, Josh Stratton  wrote:
>
>> Here's a really short example of what I don't understand.  Basically I
>> can setup the python function to accept shared pointers, but can I
>> setup it to also use normal pointers?
>
> Yes, once you've exposed the class to Python, you can pass pointers to
> Python.  However, Boost.Python won't let you pass raw pointers around
> without telling it (via call policies) what you want to do about
> lifetime management... actually what it will do, if you don't tell it
> otherwise, is copy the pointee, which is at least safe from an object
> lifetime point-of-view.
>
>> In the example code attached, if I uncomment
>> scene->sendYourselfToPython(); it fails because it's using "this" as
>> the scene pointer.
>
> (It's already uncommented)
>
>> Error in Python: : No to_python
>> (by-value) converter found for C++ type: Scene
>
> ...because you've said it's noncopyable, there's no way to copy the
> pointee, which is what happens when it is passed by value.
>
>> So it appears to only work for shared pointers at this point.  Can I
>> also get it to work with "this" at the same time?  Jim pointed me to
>> "enable_shared_from_this" as a way for creating a shared pointer
>> object, which makes sense from the docs of how to do it, but I was
>> wondering if I necessarily NEED to send it down as a shared pointer.
>
> There *are* things you can do to subvert Python's checking, but they're
> highly discouraged.  Jim is on the right track: if you want to pass a
> mutable reference to the object and you want its lifetime to be properly
> managed, you should do what he's saying.  If you don't like
> using enable_shared_from_this, of course, you can just do it outside a
> member function:
>
>  void sendSceneToPython(SceneP s)
>    try {
>      object ignored = exec(EXAMPLE_PY_FUNCTION, pyMainNamespace);
>      object processFileFunc = pyMainModule.attr("Foo").attr("processFile");
>      processFileFunc(s, "test.txt");
>    } catch (boost::python::error_already_set const &) {
>      std::string perror = parse_python_exception();
>      std::cerr << "Error in Python: " << perror << std::endl;
>    }
>  }
>
> HTH,
>
> --
> Dave Abrahams
> BoostPro Computing
> http://www.boostpro.com
>
> ___
> Cplusplus-sig mailing list
> [email protected]
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig