类别范围的枚举 [英] Class-scoped enum

查看:185
本文介绍了类别范围的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有枚举的c ++类,我想用 boost :: python 来模仿,所以我可以写 MyClass python中的.value boost :: python :: class _ 没有一个枚举_ 方法,我正在寻找解决方法。 >


  1. 我第一次尝试使用lambdas,如

      MyClass {
    enum {value1,value2};
    };

    class_&MyClass>(MyClass)。add_property(value1,& [](){return value1;})staticmethod(value1);

    这会给编译器错误(在 get_signature add_property )。我知道我可以为每个值创建getter方法,但对我来说似乎很尴尬(打字)。


  2. 使用 attr

      auto classObj = class_< MyClass>(MyClass); 
    classObj.attr(value1)=(int)value1;
    classObj.attr(value2)=(int)value2;

    但它不能像 .def 和/或其他方法返回参考实例。


有更优雅的解决方案吗?

解决方案

您可以使用范围:

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

命名空间bp = boost :: python;

class MyClass {
public:
枚举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()
;
}

然后在python中,你的枚举值是:

 在[8]中:nestedtest.MyClass.MyEnum.values 
输出[8]:{0:nestedtest.MyEnum.value1,1:嵌套测试.MyEnum.value2}

在[9]中:nestedtest.MyClass.MyEnum.value1
输出[9]:nestedtest.MyEnum.value1

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

(从我的ipython shell,我测试了这个和所有;)


I have a c++ class with an enum inside, and I wanted to mimick that with boost::python, so that I can write MyClass.value in python. boost::python::class_ does not have an enum_ method, and I was looking for workarounds.

  1. I first tried with lambdas like

    MyClass{
        enum{value1,value2};
    };
    
    class_<MyClass>("MyClass").add_property("value1",&[](){return value1;}).staticmethod("value1");
    

    which gives compiler error (in get_signature for add_property). I know I could create getter method for each of the values, but that seems very awkward to me (typing-wise).

  2. Using attr:

    auto classObj=class_<MyClass>("MyClass");
    classObj.attr("value1")=(int)value1;
    classObj.attr("value2")=(int)value2;
    

    but it cannot be chained like .def and other methods returning reference to the instance.

Is there a more elegant solution?

解决方案

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 ;)

这篇关于类别范围的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