libcluon  0.0.148
cluon::GenericMessage Class Reference

#include <GenericMessage.hpp>

Public Member Functions

 GenericMessage ()=default
 
 GenericMessage (GenericMessage &&)=default
 
 GenericMessage (const GenericMessage &)=default
 
GenericMessageoperator= (const GenericMessage &)=default
 
int32_t ID ()
 
const std::string ShortName ()
 
const std::string LongName ()
 
template<typename T >
void createFrom (T &msg)
 
void createFrom (const MetaMessage &mm, const std::vector< MetaMessage > &mms) noexcept
 
void preVisit (int32_t id, const std::string &shortName, const std::string &longName) noexcept
 
void postVisit () noexcept
 
void visit (uint32_t id, std::string &&typeName, std::string &&name, bool &v) noexcept
 
void visit (uint32_t id, std::string &&typeName, std::string &&name, char &v) noexcept
 
void visit (uint32_t id, std::string &&typeName, std::string &&name, int8_t &v) noexcept
 
void visit (uint32_t id, std::string &&typeName, std::string &&name, uint8_t &v) noexcept
 
void visit (uint32_t id, std::string &&typeName, std::string &&name, int16_t &v) noexcept
 
void visit (uint32_t id, std::string &&typeName, std::string &&name, uint16_t &v) noexcept
 
void visit (uint32_t id, std::string &&typeName, std::string &&name, int32_t &v) noexcept
 
void visit (uint32_t id, std::string &&typeName, std::string &&name, uint32_t &v) noexcept
 
void visit (uint32_t id, std::string &&typeName, std::string &&name, int64_t &v) noexcept
 
void visit (uint32_t id, std::string &&typeName, std::string &&name, uint64_t &v) noexcept
 
void visit (uint32_t id, std::string &&typeName, std::string &&name, float &v) noexcept
 
void visit (uint32_t id, std::string &&typeName, std::string &&name, double &v) noexcept
 
void visit (uint32_t id, std::string &&typeName, std::string &&name, std::string &v) noexcept
 
template<typename T >
void visit (uint32_t &id, std::string &&typeName, std::string &&name, T &value) noexcept
 
template<class Visitor >
void accept (uint32_t fieldId, Visitor &visitor)
 
template<class Visitor >
void accept (Visitor &visitor)
 
template<class PreVisitor , class Visitor , class PostVisitor >
void accept (PreVisitor &&_preVisit, Visitor &&_visit, PostVisitor &&_postVisit)
 

Detailed Description

GenericMessage is providing an abstraction level to work with concrete messages. Therefore, it is acting as both, a Visitor to turn concrete messages into GenericMessages or as Visitable to access the contained data. GenericMessage would use C++'s std::any type; to allow C++14 compilers, we use the backport from linb::any.

Creating a GenericMessage: There are several ways to create a GenericMessage. The first option is to provide a message specification in ODVD format as result from MessageParser, from which a GenericMessage is created. This instance can then be visited afterwards by, for instance, an instance of ProtoDecoder to set the GenericMessage's actual values.

1) This example demonstrates how to process a given message specification to decode a Proto-encoded byte sequence (in protoEncodedData). The message specification is given in messageSpecification that is parsed from MessageParser. On success, it is tried to decode the Proto-encoded data into a GenericMesssage representing an instance of "MyMessage".

Using a message specification that does not match the serialized Proto data might result in unexpected behavior.

// protoEncodedData is provided from somewhere, i.e., via network for example
std::string protoEncodedData = <...>
std::stringstream sstr{protoEncodedData};
protoDecoder.decodeFrom(sstr);
const char *messageSpecification = R"(
message MyMessage [id = 123] {
int32 field1 [id = 1];
int32 field2 [id = 2];
}
cluon::MessageParser mp;
auto retVal = mp.parse(std::string(messageSpecification));
if (cluon::MessageParser::MessageParserErrorCodes::NO_MESSAGEPARSER_ERROR == retVal.second) {
cluon::GenericMessage gm;
auto listOfMetaMessages = retVal.first;
gm.createFrom(listOfMetaMessages[0], listOfMetaMessages);
// Set values in GenericMessage from protoDecoder.
gm.accept(protoDecoder);
}

