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 00023 #ifndef VARIANT_TOPIC_TOOLS_MESSAGE_VARIANT_H 00024 #define VARIANT_TOPIC_TOOLS_MESSAGE_VARIANT_H 00025 00026 #include <vector> 00027 00028 #include <variant_topic_tools/CollectionVariant.h> 00029 #include <variant_topic_tools/MessageFieldCollection.h> 00030 #include <variant_topic_tools/MessageTypeTraits.h> 00031 00032 namespace variant_topic_tools { 00035 class MessageVariant : 00036 public CollectionVariant { 00037 friend class MessageDataType; 00038 friend class Variant; 00039 public: 00042 MessageVariant(); 00043 00046 MessageVariant(const MessageVariant& src); 00047 00050 MessageVariant(const Variant& src); 00051 00054 ~MessageVariant(); 00055 00056 using Variant::operator=; 00057 00058 protected: 00061 class Value : 00062 public CollectionVariant::Value { 00063 public: 00066 Value(); 00067 00070 virtual ~Value(); 00071 00074 void setValue(const Variant::Value& value); 00075 00079 virtual const std::string& getMemberName(int index) const = 0; 00080 00084 void writeMember(std::ostream& stream, int index) const; 00085 }; 00086 00089 class ValueImplV : 00090 public Value { 00091 public: 00094 ValueImplV(const MessageFieldCollection<Variant>& members = 00095 MessageFieldCollection<Variant>()); 00096 00099 ValueImplV(const ValueImplV& src); 00100 00103 virtual ~ValueImplV(); 00104 00108 size_t getNumMembers() const; 00109 00113 void setMember(int index, const Variant& member); 00114 00118 void setMember(const std::string& name, const Variant& member); 00119 00123 Variant getMember(int index) const; 00124 00128 Variant getMember(const std::string& name) const; 00129 00133 const std::string& getMemberName(int index) const; 00134 00138 bool hasMember(const std::string& name) const; 00139 00142 ValuePtr clone() const; 00143 00146 Serializer createSerializer(const DataType& type) const; 00147 00150 MessageFieldCollection<Variant> members; 00151 }; 00152 00155 template <typename T> class ValueImplT : 00156 public Variant::ValueT<typename type_traits::MessageType<T>::ValueType>, 00157 public Value { 00158 public: 00159 BOOST_STATIC_ASSERT(type_traits::IsMessage<T>::value); 00160 00163 typedef typename type_traits::MessageType<T>::ValueType ValueType; 00164 00167 ValueImplT(const MessageFieldCollection<MessageVariable>& members = 00168 MessageFieldCollection<MessageVariable>(), const Pointer<ValueType>& 00169 message = Pointer<ValueType>()); 00170 00173 ValueImplT(const ValueImplT<T>& src); 00174 00177 virtual ~ValueImplT(); 00178 00181 void set(const Pointer<ValueType>& value); 00182 00186 ValueType& getValue(); 00187 00191 const ValueType& getValue() const; 00192 00196 size_t getNumMembers() const; 00197 00201 void setMember(int index, const Variant& member); 00202 00206 void setMember(const std::string& name, const Variant& member); 00207 00211 Variant getMember(int index) const; 00212 00216 Variant getMember(const std::string& name) const; 00217 00221 const std::string& getMemberName(int index) const; 00222 00226 bool hasMember(const std::string& name) const; 00227 00230 ValuePtr clone() const; 00231 00234 Serializer createSerializer(const DataType& type) const; 00235 00238 MessageFieldCollection<MessageVariable> members; 00239 00242 mutable Pointer<ValueType> message; 00243 }; 00244 00248 MessageVariant(const DataType& type, const MessageFieldCollection< 00249 Variant>& members); 00250 00253 template <typename T> static MessageVariant create(const DataType& type, 00254 const MessageFieldCollection<MessageVariable>& members); 00255 }; 00256 }; 00257 00258 #include <variant_topic_tools/MessageVariant.tpp> 00259 00260 #endif