MessageVariant.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/MessageDataType.h"
00020 #include <variant_topic_tools/MessageSerializer.h>
00021 #include <variant_topic_tools/MessageVariable.h>
00022 #include "variant_topic_tools/MessageVariant.h"
00023 
00024 namespace variant_topic_tools {
00025 
00026 /*****************************************************************************/
00027 /* Constructors and Destructor                                               */
00028 /*****************************************************************************/
00029 
00030 MessageVariant::MessageVariant() {
00031 }
00032 
00033 MessageVariant::MessageVariant(const DataType& type, const
00034     MessageFieldCollection<Variant>& members) :
00035   CollectionVariant(type) {
00036   if (type.isValid())
00037     value.reset(new ValueImplV(members));
00038 }
00039 
00040 MessageVariant::MessageVariant(const MessageVariant& src) :
00041   CollectionVariant(src) {
00042 }
00043 
00044 MessageVariant::MessageVariant(const Variant& src) :
00045   CollectionVariant(src) {
00046   if (value)
00047     BOOST_ASSERT(boost::dynamic_pointer_cast<Value>(value));
00048 }
00049 
00050 MessageVariant::~MessageVariant() {
00051 }
00052 
00053 MessageVariant::Value::Value() {
00054 }
00055 
00056 MessageVariant::Value::~Value() {
00057 }
00058 
00059 MessageVariant::ValueImplV::ValueImplV(const MessageFieldCollection<Variant>&
00060     members) :
00061   members(members) {
00062 }
00063 
00064 MessageVariant::ValueImplV::ValueImplV(const ValueImplV& src) :
00065   members(src.members) {
00066 }
00067 
00068 MessageVariant::ValueImplV::~ValueImplV() {
00069 }
00070 
00071 /*****************************************************************************/
00072 /* Accessors                                                                 */
00073 /*****************************************************************************/
00074 
00075 void MessageVariant::Value::setValue(const Variant::Value& value) {
00076   const Value& messageValue = dynamic_cast<const Value&>(value);
00077 
00078   for (size_t i = 0; i < getNumMembers(); ++i)
00079     setMember(i, messageValue.getMember(i));
00080 }
00081 
00082 size_t MessageVariant::ValueImplV::getNumMembers() const {
00083   return members.getNumFields();
00084 }
00085 
00086 void MessageVariant::ValueImplV::setMember(int index, const Variant&
00087     member) {
00088   members.getField(index).setValue(member);
00089 }
00090 
00091 void MessageVariant::ValueImplV::setMember(const std::string& name, const
00092     Variant& member) {
00093   members.getField(name).setValue(member);
00094 }
00095 
00096 Variant MessageVariant::ValueImplV::getMember(int index) const {
00097   return members.getField(index).getValue();
00098 }
00099 
00100 Variant MessageVariant::ValueImplV::getMember(const std::string& name) const {
00101   return members.getField(name).getValue();
00102 }
00103 
00104 const std::string& MessageVariant::ValueImplV::getMemberName(int index)
00105     const {
00106   return members.getField(index).getName();
00107 }
00108 
00109 bool MessageVariant::ValueImplV::hasMember(const std::string& name) const {
00110   return members.hasField(name);
00111 }
00112 
00113 /*****************************************************************************/
00114 /* Methods                                                                   */
00115 /*****************************************************************************/
00116 
00117 void MessageVariant::Value::writeMember(std::ostream& stream, int index)
00118     const {
00119   Variant member = getMember(index);
00120 
00121   if (!member.getType().isBuiltin()) {
00122     stream << getMemberName(index) << ":";
00123 
00124     std::stringstream memberStream;
00125     std::string line;
00126 
00127     memberStream << member;
00128 
00129     while (std::getline(memberStream, line))
00130       stream << "\n  " << line;
00131   }
00132   else
00133     stream << getMemberName(index) << ": " << member;
00134 }
00135 
00136 Variant::ValuePtr MessageVariant::ValueImplV::clone() const {
00137   return Variant::ValuePtr(new ValueImplV(*this));
00138 }
00139 
00140 Serializer MessageVariant::ValueImplV::createSerializer(const DataType& type)
00141     const {
00142   MessageFieldCollection<Serializer> memberSerializers;
00143 
00144   for (size_t i = 0; i < members.getNumFields(); ++i)
00145     memberSerializers.appendField(members[i].getName(),
00146       members[i].getValue().createSerializer());
00147 
00148   return MessageSerializer(memberSerializers);
00149 }
00150 
00151 }


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