Using enums (in C++)

892 views
Skip to first unread message

Wichert Akkerman

unread,
Feb 11, 2011, 7:27:30 AM2/11/11
to cython...@googlegroups.com
I have a C++ API which looks like this:

class Region {
public:
enum region_type { west, east };
void setRegion(region_type region);
}

the cython wrapper looks like this:

cdef:
enum region_type:
Europe
Asia

cdef extern from "region.hh":
cdef cppclass Region:
void setRegion(region_type region)


This fails to compile:

region.cpp: In function �PyObject*
__pyx_f_8blackbox_SetRegion(__pyx_t_7cregion_region_type)�:
region.cpp:437: error: no matching function for call to
�Region.setRegion(__pyx_t_7cregion_region_type&)�
region.hh:52: note: candidates are: void
Region::setRegion(Region::region_type)

Is there a way to use the real Region::region_type enum?

Wichert.

Dag Sverre Seljebotn

unread,
Feb 11, 2011, 7:37:39 AM2/11/11
to cython...@googlegroups.com
On 02/11/2011 01:27 PM, Wichert Akkerman wrote:
> I have a C++ API which looks like this:
>
> class Region {
> public:
> enum region_type { west, east };
> void setRegion(region_type region);
> }
>
>
>
> the cython wrapper looks like this:
>
> cdef:

Should have:

cdef extern from "region.hh":

As it is, you made Cython recreate a (different) enum type.

Dag

Wichert Akkerman

unread,
Feb 11, 2011, 7:51:15 AM2/11/11
to cython...@googlegroups.com
On 2/11/11 13:37 , Dag Sverre Seljebotn wrote:
> On 02/11/2011 01:27 PM, Wichert Akkerman wrote:
>> I have a C++ API which looks like this:
>>
>> class Region {
>> public:
>> enum region_type { west, east };
>> void setRegion(region_type region);
>> }
>>
>>
>>
>> the cython wrapper looks like this:
>>
>> cdef:
>
> Should have:
>
> cdef extern from "region.hh":
>
> As it is, you made Cython recreate a (different) enum type.

That's certainly an improvement and an obvious mistake om my side.
Unfortunately that assumes a global enum, while I have an enum inside
the class. Some quick experiments suggest that cython does not support
nested types inside classes other than other classes. For example:

// C++ source
class Foo {
public:
enum my_enum { one, two };
type std::vector<int> int_vector;
};


# Cython source
from libcpp.vector import vector
cdef extern from "foo.hh":
cdef cppclass Foo:
enum my_enum:
one
two
ctypedef vector[int_vector]


results in syntax errors both for the enum and the ctypedef lines. Is
that analysis correct, or am I making another mistake here?

Wichert.

Robert Bradshaw

unread,
Feb 11, 2011, 12:57:54 PM2/11/11
to cython...@googlegroups.com

You are correct, it's not supported yet. You may be able to get the
behavior you want by specifying

cdef extern from "foo.hh":
enum my_enum "Foo::my_enum":
one "Foo::one"
...

I've created http://trac.cython.org/cython_trac/ticket/659

- Robert

Wichert Akkerman

unread,
Feb 12, 2011, 4:05:11 AM2/12/11
to cython...@googlegroups.com

Unfortunately I appear to run into another issue: Cython does not like a
ctypedef for a template type. This code:

from libcpp.vector cimport vector
cdef extern from "foo.hh":
ctypedef vector[int] my_type

Results in a syntax error for the ctypedef statement.

Wichert.

--
Wichert Akkerman <wic...@wiggy.net> It is simple to make things.
http://www.wiggy.net/ It is hard to make things simple.

Robert Bradshaw

unread,
Feb 12, 2011, 12:24:38 PM2/12/11
to cython...@googlegroups.com
On Sat, Feb 12, 2011 at 1:05 AM, Wichert Akkerman <wic...@wiggy.net> wrote:

> Unfortunately I appear to run into another issue: Cython does not like a
> ctypedef for a template type. This code:
>
> from libcpp.vector cimport vector
> cdef extern from "foo.hh":
>    ctypedef vector[int] my_type
>
> Results in a syntax error for the ctypedef statement.
>
> Wichert.

Yep, this is a known issue: http://trac.cython.org/cython_trac/ticket/551

C++ support is a relatively recent and still incomplete addition.

- Robert

Reply all
Reply to author
Forward
0 new messages