2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-23 05:42:30 +00:00

Fix to allow accessing enums as data members

[SVN r16656]
This commit is contained in:
Dave Abrahams
2002-12-18 21:11:16 +00:00
parent 809535b934
commit 0df5ebf0fa
3 changed files with 42 additions and 14 deletions

View File

@@ -6,18 +6,25 @@
#ifndef DATA_MEMBERS_DWA2002328_HPP
# define DATA_MEMBERS_DWA2002328_HPP
# include <boost/python/detail/config.hpp>
# include <boost/python/detail/wrap_python.hpp>
# include <boost/type_traits/transform_traits.hpp>
# include <boost/type_traits/add_const.hpp>
# include <boost/type_traits/add_reference.hpp>
# include <boost/python/return_value_policy.hpp>
# include <boost/python/return_by_value.hpp>
# include <boost/python/return_internal_reference.hpp>
# include <boost/python/object/function_object.hpp>
# include <boost/python/arg_from_python.hpp>
# include <boost/python/object/function_object.hpp>
# include <boost/python/converter/builtin_converters.hpp>
# include <boost/python/detail/indirect_traits.hpp>
# include <boost/python/detail/config.hpp>
# include <boost/python/detail/wrap_python.hpp>
# include <boost/type_traits/transform_traits.hpp>
# include <boost/type_traits/add_const.hpp>
# include <boost/type_traits/add_reference.hpp>
# include <boost/mpl/if.hpp>
# include <boost/bind.hpp>
namespace boost { namespace python {
@@ -71,16 +78,21 @@ namespace detail
// and get the right result.
template <class T>
struct default_getter_policy
: mpl::if_c<
to_python_value<
typename add_reference<
typename add_const<T>::type
>::type
>::uses_registry
{
typedef typename add_reference<
typename add_const<T>::type
>::type t_cref;
BOOST_STATIC_CONSTANT(
bool, by_ref = to_python_value<t_cref>::uses_registry
&& is_reference_to_class<t_cref>::value);
typedef typename mpl::if_c<
by_ref
, return_internal_reference<>
, return_value_policy<return_by_value>
>
{};
>::type type;
};
}
template <class C, class D>

View File

@@ -6,6 +6,7 @@
#include <boost/python/enum.hpp>
#include <boost/python/def.hpp>
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
using namespace boost::python;
@@ -13,6 +14,11 @@ enum color { red = 1, green = 2, blue = 4 };
color identity_(color x) { return x; }
struct colorized {
colorized() : x(red) {}
color x;
};
BOOST_PYTHON_MODULE(enum_ext)
{
enum_<color>("color")
@@ -23,6 +29,10 @@ BOOST_PYTHON_MODULE(enum_ext)
;
def("identity", identity_);
class_<colorized>("colorized")
.def_readwrite("x", &colorized::x)
;
}
#include "module_tail.cpp"

View File

@@ -37,6 +37,12 @@ enum_ext.color.blue
... except TypeError: pass
... else: print 'expected a TypeError'
>>> c = colorized()
>>> c.x
enum_ext.color.red
>>> c.x = green
>>> c.x
enum_ext.color.green
'''
def run(args = None):