[C++-sig] boost ref problem

Jim Bosch talljimbo at gmail.com
Tue Feb 7 17:11:46 CET 2012


On 02/06/2012 08:33 PM, Jay Riley wrote:
>
> I'm trying to wrap a C++ class into python. I need to pass an enum value by reference through a virtual function. I'm doing it like so (non relevant code omitted):
>
> class BasicRMLScreenWrap : public BasicRMLScreen		{		public:                        bool HandleKeyPressedDefault(const sf::Uint32 time, const ::Input::InputModule* inputModule, ::Input::PlayerInput pinput, ::Input::InputAction&  iaction)			{				return BasicRMLScreen::HandleKeyPressed(time, inputModule, pinput, iaction);			}			bool HandleKeyReleased(const sf::Uint32 time, const ::Input::InputModule* inputModule, ::Input::PlayerInput pinput, ::Input::InputAction&  iaction) override			{				return call_method<bool>(self, "HandleKeyReleased", time, ptr(inputModule), pinput, boost::ref(iaction));			}      private:			PyObject* self;		};
> class_<BasicRMLScreen, bases<GameScreen>, boost::shared_ptr<BasicRMLScreenWrap>, boost::noncopyable>("BasicRMLScreen", init<const std::string&, const std::string&, Engine*, int>())			.def("HandleKeyPressed",&BasicRMLScreen::HandleKeyPressed,&BasicRMLScreenWrap::HandleKeyPressedDefault)			;
> the problem is that when I wrap it like this, I get the following error:
> Error	74	error C2664: 'boost::mpl::assertion_failed' : cannot convert parameter 1 from 'boost::mpl::failed ************boost::mpl::or_<T1,T2>::* ***********' to 'boost::mpl::assert<false>::type'
>
> if I switch boost::ref(iaction) to iaction it compiles fine, but I need to pass it by ref for correct behaviour. I'm not sure if this is a boost::ref problem or a boost python issue but was curious if someone might have an idea how to correct it. I can pass using boost:ref fine in the other places ive used it with call_method. Is this is an issues because it's an enum?
> Any help would be appreciated,

I'm afraid you're run into a bit of an impossible problem.  Boost.Python 
only defines rvalue from-python converters for enums, and this actually 
makes sense - like int, float, and str in Python, enums are immutable 
Python objects; when you set them, you're really just assigning the name 
of the variable to a new object.  There's really no way to express 
modifying an argument in-place in Python when the argument is an 
immutable object.

I think you'll manually have to transform the signature to return the 
new enum value in Python (i.e. make a derived class that implements the 
enum ref virtual member function by delegating to a new enum-returning 
virtual member function that you can wrap).

Jim



More information about the Cplusplus-sig mailing list