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:
@@ -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" -->
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
@@ -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_))));
|
||||
|
||||
@@ -19,6 +19,7 @@ BOOST_PYTHON_MODULE(enum_ext)
|
||||
.value("red", red)
|
||||
.value("green", green)
|
||||
.value("blue", blue)
|
||||
.export_values()
|
||||
;
|
||||
|
||||
def("identity", identity_);
|
||||
|
||||
11
test/enum.py
11
test/enum.py
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user