[gambit-list] FFI/c-define-type is fine with the C++ std::shared_ptr RAII type?

Phillip Suero philsuero at gmail.com
Thu May 20 08:55:38 EDT 2021


Dear Marc, I was thinking, is the FFI (|c-define-type|) compatible with the
C++ RAII in such a way that C++'s std::shared_ptr can be used as
(c-define-type my-struct-shared "shared_ptr<my_struct>")?

shared_ptr is a refcounted slot, where the +1 and -1 refcount update is
carried out by the RAII.

If I would not pass the std::shared_ptr as (c-define-type my-struct-shared
"shared_ptr<my_struct>"), then instead I would wrap it in a C/C++ struct
which then contains the std::shared_ptr as its only slot.

Please let me know your thoughts on what's the best approach.

Here is a test case:


(c-declare #<<c-declare-end

#include <memory>

typedef struct my_struct
{
  int value;
} my_struct;
using namespace std;
c-declare-end
 )
(c-define-type my-struct-shared "shared_ptr<my_struct>")

(define (make-my-struct)
  (c-lambda () my-struct-shared #<<c-lambda-end
      std::shared_ptr<my_struct> out = std::make_shared<my_struct>();
      out->value = 0;
      ___return(out);
c-lambda-end
    ))

(make-my-struct)


Gambit v4.9.3

> (load "shared_ptr")
"/path/to/shared_ptr.o2"
> (make-my-struct)
#<procedure #2 shared_ptr#0>
> (define a (make-my-struct))
> a
#<procedure #2 shared_ptr#0>


At least it does compile! Looking forward to your confirmation if this is
safe & best practice.

( When I skip the "using namespace std;" and prepend "std::" to
(c-define-type my-struct-shared "shared_ptr<my_struct>"), I do get weird
compiler errors however. )

Phil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20210520/640e828d/attachment.htm>


More information about the Gambit-list mailing list