Variant.h
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 
00023 #ifndef VARIANT_TOPIC_TOOLS_VARIANT_H
00024 #define VARIANT_TOPIC_TOOLS_VARIANT_H
00025 
00026 #include <typeinfo>
00027 
00028 #include <boost/type_traits.hpp>
00029 
00030 #include <ros/ros.h>
00031 
00032 #include <variant_topic_tools/DataType.h>
00033 #include <variant_topic_tools/DataTypeTraits.h>
00034 #include <variant_topic_tools/Pointer.h>
00035 
00036 namespace variant_topic_tools {
00039   class Variant {
00040   friend class MessageVariable;
00041   public:
00044     Variant();
00045 
00048     template <typename T> Variant(const T& src);
00049 
00052     ~Variant();
00053 
00056     template <typename T> void setValue(const T& value);
00057 
00060     template <typename T> typename type_traits::DataType<T>::ValueType&
00061       getValue();
00062 
00065     template <typename T> const typename type_traits::DataType<T>::ValueType&
00066       getValue() const;
00067 
00070     const DataType& getType() const;
00071 
00077     const std::type_info& getValueTypeInfo() const;
00078 
00081     bool hasType() const;
00082 
00085     bool isArray() const;
00086 
00089     bool isBuiltin() const;
00090 
00093     bool isCollection() const;
00094 
00097     bool isMessage() const;
00098 
00102     bool isEmpty() const;
00103 
00106     ArrayVariant asArray() const;
00107 
00110     BuiltinVariant asBuiltin() const;
00111 
00114     CollectionVariant asCollection() const;
00115 
00118     MessageVariant asMessage() const;
00119 
00122     void clear();
00123 
00126     void read(std::istream& stream);
00127 
00130     void write(std::ostream& stream) const;
00131 
00134     Serializer createSerializer() const;
00135 
00138     template <typename T> Variant& operator=(const T& src);
00139 
00142     template <typename T> operator T() const;
00143 
00146     template <typename T> bool operator==(const T& value) const;
00147 
00150     template <typename T> bool operator!=(const T& value) const;
00151 
00152   protected:
00155     class Value;
00156 
00159     typedef boost::shared_ptr<Value> ValuePtr;
00160 
00163     typedef boost::weak_ptr<Value> ValueWPtr;
00164 
00167     class Value {
00168     public:
00171       Value();
00172 
00175       virtual ~Value();
00176 
00179       virtual const std::type_info& getTypeInfo() const;
00180 
00183       virtual void setValue(const Value& value) = 0;
00184 
00188       virtual bool isEqual(const Value& value) const = 0;
00189 
00192       virtual ValuePtr clone() const = 0;
00193 
00196       virtual void read(std::istream& stream) = 0;
00197 
00200       virtual void write(std::ostream& stream) const = 0;
00201 
00204       virtual Serializer createSerializer(const DataType& type) const = 0;
00205     };
00206 
00209     template <typename T> class ValueT :
00210       public virtual Value {
00211     public:
00214       ValueT();
00215 
00218       virtual ~ValueT();
00219 
00223       const std::type_info& getTypeInfo() const;
00224 
00227       virtual void set(const Pointer<T>& value) = 0;
00228 
00232       void setValue(const T& value);
00233 
00237       virtual T& getValue() = 0;
00238 
00242       virtual const T& getValue() const = 0;
00243     };
00244 
00247     DataType type;
00248 
00251     ValuePtr value;
00252 
00255     Variant(const DataType& type);
00256 
00259     template <typename T> static void set(Variant& variant, const
00260       Pointer<typename type_traits::DataType<T>::ValueType>& value);
00261 
00264     template <typename T> static void setValue(Variant& dst, const T&
00265       value, typename boost::enable_if<boost::is_base_of<Variant, T> >::
00266       type* = 0);
00267 
00271     template <typename T> static void setValue(Variant& dst, const T& value,
00272       typename boost::disable_if<boost::is_base_of<Variant, T> >::type* = 0);
00273 
00277     template <typename T> static bool isEqual(const Variant& variant, const
00278       T& value, typename boost::enable_if<boost::is_base_of<Variant, T> >::
00279       type* = 0);
00280 
00284     template <typename T> static bool isEqual(const Variant& variant, const
00285       T& value, typename boost::disable_if<boost::is_base_of<Variant, T> >::
00286       type* = 0);
00287 
00290     template <typename T> static void assign(Variant& dst, const T& src,
00291       typename boost::enable_if<boost::is_base_of<Variant, T> >::type* = 0);
00292 
00295     template <typename T> static void assign(Variant& dst, const T& src,
00296       typename boost::disable_if<boost::is_base_of<Variant, T> >::type* = 0);
00297   };
00298 
00301   std::istream& operator>>(std::istream& stream, Variant& variant);
00302 
00305   std::ostream& operator<<(std::ostream& stream, const Variant& variant);
00306 };
00307 
00308 #include <variant_topic_tools/Variant.tpp>
00309 
00310 #endif


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