www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to pack types with variables in one message to send it to another

reply "MarisaLovesUsAll" <marisalovesusall gmail.com> writes:
Hi!
I'm trying to make my program multithreaded, and I was stuck at 
messaging between threads.
I need to pack types and variables into one message. Will I use 
Tuples or something?

e.g.

class Sprite {};

send(tid, Sprite, "create", myInt);

................

Also I don't understand how to use Variant. Messages can be 
different, and I don't know how to extract data from variant.

send(tid, "One", "Two", myInt);

receive(
     (Variant args)
     {
         /*
         args contains Tuple!(string, string, int)("One", "Two", 
42);
         I need simple access to data, e.g. args[0] args[1] args[2]
         but I don't know how to do this
         because `.get` method need precise type of Tuple
         */
     }
);

Regards,
MarisaLovesUsAll
Sep 07 2014
next sibling parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 7/09/2014 10:42 p.m., MarisaLovesUsAll wrote:
 Hi!
 I'm trying to make my program multithreaded, and I was stuck at
 messaging between threads.
 I need to pack types and variables into one message. Will I use Tuples
 or something?

 e.g.

 class Sprite {};

 send(tid, Sprite, "create", myInt);
Don't worry about the packing when calling send. It'll automatically be converted into a "tuple". Also you should only be using immutable or primitive types. Strings are immutable so thats ok. A class instance that isn't immutable isn't. Note Sprite is a class type not a class instance.
 ................

 Also I don't understand how to use Variant. Messages can be different,
 and I don't know how to extract data from variant.

 send(tid, "One", "Two", myInt);

 receive(
      (Variant args)
      {
          /*
          args contains Tuple!(string, string, int)("One", "Two", 42);
          I need simple access to data, e.g. args[0] args[1] args[2]
          but I don't know how to do this
          because `.get` method need precise type of Tuple
          */
      }
 );
Don't worry about it. Just have separate receiving functions per the data type. You'll probably be better off. In other words Variant is overkill. It basically just wraps a piece of data so that it can be passed around without knowing its type. Which in this case is bad. You would end up having to know the datatype to do anything with it anyway.
Sep 07 2014
parent reply "MarisaLovesUsAll" <marisalovesusall gmail.com> writes:
Thanks for reply.
 Strings are immutable so thats ok. A class instance that isn't 
 immutable isn't.
It's not a class instance, it's a class type. Something like `cast(Sprite) null` in parameters. It can be replaced by string "Sprite", but in this case I can't use receive() as it is. E.g. send(tid,gameobjectId,"Sprite","reload"); //must call sprite.reload(); send(tid,gameobjectId,"Animation","reload"); //must call animation.reload();
Just have separate receiving functions per the data type.
But both messages are (int, string, string) so they can't be separate by different receiving functions. It will be better if messages was (int, Sprite, string) / (int, Animation, string). And it solves my problem. :) But I don't know how to achieve this.
 In other words Variant is overkill.
 It basically just wraps a piece of data so that it can be 
 passed around without knowing its type. Which in this case is 
 bad.
 You would end up having to know the datatype to do anything 
 with it anyway.
Then I need something like Variant[] to store this data in array. MyVariant[] args; if(args[0] == typeid(int)) { if(args[1] == "Sprite") {} if(args[1] == "Animation") {} } etc. I'm trying to make something like messages in Smalltalk (?), but between threads. Thread can receive anything and thread decides what to do on its own.
Sep 07 2014
parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 8/09/2014 12:39 a.m., MarisaLovesUsAll wrote:
 Thanks for reply.
 Strings are immutable so thats ok. A class instance that isn't
 immutable isn't.
It's not a class instance, it's a class type. Something like `cast(Sprite) null` in parameters. It can be replaced by string "Sprite", but in this case I can't use receive() as it is. E.g. send(tid,gameobjectId,"Sprite","reload"); //must call sprite.reload(); send(tid,gameobjectId,"Animation","reload"); //must call animation.reload();
Those calls to send are fine.
 Just have separate receiving functions per the data type.
