IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit
PythonEnumUtils.h
Go to the documentation of this file.
1 // PythonEnumUtils.h:
3 // ------------------
4 //
14 
15 #ifndef __PYIPSDKBASE_PYTHONENUMUTILS_H__
16 #define __PYIPSDKBASE_PYTHONENUMUTILS_H__
17 
18 #include <boost/python/enum.hpp>
19 #include <boost/python/implicit.hpp>
20 
21 namespace ipsdk {
22 namespace python {
23 
26 
27 template <class T>
28 struct ipsdkEnum_ : public boost::python::objects::enum_base
29 {
30  typedef boost::python::objects::enum_base base;
31 
32  // Declare a new enumeration type in the current scope()
33  ipsdkEnum_(char const* name, char const* doc = 0);
34 
35  // Add a new enumeration value with the given name and value.
36  inline ipsdkEnum_<T>& value(char const* name, T);
37 
38  // Add all of the defined enumeration values to the current scope with the
39  // same names used here.
40  inline ipsdkEnum_<T>& export_values();
41 private:
42  static PyObject* to_python(void const* x);
43  static void* convertible_from_python(PyObject* obj);
44  static void construct(PyObject* obj, boost::python::converter::rvalue_from_python_stage1_data* data);
45 };
46 
47 template <class T>
48 inline ipsdkEnum_<T>::ipsdkEnum_(char const* name, char const* doc)
49  : base(
50  name
51  , &ipsdkEnum_<T>::to_python
52  , &ipsdkEnum_<T>::convertible_from_python
53  , &ipsdkEnum_<T>::construct
54  , boost::python::type_id<T>()
55  , doc
56  )
57 {
58 }
59 
60 // This is the conversion function that gets registered for converting
61 // these enums to Python.
62 template <class T>
63 PyObject* ipsdkEnum_<T>::to_python(void const* x)
64 {
65  return base::to_python(
66  boost::python::converter::registered<T>::converters.m_class_object
67  , ((T const*)x)->value());
68 }
69 
70 //
71 // The following two static functions serve as the elements of an
72 // rvalue from_python converter for the enumeration type.
73 //
74 
75 // This checks that a given Python object can be converted to the
76 // enumeration type.
77 template <class T>
78 void* ipsdkEnum_<T>::convertible_from_python(PyObject* obj)
79 {
80  return PyObject_IsInstance(
81  obj
82  , boost::python::upcast<PyObject>(
83  boost::python::converter::registered<T>::converters.m_class_object))
84 
85  ? obj : 0;
86 }
87 
88 // Constructs an instance of the enumeration type in the from_python
89 // data.
90 template <class T>
91 void ipsdkEnum_<T>::construct(PyObject* obj, boost::python::converter::rvalue_from_python_stage1_data* data)
92 {
93 #if PY_VERSION_HEX >= 0x03000000
94  T x = static_cast<typename T::domain>(PyLong_AS_LONG(obj));
95 #else
96  T x = static_cast<typename T::domain>(PyInt_AS_LONG(obj));
97 #endif
98  void* const storage = ((boost::python::converter::rvalue_from_python_storage<T>*)data)->storage.bytes;
99  new (storage)T(x);
100  data->convertible = storage;
101 }
102 
103 template <class T>
104 inline ipsdkEnum_<T>& ipsdkEnum_<T>::value(char const* name, T x)
105 {
106  this->add_value(name, x.value());
107  return *this;
108 }
109 
110 template <class T>
111 inline ipsdkEnum_<T>& ipsdkEnum_<T>::export_values()
112 {
113  this->base::export_values();
114  return *this;
115 }
116 
119 
120 template <typename EnumType>
121 void
122 ipsdkToPythonEnum(const char* enumName)
123 {
124  // registration of all enumerates values
125  ipsdkEnum_<EnumType> curEnum(enumName);
126  typename EnumType::const_iterator iter = EnumType::begin();
127  while (iter != EnumType::end()) {
128 
129  curEnum.value(iter->str(), static_cast<typename EnumType::domain>(iter->value()));
130  ++iter;
131  }
132  curEnum.export_values();
133  boost::python::implicitly_convertible<typename EnumType::domain, EnumType>();
134 }
135 
138 
139 } // end of namespace python
140 } // end of namespace ipsdk
141 
142 #endif // __PYIPSDKBASE_PYTHONENUMUTILS_H__
Main namespace for IPSDK library.
Definition: AlgorithmFunctionEfficiency.h:22
Definition: DataItemNodeHdrMacrosDetails.h:48
Definition: PythonEnumUtils.h:28