Result.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  * 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/extern/SharedPtr.h"
00042 #include "keyvalue/value/Value.h"
00043 
00044 namespace keyvalue {
00045 namespace value {
00046 
00059 typedef shared_ptr<void> ObjectPtr;
00060 
00066 class Result {
00067 
00068 public:
00069 
00070   typedef boost::variant<ObjectPtr, Value> DataType_;
00071 
00075   Result();
00076 
00084   Result(const DataType_& data);
00085 
00093   template<typename Rhs>
00094   Result(const Rhs& rhs);
00095 
00105   template<typename Rhs>
00106   Result&
00107   operator=(const Rhs& rhs);
00108 
00115   ObjectPtr
00116   getObjectPtr() const;
00117 
00121   ObjectPtr
00122   getObjectPtr();
00123 
00130   const Value*
00131   getValue() const;
00132 
00136   Value*
00137   getValue();
00138 
00139 private:
00140 
00141   DataType_ data_;
00142 
00143 }; // class Result
00144 
00148 std::ostream&
00149 operator<<(std::ostream& os, const Result& rhs);
00150 
00164 template<typename Rhs>
00165 struct Parent;
00166 
00167 template<>
00168 struct Parent<Result::DataType_> {
00169   typedef Result Type_;
00170 };
00171 
00172 template<>
00173 struct Parent<ObjectPtr> {
00174   typedef Result::DataType_ Type_;
00175 };
00176 
00177 template<typename T>
00178 struct Parent<shared_ptr<T> > {
00179   typedef ObjectPtr Type_;
00180 };
00181 
00182 template<typename T>
00183 struct Parent<T*> {
00184   typedef shared_ptr<T> Type_;
00185 };
00186 
00187 template<>
00188 struct Parent<Value> {
00189   typedef Result::DataType_ Type_;
00190 };
00191 
00192 /*--------------------------------------------------------------------------
00193  * Result()
00194  *------------------------------------------------------------------------*/
00195 
00196 template<typename Rhs>
00197 Result::Result(const Rhs& rhs) :
00198   data_(Result(typename Parent<Rhs>::Type_(rhs)).data_) {
00203 }
00204 
00205 /*--------------------------------------------------------------------------
00206  * operator=()
00207  *------------------------------------------------------------------------*/
00208 
00209 template<typename Rhs>
00210 Result&
00211 Result::operator=(const Rhs& rhs) {
00216   return *this = typename Parent<Rhs>::Type_(rhs);
00217 }
00218 
00219 } // namespace value
00220 } // namespace keyvalue
00221 
00222 #endif // KEYVALUE_VALUE_RESULT_H_