Update contrib.
1 // Copyright David Abrahams 2002.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 #ifndef ENUM_DWA200298_HPP
6 # define ENUM_DWA200298_HPP
8 # include <boost/python/detail/prefix.hpp>
10 # include <boost/python/object/enum_base.hpp>
11 # include <boost/python/converter/rvalue_from_python_data.hpp>
12 # include <boost/python/converter/registered.hpp>
14 namespace boost { namespace python {
17 struct enum_ : public objects::enum_base
19 typedef objects::enum_base base;
21 // Declare a new enumeration type in the current scope()
22 enum_(char const* name);
24 // Add a new enumeration value with the given name and value.
25 inline enum_<T>& value(char const* name, T);
27 // Add all of the defined enumeration values to the current scope with the
28 // same names used here.
29 inline enum_<T>& export_values();
31 static PyObject* to_python(void const* x);
32 static void* convertible_from_python(PyObject* obj);
33 static void construct(PyObject* obj, converter::rvalue_from_python_stage1_data* data);
37 inline enum_<T>::enum_(char const* name)
40 , &enum_<T>::to_python
41 , &enum_<T>::convertible_from_python
42 , &enum_<T>::construct
47 // This is the conversion function that gets registered for converting
48 // these enums to Python.
50 PyObject* enum_<T>::to_python(void const* x)
52 return base::to_python(
53 converter::registered<T>::converters.m_class_object
54 , static_cast<long>(*(T const*)x));
58 // The following two static functions serve as the elements of an
59 // rvalue from_python converter for the enumeration type.
62 // This checks that a given Python object can be converted to the
65 void* enum_<T>::convertible_from_python(PyObject* obj)
67 return PyObject_IsInstance(
70 converter::registered<T>::converters.m_class_object))
75 // Constructs an instance of the enumeration type in the from_python
78 void enum_<T>::construct(PyObject* obj, converter::rvalue_from_python_stage1_data* data)
80 T x = static_cast<T>(PyInt_AS_LONG(obj));
81 void* const storage = ((converter::rvalue_from_python_storage<T>*)data)->storage.bytes;
83 data->convertible = storage;
87 inline enum_<T>& enum_<T>::value(char const* name, T x)
89 this->add_value(name, static_cast<long>(x));
94 inline enum_<T>& enum_<T>::export_values()
96 this->base::export_values();
100 }} // namespace boost::python
102 #endif // ENUM_DWA200298_HPP