2) This example demonstrates how to turn a given concrete message into a GenericMessage. Afterwards, the GenericMessage can be post-processed with Visitors.

MyMessage msg;
// set some fields in msg.
gm.createFrom<MyMessage>(msg);

After an instance of GenericMessage is available, it can be post-processed into various representations:

1) Printing the contained data ("toString"; GenericMessage is being visited):

// gm is created using one of the aforementioned options.
std::stringstream buffer;
gm.accept([](uint32_t, const std::string &, const std::string &) {},
[&buffer](uint32_t, std::string &&, std::string &&n, auto v) { buffer << n << " = " << v << std::endl; },
[]() {});
std::cout << buffer.str() << std::endl;

2) Filling the values of another concrete message (GenericMessage is acting as Visitor to another message):

// gm is created using one of the aforementioned options.
Message msg;
msg.accept(gm);

3) Serialize the GenericMessage gm into a Proto-format:

// gm is created using one of the aforementioned options.
cluon::ToProtoVisitor protoEncoder;
gm.accept(protoEncoder);
const std::string{protoEncoder.encodedData()};

4) Serialize the GenericMessage gm into JSON:

// gm is created using one of the aforementioned options.
gm.accept(j);
std::cout << j.json();

4) Dynamically transforming a given Proto-encoded byte sequence into JSON at runtime:

// protoEncodedData is provided from somewhere, i.e., via network for example
std::string protoEncodedData = <...>
std::stringstream sstr{protoEncodedData};
protoDecoder.decodeFrom(sstr);
const char *messageSpecification = R"(
message MyMessage [id = 123] {
int32 field1 [id = 1];
int32 field2 [id = 2];
}
cluon::MessageParser mp;
auto retVal = mp.parse(std::string(messageSpecification));
if (cluon::MessageParser::MessageParserErrorCodes::NO_MESSAGEPARSER_ERROR == retVal.second) {
cluon::GenericMessage gm;
auto listOfMetaMessages = retVal.first;
gm.createFrom(listOfMetaMessages[0], listOfMetaMessages);
// Set values in GenericMessage from protoDecoder.
gm.accept(protoDecoder);
}
cluon::ToJSONVisitor j;
gm.accept(j);
std::cout << j.json();

Constructor & Destructor Documentation

◆ GenericMessage() [1/3]

cluon::GenericMessage::GenericMessage ( )
default

◆ GenericMessage() [2/3]

cluon::GenericMessage::GenericMessage ( GenericMessage &&  )
default

◆ GenericMessage() [3/3]

cluon::GenericMessage::GenericMessage ( const GenericMessage )
default

Member Function Documentation

◆ accept() [1/3]

template<class Visitor >
void cluon::GenericMessage::accept ( uint32_t  fieldId,
Visitor &  visitor 
)
inline

This method allows other instances to visit this GenericMessage for post-processing the contained data on an individual field basis.

Parameters
fieldIdIdentifier for the field to visit.
visitorInstance of the visitor visiting this GenericMessage.

Referenced by cluon::LCMToGenericMessage::getGenericMessage(), cluon::EnvelopeConverter::getJSONFromEnvelope(), cluon::EnvelopeConverter::getProtoEncodedEnvelopeFromJSON(), and visit().

◆ accept() [2/3]

template<class Visitor >
void cluon::GenericMessage::accept ( Visitor &  visitor)
inline

This method allows other instances to visit this GenericMessage for post-processing the contained data.

Parameters
visitorInstance of the visitor visiting this GenericMessage.

◆ accept() [3/3]

