Differences between revisions 1 and 2
Revision 1 as of 2002-09-26 01:12:02
Size: 1021
Editor: MikeRovner
Comment:
Revision 2 as of 2002-11-15 19:46:18
Size: 937
Editor: MikeRovner
Comment: v2
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
#include <boost/python/enum.hpp>
#include <boost/python/def.hpp>
#include <boost/python/module_init.hpp>
#include <boost/python.hpp>
Line 13: Line 11:
BOOST_PYTHON_MODULE_INIT(enum_ext) BOOST_PYTHON_MODULE(enum_ext)

For example, let's define an object in module namespace to represent enum:

#include <boost/python.hpp>

using namespace boost::python;

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

color identity_(color x) { return x; }

BOOST_PYTHON_MODULE(enum_ext)
{
    enum_<color>("color")
        .value("red", red)
        .value("green", green)
        .value("blue", blue)
        ;
    
    def("identity", identity_);
}

The usage is follows:

>>> from enum_ext import *

>>> identity(color.red)
enum_ext.color.red

>>> identity(color.green)
enum_ext.color.green

>>> identity(color.blue)
enum_ext.color.blue

>>> identity(color(1))
enum_ext.color.red

>>> identity(color(2))
enum_ext.color.green

>>> identity(color(3))
enum_ext.color(3)

>>> identity(color(4))
enum_ext.color.blue

>>> try: identity(1)
... except TypeError: pass
... else: print 'expected a TypeError'

boost.python/WrappingEnums (last edited 2008-11-15 14:01:18 by localhost)

Unable to edit the page? See the FrontPage for instructions.