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