http://qs1969.pair.com?node_id=197074


in reply to Re: Re: Writing XS frontends to C++ libraries.
in thread Writing XS frontends to C++ libraries.

The code you posted should look like this:

void MySettings::set(key, value) string * key string * value
You're right about the problems with references. Don't try to use '&' in the XS prototype -- it definitely does not mean the same as C++. Also remember that in XS a const object is different from non-const, so you need two typemap definitions if you want to handle const objects.

In general, you should use non-const pointers to interface C++ with perl -- eventually perl is going to stuff your object into an int, so it can't be bigger than a pointer. Pointers are conceptually easier to store, and const is going to be casted away anyways. Do your typemaps all use pointers?

If you want to interface perl with methods that take const or reference arguments, the easiest way is to write the XS prototype with non-const pointers and then call the method yourself in the CODE: section. Here's how you'd call MySettings::set(const string &key, const string &value) from XS:

void MySettings::set(key, value) string * key string * value CODE: THIS->set(*key, *value);
Try perldoc perlxs for more on XS. If you stick to non-const pointers you'll be able to follow most of the examples designed for C.

Replies are listed 'Best First'.
Re: XS and C++: keep it simple and use pointers
by kilinrax (Deacon) on Sep 11, 2002 at 22:50 UTC

    Thanks, that's helped a lot :)
    Unfortunately, however, some of the methods return objects, rather than pointers to objects; or take arguments in the same way - so still give errors as above.

    Currently I have lines like these for each object in my typemap:

    MySettings O_OBJECT MYSettings * O_OBJECT
    Which, looking at it, seems very wrong. IIrc, O_OBJECT is for pointers to objects, not objects. Is there an equivalent to O_OBJECT for objects themselves, or will this require a specialised typemap?