Re: How can I make this work?

2021-03-15 Thread Jack via Digitalmars-d-learn

On Sunday, 28 February 2021 at 13:15:47 UTC, Adam D. Ruppe wrote:

On Sunday, 28 February 2021 at 07:05:27 UTC, Jack wrote:
I'm using a windows callback function where the user-defined 
value is passed thought a LPARAM argument type. I'd like to 
pass my D array then access it from that callback function. 
How is the casting from LPARAM to my type array done in that 
case?


The best way to do this is to put the array inside a struct and 
pass the address of the struct instead. This way both length 
and pointer are passed, and you have the option to add more 
things if you ended up needing it later, and there's fewer 
weird things to worry about.


int[] arr = [1, 2, 3];

struct MyMessage {
 int[] arr;
}

// this far is easy enough
MyMessage* messagePointer = new MyMessage(arr);

// but we do need to tell the GC we intend to pass this to the 
outside world
// failure to do this MIGHT lead to random crashes as the GC 
can't see it (it can't look inside the Windows message queue), 
assumes it is unused, and frees it out from under you.

import core.memory;
GC.addRoot(messagePointer);

// when the GC has a root, it will consider that pointer live 
until further notice and not collect it nor its member 
variables.


// so it is now cool to do this
PostMessage(hwnd, MSG_WHATEVER, 0, cast(LPARAM) messagePointer);


/* then on the other side */

switch(iMsg) {
   case MSG_WHATEVER:
   MyMessage* messagePointer = cast(MyMessage*) lParam;

   // need to tell the GC the pointer can be automatically 
managed normally again. failure to do this will lead to a 
memory leak

   import core.memory;
   GC.removeRoot(messagePointer);

   // now can use it
   foreach(item; messagePointer.arr) {
  // yada yada yada
   }
}



And it is the simplest thing, no missing length, no weird 
property casting. The GC handled with two simple add/remove 
calls.


This is what I ended up using. using a single pointer such as 
MyMessage makes things much simpler. Thanks for the rememinder of 
GC.removeRoot()


Everyone else in this theread, thank you guys. Always helpful


D in AI Field

2021-03-15 Thread Lasheen via Digitalmars-d-learn

Hello, I'm a C programmer, and now i want to migrate to D.
i work in AI field, so i have some questions about D language:

1- is D suitable for mission critical systems(such as avionics 
systems) ?

 if (true){
2- what about safety, Security and reliability of D in Embedded 
systems ?

3- what about memory issues in D if i use it in AI ?
4- is there a compiler for AI ?
};
Thank you.


Can't call splitter with range struct

2021-03-15 Thread David Skluzacek via Digitalmars-d-learn
I came across this problem as I was trying to see if could write 
a quick range-based solution with std.zlib to do what was asked 
about in a different Learn forum post - read a gzipped file.


This seems like it should work:

import std.stdio, std.algorithm, std.zlib;
import std.range.primitives;

void main(string[] args)
{
auto f = GZippedFile(File(args[1], "rb"));
f.splitter("\n").each!writeln;
}

struct GZippedFile
{
File file;
UnCompress uncompressor;
ubyte[] readBuffer;
const(char)[] buffer;

this(File f) {
file = f;
uncompressor = new UnCompress(HeaderFormat.gzip);
readBuffer = new ubyte[4096];
}

dchar front() const {
return buffer.front;
}

void popFront() {
if (buffer.empty) {
buffer = cast(const(char)[])
uncompressor.uncompress(file.rawRead(readBuffer));
}
else {
buffer.popFront();
}
}

bool empty() {
return buffer.empty && file.eof();
}
}

But I get:

Error: template std.algorithm.iteration.splitter cannot deduce 
function from argument types !()(GZippedFile, string), candidates 
are:
/usr/include/dlang/dmd/std/algorithm/iteration.d(4678):
splitter(alias pred = "a == b", Range, Separator)(Range r, 
Separator s)

  with pred = "a == b",
   Range = GZippedFile,
   Separator = string
  must satisfy the following constraint:
   is(typeof(binaryFun!pred(r.front, s)) : bool)
