From 0df5ebf0fad2dcac06a85cbdb94d7a310328a2e9 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Wed, 18 Dec 2002 21:11:16 +0000 Subject: [PATCH] Fix to allow accessing enums as data members [SVN r16656] --- include/boost/python/data_members.hpp | 40 +++++++++++++++++---------- test/enum.cpp | 10 +++++++ test/enum.py | 6 ++++ 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/include/boost/python/data_members.hpp b/include/boost/python/data_members.hpp index a8ff3871..9c4574b4 100644 --- a/include/boost/python/data_members.hpp +++ b/include/boost/python/data_members.hpp @@ -6,18 +6,25 @@ #ifndef DATA_MEMBERS_DWA2002328_HPP # define DATA_MEMBERS_DWA2002328_HPP -# include -# include -# include -# include -# include # include # include # include -# include # include + +# include + # include + +# include +# include +# include + +# include +# include +# include + # include + # include namespace boost { namespace python { @@ -71,16 +78,21 @@ namespace detail // and get the right result. template struct default_getter_policy - : mpl::if_c< - to_python_value< - typename add_reference< - typename add_const::type - >::type - >::uses_registry + { + typedef typename add_reference< + typename add_const::type + >::type t_cref; + + BOOST_STATIC_CONSTANT( + bool, by_ref = to_python_value::uses_registry + && is_reference_to_class::value); + + typedef typename mpl::if_c< + by_ref , return_internal_reference<> , return_value_policy - > - {}; + >::type type; + }; } template diff --git a/test/enum.cpp b/test/enum.cpp index fb1439c7..09a916c3 100644 --- a/test/enum.cpp +++ b/test/enum.cpp @@ -6,6 +6,7 @@ #include #include #include +#include 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") @@ -23,6 +29,10 @@ BOOST_PYTHON_MODULE(enum_ext) ; def("identity", identity_); + + class_("colorized") + .def_readwrite("x", &colorized::x) + ; } #include "module_tail.cpp" diff --git a/test/enum.py b/test/enum.py index 00484670..e99e401e 100644 --- a/test/enum.py +++ b/test/enum.py @@ -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):