Result.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_RESULT_H_
00035 #define KEYVALUE_VALUE_RESULT_H_
00036 
00037 #include <iosfwd>
00038 
00039 #include <boost/variant.hpp>
00040 
00041 #include "keyvalue/value/ObjectPtr.h"
00042 #include "keyvalue/value/Value.h"
00043 
00044 namespace keyvalue {
00045 namespace value {
00046 
00052 class Result {
00053 
00054 public:
00055 
00056   typedef ::boost::variant<ObjectPtr, Value> DataType_;
00057 
00061   Result();
00062 
00070   Result(const DataType_& data);
00071 
00079   template <typename Rhs>
00080   Result(const Rhs& rhs);
00081 
00091   template <typename Rhs>
00092   Result&
00093   operator=(const Rhs& rhs);
00094 
00101   ObjectPtr
00102   getObjectPtr() const;
00103 
00107   ObjectPtr
00108   getObjectPtr();
00109 
00116   const Value*
00117   getValue() const;
00118 
00122   Value*
00123   getValue();
00124 
00125 private:
00126 
00127   DataType_ data_;
00128 
00129 }; // class Result
00130 
00134 ::std::ostream&
00135 operator<<(::std::ostream& os, const Result& rhs);
00136 
00148 template <typename Rhs>
00149 struct Parent;
00150 
00151 template <>
00152 struct Parent<Result::DataType_> {
00153   typedef Result Type_;
00154 };
00155 
00156 template <>
00157 struct Parent<ObjectPtr> {
00158   typedef Result::DataType_ Type_;
00159 };
00160 
00161 template <>
00162 struct Parent<Value> {
00163   typedef Result::DataType_ Type_;
00164 };
00165 
00166 /*--------------------------------------------------------------------------
00167  * Result()
00168  *------------------------------------------------------------------------*/
00169 
00170 template <typename Rhs>
00171 Result::Result(const Rhs& rhs) :
00172   data_(Result(typename Parent<Rhs>::Type_(rhs)).data_) {
00173 
00174   /*
00175    * If you get an error here about 'keyvalue::value::Parent<Rhs>', this
00176    * means that Rhs isn't in a lower level in the hierarchy.
00177    */
00178 }
00179 
00180 /*--------------------------------------------------------------------------
00181  * operator=()
00182  *------------------------------------------------------------------------*/
00183 
00184 template <typename Rhs>
00185 Result&
00186 Result::operator=(const Rhs& rhs) {
00187 
00188   /*
00189    * If you get an error here about 'keyvalue::value::Parent<P>', this
00190    * means that Rhs isn't in a lower level in the hierarchy.
00191    */
00192 
00193   return *this = typename Parent<Rhs>::Type_(rhs);
00194 }
00195 
00196 } // namespace value
00197 } // namespace keyvalue
00198 
00199 #endif // KEYVALUE_VALUE_RESULT_H_