[C++-sig] Need help on enum--a concrete example

David Abrahams dave at boost-consulting.com
Sat Sep 7 05:20:30 CEST 2002


Hi Min,

You are right that there's a problem with enum conversions.
Implicitly_convertible<> does not work for this job as one might have
hoped. Unfortunately, I don't think I'll be able to work on code for
solving this problem before 10/1. In the meantime, my best suggestion is to
use "thin wrapper" functions which take ints in place of the enum
parameters, and then forward the arguments on. To deal with class
constructors, you'll need to wrap a derived class:

struct A_wrap : A
{
   A_wrap() {}
   A_wrap(double x, int c) : A(x, (choice)c) {}
};

    ...

    class_<A, A_wrap>("A")
        .def(init<double,int>())
        ...
        ;


Tedious but effective.

HTH,
Dave

-----------------------------------------------------------
           David Abrahams * Boost Consulting
dave at boost-consulting.com * http://www.boost-consulting.com


----- Original Message -----
From: "Min Xu" <minxu at sci.ccny.cuny.edu>
To: <c++-sig at python.org>
Sent: Friday, September 06, 2002 11:06 PM
Subject: [C++-sig] Need help on enum--a concrete example


Hi,

I understand that the questions about enum had been asked before. I read
through them to no vail to solve my small problem at hand. From my
impression
of the document implicit.html, I wrote the following code:

#include <boost/python/module_init.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/implicit.hpp>

enum choice {red, blue};

class A
{
public:
A() : x(0), y(0) {}
A(double x, choice c) : x(x), y(c) {}
double get() { return x; }
double get1(double x) { return x; }
private:
double x, y;
};


BOOST_PYTHON_MODULE_INIT(AA)
{
  using namespace boost::python;

implicitly_convertible<int, enum choice>();

class_<A>("A")
           .def_init(args<double, choice>())
      .def("get", &A::get)
      .def("get1", &A::get1);
}


The compilation gave me:
g++ -I/usr/local/include -I/usr/include/python2.2  -c -Wall
-ftemplate-depth-100  -DBOOST_PYTHON_DYNAMIC_LIB -DBOOST_PYTHON_V2  -fPIC
AA.C
/usr/local/include/boost/python/converter/implicit.hpp: In function `static
void boost::python::converter::implicit<int,choice>::construct(PyObject *,
boost::python::converter::rvalue_from_python_stage1_data *)':
/usr/local/include/boost/python/implicit.hpp:23:   instantiated from
`boost::python::implicitly_convertible<int, choice>(boost::type<int> *,
boost::type<choice> *)'
AA.C:24:   instantiated from here
/usr/local/include/boost/python/converter/implicit.hpp:43: conversion from
`int' to `enum choice'
make: *** [AA.so] Error 1

Thanks!!!


By the way, I have also tried the alternative:
// namespace boost { namespace python {

//     template <class T>
//     struct enum_to_int_converter
//     {
// static PyObject* convert(T const& x)
//     {
// return PyInt_FromLong(x);
//     }
//     };

//     template <class T>
//     void enum_as_int(T* = 0)
//     {
// to_python_converter<T, enum_to_int_converter<T> >();
// implicitly_convertible<int, T>();
//     };

//     // position1:
//     void enum_as_int<choice>();
// }};

It doesnot work either.

Min Xu



_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig





More information about the Cplusplus-sig mailing list