Variant.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *
00003  * Copyright (C) 2009-2010 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  **************************************************************************/
00021 
00028 #ifndef KEYVALUE_VALUE_VARIANT_H_
00029 #define KEYVALUE_VALUE_VARIANT_H_
00030 
00031 #include <iosfwd>
00032 
00033 #include <boost/variant.hpp>
00034 
00035 #include "keyvalue/extern/String.h"
00036 #include "keyvalue/extern/Ptime.h"
00037 #include "keyvalue/frontend/LexicalToolKit.h"
00038 #include "keyvalue/value/Nothing.h"
00039 
00040 namespace keyvalue {
00041 namespace value {
00042 
00043 // Forward declaration
00044 class Single;
00045 
00059 class Variant {
00060 
00061 public:
00062 
00063   typedef boost::variant<Nothing, bool, double, ptime, string> DataType_;
00064 
00068   Variant();
00069 
00077   Variant(const DataType_& data);
00078 
00086   template <typename Rhs>
00087   Variant(const Rhs& rhs);
00088 
00096   Variant(const Single& data);
00097 
00107   template <typename Rhs>
00108   Variant&
00109   operator=(const Rhs& rhs);
00110 
00141   template <typename ElementType>
00142   bool
00143   is(frontend::LexicalToolKit::IO io) const;
00144 
00179   template <typename ElementType>
00180   ElementType
00181   get(frontend::LexicalToolKit::IO io) const;
00182 
00183   friend std::ostream&
00184   operator<<(std::ostream& os, const Variant& variant);
00185 
00186 private:
00187 
00199   template <typename ElementType>
00200   const ElementType*
00201   get() const;
00202 
00203   DataType_ data_;
00204 
00205 }; // class Variant
00206 
00212 std::ostream&
00213 operator<<(std::ostream& os, const Variant& rhs);
00214 
00235 template <typename Rhs>
00236 struct Parent;
00237 
00238 template <>
00239 struct Parent<Variant::DataType_> {
00240   typedef Variant Type_;
00241 };
00242 
00243 template <>
00244 struct Parent<Nothing> {
00245   typedef Variant::DataType_ Type_;
00246 };
00247 
00248 template <>
00249 struct Parent<bool> {
00250   typedef Variant::DataType_ Type_;
00251 };
00252 
00253 template <>
00254 struct Parent<double> {
00255   typedef Variant::DataType_ Type_;
00256 };
00257 
00258 template <>
00259 struct Parent<unsigned int> {
00260   typedef double Type_;
00261 };
00262 
00263 template <>
00264 struct Parent<long unsigned int> {
00265   typedef double Type_;
00266 };
00267 
00268 template <>
00269 struct Parent<string> {
00270   typedef Variant::DataType_ Type_;
00271 };
00272 
00273 template <>
00274 struct Parent<char[]> {
00275   typedef string Type_;
00276 };
00277 
00278 template <size_t N>
00279 struct Parent<char[N]> {
00280   typedef string Type_;
00281 };
00282 
00283 template <size_t N>
00284 struct Parent<const char[N]> {
00285   typedef string Type_;
00286 };
00287 
00288 template <>
00289 struct Parent<char *> {
00290   typedef string Type_;
00291 };
00292 
00293 template <>
00294 struct Parent<ptime> {
00295   typedef Variant::DataType_ Type_;
00296 };
00297 
00298 /*--------------------------------------------------------------------------
00299  * Variant()
00300  *------------------------------------------------------------------------*/
00301 
00302 template <typename Rhs>
00303 Variant::Variant(const Rhs& rhs) :
00304   data_(Variant(typename Parent<Rhs>::Type_(rhs)).data_) {
00309 }
00310 
00311 /*--------------------------------------------------------------------------
00312  * operator=()
00313  *------------------------------------------------------------------------*/
00314 
00315 template <typename Rhs>
00316 Variant&
00317 Variant::operator=(const Rhs& rhs) {
00322   return *this = typename Parent<Rhs>::Type_(rhs);
00323 }
00324 
00325 } // namespace value
00326 } // namespace keyvalue
00327 
00328 #endif // KEYVALUE_VALUE_VARIANT_H_

Generated on Sat Mar 20 15:08:29 2010 for KeyValue by  doxygen 1.6.1