problems with swig generated code
Ernesto Castellotti
erny.castell at gmail.com
Thu Sep 12 09:38:04 UTC 2019
On Tuesday, 3 September 2019 at 20:03:37 UTC, Martin DeMello
wrote:
> On Sunday, 1 September 2019 at 11:19:11 UTC, DanielG wrote:
>> Do you know whether SWIG's D generator is even being
>> maintained?
>>
>> I've searched for it on the forums in the past and got the
>> impression that it's outdated.
>
> I didn't realise that :( It was included in the current release
> of swig, so I figured it was maintained.
>
> It's pretty sad if it's not, because trying to access C++
> libraries directly from D has some limitations (most notably
> not being able to create new C++ objects from D) and swig would
> have let things just work.
Not true, with the core.stdcpp.new_ module it is possible to
allocate the memory to create an object directly from D!
Just allocate the memory (YourCPPClass.sizeof) and call
std.conv.emplace to use the constructor
If you want there is also a wrapper that simplifies the operation
https://github.com/ErnyTech/CPPNew
A small example:
// test.cpp
#include <iostream>
class Test {
public:
Test(int);
~Test();
void set(int);
void print();
private:
int a;
};
Test::Test(int number) {
this->set(number);
}
Test::~Test() {
std::cout << "Destructor called" << std::endl;
}
void Test::set(int number) {
this->a = number;
}
void Test::print() {
std::cout << this->a << std::endl;
}
// test.d
extern(C++) {
class Test {
final this(int);
final ~this();
final void set(int);
final void print();
}
}
void main() {
import cppnew : CPPNew;
import cppnew : CPPDelete;
auto test = CPPNew!Test(67);
test.print(); // will print 67
test.setNumber(12);
test.print(); // will print 12
CPPDelete(test); // will print Destructor called
}
More information about the Digitalmars-d-learn
mailing list