Message.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  * Copyright (C) 2014 by Ralf Kaestner                                        *
00003  * ralf.kaestner@gmail.com                                                    *
00004  *                                                                            *
00005  * This program is free software; you can redistribute it and/or modify       *
00006  * it under the terms of the Lesser GNU General Public License as published by*
00007  * the Free Software Foundation; either version 3 of the License, or          *
00008  * (at your option) any later version.                                        *
00009  *                                                                            *
00010  * This program is distributed in the hope that it will be useful,            *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the               *
00013  * Lesser GNU General Public License for more details.                        *
00014  *                                                                            *
00015  * You should have received a copy of the Lesser GNU General Public License   *
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.       *
00017  ******************************************************************************/
00018 
00019 #include "variant_topic_tools/DataTypeRegistry.h"
00020 #include "variant_topic_tools/Message.h"
00021 #include "variant_topic_tools/MessageDefinition.h"
00022 #include "variant_topic_tools/MessageSerializer.h"
00023 #include "variant_topic_tools/MessageVariant.h"
00024 
00025 namespace variant_topic_tools {
00026 
00027 /*****************************************************************************/
00028 /* Constructors and Destructor                                               */
00029 /*****************************************************************************/
00030 
00031 Message::Message() {
00032 }
00033 
00034 Message::Message(const Message& src) :
00035   header(src.header),
00036   type(src.type),
00037   data(src.data) {
00038 }
00039 
00040 Message::~Message() {
00041 }
00042 
00043 /*****************************************************************************/
00044 /* Accessors                                                                 */
00045 /*****************************************************************************/
00046 
00047 void Message::setHeader(const MessageHeader& header) {
00048   this->header = header;
00049   
00050   type.setMD5Sum(header["md5sum"]);
00051   type.setDataType(header["type"]);
00052   type.setDefinition(header["message_definition"]);
00053 }
00054 
00055 const MessageHeader& Message::getHeader() const {
00056   return header;
00057 }
00058 
00059 void Message::setType(const MessageType& type) {
00060   this->type = type;
00061   
00062   header["md5sum"] = type.getMD5Sum();
00063   header["type"] = type.getDataType();
00064   header["message_definition"] = type.getDefinition();
00065 }
00066 
00067 const MessageType& Message::getType() const {
00068   return type;
00069 }
00070 
00071 void Message::setData(const std::vector<uint8_t>& data) {
00072   this->data = data;
00073 }
00074 
00075 std::vector<uint8_t>& Message::getData() {
00076   return data;
00077 }
00078 
00079 const std::vector<uint8_t>& Message::getData() const {
00080   return data;
00081 }
00082 
00083 void Message::setSize(size_t size) {
00084   data.resize(size);
00085 }
00086 
00087 size_t Message::getSize() const {
00088   return data.size();
00089 }
00090 
00091 /*****************************************************************************/
00092 /* Methods                                                                   */
00093 /*****************************************************************************/
00094 
00095 void Message::serialize(const MessageVariant& variant) {
00096   MessageDataType dataType = variant.getType();
00097   MessageType type(dataType.getIdentifier(), dataType.getMD5Sum(),
00098     dataType.getDefinition());
00099   
00100   setType(type);
00101   MessageSerializer serializer = variant.createSerializer();
00102   data.resize(serializer.getSerializedLength(variant));
00103   ros::serialization::OStream stream(const_cast<uint8_t*>(
00104     data.data()), data.size());
00105   
00106   serializer.serialize(stream, variant);
00107 }
00108 
00109 void Message::deserialize(MessageVariant& variant) const {
00110   DataTypeRegistry registry;
00111   DataType dataType = registry.getDataType(type.getDataType());
00112   
00113   if (!dataType) {
00114     MessageDefinition definition(type);
00115     dataType = definition.getMessageDataType();
00116   }
00117   
00118   variant = dataType.createVariant();
00119   MessageSerializer serializer = variant.createSerializer();
00120   ros::serialization::IStream stream(const_cast<uint8_t*>(
00121     data.data()), data.size());
00122   
00123   serializer.deserialize(stream, variant);
00124 }
00125 
00126 boost::shared_ptr<variant_msgs::Variant> Message::toVariantMessage() const {
00127   boost::shared_ptr<variant_msgs::Variant> variant(
00128     new variant_msgs::Variant());
00129   
00130   variant->header.publisher = header.getPublisher();
00131   variant->header.topic = header.getTopic();
00132   variant->header.latched = header.isLatched();
00133   
00134   variant->type.data_type = type.getDataType();
00135   variant->type.md5_sum = type.getMD5Sum();
00136   variant->type.definition = type.getDefinition();
00137   
00138   variant->data = data;
00139   
00140   return variant;
00141 }
00142 
00143 }


variant_topic_tools
Author(s): Ralf Kaestner
autogenerated on Tue Jul 9 2019 03:18:42