Under which conditions does the Message Compiler generate setFieldValueAsString() support?

41 views
Skip to first unread message

Henning P

unread,
Mar 7, 2018, 11:13:30 AM3/7/18
to OMNeT++ Users

Hello everybody,

I have a slightly tricky question about the behaviour of the Compiler for *.msg files. I came across this issue during the work with the Java Extensions. This post refers to OMNeT 5.0 and Inet 3.4.0. However, I assume that the observed behaviour is more general.

My question is: under which conditions does the Message Compiler generate setFieldValueAsString() support for a field? Only if a corresponding method in the class cClassDescriptor exists for the type this specific field (e.g., string2long() if the field is of type long)? This there a way to convince the Message Compiler to support setFieldValueAsString() for complex types (e.g., MyClass).

Actually, I am wondering that a field having a complex type (e.g., MyClass) cannot be set via setFieldValueAsString(). This is also the case, if a string constructor exists for MyClass and hence the conversion from string is trivial.

 

Example:

For the following file test.msg, the Message Compiler generates a method getFieldValueAsString() that supports all fields (testInt and testMacAddress) and a method setFieldValueAsString() that supports only the ones having primitive types (testInt). (Note: I know that I can manually edit the method setFieldValueAsString() in Test_m.cc, which actually works. However, I would like to have a cleaner implementation and not manually modify a generated file).

 

Test.msg:

cplusplus {{

#include "inet/linklayer/common/MACAddress.h"

}}

class noncobject MACAddress;

namespace inet;

 

message Test{

      int testInt;

      MACAddress testMacAddress;

 

}

 

 

Test_m.cc (generated from Test.msg by the Message Compiler):

std::string TestDescriptor::getFieldValueAsString(void *object, int field, int i) const

{

    omnetpp::cClassDescriptor *basedesc = getBaseClassDescriptor();

    if (basedesc) {

        if (field < basedesc->getFieldCount())

            return basedesc->getFieldValueAsString(object,field,i);

        field -= basedesc->getFieldCount();

    }

    Test *pp = (Test *)object; (void)pp;

    switch (field) {

        case 0: return long2string(pp->getTestInt());

        case 1: {std::stringstream out; out << pp->getTestMacAddress(); return out.str();}

        default: return "";

    }

}

 

bool TestDescriptor::setFieldValueAsString(void *object, int field, int i, const char *value) const

{

    omnetpp::cClassDescriptor *basedesc = getBaseClassDescriptor();

    if (basedesc) {

        if (field < basedesc->getFieldCount())

            return basedesc->setFieldValueAsString(object,field,i,value);

        field -= basedesc->getFieldCount();

    }

    Test *pp = (Test *)object; (void)pp;

    switch (field) {

        case 0: pp->setTestInt(string2long(value)); return true;

        default: return false;

    }

}


Regards

Henning

Attila Török

unread,
Mar 8, 2018, 3:00:36 PM3/8/18
to OMNeT++ Users
Hi!

Judging from a quick look at the source of msgc (src/nedxml/msgcodegenerator.cc), I believe the field has to have the @editable property, and maybe @fromString(...) as well.

Attila

--
You received this message because you are subscribed to the Google Groups "OMNeT++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to omnetpp+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/omnetpp.
For more options, visit https://groups.google.com/d/optout.

Henning P

unread,
Apr 24, 2018, 7:59:52 AM4/24/18
to omn...@googlegroups.com

Hi,


Actually, the answer to my question is that there are two solutions to get the possibility to the field via setFieldValueAsString().


Solution 1: There is a conversion method (e.g., string2long() if the field is of type long) in the class cClassDescriptor.


Solution 2: The @fromString(...) property can be used to link towards a method for the conversion (e.g., the string constructor). (e.g.: MACAddress testMacAddress @editable() @fromstring(inet::MACAddress($));)


Many thanks to Attila for the hints to get towards these solutions!

 

Regards

Henning

 

 

PS: sorry for the late reply but we wanted to examine this in detail before.

To unsubscribe from this group and stop receiving emails from it, send an email to omnetpp+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages