enum.qbk 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. [section boost/python/enum.hpp]
  2. [section Introduction]
  3. <boost/python/enum.hpp> defines the interface through which users expose their C++ enumeration types to Python. It declares the `enum_` class template, which is parameterized on the enumeration type being exposed.
  4. [endsect]
  5. [section Class template `enum_`]
  6. Creates a Python class derived from Python's `int` type which is associated with the C++ type passed as its first parameter.
  7. ``
  8. namespace boost { namespace python
  9. {
  10. template <class T>
  11. class enum_ : public object
  12. {
  13. enum_(char const* name, char const* doc = 0);
  14. enum_<T>& value(char const* name, T);
  15. enum_<T>& export_values();
  16. };
  17. }}
  18. ``
  19. [endsect]
  20. [section Class template `enum_` constructors]
  21. ``enum_(char const* name, char const* doc=0);``
  22. [variablelist
  23. [[Requires][name is an [link ntbs] which conforms to Python's [@http://www.python.org/doc/current/ref/identifiers.html identifier naming rules].]]
  24. [[Effects][Constructs an `enum_` object holding a Python extension type derived from `int` which is named `name`. The named attribute of the [link high_level_components.boost_python_scope_hpp current scope] is bound to the new extension type.]]
  25. ]
  26. [endsect]
  27. [section Class template `enum_` modifier functions]
  28. ``enum_<T>& value(char const* name, T x);``
  29. [variablelist
  30. [[Requires][name is an [link ntbs] which conforms to Python's [@http://www.python.org/doc/current/ref/identifiers.html identifier naming rules].]]
  31. [[Effects][adds an instance of the wrapped enumeration type with value x to the type's dictionary as the named attribute.]]
  32. [[Returns][`*this`]]
  33. ]
  34. ``enum_<T>& export_values();``
  35. [variablelist
  36. [[Effects][sets attributes in the [link high_level_components.boost_python_scope_hpp current scope] with the same names and values as all enumeration values exposed so far by calling value().]]
  37. [[Returns][`*this`]]
  38. ]
  39. [endsect]
  40. [section Example]
  41. C++ module definition
  42. ``
  43. #include <boost/python/enum.hpp>
  44. #include <boost/python/def.hpp>
  45. #include <boost/python/module.hpp>
  46. using namespace boost::python;
  47. enum color { red = 1, green = 2, blue = 4 };
  48. color identity_(color x) { return x; }
  49. BOOST_PYTHON_MODULE(enums)
  50. {
  51. enum_<color>("color")
  52. .value("red", red)
  53. .value("green", green)
  54. .export_values()
  55. .value("blue", blue)
  56. ;
  57. def("identity", identity_);
  58. }
  59. ``
  60. Interactive Python:
  61. ``
  62. >>> from enums import *
  63. >>> identity(red)
  64. enums.color.red
  65. >>> identity(color.red)
  66. enums.color.red
  67. >>> identity(green)
  68. enums.color.green
  69. >>> identity(color.green)
  70. enums.color.green
  71. >>> identity(blue)
  72. Traceback (most recent call last):
  73. File "<stdin>", line 1, in ?
  74. NameError: name 'blue' is not defined
  75. >>> identity(color.blue)
  76. enums.color.blue
  77. >>> identity(color(1))
  78. enums.color.red
  79. >>> identity(color(2))
  80. enums.color.green
  81. >>> identity(color(3))
  82. enums.color(3)
  83. >>> identity(color(4))
  84. enums.color.blue
  85. >>> identity(1)
  86. Traceback (most recent call last):
  87. File "<stdin>", line 1, in ?
  88. TypeError: bad argument type for built-in operation
  89. ``
  90. [endsect]
  91. [endsect]