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