www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Interfacing with C++ std::shared_ptr and std::unique_ptr

reply Andre Pany <andre s-e-a-p.de> writes:
Hi,

I would like to interface with the library 
https://github.com/NTNU-IHB/FMI4cpp
and have following class definitions in the header file:

``` c++
namespace fmi4cpp {

     template<class ModelDescription>
     class fmu_base {

     public:

         const std::string guid() const {
             return get_model_description()->guid;
         }

         const std::string model_name() const {
             return get_model_description()->model_name;
         }

         virtual std::shared_ptr<const ModelDescription> 
get_model_description() const = 0;

     };

     template<class model_description, class cs_fmu, class me_fmu>
     class fmu_provider : public virtual 
fmu_base<model_description> {

     public:

         virtual bool supports_cs() const = 0;

         virtual bool supports_me() const = 0;

         virtual std::unique_ptr <cs_fmu> as_cs_fmu() const = 0;

         virtual std::unique_ptr <me_fmu> as_me_fmu() const = 0;

     };
```

While unique_ptr is already available in drunime library 
core.stdcpp.memory,
shared_ptr is missing. Does someone already have translated 
shared_ptr?

Also, the C++ classes make use of templates. Is it still possible 
to call these
classes from D?

Kind regards
Andre
Jun 09 2020
next sibling parent evilrat <evilrat666 gmail.com> writes:
On Wednesday, 10 June 2020 at 06:43:24 UTC, Andre Pany wrote:
 Also, the C++ classes make use of templates. Is it still 
 possible to call these
 classes from D?
It should be, I did something similar and it worked. But it was quite some time ago so I don't remember exact situation and any details. However you still be out of luck if the types you are trying to use is present in header only, since there is simply nothing to link with, so now you have to port whole template to D, or make dummy wrapper in C++ that forces the compiler to emit those symbols. Same with ctor/dtors, if you have a class that is only used in some executable and not present in any of a .lib/.a form you're stuck. This is especially annoying with destructors because it renders the whole thing unusable without helper libraries that does new/delete in order to get the damn symbols emitted in object files to be able to link.
Jun 09 2020
prev sibling parent reply Mathias LANG <geod24 gmail.com> writes:
On Wednesday, 10 June 2020 at 06:43:24 UTC, Andre Pany wrote:
 Hi,

 I would like to interface with the library 
 https://github.com/NTNU-IHB/FMI4cpp
 and have following class definitions in the header file:

 ``` c++
 namespace fmi4cpp {

     template<class ModelDescription>
     class fmu_base {

     public:

         const std::string guid() const {
             return get_model_description()->guid;
         }

         const std::string model_name() const {
             return get_model_description()->model_name;
         }

         virtual std::shared_ptr<const ModelDescription> 
 get_model_description() const = 0;

     };

     template<class model_description, class cs_fmu, class 
 me_fmu>
     class fmu_provider : public virtual 
 fmu_base<model_description> {

     public:

         virtual bool supports_cs() const = 0;

         virtual bool supports_me() const = 0;

         virtual std::unique_ptr <cs_fmu> as_cs_fmu() const = 0;

         virtual std::unique_ptr <me_fmu> as_me_fmu() const = 0;

     };
 ```

 While unique_ptr is already available in drunime library 
 core.stdcpp.memory,
 shared_ptr is missing. Does someone already have translated 
 shared_ptr?

 Also, the C++ classes make use of templates. Is it still 
 possible to call these
 classes from D?

 Kind regards
 Andre
Depending on your needs, it might be trivial. We use this, and it works accross all 3 platforms: https://github.com/bpfkorea/agora/blob/ddd65e2fc3975d9c14ad36bcf28e5cd32de08945/source/scpd/Cpp.d#L38-L64 As mentioned, you will need to have the instantiation done on the C++ side, but that's true for any template. Feel free to ask on slack #cpp-interop.
Jun 10 2020
parent Andre Pany <andre s-e-a-p.de> writes:
On Wednesday, 10 June 2020 at 08:31:47 UTC, Mathias LANG wrote:
 On Wednesday, 10 June 2020 at 06:43:24 UTC, Andre Pany wrote:
 [...]
Depending on your needs, it might be trivial. We use this, and it works accross all 3 platforms: https://github.com/bpfkorea/agora/blob/ddd65e2fc3975d9c14ad36bcf28e5cd32de08945/source/scpd/Cpp.d#L38-L64 As mentioned, you will need to have the instantiation done on the C++ side, but that's true for any template. Feel free to ask on slack #cpp-interop.
Thanks for all the answers. I will have a look. Kind regards Andre
Jun 10 2020