11

Suppose I want to get the reference to some global / internal c++ object, one method is to declare function with boost::python::return_value_policy<reference_existing_object>().

Both GetGlobalObjectA and GetGlobalObjectB return the reference to the original c++ object without create a new copy;

But how to make GetGlobalObjectByID return a ref to the existing c++ object?

struct A { uint32_t value; };
struct B { uint64_t value; };

A globalA;
B globalB;

boost::python::object GetGlobalObjectByID(int id)
{
    // boost::python::object will return a new copy of C++ object, not the global one.

    if (id == 1)
        return boost::python::object(&globalA);
    else if (id == 2)
        return boost::python::object(&globalB);
    else
        return boost::python::object(nullptr);
}

A& GetGlobalObjectA() { return globalA; }
B& GetGlobalObjectB() { return globalB; }

BOOST_PYTHON_MODULE(myModule)
{
    using namespace boost::python;

    class_<A>("A");
    class_<B>("B");

    def("GetGlobalObjectByID", GetGlobalObjectByID);

    def("GetGlobalObjectA", GetGlobalObjectA, return_value_policy<reference_existing_object>());
    def("GetGlobalObjectB", GetGlobalObjectB, return_value_policy<reference_existing_object>());

}

1 Answer 1

3

Based on this answer, I found it is possible to use reference_existing_object::apply as a converter.

template <typename T>
inline object MagicMethod(T* ptr)
{
    typename reference_existing_object::apply::type converter;

    handle handle(converter(ptr));

    return object(handle);
}

And here is the modified version.

boost::python::object GetGlobalObjectByID(int id)
{
    if (id == 1)
        return MagicMethod(&globalA);
    else if (id == 2)
        return MagicMethod(&globalB);
    else
        return boost:python::object();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.