Re: sending the address of a struct

2020-09-06 Thread Johann Lermer via Digitalmars-d-learn

On Sunday, 6 September 2020 at 11:10:14 UTC, Simen Kjærås wrote:

auto e = receiveOnly!(shared(Env)*);


Oh, thanks. Seems, that I just missed that bit with the 
pranetheses.


sending the address of a struct

2020-09-06 Thread Johann Lermer via Digitalmars-d-learn

Hi,

I have a struct in a separate thread and want to pass it's 
address back to the main thread. This is how I think it should 
work:


import std.concurrency;

struct Env {}

void run ()
{
shared Env env;
ownerTid.send ();
for (;;) {}
}

void main ()
{
spawn ();
auto e = receiveOnly!(shared Env*);
}

but I'm getting an error when main tries to receive the pointer. 
The error message says:


std.concurrency.MessageMismatch@std/concurrency.d(237): 
Unexpected message type: expected 'shared(Env*)', got 
'shared(test.Env)*'


Now, how can I pass that pointer back to main?



Re: How to implement Canceleable spawn() from parent

2020-06-29 Thread Johann Lermer via Digitalmars-d-learn
I'm doing this in an X11 application in order to send a timer 
event every 100 milliseconds to the main event queue.


class Application
{
  shared private bool s_tick;

  void clock_task (shared X11.Display* disp, X11.Atom atom, 
X11.Window win)

  {
for (;;)
{
  try
  {
receiveTimeout (100.msecs);

if (disp && atomicLoad(s_tick))
{
  // disable ticking until it is allowed again at the end 
of the event loop

  atomicStore(s_tick, false);

  X11.XClientMessageEvent event;
  event.type = X11.ClientMessage;
  event.window   = win;
  event.message_type = atom;
  event.format   = 32;
  event.data.l   = [0, 0, 0, 0, 0];

  X11.XSendEvent (cast (X11.Display*) disp, win, 0, 0,  
cast(X11.XEvent*));

  X11.XFlush (cast (X11.Display*) disp);
}
  }
  catch (Throwable)
  {
return;
  }
}
  }

  this ()
  {
...
spawn (_task, cast(shared)x11Display, x11SigClockAtom, 
_x11_proxyWindow);

  }

  run ()
  {
while (true)
{
...
  // event processing starts here: read in X11 event and 
convert it to a wit Event

  X11.XEvent x11_event;
  X11.XNextEvent (_x11.display, _event);
...
  atomicStore(s_tick, true);
}
  }
}


Re: const pointers C vs. D

2020-02-06 Thread Johann Lermer via Digitalmars-d-learn

On Tuesday, 4 February 2020 at 10:17:39 UTC, Dennis wrote:
C++ has a const system that is closer to D's than any other 
language, but it still has huge differences:


Thanks, that clears it up a bit!


const pointers C vs. D

2020-02-04 Thread Johann Lermer via Digitalmars-d-learn

Hi,

I'm just wondering about defining const pointers and if there's a 
difference in C and D.


in C, this works:

const char* text = "Hello";
text = "world";

but in D it doesn't, because the char* is const. Ff I would like 
tho have the same behaviour in D as in C, I need to write:


const (char)* text = "Hello";
text = "world";

In C, this would not be valid. So the question for me now is: is 
const char* in D different from C?




Re: Distinguish float and integer types from string

2019-03-11 Thread Johann Lermer via Digitalmars-d-learn

On Saturday, 9 March 2019 at 18:11:09 UTC, Jacob Shtokolov wrote:
I tried to use std.conv.to and std.conv.parse, but found that 
they can't really do this. When I call `data.to!int`, the value 
of "123.45" will be converted to int!


Are you sure? This here works for me:

import std.stdio;
import std.string;
import std.conv;

void main ()
{
auto s = readln.strip;

try
{
int i = to!int (s);
writefln ("string is an int: %s", i);
}
catch(Exception e)
{
try
{
float f = to!float(s);
writefln ("string is a float: %s", f);
}
catch(Exception e)
{
writefln ("string is an neither an int nor a float: 
%s", s);

}
}
}


C structs

2014-06-20 Thread Johann Lermer via Digitalmars-d-learn

Hi,

I'm fiddling around with cairo (downloaded 
fromhttps://github.com/D-Programming-Deimos/cairo) and I stumbled 
over this problem:


(file rectandmatrix.d)

import deimos.cairo.cairo;

void main ()
{
cairo_rectangle_int_t rect;
cairo_matrix_t matrix;
}


Compiling that with 'dmd rectandmatrix' fails with this error:

rectandmatrix.o: In function `_Dmain':
rectandmatrix.d:(.text._Dmain+0x18): undefined reference to 
`_D6deimos5cairo5cairo14cairo_matrix_t6__initZ'

collect2: error: ld returned 1 exit status
--- errorlevel 1

The rectangle is OK - no error there. The matrix however, 
produces the error. The difference between both is that 
cairo_rectangle_int_t is a struct containing integers, and 
cairo_matrix_t contains doubles.


I can, however, compile like that:

'dmd rectandmatrix deimos/cairo/cairo.d' (deimos being in a 
subdir of the working directory)


then cairo.d is compiled as well and linked to the code.

So, my questions are:

1) Why do I have to compile and link cairo.d - it's only an 
definition file for C code and IMHO there shouldn't be any need 
to compile cairo.d at all.


2) Why ist a struct containing only integers handled so 
differently from a struct holding doubles?


Thanks


Re: C structs

2014-06-20 Thread Johann Lermer via Digitalmars-d-learn
Agreed, but I assumed, that since all definitions in cairo.d are 
defined as
extern (System), (same happens with extern (C), btw.), I would 
have expected,

that D does not implicitly generate initialisation functions.

So, why is there no init routine for the rectangle? There's only 
one for the matrix.