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