template<class PreVisitor , class Visitor , class PostVisitor >
void cluon::GenericMessage::accept ( PreVisitor &&  _preVisit,
Visitor &&  _visit,
PostVisitor &&  _postVisit 
)
inline

◆ createFrom() [1/2]

template<typename T >
void cluon::GenericMessage::createFrom ( T &  msg)
inline

This methods creates this GenericMessage from a given concrete message.

Parameters
msgConcrete message used to derive this GenericMessage from.

References mm.

Referenced by createFrom(), cluon::LCMToGenericMessage::getGenericMessage(), cluon::EnvelopeConverter::getJSONFromEnvelope(), and cluon::EnvelopeConverter::getProtoEncodedEnvelopeFromJSON().

◆ createFrom() [2/2]

◆ ID()

int32_t cluon::GenericMessage::ID ( )

◆ LongName()

const std::string cluon::GenericMessage::LongName ( )

◆ operator=()

GenericMessage& cluon::GenericMessage::operator= ( const GenericMessage )
default

◆ postVisit()

void cluon::GenericMessage::postVisit ( )
noexcept

◆ preVisit()

void cluon::GenericMessage::preVisit ( int32_t  id,
const std::string &  shortName,
const std::string &  longName 
)
noexcept

◆ ShortName()

const std::string cluon::GenericMessage::ShortName ( )

References LongName().

◆ visit() [1/14]

void cluon::GenericMessage::visit ( uint32_t  id,
std::string &&  typeName,
std::string &&  name,
bool &  v 
)
noexcept

◆ visit() [2/14]

void cluon::GenericMessage::visit ( uint32_t  id,
std::string &&  typeName,
std::string &&  name,
char &  v 
)
noexcept

◆ visit() [3/14]

void cluon::GenericMessage::visit ( uint32_t  id,
std::string &&  typeName,
std::string &&  name,
int8_t &  v 
)
noexcept

◆ visit() [4/14]

void cluon::GenericMessage::visit ( uint32_t  id,
std::string &&  typeName,
std::string &&  name,
uint8_t &  v 
)
noexcept

◆ visit() [5/14]

void cluon::GenericMessage::visit ( uint32_t  id,
std::string &&  typeName,
std::string &&  name,
int16_t &  v 
)
noexcept

◆ visit() [6/14]

void cluon::GenericMessage::visit ( uint32_t  id,
std::string &&  typeName,
std::string &&  name,
uint16_t &  v 
)
noexcept

◆ visit() [7/14]

void cluon::GenericMessage::visit ( uint32_t  id,
std::string &&  typeName,
std::string &&  name,
int32_t &  v 
)
noexcept

◆ visit() [8/14]

void cluon::GenericMessage::visit ( uint32_t  id,
std::string &&  typeName,
std::string &&  name,
uint32_t &  v 
)
noexcept

◆ visit() [9/14]

void cluon::GenericMessage::visit ( uint32_t  id,
std::string &&  typeName,
std::string &&  name,
int64_t &  v 
)
noexcept

◆ visit() [10/14]

void cluon::GenericMessage::visit ( uint32_t  id,
std::string &&  typeName,
std::string &&  name,
uint64_t &  v 
)
noexcept

◆ visit() [11/14]

void cluon::GenericMessage::visit ( uint32_t  id,
std::string &&  typeName,
std::string &&  name,
float &  v 
)
noexcept

◆ visit() [12/14]

void cluon::GenericMessage::visit ( uint32_t  id,
std::string &&  typeName,
std::string &&  name,
double &  v 
)
noexcept

◆ visit() [13/14]

void cluon::GenericMessage::visit ( uint32_t  id,
std::string &&  typeName,
std::string &&  name,
std::string &  v 
)
noexcept

◆ visit() [14/14]

template<typename T >
void cluon::GenericMessage::visit ( uint32_t &  id,
std::string &&  typeName,
std::string &&  name,
T &  value 
)
inlinenoexcept

References accept().