Re: A question about C++ interop

2020-03-29 Thread YD via Digitalmars-d-learn

On Sunday, 29 March 2020 at 17:32:59 UTC, kinke wrote:

On Sunday, 29 March 2020 at 15:20:52 UTC, YD wrote:
So what do I need to declare in the D file for it to match the 
library entry? Thanks!


This is similar to 
https://issues.dlang.org/show_bug.cgi?id=19260, and can be 
worked around the same way by messing manually with the mangled 
name, if you can't adapt the C++ side. The actual problem is 
that you can't express a mutable reference to a const class 
object (as opposed to a struct) in D (`const Y` is a const 
reference to a const Y object).


(This problem somehow does not appear on Linux where the 
library file is compiled with gcc, though)


The Itanium C++ mangling doesn't differentiate between:

void foo(const Y *);   // what you have on the C++ side
void foo(const Y * const); // corresponds to D `void foo(const 
Y)`


Thanks! I tried this:

class X {
version(Windows) {
pragma(mangle, X.call.mangleof.replace("QBV","PBV"))
final void call(const(Y)) const;
} else {
final void call(const(Y)) const;
}
}

and it worked. Thanks very much again!



Re: A question about C++ interop

2020-03-29 Thread kinke via Digitalmars-d-learn

On Sunday, 29 March 2020 at 15:20:52 UTC, YD wrote:
So what do I need to declare in the D file for it to match the 
library entry? Thanks!


This is similar to 
https://issues.dlang.org/show_bug.cgi?id=19260, and can be worked 
around the same way by messing manually with the mangled name, if 
you can't adapt the C++ side. The actual problem is that you 
can't express a mutable reference to a const class object (as 
opposed to a struct) in D (`const Y` is a const reference to a 
const Y object).


(This problem somehow does not appear on Linux where the 
library file is compiled with gcc, though)


The Itanium C++ mangling doesn't differentiate between:

void foo(const Y *);   // what you have on the C++ side
void foo(const Y * const); // corresponds to D `void foo(const Y)`



Re: A question about C++ interop

2020-03-29 Thread YD via Digitalmars-d-learn

On Sunday, 29 March 2020 at 15:20:52 UTC, YD wrote:

On Sunday, 29 March 2020 at 01:50:24 UTC, evilrat wrote:

[...]


Thanks, dummy placeholder works. But there is a new problem on 
Windows, let's say there are two classes in C++:


[...]


Actually I found that if I create a C wrapper like

extern "C" void X_call(X const *, Y const *);

then it will work. So I have a workaround now. However, out of 
curiosity, is there a way to do this directly without C wrapper? 
Thanks.


Re: A question about C++ interop

2020-03-29 Thread YD via Digitalmars-d-learn

On Sunday, 29 March 2020 at 01:50:24 UTC, evilrat wrote:

...

Same here, STL bindings is not yet finished. If you don't need 
that method specifically, just replace it with a dummy. Or make 
your own bindings.


Thanks, dummy placeholder works. But there is a new problem on 
Windows, let's say there are two classes in C++:


class Y {
...
};
class X {
public:
void call(Y const * y) const;
};

on Windows, the library file (compiled with Visual C++) contains 
an entry like this:


16C 0020 SECT4  notype ()External | 
?call@X@@QBEXPBVY@@@Z (public: void __thiscall X::call(class Y 
const *)const )


Then I declare it in D like this:

extern(C++) {
class Y {
...
}
class X {
final void call(const(Y) y) const;
}
}

The object file for this D code (compiled with ldc2) will contain 
an entry like this:


0A7  UNDEF  notype   External | 
?call@X@@QBEXQBVY@@@Z (public: void __thiscall X::call(class Y 
const * const)const )


So there is a subtle difference in the signature, and the linker 
refuses to resolve the symbol.


I tried "const Y y", "const(Y*) y", and "ref const(Y) y", but 
none of them manages to match the library file entry.


(This problem somehow does not appear on Linux where the library 
file is compiled with gcc, though)


So what do I need to declare in the D file for it to match the 
library entry? Thanks!


Re: Find the heir.

2020-03-29 Thread Simen Kjærås via Digitalmars-d-learn

On Sunday, 29 March 2020 at 14:04:53 UTC, TodNaz wrote:

Hello!

class A
{
   ...
}

class B : A
{
   ...
}

class C : A
{
   ...
}

A example1;
B example2 = new B(...);
A = example2;
auto heir = A.whoheir(); ///


The question in this code is: is it possible to track the class 
inheritor? Or is it beyond D?

Sorry if the question is fool ...


The question is a bit unclear - what is whoheir expected to 
return? This is one way that may or may not fulfill your 
requirements:


module foo;
class A {
string whoheir() {
return typeid(this).name;
}
}
class B : A {}
class C : A {}

unittest {
A a = new B();
assert(a.whoheir == "foo.B");
a = new C();
assert(a.whoheir == "foo.C");
}

--
  Simen


Re: Find the heir.

2020-03-29 Thread Stefan Koch via Digitalmars-d-learn

On Sunday, 29 March 2020 at 14:04:53 UTC, TodNaz wrote:

Hello!

class A
{
   ...
}

class B : A
{
   ...
}

class C : A
{
   ...
}

A example1;
B example2 = new B(...);
A = example2;
auto heir = A.whoheir(); ///


The question in this code is: is it possible to track the class 
inheritor? Or is it beyond D?

Sorry if the question is fool ...


It is not generally known who has inherited a class from the 
parents perspective.




Find the heir.

2020-03-29 Thread TodNaz via Digitalmars-d-learn

Hello!

class A
{
   ...
}

class B : A
{
   ...
}

class C : A
{
   ...
}

A example1;
B example2 = new B(...);
A = example2;
auto heir = A.whoheir(); ///


The question in this code is: is it possible to track the class 
inheritor? Or is it beyond D?

Sorry if the question is fool ...



Re: Wired question related with Chinese characters

2020-03-29 Thread lovemini via Digitalmars-d-learn

import std.stdio;

void main()
{
version( Windows ) {
//直接运行中文显示乱码,原因在于Windows控制台默认编码为 936,而D语言输出utf-8
//可以将控制台编码修改为 utf-8,命令为 "CHCP 65001"
//修改后就可以显示中文了
import core.sys.windows.windows;
SetConsoleCP(65001);
SetConsoleOutputCP( 65001 );
}

writeln("Hello World! 你好,中国!");
}



Re: Wired question related with Chinese characters

2020-03-29 Thread lovemini via Digitalmars-d-learn

On Sunday, 22 March 2020 at 15:19:13 UTC, walker wrote:

I am new to dlang, I like it :)
I am on windows10 and use the terminal preview to test and run 
programs.


In order to print Chinese characters correctly, I always use

void main()
{
string var1 = "你好"; # to!string(in_other_conditions)
writeln(var1);
}

I tried dstring but not working. Need Help Here.

-
The wired thing is the above approach may not work sometimes -> 
corrupted characters show up.


However, if I run in the same place a little program written in 
nim (just print hello world), then I run the same dlang 
program, the Chinese character is print correctly.


Why that happened?