Re: How to use core.atomic.cas with (function) pointers?

2019-01-22 Thread Kagamin via Digitalmars-d-learn
I believe this is historical. It will fail because atomics are 
combinatorially parameterized because the compiler couldn't 
properly infer template arguments until recently (this also 
resulted in memory corruption in ldc fork of druntime). Now that 
the compiler was fixed, atomics can be fixed too and have single 
template parameter.


Re: How to use core.atomic.cas with (function) pointers?

2019-01-22 Thread Nicholas Wilson via Digitalmars-d-learn

On Tuesday, 22 January 2019 at 14:13:23 UTC, Johan Engelen wrote:

The following code compiles:
```
alias T = shared(int)*;

shared T a;
shared T b;
shared T c;

void foo() {
import core.atomic: cas;
cas(&a, b, c);
}
```

The type of T has to be a pointer to a shared int (you get a 
template match error for `cas` if `T = int*`), which is 
annoying but I kind-of understand.
However, change b to null (`cas(&a, null, c);`) and things no 
longer work: "Error: template `core.atomic.cas` cannot deduce 
function from argument types"


I have not succeeded to make things work with function pointers 
(neither with nor without `null` as second argument). What am I 
doing wrong if `alias T = void function();` ?


Thanks,
  Johan


You need to cast stuff (as seems to be the case with everything 
to do with shared, at least until/if Manu's proposal goes 
through):


alias T = void function();
alias S = shared(size_t*);
shared T a;
shared T b;
shared T c;

void foo() {
import core.atomic: cas;
cas(cast(S*)&a, cast(S)b, cast(S)c);
}


Re: How to ensure string compatibility In D?

2019-01-22 Thread FrankLike via Digitalmars-d-learn

On Tuesday, 22 January 2019 at 21:49:00 UTC, bauss wrote:

On Tuesday, 22 January 2019 at 19:14:43 UTC, Jonathan M Davis



Is there a reason we cannot implement toStringz like:

immutable(TChar)* toStringz(TChar = char)(scope const(TChar)[] 
s) @trusted pure nothrow;

// Couldn't find a way to get the char type of a string, so



couldn't make the following generic:
immutable(char)* toStringz(return scope string s) @trusted pure 
nothrow;
immutable(wchar)* toStringz(return scope wstring s) @trusted 
pure nothrow;
immutable(dchar)* toStringz(return scope dstring s) @trusted 
pure nothrow;


For example:
///start///
import core.sys.windows.windows;
import std.stdio;
import std.string;
import std.conv;

void main()
{
autostrA_Z ="CD"w;
auto type = GetDriveType(tos(to!wstring(strA_Z[0])~":\\"));
writeln(to!wstring(strA_Z[0])~" is ",type);
}

private auto tos(T)(T str)
{
 version (ANSI)
 {
writeln("ANSI");
return cast(const(char)*)(str);
 }
 else
 {
writeln("Unicode");
return cast(const(wchar)*)(str);

 }
}
///end/
It's work ok.


Re: How to ensure string compatibility In D?

2019-01-22 Thread FrankLike via Digitalmars-d-learn

On Tuesday, 22 January 2019 at 21:49:00 UTC, bauss wrote:

On Tuesday, 22 January 2019 at 19:14:43 UTC, Jonathan M Davis



Is there a reason we cannot implement toStringz like:

immutable(TChar)* toStringz(TChar = char)(scope const(TChar)[] 
s) @trusted pure nothrow;

// Couldn't find a way to get the char type of a string, so
"core.sys.windows.windows.winbase",it's implementation is a good 
choice.

couldn't make the following generic:
immutable(char)* toStringz(return scope string s) @trusted pure 
nothrow;
immutable(wchar)* toStringz(return scope wstring s) @trusted 
pure nothrow;
immutable(dchar)* toStringz(return scope dstring s) @trusted 
pure nothrow;



For example:
/START//
import core.sys.windows.windows;
import std.stdio;
import std.string;
import std.conv;

void main()
{
autostrA_Z ="CD"w;
auto type = GetDriveType((to!wstring(strA_Z[0])~":\\"w).tos);
writeln(to!wstring(strA_Z[0])~" is ",type);
}

