Neal Becker | 4 Mar 17:21
Picon

Re: [Cython] wrapping c++

Stefan Behnel wrote:

> William Stein wrote:
>> cdef extern from "":
>>     void DEFINE_SET "#define SET(a,b,c) a[b]=c; //"()
>>     void SET(intvec, int, int)
>> DEFINE_SET()
>>
>> The key thing is the evil dirty trick to define a SET macro directly in
>> Cython.
> 
> "Evil" and "dirty" were exactly the two words that came to my mind before
> I even got to reading your own comment on this. :)
> 
> I would really put something like this into an external header file, and
> just declare "SET()" as being external...
> 
> Stefan
Any thoughts on what it would take to support a non-evil implementation?

Stefan Behnel | 4 Mar 18:07
Picon
Favicon

Re: [Cython] wrapping c++

Neal Becker wrote:
> Stefan Behnel wrote:
>
>> William Stein wrote:
>>> cdef extern from "":
>>>     void DEFINE_SET "#define SET(a,b,c) a[b]=c; //"()
>>>     void SET(intvec, int, int)
>>> DEFINE_SET()
>>>
>>> The key thing is the evil dirty trick to define a SET macro directly in
>>> Cython.
>>
>> "Evil" and "dirty" were exactly the two words that came to my mind
>> before I even got to reading your own comment on this. :)
>>
>> I would really put something like this into an external header file, and
>> just declare "SET()" as being external...
>>
> Any thoughts on what it would take to support a non-evil implementation?

The above is ok, except that I wouldn't write something like this into a
Cython file. You will likely need a header file for a couple of low-level
things anyway, so I would just create one with the "#define" above and
keep the 'cdef extern from "theheader.h"' for the "SET()" function (read:
macro).

Stefan

Robert Bradshaw | 4 Mar 20:30
Favicon

Re: [Cython] wrapping c++

On Mar 4, 2008, at 9:07 AM, Stefan Behnel wrote:

> Neal Becker wrote:
>> Stefan Behnel wrote:
>>
>>> William Stein wrote:
>>>> cdef extern from "":
>>>>     void DEFINE_SET "#define SET(a,b,c) a[b]=c; //"()
>>>>     void SET(intvec, int, int)
>>>> DEFINE_SET()
>>>>
>>>> The key thing is the evil dirty trick to define a SET macro  
>>>> directly in
>>>> Cython.
>>>
>>> "Evil" and "dirty" were exactly the two words that came to my mind
>>> before I even got to reading your own comment on this. :)
>>>
>>> I would really put something like this into an external header  
>>> file, and
>>> just declare "SET()" as being external...
>>>
>> Any thoughts on what it would take to support a non-evil  
>> implementation?
>
> The above is ok, except that I wouldn't write something like this  
> into a
> Cython file. You will likely need a header file for a couple of low- 
> level
> things anyway, so I would just create one with the "#define" above and
(Continue reading)

Neal Becker | 4 Mar 22:28
Picon

Re: [Cython] wrapping c++

Robert Bradshaw wrote:

> On Mar 4, 2008, at 9:07 AM, Stefan Behnel wrote:
> 
>> Neal Becker wrote:
>>> Stefan Behnel wrote:
>>>
>>>> William Stein wrote:
>>>>> cdef extern from "":
>>>>>     void DEFINE_SET "#define SET(a,b,c) a[b]=c; //"()
>>>>>     void SET(intvec, int, int)
>>>>> DEFINE_SET()
>>>>>
>>>>> The key thing is the evil dirty trick to define a SET macro
>>>>> directly in
>>>>> Cython.
>>>>
>>>> "Evil" and "dirty" were exactly the two words that came to my mind
>>>> before I even got to reading your own comment on this. :)
>>>>
>>>> I would really put something like this into an external header
>>>> file, and
>>>> just declare "SET()" as being external...
>>>>
>>> Any thoughts on what it would take to support a non-evil
>>> implementation?
>>
>> The above is ok, except that I wouldn't write something like this
>> into a
>> Cython file. You will likely need a header file for a couple of low-
(Continue reading)

Stefan Behnel | 5 Mar 08:40
Picon
Favicon

Re: [Cython] wrapping c++

Neal Becker wrote:
> Is operator the real issue here?  In the std::vector example, there is
> 'at',
> which does (almost) the same thing as operator[].  My problem is that
> both 'at' and 'operator[]' return int&, and to use them you need to
> say 'at[i]=j', which I don't know how to do in cython (except for the
> macro
> version above,  or to write additional wrapper code in more c++ headers).

If you declare a variable

    cdef int* myvar

Cython allows you to say

    myvar[5] = 1

Is that what you want?

Stefan

Neal Becker | 5 Mar 12:09
Picon

Re: [Cython] wrapping c++

On Wednesday 05 March 2008, Stefan Behnel wrote:
> Neal Becker wrote:
> > Is operator the real issue here?  In the std::vector example, there is
> > 'at',
> > which does (almost) the same thing as operator[].  My problem is that
> > both 'at' and 'operator[]' return int&, and to use them you need to
> > say 'at[i]=j', which I don't know how to do in cython (except for the
> > macro
> > version above,  or to write additional wrapper code in more c++ headers).
>
> If you declare a variable
>
>     cdef int* myvar
>
> Cython allows you to say
>
>     myvar[5] = 1
>
> Is that what you want?
>
> Stefan

Does that help for a function that returns int&?

Let's say we have a c++ class intvec, it has
int& at(int index);
Stefan Behnel | 5 Mar 13:36
Picon
Favicon

Re: [Cython] wrapping c++

Neal Becker wrote:
>> Neal Becker wrote:
>> > Is operator the real issue here?  In the std::vector example, there is
>> > 'at',
>> > which does (almost) the same thing as operator[].  My problem is that
>> > both 'at' and 'operator[]' return int&, and to use them you need to
>> > say 'at[i]=j', which I don't know how to do in cython (except for the
>> > macro
>> > version above,  or to write additional wrapper code in more c++
>> headers).
>
> Let's say we have a c++ class intvec, it has
> int& at(int index);

I just looked that up, int& is a C++ reference here (didn't know that
notation). What I would do is: create a header file and use it to define
macros that do C++ operations on references in a function-like style.
Things like the SET() 'function' that William proposed.

Then, you define these macros as being external functions in a .pxd file,
and cimport them into your .pyx file. Then you can use them in your Cython
code as normal functions, and Cython will generate the macro calls for
you, that the C preprocessor will translate into the correct operations.

As Robert suggested, enhancing Cython's syntax to understand references
would be helpful in the long term.

As for the return type of your at() function, have you tried declaring it
as returning "int*" instead? What does Cython do in this case?

(Continue reading)

Picon
Picon

Re: [Cython] wrapping c++


>
> As for the return type of your at() function, have you tried declaring it
> as returning "int*" instead? What does Cython do in this case?
>   
C++ references doesn't work that way, I don't think that would work. C++ 
code would look like this:

vector<int> v(10);
v.at(3) = 4;

If you declare the return type to be int* you simply have a problem...

Dag Sverre
Picon
Picon

Re: [Cython] wrapping c++


>
> If you declare the return type to be int* you simply have a problem...
Aw...sorry, hit the send combination while in the middle of typing!

What I meant to say is that it won't solve the problem - Cython will 
treat it as a pointer type. AFAIK the only way to assign to an lvalue in 
C is if you dereference it, ie

*lvalueReturningFunc() = rvalue;

But references have automatic dereferencing, so the syntax above can 
never work, and the needed syntax:

lvalueReturningFunc() = rvalue

is not legal C IIRC. So macros are the way to go.

--

-- 
Dag Sverre


Gmane