Class-scoped enum

百般思念 提交于 2019-12-04 23:10:32

You can do it using a scope:

#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/scope.hpp>
#include <boost/python/enum.hpp>

namespace bp = boost::python;

class MyClass{
    public:
        enum MyEnum {value1,value2};
};

BOOST_PYTHON_MODULE(nestedtest){

    bp::scope the_scope
        = bp::class_<MyClass>("MyClass")
        ;

    bp::enum_<MyClass::MyEnum>("MyEnum")
        .value("value1", MyClass::value1)
        .value("value2", MyClass::value2)
        .export_values()
        ;
}

Then in python, your enum values are:

In [8]: nestedtest.MyClass.MyEnum.values
Out[8]: {0: nestedtest.MyEnum.value1, 1: nestedtest.MyEnum.value2}

In [9]: nestedtest.MyClass.MyEnum.value1
Out[9]: nestedtest.MyEnum.value1

In [10]: nestedtest.MyClass.MyEnum.value2
Out[10]: nestedtest.MyEnum.value2

(from my ipython shell, I tested this and all ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!