os/ossrv/ossrv_pub/boost_apis/boost/python/enum.hpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/python/enum.hpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,102 @@
     1.4 +// Copyright David Abrahams 2002.
     1.5 +// Distributed under the Boost Software License, Version 1.0. (See
     1.6 +// accompanying file LICENSE_1_0.txt or copy at
     1.7 +// http://www.boost.org/LICENSE_1_0.txt)
     1.8 +#ifndef ENUM_DWA200298_HPP
     1.9 +# define ENUM_DWA200298_HPP
    1.10 +
    1.11 +# include <boost/python/detail/prefix.hpp>
    1.12 +
    1.13 +# include <boost/python/object/enum_base.hpp>
    1.14 +# include <boost/python/converter/rvalue_from_python_data.hpp>
    1.15 +# include <boost/python/converter/registered.hpp>
    1.16 +
    1.17 +namespace boost { namespace python { 
    1.18 +
    1.19 +template <class T>
    1.20 +struct enum_ : public objects::enum_base
    1.21 +{
    1.22 +    typedef objects::enum_base base;
    1.23 +
    1.24 +    // Declare a new enumeration type in the current scope()
    1.25 +    enum_(char const* name);
    1.26 +
    1.27 +    // Add a new enumeration value with the given name and value.
    1.28 +    inline enum_<T>& value(char const* name, T);
    1.29 +
    1.30 +    // Add all of the defined enumeration values to the current scope with the
    1.31 +    // same names used here.
    1.32 +    inline enum_<T>& export_values();
    1.33 + private:
    1.34 +    static PyObject* to_python(void const* x);
    1.35 +    static void* convertible_from_python(PyObject* obj);
    1.36 +    static void construct(PyObject* obj, converter::rvalue_from_python_stage1_data* data);
    1.37 +};
    1.38 +
    1.39 +template <class T>
    1.40 +inline enum_<T>::enum_(char const* name)
    1.41 +    : base(
    1.42 +        name
    1.43 +        , &enum_<T>::to_python
    1.44 +        , &enum_<T>::convertible_from_python
    1.45 +        , &enum_<T>::construct
    1.46 +        , type_id<T>())
    1.47 +{
    1.48 +}
    1.49 +
    1.50 +// This is the conversion function that gets registered for converting
    1.51 +// these enums to Python.
    1.52 +template <class T>
    1.53 +PyObject* enum_<T>::to_python(void const* x)
    1.54 +{
    1.55 +    return base::to_python(
    1.56 +        converter::registered<T>::converters.m_class_object
    1.57 +        , static_cast<long>(*(T const*)x));
    1.58 +}
    1.59 +
    1.60 +//
    1.61 +// The following two static functions serve as the elements of an
    1.62 +// rvalue from_python converter for the enumeration type.
    1.63 +//
    1.64 +
    1.65 +// This checks that a given Python object can be converted to the
    1.66 +// enumeration type.
    1.67 +template <class T>
    1.68 +void* enum_<T>::convertible_from_python(PyObject* obj)
    1.69 +{
    1.70 +    return PyObject_IsInstance(
    1.71 +        obj
    1.72 +        , upcast<PyObject>(
    1.73 +            converter::registered<T>::converters.m_class_object))
    1.74 +        
    1.75 +        ? obj : 0;
    1.76 +}
    1.77 +
    1.78 +// Constructs an instance of the enumeration type in the from_python
    1.79 +// data.
    1.80 +template <class T>
    1.81 +void enum_<T>::construct(PyObject* obj, converter::rvalue_from_python_stage1_data* data)
    1.82 +{
    1.83 +    T x = static_cast<T>(PyInt_AS_LONG(obj));
    1.84 +    void* const storage = ((converter::rvalue_from_python_storage<T>*)data)->storage.bytes;
    1.85 +    new (storage) T(x);
    1.86 +    data->convertible = storage;
    1.87 +}
    1.88 +
    1.89 +template <class T>
    1.90 +inline enum_<T>& enum_<T>::value(char const* name, T x)
    1.91 +{
    1.92 +    this->add_value(name, static_cast<long>(x));
    1.93 +    return *this;
    1.94 +}
    1.95 +
    1.96 +template <class T>
    1.97 +inline enum_<T>& enum_<T>::export_values()
    1.98 +{
    1.99 +    this->base::export_values();
   1.100 +    return *this;
   1.101 +}
   1.102 +
   1.103 +}} // namespace boost::python
   1.104 +
   1.105 +#endif // ENUM_DWA200298_HPP