Skip to content

Commit 0df5ebf

Browse files
committed
Fix to allow accessing enums as data members
[SVN r16656]
1 parent 809535b commit 0df5ebf

3 files changed

Lines changed: 42 additions & 14 deletions

File tree

include/boost/python/data_members.hpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,25 @@
66
#ifndef DATA_MEMBERS_DWA2002328_HPP
77
# define DATA_MEMBERS_DWA2002328_HPP
88

9-
# include <boost/python/detail/config.hpp>
10-
# include <boost/python/detail/wrap_python.hpp>
11-
# include <boost/type_traits/transform_traits.hpp>
12-
# include <boost/type_traits/add_const.hpp>
13-
# include <boost/type_traits/add_reference.hpp>
149
# include <boost/python/return_value_policy.hpp>
1510
# include <boost/python/return_by_value.hpp>
1611
# include <boost/python/return_internal_reference.hpp>
17-
# include <boost/python/object/function_object.hpp>
1812
# include <boost/python/arg_from_python.hpp>
13+
14+
# include <boost/python/object/function_object.hpp>
15+
1916
# include <boost/python/converter/builtin_converters.hpp>
17+
18+
# include <boost/python/detail/indirect_traits.hpp>
19+
# include <boost/python/detail/config.hpp>
20+
# include <boost/python/detail/wrap_python.hpp>
21+
22+
# include <boost/type_traits/transform_traits.hpp>
23+
# include <boost/type_traits/add_const.hpp>
24+
# include <boost/type_traits/add_reference.hpp>
25+
2026
# include <boost/mpl/if.hpp>
27+
2128
# include <boost/bind.hpp>
2229

2330
namespace boost { namespace python {
@@ -71,16 +78,21 @@ namespace detail
7178
// and get the right result.
7279
template <class T>
7380
struct default_getter_policy
74-
: mpl::if_c<
75-
to_python_value<
76-
typename add_reference<
77-
typename add_const<T>::type
78-
>::type
79-
>::uses_registry
81+
{
82+
typedef typename add_reference<
83+
typename add_const<T>::type
84+
>::type t_cref;
85+
86+
BOOST_STATIC_CONSTANT(
87+
bool, by_ref = to_python_value<t_cref>::uses_registry
88+
&& is_reference_to_class<t_cref>::value);
89+
90+
typedef typename mpl::if_c<
91+
by_ref
8092
, return_internal_reference<>
8193
, return_value_policy<return_by_value>
82-
>
83-
{};
94+
>::type type;
95+
};
8496
}
8597

8698
template <class C, class D>

test/enum.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66
#include <boost/python/enum.hpp>
77
#include <boost/python/def.hpp>
88
#include <boost/python/module.hpp>
9+
#include <boost/python/class.hpp>
910

1011
using namespace boost::python;
1112

1213
enum color { red = 1, green = 2, blue = 4 };
1314

1415
color identity_(color x) { return x; }
1516

17+
struct colorized {
18+
colorized() : x(red) {}
19+
color x;
20+
};
21+
1622
BOOST_PYTHON_MODULE(enum_ext)
1723
{
1824
enum_<color>("color")
@@ -23,6 +29,10 @@ BOOST_PYTHON_MODULE(enum_ext)
2329
;
2430

2531
def("identity", identity_);
32+
33+
class_<colorized>("colorized")
34+
.def_readwrite("x", &colorized::x)
35+
;
2636
}
2737

2838
#include "module_tail.cpp"

test/enum.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
... except TypeError: pass
3838
... else: print 'expected a TypeError'
3939
40+
>>> c = colorized()
41+
>>> c.x
42+
enum_ext.color.red
43+
>>> c.x = green
44+
>>> c.x
45+
enum_ext.color.green
4046
'''
4147

4248
def run(args = None):

0 commit comments

Comments
 (0)