digitalmars.D.learn - Interfacing with C++ std::shared_ptr and std::unique_ptr
- Andre Pany (37/37) Jun 09 2020 Hi,
- evilrat (15/18) Jun 09 2020 It should be, I did something similar and it worked. But it was
- Mathias LANG (7/45) Jun 10 2020 Depending on your needs, it might be trivial. We use this, and it
- Andre Pany (4/12) Jun 10 2020 Thanks for all the answers. I will have a look.
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
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
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 AndreDepending 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
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:Thanks for all the answers. I will have a look. 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