Re: question about call cpp class constructer without new , and define cpp delegate

2019-06-27 Thread evilrat via Digitalmars-d-learn

On Thursday, 27 June 2019 at 17:00:01 UTC, Rémy Mouëza wrote:


I though support for C++ allocation had improved. In a recent 
release, there was the addition of core.stdcpp.new, but I 
didn't try it out:

- http://dpldocs.info/experimental-docs/core.stdcpp.new_.html
- https://en.cppreference.com/w/cpp/memory/new/operator_new


It seems at this moment these operators only supported on Windows.


Are there still pitfalls to be wary of?


Not possible:

- new/delete (not entirely impossible, but requires to dig in 
compiler/library internals, generally fragile and not portable, 
not even mention that deciphering STL code is just meh...), 
though many libraries provides their own allocator/deallocator 
functions and some even provides pluggable hooks to use.

- Member functions pointers
- Multiple inheritance (tbh this is considered a bad practice 
anyway and chances to encounter it relatively small)

- lambdas?
- Exceptions? (except maybe on Windows, because of SEH)
- Template instantiations code gen(this basically requires to 
implement entire C++ compiler), what this means is that if the 
concrete template instance isn't used in a library you use you 
have to make dummy C++ function that forces compiler to emit the 
code for it.



Possible, but annoying:

- Functions with ref parameters

- Functions with const pointers to mutable data parameters
  ex: float calcStuff(float * const arr, size_t len);
It doesn't make sense in D, and I doubt there is much sense in 
C++ as well (except maybe to convey the meaning that this 
function won't try to free data).
So the workaround of course is to slap pragma mangle, which of 
course requires manually getting the mangled name for the 
function...


On Windows using MS compiler for the example above the mangled 
name will look like

  float calc(float * const arr); // ?calc@@YAMQEAM@Z
and for non const
  float calc(float * arr); // ?calc@@YAMPEAM@Z

so it is possible to do something like this
  pragma(mangle, calc.mangleof.replace("QEA","PEA"))
  float calc(float * arr);

- Some operator overloads also needs pragma mangle treatment.

- Incomplete/buggy mangling support, especially annoying with 
templates. Same treatment.



Maybe I've missed something else though, but anything not on the 
list usually works without surprises. And thanks to string 
namespaces it is now such a relief to use.


Re: question about call cpp class constructer without new , and define cpp delegate

2019-06-27 Thread Rémy Mouëza via Digitalmars-d-learn

On Thursday, 27 June 2019 at 05:57:49 UTC, evilrat wrote:

On Thursday, 27 June 2019 at 05:37:08 UTC, ChangLoong wrote:
If I want call cpp class constructer without new method, is 
there a way to do that ?


If what you really want is to actually allocate using C++ new 
operator from D, then that is very problematic and not portable 
even across compilers on same OS.


If C++ side has poor design around this specific issue and 
expects passed object to be delete'd (using the C++ delete 
operator) later then you are in trouble. In that case you have 
to make simple wrapper on C++ side to be able to call 
new/delete from D.


I though support for C++ allocation had improved. In a recent 
release, there was the addition of core.stdcpp.new, but I didn't 
try it out:

- http://dpldocs.info/experimental-docs/core.stdcpp.new_.html
- https://en.cppreference.com/w/cpp/memory/new/operator_new

Are there still pitfalls to be wary of?


Re: question about call cpp class constructer without new , and define cpp delegate

2019-06-27 Thread evilrat via Digitalmars-d-learn

On Thursday, 27 June 2019 at 05:37:08 UTC, ChangLoong wrote:
If I want call cpp class constructer without new method, is 
there a way to do that ?


If what you really want is to actually allocate using C++ new 
operator from D, then that is very problematic and not portable 
even across compilers on same OS.


If C++ side has poor design around this specific issue and 
expects passed object to be delete'd (using the C++ delete 
operator) later then you are in trouble. In that case you have to 
make simple wrapper on C++ side to be able to call new/delete 
from D.


If all you want is to allocate memory for object(existing buffer, 
malloc, etc..) and place it there you can use emplace function 
and call ctor later (see below)
https://dlang.org/phobos/core_lifetime.html#.emplace , or there 
was one in "object" module IIRC


Otherwise it is also possible to just call constructors manually 
using its internal name

   myObj.__ctor(..params..) / this.__ctor(...)

(destructors also possible, see __dtor/__xdtor. hint: __dtor is 
probably not what you want, read the docs first)


And finally to just allocate with GC using D new operator
   auto myObj = new MyClass(...);

Just make sure that this object won't be delete'd from C++


and also if the cpp api accept a delegate as parameter, how to 
create one from d and pass to cpp ?


Probably not possible. There are no delegates in C++, instead it 
has pointers to member functions and limited lambdas, and there 
is no analogs in D. You can try to craft it somehow to be ABI 
compatible, but probably easier to just make simple wrapper on 
C++ side.
IIRC member pointers is just pointer, and you provide 'this' 
context on call, while in D delegate is 2 pointers - context AND 
function


question about call cpp class constructer without new , and define cpp delegate

2019-06-26 Thread ChangLoong via Digitalmars-d-learn
If I want call cpp class constructer without new method, is there 
a way to do that ?


and also if the cpp api accept a delegate as parameter, how to 
create one from d and pass to cpp ?