digitalmars.D.learn - How to pack types with variables in one message to send it to another
- MarisaLovesUsAll (26/26) Sep 07 2014 Hi!
- Rikki Cattermole (13/36) Sep 07 2014 Don't worry about the packing when calling send.
- MarisaLovesUsAll (23/32) Sep 07 2014 It's not a class instance, it's a class type. Something like
- Rikki Cattermole (17/45) Sep 07 2014 In the given send function calls you don't need to. Just use if
- Philippe Sigaud via Digitalmars-d-learn (15/27) Sep 07 2014 You can also create new types:
- MarisaLovesUsAll (23/24) Sep 07 2014 Message has additional arguments.
- hane (4/31) Sep 07 2014 receive() automatically expands tuples into multiple arguments.
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
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
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
On 8/09/2014 12:39 a.m., MarisaLovesUsAll wrote:Thanks for reply.Those calls to send are fine.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();In the given send function calls you don't need to. Just use if statements to check the string type.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.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(); } } );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.
Sep 07 2014
You can also create new types: struct UseSprite { string s;} struct UseAnimation { string s;}You could use: sent(tid, gameobjectId, UseSprite("reload"));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();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);send(tid,gameobjectId,"Animation","reload"); //must call animation.reload();See my proposal: define your message as types, directly, and load them for any data necessary for the call. UseAnimation("reload"), or whatever.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.
Sep 07 2014
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
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, MarisaLovesUsAllreceive() automatically expands tuples into multiple arguments. receive((string s, string t, int i) { });
Sep 07 2014