Value.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *
00003  * Copyright (C) 2009-2011 Cassio Neri Moreira
00004  *
00005  * This file is part of the KeyValue library.
00006  *
00007  * The KeyValue library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as published
00009  * by the Free Software Foundation, either version 3 of the License, or (at
00010  * your option) any later version.
00011  *
00012  * The KeyValue library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00015  * Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License along
00018  * with KeyValue. If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  * If you modify this library, or any covered work, by linking or combining
00021  * it with Excel (or a modified version of that program), containing parts
00022  * covered by the terms of End-User License Agreement for Microsoft
00023  * Software, the licensors of KeyValue grant you additional permission to
00024  * convey the resulting work.
00025  *
00026  **************************************************************************/
00027 
00034 #ifndef KEYVALUE_VALUE_VALUE_H_
00035 #define KEYVALUE_VALUE_VALUE_H_
00036 
00037 #include <iosfwd>
00038 
00039 #include <boost/variant.hpp>
00040 
00041 #include "keyvalue/value/Matrix.h"
00042 #include "keyvalue/value/Single.h"
00043 #include "keyvalue/value/Vector.h"
00044 
00045 namespace keyvalue {
00046 
00050 namespace value {
00051 
00057 class Value {
00058 
00059 public:
00060 
00061   typedef ::boost::variant<Single, Vector, Matrix> DataType_;
00062 
00067   Value();
00068 
00076   Value(const DataType_& data);
00077 
00085   template <typename Rhs>
00086   Value(const Rhs& rhs);
00087 
00097   template <typename Rhs>
00098   Value&
00099   operator=(const Rhs& rhs);
00100 
00104   bool
00105   operator==(const Value& rhs) const;
00106 
00115   template <typename Type>
00116   const Type*
00117   get() const;
00118 
00122   template <typename Type>
00123   Type*
00124   get();
00125 
00126 private:
00127 
00128   DataType_ data_;
00129 
00130 }; // class Value
00131 
00137 ::std::ostream&
00138 operator<<(::std::ostream& os, const Value& rhs);
00139 
00152 template <typename Rhs>
00153 struct Parent;
00154 
00155 template <>
00156 struct Parent<Value::DataType_> {
00157   typedef Value Type_;
00158 };
00159 
00160 template <>
00161 struct Parent<Single> {
00162   typedef Value::DataType_ Type_;
00163 };
00164 
00165 template <>
00166 struct Parent<Vector> {
00167   typedef Value::DataType_ Type_;
00168 };
00169 
00170 template <>
00171 struct Parent<Matrix> {
00172   typedef Value::DataType_ Type_;
00173 };
00174 
00175 /*--------------------------------------------------------------------------
00176  * Value()
00177  *------------------------------------------------------------------------*/
00178 
00179 template <typename Rhs>
00180 Value::Value(const Rhs& rhs) :
00181   data_(Value(typename Parent<Rhs>::Type_(rhs)).data_) {
00182 
00183   /*
00184    * If you get an error here about 'keyvalue::value::Parent<Rhs>', this
00185    * means that Rhs isn't in a lower level in the hierarchy.
00186    */
00187 }
00188 
00189 /*--------------------------------------------------------------------------
00190  * operator=()
00191  *------------------------------------------------------------------------*/
00192 
00193 template <typename Rhs>
00194 Value&
00195 Value::operator=(const Rhs& rhs) {
00196 
00197   /*
00198    * If you get an error here about 'keyvalue::value::Parent<P>', this
00199    * means that Rhs isn't in a lower level in the hierarchy.
00200    */
00201 
00202   return *this = typename Parent<Rhs>::Type_(rhs);
00203 }
00204 
00205 } // namespace value
00206 } // namespace keyvalue
00207 
00208 #endif // KEYVALUE_VALUE_VALUE_H_