|Re: Protocol with pointers and arrays [message #1770054 is a reply to message #1769954]
||Mon, 07 August 2017 19:25
| Ernesto Posse
Registered: March 2011
You've ran into a new bug in the code generator, but there is a workaround.|
The bug is this: whenever you send data from one capsule to another, the runtime must know how to serialize the data in order to send it. It cannot simply send a pointer, because the receiver could be in a separate memory space. Well, the current runtime doesn't yet support multi-core or multi-processes on separate memory spaces, but it is designed with that in mind, so the code generator produces code that serializes the data payload of messages. When the data in question is a user-defined type, this serialization is done by looking at a data structure that describes the fields of the data class. This is encoded by the code generator in what we call "field descriptors", which are generated for the passive class. You can find the "fields" variable in the generated code. Now, the problem in you case is that that you are trying to sent an instance of WidgetVector, which has an attribute whose type (std::vector<Widget>) is specified with the AttributeProperties stereotype's "type" string, and unfortunately, the code generator does not know how to generate field descriptors for such attributes. It only generates descriptors for types it knows, i.e. primitive types, or types defined by the user in the model, i.e. specified directly in the type of the attribute, rather than in the AttributeProperties stereotype. So the generated code doesn't have the required field descriptor.
Now, before I explain the workaround, I want to say that you cannot send an array or pointer directly as the data of a message. It would be a very bad idea. First, doing so would also make the assumption that the sender and the receiver are running both in the same memory space, which, as I mentioned above, is an assumption that will likely change in the future. Conceptually you should always think of capsules as being "isolated", i.e. not sharing memory. Second, even if we allowed you to pass pointers directly, you could easily get into trouble because if the capsules are running in separate threads, you now have to deal with potential race-conditions and other concurrent safety issues, so you would be forced to use things like mutexes to avoid those problems.
Having said that, there is a way where you can share data and have a result which is analogous to passing a pointer: create a "wrapper" data class that acts like a pointer. See the attached, updated model. In the new model, I've created a "WidgetVectorWrapper" which has a single attribute that is the WidgetVector. So instead of sending the WidgetVector, I create a WidgetVectorWrapper whose attribute is set to the WidgetVector and I send the WidgetVectorWrapper. Now the data is correctly serialized on send, and extracted on the receiving end.
[Updated on: Mon, 07 August 2017 19:27]
Report message to a moderator
Powered by FUDForum
. Page generated in 0.02213 seconds