Discussion:
returning Object& from C++ methods
(too old to reply)
Scott Lanning
2007-09-07 13:02:06 UTC
Permalink
This is the last question I can remember having.
I'm unsure how to implement methods which return an Object& .

For example, I have a method getPosition here
http://www.ogre3d.org/docs/api/html/classOgre_1_1Node.html#Ogre_1_1TagPointa35

virtual const Vector3& Ogre::Node::getPosition(void) const

My understanding is that a reference is just an alias,
but....the memory handling confuses me.
Here's how I have it

Vector3 *
Node::getPosition()
CODE:
RETVAL = new Vector3;
*RETVAL = THIS->getPosition();
OUTPUT:
RETVAL

where the OUTPUT typemap for Vector3* would be

sv_setref_pv($arg, "Ogre::Vector3", (void *) $var);

Is that correct?


By the way, I'd love to get critiques of
http://search.cpan.org/~slanning/Ogre-0.25/MANIFEST
There are a lot of places where I feel uncertain
that what I'm doing is correct.

Thanks for your time
Steve Fink
2007-09-08 04:05:21 UTC
Permalink
Post by Scott Lanning
Vector3 *
Node::getPosition()
RETVAL = new Vector3;
*RETVAL = THIS->getPosition();
RETVAL
where the OUTPUT typemap for Vector3* would be
sv_setref_pv($arg, "Ogre::Vector3", (void *) $var);
Is that correct?
You might want to set the READONLY flag on the referenced SV, but
that's a minor safety nitpick. Otherwise that seems fine.

The memory management is problematic, though. Nothing is going to free
that Vector3 for you. For a leaf type like that, though, you can
probably get away with defining an Ogre::Vector3::DESTROY that frees
the memory.

I am wrapping something very similar (involving a proprietary C++ 3D
graphics framework), and I took a different approach for my equivalent
to your Vector3 (for which I use OpenInventor types, so that would be
SbVec3f). I convert it to an array ref of three values. I also convert
the other way. More recently, I've started blessing the reference into
a vector package that overloads various mathematical operators. But
it's all exposed directly at the Perl level, which saves having to go
through another layer of accessors to get at the contained X, Y, and Z
values. (It also avoids the memory management issue.)
Scott Lanning
2007-09-10 08:50:36 UTC
Permalink
Post by Steve Fink
Post by Scott Lanning
Vector3 *
Node::getPosition()
RETVAL = new Vector3;
*RETVAL = THIS->getPosition();
RETVAL
where the OUTPUT typemap for Vector3* would be
sv_setref_pv($arg, "Ogre::Vector3", (void *) $var);
Is that correct?
You might want to set the READONLY flag on the referenced SV, but
that's a minor safety nitpick. Otherwise that seems fine.
The memory management is problematic, though. Nothing is going to free
that Vector3 for you. For a leaf type like that, though, you can
probably get away with defining an Ogre::Vector3::DESTROY that frees
the memory.
Urgh.. at least that's what I was thinking. Thanks.
Post by Steve Fink
I am wrapping something very similar (involving a proprietary C++ 3D
graphics framework), and I took a different approach for my equivalent
to your Vector3 (for which I use OpenInventor types, so that would be
SbVec3f). I convert it to an array ref of three values. I also convert
the other way. More recently, I've started blessing the reference into
a vector package that overloads various mathematical operators. But
it's all exposed directly at the Perl level, which saves having to go
through another layer of accessors to get at the contained X, Y, and Z
values. (It also avoids the memory management issue.)
It's a good idea, then, I think, to bless an array ref instead of
a pointer to the C++ object. I'd been thinking of a hashref, too,
with $v->{x}, etc..
Either way, like you said, it would get rid of the ugly (and Perl-specific)
$v->getX, $v->getY, etc. methods by hopefully allowing assignment to each
vector component (like $v->[0] = 0.5, or $v->{x} = 0.5, or $v->x = 0.5).
Nicod Thomas
2007-09-10 09:36:20 UTC
Permalink
Dear all,

Could anyone of you provide us with a simple example on exchanging complex data between C/C++ methods and perl.
Thank you very much for your advices because I just can't find anything simple on this.
Regards,

Thomas

Loading...