BuilderFrom.h

00001 /***************************************************************************
00002  *
00003  * Copyright (C) 2009 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 
00035 #ifndef KEYVALUE_MNGT_BUILDER_BUILDERFROM_H_
00036 #define KEYVALUE_MNGT_BUILDER_BUILDERFROM_H_
00037 
00038 #include "keyvalue/extern/SharedPtr.h"
00039 #include "keyvalue/frontend/LexicalToolKit.h"
00040 #include "keyvalue/sys/exception/Exception.h"
00041 #include "keyvalue/value/Variant.h"
00042 
00043 namespace keyvalue {
00044 
00058 template<typename ObjectType>
00059 class BuilderFromVariant {
00060 
00061 public:
00062 
00091   shared_ptr<ObjectType>
00092   getObject(const value::Variant& data) const;
00093 
00094 private:
00095 
00096   virtual const char*
00097   getName() const = 0;
00098 
00112   template<typename InputType>
00113   shared_ptr<ObjectType>
00114   build(const value::Variant& data) const;
00115 
00116 }; // class BuilderFromVariant
00117 
00134 template<typename ObjectType, typename InputType>
00135 class BuilderFrom {
00136 
00137 public:
00138 
00149   virtual shared_ptr<ObjectType>
00150   getObject(const InputType& data) const = 0;
00151 
00152 }; // class BuilderFrom
00153 
00154 /*--------------------------------------------------------------------------
00155  * getObject()
00156  *------------------------------------------------------------------------*/
00157 
00158 template<typename ObjectType>
00159 shared_ptr<ObjectType> BuilderFromVariant<ObjectType>::getObject(
00160     const value::Variant& data) const {
00161 
00162   using frontend::LexicalToolKit;
00163 
00164   if (data.is<bool> (LexicalToolKit::input))
00165     return build<bool> (data);
00166 
00167   if (data.is<double> (LexicalToolKit::input))
00168     return build<double> (data);
00169 
00170   if (data.is<ptime> (LexicalToolKit::input))
00171     return build<ptime> (data);
00172 
00173   if (data.is<string> (LexicalToolKit::input))
00174     return build<string> (data);
00175 
00176   throw ::keyvalue::LogicError() & "Unknown keyvalue::Variant content!";
00177 }
00178 
00179 /*--------------------------------------------------------------------------
00180  * build()
00181  *------------------------------------------------------------------------*/
00182 
00183 template<typename ObjectType>
00184 template<typename InputType>
00185 shared_ptr<ObjectType> BuilderFromVariant<ObjectType>::build(
00186     const value::Variant& data) const {
00187 
00188   const BuilderFrom<ObjectType, InputType>* ptr(
00189       dynamic_cast<const BuilderFrom<ObjectType, InputType>*> (this));
00190 
00191   using frontend::LexicalToolKit;
00192   if (ptr)
00193     return ptr->getObject(data.get<InputType> (LexicalToolKit::input));
00194 
00195   throw RuntimeError() & "Builder '" & getName() & "'cannot build from "
00196     "input " & data & "'!";
00197 }
00198 
00199 } // namespace keyvalue
00200 
00201 #endif // KEYVALUE_MNGT_BUILDER_BUILDERFROM_H_