简体   繁体   中英

Automatically exposing properties in boost python

I have a C++ application with boost::python bindings to allow users to access various classes and methods. So far, I have been defining the methods and properties that I want to expose manually by using the .def and .add_property methods.

However, one of the classes that I have exposed to python is a base class with many many subclass implementations. Each of these subclasses has a number of properties that I want exposed to the python layer. To make things more complicated, each of these properties is templated on a specific type, string, bool, int etc.

Below is an example:

class Base {
    public:

        Base();

        static const std::string readableType();

        virtual void registration();
}

class Subclass1 : public Base {
    public:
        Subclass1();

        static const std::string readableType();

        virtual void registration();

    protected:
    private:
        Core::Parameter<unsigned> m_seed;
        Core::Parameter<float> m_minVel, m_maxVel;
        Core::Parameter<bool> m_activated;
}

class Subclass2 : public Base {
    public:
        Subclass2();

        static const std::string readableType();

        virtual void registration();

    protected:
    private:
        Core::Parameter<int> m_id;
        Core::Parameter<Core::Types::vec3> m_velVector;
}

The Core::Parameter class is simply a templated object that holds data of that type, but there are two many subclasses to go through them all and manually add, and then add all of their specific parameters. Is there any way of automatically interpreting these properties, adding them as accessible parameters on the python object, and also mapping the types to the correct types in python? (Some of these would have to be my own python objects, ie the vec3 object).

I hope I've described the problem well, any help would be greatly appreciated.

Many thanks.

Not in boost::python. Swig does a pretty good job at automatically generating bindings, but I find that it's more overhead. Boost::python is just a library at the end of the day. You get more control, but you have to do all the work yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM