Chris@16
|
1 // Copyright David Abrahams 2002.
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
3 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5 #ifndef ENUM_DWA200298_HPP
|
Chris@16
|
6 # define ENUM_DWA200298_HPP
|
Chris@16
|
7
|
Chris@16
|
8 # include <boost/python/detail/prefix.hpp>
|
Chris@16
|
9
|
Chris@16
|
10 # include <boost/python/object/enum_base.hpp>
|
Chris@16
|
11 # include <boost/python/converter/rvalue_from_python_data.hpp>
|
Chris@16
|
12 # include <boost/python/converter/registered.hpp>
|
Chris@16
|
13
|
Chris@16
|
14 namespace boost { namespace python {
|
Chris@16
|
15
|
Chris@16
|
16 template <class T>
|
Chris@16
|
17 struct enum_ : public objects::enum_base
|
Chris@16
|
18 {
|
Chris@16
|
19 typedef objects::enum_base base;
|
Chris@16
|
20
|
Chris@16
|
21 // Declare a new enumeration type in the current scope()
|
Chris@16
|
22 enum_(char const* name, char const* doc = 0);
|
Chris@16
|
23
|
Chris@16
|
24 // Add a new enumeration value with the given name and value.
|
Chris@16
|
25 inline enum_<T>& value(char const* name, T);
|
Chris@16
|
26
|
Chris@16
|
27 // Add all of the defined enumeration values to the current scope with the
|
Chris@16
|
28 // same names used here.
|
Chris@16
|
29 inline enum_<T>& export_values();
|
Chris@16
|
30 private:
|
Chris@16
|
31 static PyObject* to_python(void const* x);
|
Chris@16
|
32 static void* convertible_from_python(PyObject* obj);
|
Chris@16
|
33 static void construct(PyObject* obj, converter::rvalue_from_python_stage1_data* data);
|
Chris@16
|
34 };
|
Chris@16
|
35
|
Chris@16
|
36 template <class T>
|
Chris@16
|
37 inline enum_<T>::enum_(char const* name, char const* doc )
|
Chris@16
|
38 : base(
|
Chris@16
|
39 name
|
Chris@16
|
40 , &enum_<T>::to_python
|
Chris@16
|
41 , &enum_<T>::convertible_from_python
|
Chris@16
|
42 , &enum_<T>::construct
|
Chris@16
|
43 , type_id<T>()
|
Chris@16
|
44 , doc
|
Chris@16
|
45 )
|
Chris@16
|
46 {
|
Chris@16
|
47 }
|
Chris@16
|
48
|
Chris@16
|
49 // This is the conversion function that gets registered for converting
|
Chris@16
|
50 // these enums to Python.
|
Chris@16
|
51 template <class T>
|
Chris@16
|
52 PyObject* enum_<T>::to_python(void const* x)
|
Chris@16
|
53 {
|
Chris@16
|
54 return base::to_python(
|
Chris@16
|
55 converter::registered<T>::converters.m_class_object
|
Chris@16
|
56 , static_cast<long>(*(T const*)x));
|
Chris@16
|
57 }
|
Chris@16
|
58
|
Chris@16
|
59 //
|
Chris@16
|
60 // The following two static functions serve as the elements of an
|
Chris@16
|
61 // rvalue from_python converter for the enumeration type.
|
Chris@16
|
62 //
|
Chris@16
|
63
|
Chris@16
|
64 // This checks that a given Python object can be converted to the
|
Chris@16
|
65 // enumeration type.
|
Chris@16
|
66 template <class T>
|
Chris@16
|
67 void* enum_<T>::convertible_from_python(PyObject* obj)
|
Chris@16
|
68 {
|
Chris@16
|
69 return PyObject_IsInstance(
|
Chris@16
|
70 obj
|
Chris@16
|
71 , upcast<PyObject>(
|
Chris@16
|
72 converter::registered<T>::converters.m_class_object))
|
Chris@16
|
73
|
Chris@16
|
74 ? obj : 0;
|
Chris@16
|
75 }
|
Chris@16
|
76
|
Chris@16
|
77 // Constructs an instance of the enumeration type in the from_python
|
Chris@16
|
78 // data.
|
Chris@16
|
79 template <class T>
|
Chris@16
|
80 void enum_<T>::construct(PyObject* obj, converter::rvalue_from_python_stage1_data* data)
|
Chris@16
|
81 {
|
Chris@16
|
82 #if PY_VERSION_HEX >= 0x03000000
|
Chris@16
|
83 T x = static_cast<T>(PyLong_AS_LONG(obj));
|
Chris@16
|
84 #else
|
Chris@16
|
85 T x = static_cast<T>(PyInt_AS_LONG(obj));
|
Chris@16
|
86 #endif
|
Chris@16
|
87 void* const storage = ((converter::rvalue_from_python_storage<T>*)data)->storage.bytes;
|
Chris@16
|
88 new (storage) T(x);
|
Chris@16
|
89 data->convertible = storage;
|
Chris@16
|
90 }
|
Chris@16
|
91
|
Chris@16
|
92 template <class T>
|
Chris@16
|
93 inline enum_<T>& enum_<T>::value(char const* name, T x)
|
Chris@16
|
94 {
|
Chris@16
|
95 this->add_value(name, static_cast<long>(x));
|
Chris@16
|
96 return *this;
|
Chris@16
|
97 }
|
Chris@16
|
98
|
Chris@16
|
99 template <class T>
|
Chris@16
|
100 inline enum_<T>& enum_<T>::export_values()
|
Chris@16
|
101 {
|
Chris@16
|
102 this->base::export_values();
|
Chris@16
|
103 return *this;
|
Chris@16
|
104 }
|
Chris@16
|
105
|
Chris@16
|
106 }} // namespace boost::python
|
Chris@16
|
107
|
Chris@16
|
108 #endif // ENUM_DWA200298_HPP
|