public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
* Calling c++ functions returning non-pod types
@ 2013-02-21 13:31 Philip Ashmore
  2013-02-21 15:19 ` Andrew Haley
  0 siblings, 1 reply; 6+ messages in thread
From: Philip Ashmore @ 2013-02-21 13:31 UTC (permalink / raw)
  To: libffi-discuss

Hi there.

I recently had the need to call a function that returns a c++ string.
It didn't work as std::string has constructors, destructors, assignment 
operators...

I eventually found the relevant documentation on this

http://mentorembedded.github.com/cxx-abi/abi.html#calls

which states that for such return types a hidden pointer to a temporary 
is needed.

I got it to work by lying about the function. Instead of

    string run_cmd(const char * cmd)

I told libffi that the function looked like

    void run_cmd(string * ret, const char * cmd)

and passed in a pointer to a string to assign the result to as the first 
argument.

You should know that I described the string instance as a pointer:

     string rc;
     string * rc_addr = & rc;

     ffi_type *args[2];
     args[0] = &ffi_type_pointer;
     args[1] = &ffi_type_pointer;

     void *values[2];
     values[0] = &rc_addr;
     values[1] = &s;

I think a better option would be to tell libffi that the return type is 
a "non-pod" and let it jump the hoops appropriate for the architecture - 
am I correct in guessing that my approach isn't portable?

Regards,
Philip Ashmore

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-02-21 17:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-21 13:31 Calling c++ functions returning non-pod types Philip Ashmore
2013-02-21 15:19 ` Andrew Haley
2013-02-21 17:48   ` Philip Ashmore
2013-02-21 17:51     ` Andrew Haley
2013-02-21 17:53       ` Andrew Haley
2013-02-21 17:57       ` Philip Ashmore

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).