private auto tos(wstring str)
{
 version (ANSI)
 {
writeln("ANSI");
return cast(const(char)*)(str);
 }
 else
 {
writeln("Unicode");
return cast(const(wchar)*)(str);

 }
}
private auto tos(string str)
{
 version (ANSI)
 {
writeln("ANSI");
return cast(const(char)*)(str);
 }
 else
 {
writeln("Unicode");
return cast(const(wchar)*)(str);
 }
}
/END//

It's work ok.


Re: How to ensure string compatibility In D?

2019-01-22 Thread bauss via Digitalmars-d-learn
On Tuesday, 22 January 2019 at 19:14:43 UTC, Jonathan M Davis 
wrote:
On Tuesday, January 22, 2019 12:05:32 PM MST Stefan Koch via 
Digitalmars-d- learn wrote:

On Tuesday, 22 January 2019 at 16:47:45 UTC, FrankLike wrote:
> On Tuesday, 22 January 2019 at 16:18:17 UTC, Adam D. Ruppe
>
> wrote:
>> Use "mystring"w, notice the w after the closing quote.
>
> Or toStringz is not work like c_str() in C++?

stringz creates a char*
but you need a wchar*


std.utf.toUTF16z or toUTFz can do that for you, though if your 
string is already a wstring, then you can also just concatenate 
'\0' to it. the big advantage toUTF16z is that it will also 
convert strings of other character types rather than just 
wstrings. So, you can write your program using proper UTF-8 
strings and then only convert to UTF-16 for the Windows stuff 
when you have to.


https://dlang.org/phobos/std_utf.html#toUTF16z 
https://dlang.org/phobos/std_utf.html#toUTFz


- Jonathan M Davis


Is there a reason we cannot implement toStringz like:

immutable(TChar)* toStringz(TChar = char)(scope const(TChar)[] s) 
@trusted pure nothrow;
// Couldn't find a way to get the char type of a string, so 
couldn't make the following generic:
immutable(char)* toStringz(return scope string s) @trusted pure 
nothrow;
immutable(wchar)* toStringz(return scope wstring s) @trusted pure 
nothrow;
immutable(dchar)* toStringz(return scope dstring s) @trusted pure 
nothrow;




Re: How to ensure string compatibility In D?

2019-01-22 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, January 22, 2019 12:05:32 PM MST Stefan Koch via Digitalmars-d-
learn wrote:
> On Tuesday, 22 January 2019 at 16:47:45 UTC, FrankLike wrote:
> > On Tuesday, 22 January 2019 at 16:18:17 UTC, Adam D. Ruppe
> >
> > wrote:
> >> Use "mystring"w, notice the w after the closing quote.
> >
> > Or toStringz is not work like c_str() in C++?
>
> stringz creates a char*
> but you need a wchar*

std.utf.toUTF16z or toUTFz can do that for you, though if your string is
already a wstring, then you can also just concatenate '\0' to it. the big
advantage toUTF16z is that it will also convert strings of other character
types rather than just wstrings. So, you can write your program using proper
UTF-8 strings and then only convert to UTF-16 for the Windows stuff when you
have to.

https://dlang.org/phobos/std_utf.html#toUTF16z
https://dlang.org/phobos/std_utf.html#toUTFz

- Jonathan M Davis





Re: How to ensure string compatibility In D?

2019-01-22 Thread Stefan Koch via Digitalmars-d-learn

On Tuesday, 22 January 2019 at 16:47:45 UTC, FrankLike wrote:
On Tuesday, 22 January 2019 at 16:18:17 UTC, Adam D. Ruppe 
wrote:

Use "mystring"w, notice the w after the closing quote.


Or toStringz is not work like c_str() in C++?


stringz creates a char*
but you need a wchar*


Re: How to ensure string compatibility In D?

2019-01-22 Thread FrankLike via Digitalmars-d-learn

On Tuesday, 22 January 2019 at 16:18:17 UTC, Adam D. Ruppe wrote:

Use "mystring"w, notice the w after the closing quote.


Or toStringz is not work like c_str() in C++?


Re: How to ensure string compatibility In D?

2019-01-22 Thread FrankLike via Digitalmars-d-learn

On Tuesday, 22 January 2019 at 16:18:17 UTC, Adam D. Ruppe wrote:

