Chris@16: // Copyright David Abrahams 2002. Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: #ifndef ENUM_DWA200298_HPP Chris@16: # define ENUM_DWA200298_HPP Chris@16: Chris@16: # include Chris@16: Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: Chris@16: namespace boost { namespace python { Chris@16: Chris@16: template Chris@16: struct enum_ : public objects::enum_base Chris@16: { Chris@16: typedef objects::enum_base base; Chris@16: Chris@16: // Declare a new enumeration type in the current scope() Chris@16: enum_(char const* name, char const* doc = 0); Chris@16: Chris@16: // Add a new enumeration value with the given name and value. Chris@16: inline enum_& value(char const* name, T); Chris@16: Chris@16: // Add all of the defined enumeration values to the current scope with the Chris@16: // same names used here. Chris@16: inline enum_& export_values(); Chris@16: private: Chris@16: static PyObject* to_python(void const* x); Chris@16: static void* convertible_from_python(PyObject* obj); Chris@16: static void construct(PyObject* obj, converter::rvalue_from_python_stage1_data* data); Chris@16: }; Chris@16: Chris@16: template Chris@16: inline enum_::enum_(char const* name, char const* doc ) Chris@16: : base( Chris@16: name Chris@16: , &enum_::to_python Chris@16: , &enum_::convertible_from_python Chris@16: , &enum_::construct Chris@16: , type_id() Chris@16: , doc Chris@16: ) Chris@16: { Chris@16: } Chris@16: Chris@16: // This is the conversion function that gets registered for converting Chris@16: // these enums to Python. Chris@16: template Chris@16: PyObject* enum_::to_python(void const* x) Chris@16: { Chris@16: return base::to_python( Chris@16: converter::registered::converters.m_class_object Chris@16: , static_cast(*(T const*)x)); Chris@16: } Chris@16: Chris@16: // Chris@16: // The following two static functions serve as the elements of an Chris@16: // rvalue from_python converter for the enumeration type. Chris@16: // Chris@16: Chris@16: // This checks that a given Python object can be converted to the Chris@16: // enumeration type. Chris@16: template Chris@16: void* enum_::convertible_from_python(PyObject* obj) Chris@16: { Chris@16: return PyObject_IsInstance( Chris@16: obj Chris@16: , upcast( Chris@16: converter::registered::converters.m_class_object)) Chris@16: Chris@16: ? obj : 0; Chris@16: } Chris@16: Chris@16: // Constructs an instance of the enumeration type in the from_python Chris@16: // data. Chris@16: template Chris@16: void enum_::construct(PyObject* obj, converter::rvalue_from_python_stage1_data* data) Chris@16: { Chris@16: #if PY_VERSION_HEX >= 0x03000000 Chris@16: T x = static_cast(PyLong_AS_LONG(obj)); Chris@16: #else Chris@16: T x = static_cast(PyInt_AS_LONG(obj)); Chris@16: #endif Chris@16: void* const storage = ((converter::rvalue_from_python_storage*)data)->storage.bytes; Chris@16: new (storage) T(x); Chris@16: data->convertible = storage; Chris@16: } Chris@16: Chris@16: template Chris@16: inline enum_& enum_::value(char const* name, T x) Chris@16: { Chris@16: this->add_value(name, static_cast(x)); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: inline enum_& enum_::export_values() Chris@16: { Chris@16: this->base::export_values(); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: }} // namespace boost::python Chris@16: Chris@16: #endif // ENUM_DWA200298_HPP