2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-21 05:02:17 +00:00

enum export

[SVN r16604]
This commit is contained in:
Dave Abrahams
2002-12-13 20:04:34 +00:00
parent eab084c9a2
commit 4a5817d8ba
7 changed files with 39 additions and 1 deletions

View File

@@ -219,7 +219,7 @@ TypeError: bad argument type for built-in operation
Revised
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
13 November, 2002
13 December, 2002
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->

View File

@@ -23,6 +23,9 @@ struct enum_ : public objects::enum_base
// Add a new enumeration value with the given name and value.
inline enum_<T>& value(char const* name, T);
// Add all of the defined enumeration values to the current scope with the
// same names used here.
inline enum_<T>& export_values();
private:
static PyObject* to_python(void const* x);
static void* convertible_from_python(PyObject* obj);
@@ -86,6 +89,13 @@ inline enum_<T>& enum_<T>::value(char const* name, T x)
return *this;
}
template <class T>
inline enum_<T>& enum_<T>::export_values()
{
this->enum_base::export_values();
return *this;
}
}} // namespace boost::python
#endif // ENUM_DWA200298_HPP

View File

@@ -25,6 +25,7 @@ struct BOOST_PYTHON_DECL enum_base : python::api::object
, type_info);
void add_value(char const* name, long value);
void export_values();
static PyObject* to_python(PyTypeObject* type, long x);
};

View File

@@ -12,6 +12,8 @@
#include <boost/python/dict.hpp>
#include <boost/python/str.hpp>
#include <boost/python/extract.hpp>
#include <boost/python/object_protocol.hpp>
#include <boost/python/detail/api_placeholder.hpp>
#include <structmember.h>
#include <cstdio>
@@ -194,6 +196,18 @@ void enum_base::add_value(char const* name_, long value)
p->name = incref(name.ptr());
}
void enum_base::export_values()
{
dict d = extract<dict>(this->attr("values"))();
list values = d.values();
scope current;
for (unsigned i = 0, max = len(values); i < max; ++i)
{
api::setattr(current, object(values[i].attr("name")), values[i]);
}
}
PyObject* enum_base::to_python(PyTypeObject* type_, long x)
{
object type((type_handle(borrowed(type_))));

View File

@@ -19,6 +19,7 @@ BOOST_PYTHON_MODULE(enum_ext)
.value("red", red)
.value("green", green)
.value("blue", blue)
.export_values()
;
def("identity", identity_);

View File

@@ -20,6 +20,17 @@ enum_ext.color.green
enum_ext.color(3)
>>> identity(color(4))
enum_ext.color.blue
--- check export to scope ---
>>> identity(red)
enum_ext.color.red
>>> identity(green)
enum_ext.color.green
>>> identity(blue)
enum_ext.color.blue
>>> try: identity(1)

View File

@@ -6,6 +6,7 @@ types
FILE* conversions: http://aspn.activestate.com/ASPN/Mail/Message/1411366
Finish shared_ptr support: http://aspn.activestate.com/ASPN/Mail/Message/c++-sig/1456023
Medium Priority:
----------------