But both messages are (int, string, string) so they can't be separate by different receiving functions. It will be better if messages was (int, Sprite, string) / (int, Animation, string). And it solves my problem. :) But I don't know how to achieve this.
In the given send function calls you don't need to. Just use if statements to check the string type.
 In other words Variant is overkill.
 It basically just wraps a piece of data so that it can be passed
 around without knowing its type. Which in this case is bad.
 You would end up having to know the datatype to do anything with it
 anyway.
Then I need something like Variant[] to store this data in array. MyVariant[] args; if(args[0] == typeid(int)) { if(args[1] == "Sprite") {} if(args[1] == "Animation") {} } etc.
No need. receive( (int id, string type, string action) { if (type == "Sprite") { if (action == "reload") mySprite.reload(); } else if (type == "Animation") { if (action == "reload") myAnimation.reload(); } } );
Sep 07 2014
next sibling parent Philippe Sigaud via Digitalmars-d-learn writes:
You can also create new types:

struct UseSprite { string s;}
struct UseAnimation { string s;}


 It's not a class instance, it's a class type. Something like
 `cast(Sprite) null` in parameters. It can be replaced by string
 "Sprite", but in this case I can't use receive() as it is. E.g.

 send(tid,gameobjectId,"Sprite","reload");
 //must call sprite.reload();
You could use: sent(tid, gameobjectId, UseSprite("reload"));
 send(tid,gameobjectId,"Animation","reload");
 //must call animation.reload();
sent(tid, gameobjectId, UseAnimation("reload")); Another way, if you have way to determine that gameobjectId points to an animation or a sprite, would be to define a struct name Reload {} and then: sent(tid, gameobjectId, Reload()); Third way: if Animation.reload() and Sprite.reload() are static methods: send(tid, gameobjectId, &Sprite.reload);
 But both messages are (int, string, string) so they can't be separate by
 different receiving functions. It will be better if messages was (int,
 Sprite, string) / (int, Animation, string). And it solves my problem. :)
 But I don't know how to achieve this.
See my proposal: define your message as types, directly, and load them for any data necessary for the call. UseAnimation("reload"), or whatever.
Sep 07 2014
prev sibling parent "MarisaLovesUsAll" <marisalovesusall gmail.com> writes:
 No need.
Message has additional arguments. Btw, thanks for help! I found a solution. struct Message { uint id; string command; Variant[] args; this(T...)(uint id, string command, T args) { this.id = id; this.command = command; this.args = variantArray(args); } }; send(tid, cast(immutable Message) Message(id, "Sprite", "load", filename)); receive((immutable Message receivedMsg) { Message msg = cast(Message) receivedMsg; writeln(msg.args[1].get!uint); }); Cast to immutable and back to mutable looks like crutch, but I don't know what to do with std.concurrency restrictions.
Sep 07 2014
prev sibling parent "hane" <han.ehit.suzi.0 gmail.com> writes:
On Sunday, 7 September 2014 at 10:42:37 UTC, MarisaLovesUsAll 
wrote:
 Hi!
 I'm trying to make my program multithreaded, and I was stuck at 
 messaging between threads.
 I need to pack types and variables into one message. Will I use 
 Tuples or something?

 e.g.

 class Sprite {};

 send(tid, Sprite, "create", myInt);

 ................

 Also I don't understand how to use Variant. Messages can be 
 different, and I don't know how to extract data from variant.

 send(tid, "One", "Two", myInt);

 receive(
     (Variant args)
     {
         /*
         args contains Tuple!(string, string, int)("One", "Two", 
 42);
         I need simple access to data, e.g. args[0] args[1] 
 args[2]
         but I don't know how to do this
         because `.get` method need precise type of Tuple
         */
     }
 );

 Regards,
 MarisaLovesUsAll
receive() automatically expands tuples into multiple arguments. receive((string s, string t, int i) { });
Sep 07 2014