Use "mystring"w, notice the w after the closing quote.


 "GetDriveType" Function is auto work by "_T" in C++,but how to 
do in D?






Re: How to ensure string compatibility In D?

2019-01-22 Thread Adam D. Ruppe via Digitalmars-d-learn

Use "mystring"w, notice the w after the closing quote.


Re: How to ensure string compatibility In D?

2019-01-22 Thread FrankLike via Digitalmars-d-learn

On Tuesday, 22 January 2019 at 16:13:57 UTC, FrankLike wrote:
On Tuesday, 22 January 2019 at 14:07:48 UTC, Olivier Pisano 
wrote:


Some error is in  "core.sys.windows.windows"?

Thank you.




Re: How to ensure string compatibility In D?

2019-01-22 Thread FrankLike via Digitalmars-d-learn

On Tuesday, 22 January 2019 at 14:07:48 UTC, Olivier Pisano wrote:

On Tuesday, 22 January 2019 at 13:55:30 UTC, FrankLike wrote:


In D, there is only Unicode. The language doesn't manipulate 
strings encoded in Windows local code-pages.


For example:

 std::wstring strTest(_T("d://"));
 UINT nRes = ::GetDriveType(strTest.c_str());

It can work in C++.
But:
//here is work 
ok///

import std.stdio;
import std.string;
import std.conv;
import win32.winbase;

void main()
{
string  strA_Z ="CD";
auto type = GetDriveType((to!string(strA_Z[0])~":\\").toStringz);
writeln(to!string(strA_Z[0])~" is ",type);
}
//here is work 
error//


import core.sys.windows.windows;
import std.stdio;
import std.string;
import std.conv;

void main()
{
string  strA_Z ="CD";
auto type = GetDriveType((to!string(strA_Z[0])~":\\").toStringz);
writeln(to!string(strA_Z[0])~" is ",type);
}


 //---Error Info//
slicea2.d(9): Error: function 
core.sys.windows.winbase.GetDriveTypeW(const(wchar

)*) is not callable using argument types (immutable(char)*)
slicea2.d(9):cannot pass argument toStringz(to(strA_Z[0]) 
~ ":\\") of ty

pe immutable(char)* to parameter const(wchar)*


Some error is "core.sys.windows.windows"?

Thank you.




How to use core.atomic.cas with (function) pointers?

2019-01-22 Thread Johan Engelen via Digitalmars-d-learn

The following code compiles:
```
alias T = shared(int)*;

shared T a;
shared T b;
shared T c;

void foo() {
import core.atomic: cas;
cas(&a, b, c);
}
```

The type of T has to be a pointer to a shared int (you get a 
template match error for `cas` if `T = int*`), which is annoying 
but I kind-of understand.
However, change b to null (`cas(&a, null, c);`) and things no 
longer work: "Error: template `core.atomic.cas` cannot deduce 
function from argument types"


I have not succeeded to make things work with function pointers 
(neither with nor without `null` as second argument). What am I 
doing wrong if `alias T = void function();` ?


Thanks,
  Johan



Re: How to ensure string compatibility In D?

2019-01-22 Thread Olivier Pisano via Digitalmars-d-learn

On Tuesday, 22 January 2019 at 13:55:30 UTC, FrankLike wrote:

Hi,everyone,
  In C++, _T can guarantee that when converting from ascii 
encoding type to unicode encoding type, the program does not 
need to be modified. What do I need to do in D?


Thanks.


Hi,

_T is not relevant to C++, but to Windows programming.

In D, there is only Unicode. The language doesn't manipulate 
strings encoded in Windows local code-pages.


char means UTF-8 (Unicode encoded in 8bit units).
wchar means UTF-16 (Unicode encoded in 16bit units, what Windows 
documentation calls "Unicode").

dchar means UTF-32 (Unicode encoded in 32bit units).

When manipulating data encoded in Windows local code page, use 
the ubyte[] type.


How to ensure string compatibility In D?

2019-01-22 Thread FrankLike via Digitalmars-d-learn

Hi,everyone,
  In C++, _T can guarantee that when converting from ascii 
encoding type to unicode encoding type, the program does not need 
to be modified. What do I need to do in D?


Thanks.