Skip to content

Commit

Permalink
Refs eProsima#3243. Fixing warnings in generated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
richiware committed Jul 27, 2018
1 parent 30d9bbd commit e12bbce
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ $solution.projects : {$pub_sub_execs(project=it, libraries=solution.libraries)$}
pub_sub_execs(project, libraries) ::= <<

message(STATUS "Configuring $project.name$...")
add_executable($project.name$ $project.commonSrcFiles : {$it$}; separator=" "$ $project.projectSrcFiles : {$it$}; separator=" "$)
target_link_libraries($project.name$ $solution.libraries : {$it$}; separator=" "$)
add_library($project.name$_lib $project.commonSrcFiles : {$it$}; separator=" "$)
target_link_libraries($project.name$_lib $solution.libraries : {$it$}; separator=" "$)

add_executable($project.name$ $project.projectSrcFiles : {$it$}; separator=" "$)
target_link_libraries($project.name$ $solution.libraries : {$it$}; separator=" "$
$project.name$_lib $project.dependencies : {$it$_lib}; separator=" "$)
>>
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ typedef_decl(ctx, parent, typedefs) ::= <<>>
struct_type(ctx, parent, struct) ::= <<
$if(parent.IsInterface)$$parent.name$_$endif$$struct.name$PubSubType::$if(parent.IsInterface)$$parent.name$_$endif$$struct.name$PubSubType() {
setName("$struct.scopedname$");
m_typeSize = (uint32_t)$if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$::getMaxCdrSerializedSize() + 4 /*encapsulation*/;
m_typeSize = static_cast<uint32_t>($if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$::getMaxCdrSerializedSize()) + 4 /*encapsulation*/;
m_isGetKeyDefined = $if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$::isKeyDefined();
m_keyBuffer = (unsigned char*)malloc($if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$::getKeyMaxCdrSerializedSize()>16 ? $if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$::getKeyMaxCdrSerializedSize() : 16);
m_keyBuffer = reinterpret_cast<unsigned char*>(malloc($if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$::getKeyMaxCdrSerializedSize()>16 ? $if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$::getKeyMaxCdrSerializedSize() : 16));
}

$if(parent.IsInterface)$$parent.name$_$endif$$struct.name$PubSubType::~$if(parent.IsInterface)$$parent.name$_$endif$$struct.name$PubSubType() {
Expand All @@ -79,8 +79,8 @@ $if(parent.IsInterface)$$parent.name$_$endif$$struct.name$PubSubType::~$if(paren
}

bool $if(parent.IsInterface)$$parent.name$_$endif$$struct.name$PubSubType::serialize(void *data, SerializedPayload_t *payload) {
$if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$ *p_type = ($if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$*) data;
eprosima::fastcdr::FastBuffer fastbuffer((char*) payload->data, payload->max_size); // Object that manages the raw buffer.
$if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$ *p_type = static_cast<$if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$*>(data);
eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->max_size); // Object that manages the raw buffer.
eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
eprosima::fastcdr::Cdr::DDS_CDR); // Object that serializes the data.
payload->encapsulation = ser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE;
Expand All @@ -96,13 +96,13 @@ bool $if(parent.IsInterface)$$parent.name$_$endif$$struct.name$PubSubType::seria
return false;
}

payload->length = (uint32_t)ser.getSerializedDataLength(); //Get the serialized length
payload->length = static_cast<uint32_t>(ser.getSerializedDataLength()); //Get the serialized length
return true;
}

bool $if(parent.IsInterface)$$parent.name$_$endif$$struct.name$PubSubType::deserialize(SerializedPayload_t* payload, void* data) {
$if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$* p_type = ($if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$*) data; //Convert DATA to pointer of your type
eprosima::fastcdr::FastBuffer fastbuffer((char*)payload->data, payload->length); // Object that manages the raw buffer.
$if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$* p_type = static_cast<$if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$*>(data); //Convert DATA to pointer of your type
eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->length); // Object that manages the raw buffer.
eprosima::fastcdr::Cdr deser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
eprosima::fastcdr::Cdr::DDS_CDR); // Object that deserializes the data.
// Deserialize encapsulation.
Expand All @@ -124,28 +124,28 @@ bool $if(parent.IsInterface)$$parent.name$_$endif$$struct.name$PubSubType::deser
std::function<uint32_t()> $if(parent.IsInterface)$$parent.name$_$endif$$struct.name$PubSubType::getSerializedSizeProvider(void* data) {
return [data]() -> uint32_t
{
return (uint32_t)type::getCdrSerializedSize(*static_cast<$if(parent.IsInterface)$$parent.name$_$endif$$struct.name$*>(data)) + 4 /*encapsulation*/;
return static_cast<uint32_t>(type::getCdrSerializedSize(*static_cast<$if(parent.IsInterface)$$parent.name$_$endif$$struct.name$*>(data))) + 4 /*encapsulation*/;
};
}

void* $if(parent.IsInterface)$$parent.name$_$endif$$struct.name$PubSubType::createData() {
return (void*)new $if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$();
return reinterpret_cast<void*>(new $if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$());
}

