Variant.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2014 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
23 #ifndef VARIANT_TOPIC_TOOLS_VARIANT_H
24 #define VARIANT_TOPIC_TOOLS_VARIANT_H
25 
26 #include <typeinfo>
27 
28 #include <boost/type_traits.hpp>
29 
30 #include <ros/ros.h>
31 
35 
36 namespace variant_topic_tools {
39  class Variant {
40  friend class MessageVariable;
41  public:
44  Variant();
45 
48  template <typename T> Variant(const T& src);
49 
52  ~Variant();
53 
56  template <typename T> void setValue(const T& value);
57 
60  template <typename T> typename type_traits::DataType<T>::ValueType&
61  getValue();
62 
65  template <typename T> const typename type_traits::DataType<T>::ValueType&
66  getValue() const;
67 
70  const DataType& getType() const;
71 
77  const std::type_info& getValueTypeInfo() const;
78 
81  bool hasType() const;
82 
85  bool isArray() const;
86 
89  bool isBuiltin() const;
90 
93  bool isCollection() const;
94 
97  bool isMessage() const;
98 
102  bool isEmpty() const;
103 
106  ArrayVariant asArray() const;
107 
110  BuiltinVariant asBuiltin() const;
111 
115 
118  MessageVariant asMessage() const;
119 
122  void clear();
123 
126  void read(std::istream& stream);
127 
130  void write(std::ostream& stream) const;
131 
135 
138  template <typename T> Variant& operator=(const T& src);
139 
142  template <typename T> operator T() const;
143 
146  template <typename T> bool operator==(const T& value) const;
147 
150  template <typename T> bool operator!=(const T& value) const;
151 
152  protected:
155  class Value;
156 
160 
163  typedef boost::weak_ptr<Value> ValueWPtr;
164 
167  class Value {
168  public:
171  Value();
172 
175  virtual ~Value();
176 
179  virtual const std::type_info& getTypeInfo() const;
180 
183  virtual void setValue(const Value& value) = 0;
184 
188  virtual bool isEqual(const Value& value) const = 0;
189 
192  virtual ValuePtr clone() const = 0;
193 
196  virtual void read(std::istream& stream) = 0;
197 
200  virtual void write(std::ostream& stream) const = 0;
201 
204  virtual Serializer createSerializer(const DataType& type) const = 0;
205  };
206 
209  template <typename T> class ValueT :
210  public virtual Value {
211  public:
214  ValueT();
215 
218  virtual ~ValueT();
219 
223  const std::type_info& getTypeInfo() const;
224 
227  virtual void set(const Pointer<T>& value) = 0;
228 
232  void setValue(const T& value);
233 
237  virtual T& getValue() = 0;
238 
242  virtual const T& getValue() const = 0;
243  };
244 
248 
251  ValuePtr value;
252 
255  Variant(const DataType& type);
256 
259  template <typename T> static void set(Variant& variant, const
261 
264  template <typename T> static void setValue(Variant& dst, const T&
265  value, typename boost::enable_if<boost::is_base_of<Variant, T> >::
266  type* = 0);
267 
271  template <typename T> static void setValue(Variant& dst, const T& value,
272  typename boost::disable_if<boost::is_base_of<Variant, T> >::type* = 0);
273 
277  template <typename T> static bool isEqual(const Variant& variant, const
278  T& value, typename boost::enable_if<boost::is_base_of<Variant, T> >::
279  type* = 0);
280 
284  template <typename T> static bool isEqual(const Variant& variant, const
285  T& value, typename boost::disable_if<boost::is_base_of<Variant, T> >::
286  type* = 0);
287 
290  template <typename T> static void assign(Variant& dst, const T& src,
291  typename boost::enable_if<boost::is_base_of<Variant, T> >::type* = 0);
292 
295  template <typename T> static void assign(Variant& dst, const T& src,
296  typename boost::disable_if<boost::is_base_of<Variant, T> >::type* = 0);
297  };
298 
301  std::istream& operator>>(std::istream& stream, Variant& variant);
302 
305  std::ostream& operator<<(std::ostream& stream, const Variant& variant);
306 };
307 
308 #include <variant_topic_tools/Variant.tpp>
309 
310 #endif
Header file providing the DataType class interface.
Variant()
Default constructor.
Definition: Variant.cpp:32
Value()
Default constructor.
Definition: Variant.cpp:42
const DataType & getType() const
Retrieve the data type of this variant.
Definition: Variant.cpp:52
std::istream & operator>>(std::istream &stream, Variant &variant)
Operator for reading the variant from a stream.
Definition: Variant.cpp:149
bool hasType() const
True, if the variant has a type.
Definition: Variant.cpp:63
Serializer createSerializer() const
Create a serializer for this variant.
Definition: Variant.cpp:138
boost::weak_ptr< Value > ValueWPtr
Declaration of the variant value weak pointer type.
Definition: Variant.h:163
Variant value (abstract base)
Definition: Variant.h:167
void clear()
Clear the variant.
Definition: Variant.cpp:123
bool isBuiltin() const
True, if this variant represents a built-in.
Definition: Variant.cpp:74
DataType type
The variant&#39;s data type.
Definition: Variant.h:247
ValuePtr value
The variant&#39;s data value.
Definition: Variant.h:251
void setValue(const T &value)
Set the variant&#39;s value.
type_traits::DataType< T >::ValueType & getValue()
Retrieve the variant&#39;s value (non-const version)
bool isArray() const
True, if this variant represents an array.
Definition: Variant.cpp:67
Header file providing the data type traits.
void read(std::istream &stream)
Read the variant from a stream.
Definition: Variant.cpp:128
std::ostream & operator<<(std::ostream &stream, const DataType &dataType)
Operator for writing the data type to a stream.
Definition: DataType.cpp:190
virtual ~Value()
Destructor.
Definition: Variant.cpp:45
virtual void read(std::istream &stream)=0
Read the variant from a stream (abstract declaration)
virtual Serializer createSerializer(const DataType &type) const =0
Create a serializer for this variant (abstract declaration)
bool isCollection() const
True, if this variant represents a collection.
Definition: Variant.cpp:81
static void assign(Variant &dst, const T &src, typename boost::enable_if< boost::is_base_of< Variant, T > >::type *=0)
Assign a variant (overloaded version taking a variant)
Shared pointer.
Definition: Pointer.h:33
virtual void write(std::ostream &stream) const =0
Write this variant value to a stream (abstract declaration)
bool isEmpty() const
True, if the variant is empty, i.e., does not have a value assigned.
Definition: Variant.cpp:95
Header file providing the Pointer class interface.
virtual bool isEqual(const Value &value) const =0
True, if this variant value equals another variant value (abstract declaration)
MessageVariant asMessage() const
Retrieve this variant as message.
Definition: Variant.cpp:119
virtual void setValue(const Value &value)=0
Set the variant&#39;s value (abstract declaration)
ArrayVariant asArray() const
Retrieve this variant as array.
Definition: Variant.cpp:107
Variant value (templated abstract base)
Definition: Variant.h:209
boost::shared_ptr< Value > ValuePtr
Declaration of the variant value pointer type.
Definition: Variant.h:155
void write(std::ostream &stream) const
Write the variant to a stream.
Definition: Variant.cpp:133
bool operator!=(const T &value) const
True, if this variant&#39;s value does not equal another value.
CollectionVariant asCollection() const
Retrieve this variant as collection.
Definition: Variant.cpp:115
virtual const std::type_info & getTypeInfo() const
Retrieve the variant&#39;s value type information.
Definition: Variant.cpp:99
bool isMessage() const
True, if this variant represents a message.
Definition: Variant.cpp:88
bool operator==(const T &value) const
True, if this variant&#39;s value equals another value.
virtual ValuePtr clone() const =0
Clone this variant value (abstract declaration)
Variant & operator=(const T &src)
Assignment operator.
BuiltinVariant asBuiltin() const
Retrieve this variant as builtin.
Definition: Variant.cpp:111
const std::type_info & getValueTypeInfo() const
Retrieve the value type information of this variant.
Definition: Variant.cpp:56


variant_topic_tools
Author(s): Ralf Kaestner
autogenerated on Sat Jan 9 2021 03:56:50