Interfacing with C++ std::shared_ptr and std::unique_ptr

Mathias LANG geod24 at gmail.com
Wed Jun 10 08:31:47 UTC 2020


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.


More information about the Digitalmars-d-learn mailing list