Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/python/enum.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright David Abrahams 2002.
0002 // Distributed under the Boost Software License, Version 1.0. (See
0003 // accompanying file LICENSE_1_0.txt or copy at
0004 // http://www.boost.org/LICENSE_1_0.txt)
0005 #ifndef ENUM_DWA200298_HPP
0006 # define ENUM_DWA200298_HPP
0007 
0008 # include <boost/python/detail/prefix.hpp>
0009 
0010 # include <boost/python/object/enum_base.hpp>
0011 # include <boost/python/converter/rvalue_from_python_data.hpp>
0012 # include <boost/python/converter/registered.hpp>
0013 
0014 namespace boost { namespace python { 
0015 
0016 template <class T>
0017 struct enum_ : public objects::enum_base
0018 {
0019     typedef objects::enum_base base;
0020 
0021     // Declare a new enumeration type in the current scope()
0022     enum_(char const* name, char const* doc = 0);
0023 
0024     // Add a new enumeration value with the given name and value.
0025     inline enum_<T>& value(char const* name, T);
0026 
0027     // Add all of the defined enumeration values to the current scope with the
0028     // same names used here.
0029     inline enum_<T>& export_values();
0030  private:
0031     static PyObject* to_python(void const* x);
0032     static void* convertible_from_python(PyObject* obj);
0033     static void construct(PyObject* obj, converter::rvalue_from_python_stage1_data* data);
0034 };
0035 
0036 template <class T>
0037 inline enum_<T>::enum_(char const* name, char const* doc )
0038     : base(
0039         name
0040         , &enum_<T>::to_python
0041         , &enum_<T>::convertible_from_python
0042         , &enum_<T>::construct
0043         , type_id<T>()
0044         , doc
0045         )
0046 {
0047 }
0048 
0049 // This is the conversion function that gets registered for converting
0050 // these enums to Python.
0051 template <class T>
0052 PyObject* enum_<T>::to_python(void const* x)
0053 {
0054     return base::to_python(
0055         converter::registered<T>::converters.m_class_object
0056         , static_cast<long>(*(T const*)x));
0057 }
0058 
0059 //
0060 // The following two static functions serve as the elements of an
0061 // rvalue from_python converter for the enumeration type.
0062 //
0063 
0064 // This checks that a given Python object can be converted to the
0065 // enumeration type.
0066 template <class T>
0067 void* enum_<T>::convertible_from_python(PyObject* obj)
0068 {
0069     return PyObject_IsInstance(
0070         obj
0071         , upcast<PyObject>(
0072             converter::registered<T>::converters.m_class_object))
0073         
0074         ? obj : 0;
0075 }
0076 
0077 // Constructs an instance of the enumeration type in the from_python
0078 // data.
0079 template <class T>
0080 void enum_<T>::construct(PyObject* obj, converter::rvalue_from_python_stage1_data* data)
0081 {
0082 #if PY_VERSION_HEX >= 0x03000000
0083     T x = static_cast<T>(PyLong_AS_LONG(obj));
0084 #else
0085     T x = static_cast<T>(PyInt_AS_LONG(obj));
0086 #endif
0087     void* const storage = ((converter::rvalue_from_python_storage<T>*)data)->storage.bytes;
0088     new (storage) T(x);
0089     data->convertible = storage;
0090 }
0091 
0092 template <class T>
0093 inline enum_<T>& enum_<T>::value(char const* name, T x)
0094 {
0095     this->add_value(name, static_cast<long>(x));
0096     return *this;
0097 }
0098 
0099 template <class T>
0100 inline enum_<T>& enum_<T>::export_values()
0101 {
0102     this->base::export_values();
0103     return *this;
0104 }
0105 
0106 }} // namespace boost::python
0107 
0108 #endif // ENUM_DWA200298_HPP