void $if(parent.IsInterface)$$parent.name$_$endif$$struct.name$PubSubType::deleteData(void* data) {
delete(($if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$*)data);
delete(reinterpret_cast<$if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$*>(data));
}

bool $if(parent.IsInterface)$$parent.name$_$endif$$struct.name$PubSubType::getKey(void *data, InstanceHandle_t* handle) {
if(!m_isGetKeyDefined)
return false;
$if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$* p_type = ($if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$*) data;
eprosima::fastcdr::FastBuffer fastbuffer((char*)m_keyBuffer,$if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$::getKeyMaxCdrSerializedSize()); // Object that manages the raw buffer.
$if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$* p_type = static_cast<$if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$*>(data);
eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(m_keyBuffer),$if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$::getKeyMaxCdrSerializedSize()); // Object that manages the raw buffer.
eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::BIG_ENDIANNESS); // Object that serializes the data.
p_type->serializeKey(ser);
if($if(parent.IsInterface)$$struct.scopedname$$else$$struct.name$$endif$::getKeyMaxCdrSerializedSize()>16) {
m_md5.init();
m_md5.update(m_keyBuffer,(unsigned int)ser.getSerializedDataLength());
m_md5.update(m_keyBuffer, static_cast<unsigned int>(ser.getSerializedDataLength()));
m_md5.finalize();
for(uint8_t i = 0;i<16;++i) {
handle->value[i] = m_md5.digest[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,83 +38,92 @@ $ctx.filename$Publisher::~$ctx.filename$Publisher() { Domain::removeParticipant(

bool $ctx.filename$Publisher::init()
{
// Create RTPSParticipant

ParticipantAttributes PParam;
PParam.rtps.builtin.domainId = 0;
PParam.rtps.builtin.leaseDuration = c_TimeInfinite;
PParam.rtps.setName("Participant_publisher"); //You can put here the name you want
mp_participant = Domain::createParticipant(PParam);
if(mp_participant == nullptr)
return false;

//Register the type

Domain::registerType(mp_participant,(TopicDataType*) &myType);

// Create Publisher

PublisherAttributes Wparam;
$if(ctx.lastStructure.typeCode.hasKey)$
Wparam.topic.topicKind = WITH_KEY;
// Create RTPSParticipant

ParticipantAttributes PParam;
PParam.rtps.builtin.domainId = 0;
PParam.rtps.builtin.leaseDuration = c_TimeInfinite;
PParam.rtps.setName("Participant_publisher"); //You can put here the name you want
mp_participant = Domain::createParticipant(PParam);
if(mp_participant == nullptr)
{
return false;
}

//Register the type

Domain::registerType(mp_participant, static_cast<TopicDataType*>(&myType));

// Create Publisher

PublisherAttributes Wparam;
$if(ctx.lastStructure.typeCode.hasKey)$
Wparam.topic.topicKind = WITH_KEY;
$else$
Wparam.topic.topicKind = NO_KEY;
Wparam.topic.topicKind = NO_KEY;
$endif$
Wparam.topic.topicDataType = myType.getName(); //This type MUST be registered
Wparam.topic.topicName = "$ctx.filename$PubSubTopic";
mp_publisher = Domain::createPublisher(mp_participant,Wparam,(PublisherListener*)&m_listener);
if(mp_publisher == nullptr)
return false;
std::cout << "Publisher created, waiting for Subscribers." << std::endl;
return true;
Wparam.topic.topicDataType = myType.getName(); //This type MUST be registered
Wparam.topic.topicName = "$ctx.filename$PubSubTopic";

mp_publisher = Domain::createPublisher(mp_participant,Wparam,static_cast<PublisherListener*>(&m_listener));

if(mp_publisher == nullptr)
{
return false;
}

std::cout << "Publisher created, waiting for Subscribers." << std::endl;
return true;
}

void $ctx.filename$Publisher::PubListener::onPublicationMatched(Publisher* pub,MatchingInfo& info)
{
if (info.status == MATCHED_MATCHING)
{
n_matched++;
std::cout << "Publisher matched" << std::endl;
}
else
{
n_matched--;
std::cout << "Publisher unmatched" << std::endl;
}
(void)pub;

if (info.status == MATCHED_MATCHING)
{
n_matched++;
std::cout << "Publisher matched" << std::endl;
}
else
{
n_matched--;
std::cout << "Publisher unmatched" << std::endl;
}
}

void $ctx.filename$Publisher::run()
{
while(m_listener.n_matched == 0)
{
eClock::my_sleep(250); // Sleep 250 ms
}
// Publication code
$ctx.m_lastStructureScopedName$ st;
/* Initialize your structure here */
int msgsent = 0;
char ch = 'y';
do
{
if(ch == 'y')
{
mp_publisher->write(&st); ++msgsent;
std::cout << "Sending sample, count=" << msgsent << ", send another sample?(y-yes,n-stop): ";
}
else if(ch == 'n')
{
std::cout << "Stopping execution " << std::endl;
break;
}
else
{
std::cout << "Command " << ch << " not recognized, please enter \"y/n\":";
}
}while(std::cin \>> ch);
while(m_listener.n_matched == 0)
{
eClock::my_sleep(250); // Sleep 250 ms
}

// Publication code

$ctx.m_lastStructureScopedName$ st;

/* Initialize your structure here */

int msgsent = 0;
char ch = 'y';
do
{
if(ch == 'y')
{
mp_publisher->write(&st); ++msgsent;
std::cout << "Sending sample, count=" << msgsent << ", send another sample?(y-yes,n-stop): ";
}
else if(ch == 'n')
{
std::cout << "Stopping execution " << std::endl;
break;
}
else
{
std::cout << "Command " << ch << " not recognized, please enter \"y/n\":";
}
} while(std::cin \>> ch);
}

>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,71 +35,77 @@ $ctx.filename$Subscriber::~$ctx.filename$Subscriber() { Domain::removeParticipan

bool $ctx.filename$Subscriber::init()
{
// Create RTPSParticipant

ParticipantAttributes PParam;
PParam.rtps.builtin.domainId = 0; //MUST BE THE SAME AS IN THE PUBLISHER
PParam.rtps.builtin.leaseDuration = c_TimeInfinite;
PParam.rtps.setName("Participant_subscriber"); //You can put the name you want
mp_participant = Domain::createParticipant(PParam);
if(mp_participant == nullptr)
return false;

//Register the type

Domain::registerType(mp_participant,(TopicDataType*) &myType);

// Create Subscriber

SubscriberAttributes Rparam;
$if(ctx.lastStructure.typeCode.hasKey)$
Rparam.topic.topicKind = WITH_KEY;
// Create RTPSParticipant

ParticipantAttributes PParam;
PParam.rtps.builtin.domainId = 0; //MUST BE THE SAME AS IN THE PUBLISHER
PParam.rtps.builtin.leaseDuration = c_TimeInfinite;
PParam.rtps.setName("Participant_subscriber"); //You can put the name you want
mp_participant = Domain::createParticipant(PParam);
if(mp_participant == nullptr)
{
return false;
}

//Register the type

Domain::registerType(mp_participant, static_cast<TopicDataType*>(&myType));

// Create Subscriber

SubscriberAttributes Rparam;
$if(ctx.lastStructure.typeCode.hasKey)$
Rparam.topic.topicKind = WITH_KEY;
$else$
Rparam.topic.topicKind = NO_KEY;
Rparam.topic.topicKind = NO_KEY;
$endif$
Rparam.topic.topicDataType = myType.getName(); //Must be registered before the creation of the subscriber
Rparam.topic.topicName = "$ctx.filename$PubSubTopic";
mp_subscriber = Domain::createSubscriber(mp_participant,Rparam,(SubscriberListener*)&m_listener);
if(mp_subscriber == nullptr)
return false;
return true;
Rparam.topic.topicDataType = myType.getName(); //Must be registered before the creation of the subscriber
Rparam.topic.topicName = "$ctx.filename$PubSubTopic";
mp_subscriber = Domain::createSubscriber(mp_participant,Rparam, static_cast<SubscriberListener*>(&m_listener));
if(mp_subscriber == nullptr)
{
return false;
}
return true;
}

void $ctx.filename$Subscriber::SubListener::onSubscriptionMatched(Subscriber* sub,MatchingInfo& info)
{
if (info.status == MATCHED_MATCHING)
{
n_matched++;
std::cout << "Subscriber matched" << std::endl;
}
else
{
n_matched--;
std::cout << "Subscriber unmatched" << std::endl;
}
(void)sub;

if (info.status == MATCHED_MATCHING)
{
n_matched++;
std::cout << "Subscriber matched" << std::endl;
}
else
{
n_matched--;
std::cout << "Subscriber unmatched" << std::endl;
}
}

void $ctx.filename$Subscriber::SubListener::onNewDataMessage(Subscriber* sub)
{
// Take data
$ctx.m_lastStructureScopedName$ st;
if(sub->takeNextData(&st, &m_info))
{
if(m_info.sampleKind == ALIVE)
{
// Print your structure data here.
++n_msg;
std::cout << "Sample received, count=" << n_msg << std::endl;
}
}
// Take data
$ctx.m_lastStructureScopedName$ st;

if(sub->takeNextData(&st, &m_info))
{
if(m_info.sampleKind == ALIVE)
{
// Print your structure data here.
++n_msg;
std::cout << "Sample received, count=" << n_msg << std::endl;
}
}
}

void $ctx.filename$Subscriber::run()
{
std::cout << "Waiting for Data, press Enter to stop the Subscriber. "<<std::endl;
std::cin.ignore();
std::cout << "Shutting down the Subscriber." << std::endl;
std::cout << "Waiting for Data, press Enter to stop the Subscriber. "<<std::endl;
std::cin.ignore();
std::cout << "Shutting down the Subscriber." << std::endl;
}


Expand Down

0 comments on commit e12bbce

Please sign in to comment.