Skip to content

Commit 4a5817d

Browse files
committed
enum export
[SVN r16604]
1 parent eab084c commit 4a5817d

File tree

7 files changed

+39
-1
lines changed

7 files changed

+39
-1
lines changed

doc/v2/enum.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ <h2><a name="examples"></a>Example(s)</h2>
219219

220220
Revised
221221
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
222-
13 November, 2002
222+
13 December, 2002
223223
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
224224

225225

include/boost/python/enum.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ struct enum_ : public objects::enum_base
2323
// Add a new enumeration value with the given name and value.
2424
inline enum_<T>& value(char const* name, T);
2525

26+
// Add all of the defined enumeration values to the current scope with the
27+
// same names used here.
28+
inline enum_<T>& export_values();
2629
private:
2730
static PyObject* to_python(void const* x);
2831
static void* convertible_from_python(PyObject* obj);
@@ -86,6 +89,13 @@ inline enum_<T>& enum_<T>::value(char const* name, T x)
8689
return *this;
8790
}
8891

92+
template <class T>
93+
inline enum_<T>& enum_<T>::export_values()
94+
{
95+
this->enum_base::export_values();
96+
return *this;
97+
}
98+
8999
}} // namespace boost::python
90100

91101
#endif // ENUM_DWA200298_HPP

include/boost/python/object/enum_base.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct BOOST_PYTHON_DECL enum_base : python::api::object
2525
, type_info);
2626

2727
void add_value(char const* name, long value);
28+
void export_values();
2829

2930
static PyObject* to_python(PyTypeObject* type, long x);
3031
};

src/object/enum.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <boost/python/dict.hpp>
1313
#include <boost/python/str.hpp>
1414
#include <boost/python/extract.hpp>
15+
#include <boost/python/object_protocol.hpp>
16+
#include <boost/python/detail/api_placeholder.hpp>
1517
#include <structmember.h>
1618
#include <cstdio>
1719

@@ -194,6 +196,18 @@ void enum_base::add_value(char const* name_, long value)
194196
p->name = incref(name.ptr());
195197
}
196198

199+
void enum_base::export_values()
200+
{
201+
dict d = extract<dict>(this->attr("values"))();
202+
list values = d.values();
203+
scope current;
204+
205+
for (unsigned i = 0, max = len(values); i < max; ++i)
206+
{
207+
api::setattr(current, object(values[i].attr("name")), values[i]);
208+
}
209+
}
210+
197211
PyObject* enum_base::to_python(PyTypeObject* type_, long x)
198212
{
199213
object type((type_handle(borrowed(type_))));

test/enum.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ BOOST_PYTHON_MODULE(enum_ext)
1919
.value("red", red)
2020
.value("green", green)
2121
.value("blue", blue)
22+
.export_values()
2223
;
2324

2425
def("identity", identity_);

test/enum.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
enum_ext.color(3)
2121
2222
>>> identity(color(4))
23+
enum_ext.color.blue
24+
25+
--- check export to scope ---
26+
27+
>>> identity(red)
28+
enum_ext.color.red
29+
30+
>>> identity(green)
31+
enum_ext.color.green
32+
33+
>>> identity(blue)
2334
enum_ext.color.blue
2435
2536
>>> try: identity(1)

todo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ types
66

77
FILE* conversions: http://aspn.activestate.com/ASPN/Mail/Message/1411366
88

9+
Finish shared_ptr support: http://aspn.activestate.com/ASPN/Mail/Message/c++-sig/1456023
910

1011
Medium Priority:
1112
----------------

0 commit comments

Comments
 (0)