(...)

If I change the newline separator to a character literal, I get:

(...)
/usr/include/dlang/dmd/std/algorithm/iteration.d(5055):
splitter(alias pred = "a == b", Range, Separator)(Range r, 
Separator s)

  with pred = "a == b",
   Range = GZippedFile,
   Separator = char
  must satisfy the following constraint:
   is(typeof(binaryFun!pred(r.front, s.front)) : bool)

It seems like "\n" should pass the second constraint and '\n' 
should pass the first.  Using a dchar or dstring makes no 
difference. Adding @property to front makes no difference. Is 
this a bug?




Re: WinUI 3

2021-03-15 Thread Imperatorn via Digitalmars-d-learn

On Monday, 15 March 2021 at 22:07:35 UTC, Siemargl wrote:

On Monday, 15 March 2021 at 16:41:08 UTC, Imperatorn wrote:

Could D be used with WinUI 3?

https://docs.microsoft.com/en-us/windows/apps/winui/winui3/

Would the win32metadata help? 🤔


No need to going in another dead end


?


Re: WinUI 3

2021-03-15 Thread Siemargl via Digitalmars-d-learn

On Monday, 15 March 2021 at 16:41:08 UTC, Imperatorn wrote:

Could D be used with WinUI 3?

https://docs.microsoft.com/en-us/windows/apps/winui/winui3/

Would the win32metadata help? 🤔


No need to going in another dead end


Re: WinUI 3

2021-03-15 Thread Paulo Pinto via Digitalmars-d-learn

On Monday, 15 March 2021 at 17:04:56 UTC, evilrat wrote:

On Monday, 15 March 2021 at 16:41:08 UTC, Imperatorn wrote:

Could D be used with WinUI 3?

https://docs.microsoft.com/en-us/windows/apps/winui/winui3/

Would the win32metadata help? 🤔


I've seen some slides about WinUI 3 future directions and 
roadmap but haven't tried it yet.
Probably it will be PITA to use it right now in anything not 
.NET because of some core dependencies (IIRC some core stuff is 
in .NET), but plans have mentions it will be possible to use 
with ANY language closer to release date somewhere in Q3 2021.


But if it can be done using C++/WinRT right now then it is 
highly likely this will be possible to do it in D as well.


As for WinRT stuff there was some tools and bindings done by 
webfreak.

https://github.com/WebFreak001/dwinrt2
and/or
https://github.com/WebFreak001/dwinrt


WinUI core is WinRT, basically modern COM, no :NET dependencies.




Re: WinUI 3

2021-03-15 Thread evilrat via Digitalmars-d-learn

On Monday, 15 March 2021 at 16:41:08 UTC, Imperatorn wrote:

Could D be used with WinUI 3?

https://docs.microsoft.com/en-us/windows/apps/winui/winui3/

Would the win32metadata help? 🤔


I've seen some slides about WinUI 3 future directions and roadmap 
but haven't tried it yet.
Probably it will be PITA to use it right now in anything not .NET 
because of some core dependencies (IIRC some core stuff is in 
.NET), but plans have mentions it will be possible to use with 
ANY language closer to release date somewhere in Q3 2021.


But if it can be done using C++/WinRT right now then it is highly 
likely this will be possible to do it in D as well.


As for WinRT stuff there was some tools and bindings done by 
webfreak.

https://github.com/WebFreak001/dwinrt2
and/or
https://github.com/WebFreak001/dwinrt


WinUI 3

2021-03-15 Thread Imperatorn via Digitalmars-d-learn

Could D be used with WinUI 3?

https://docs.microsoft.com/en-us/windows/apps/winui/winui3/

Would the win32metadata help? 🤔