BuilderFrom.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 
00035 #ifndef KEYVALUE_MNGT_BUILDERFROM_H_
00036 #define KEYVALUE_MNGT_BUILDERFROM_H_
00037 
00038 #include <boost/static_assert.hpp>
00039 
00040 #include "keyvalue/frontend/LexicalToolKit.h"
00041 #include "keyvalue/mngt/AbstractBuilder.h"
00042 #include "keyvalue/sys/exception/Exception.h"
00043 #include "keyvalue/util/IsBasic.h"
00044 #include "keyvalue/value/PtrTraits.h"
00045 #include "keyvalue/value/Variant.h"
00046 
00047 namespace keyvalue {
00048 
00074 #ifdef DOXYGEN
00075   template <typename ObjectType, typename InputType>
00076   class BuilderFrom {};
00077 #endif
00078 
00079 template <typename ObjectType, typename InputType>
00080 class BuilderFrom;
00081 
00091 template <typename ObjectType>
00092 class BuilderFrom<ObjectType, value::Variant> :
00093 
00094   public AbstractBuilder {
00095 
00096 public:
00097 
00107   virtual typename value::PtrTraits<ObjectType>::Type_
00108   getObject(const value::Variant& data) const = 0;
00109 
00110 }; // class BuilderFrom<ObjectType, value::Variant>
00111 
00112 template <typename ObjectType, typename InputType>
00113 class BuilderFrom :
00114 
00115   public BuilderFrom<ObjectType, value::Variant> {
00116 
00117 public:
00118 
00119   typename value::PtrTraits<ObjectType>::Type_
00120   getObject(const value::Variant& data) const;
00121 
00122 private:
00123 
00134   virtual typename value::PtrTraits<ObjectType>::Type_
00135   getObject(const InputType& data) const = 0;
00136 
00137   /*
00138    ATTENTION: If you get a compiler error here about
00139 
00140    '::boost::STATIC_ASSERTION_FAILURE<x>'
00141 
00142    this means that you have defined BUILDS_FROM to something different from
00143    bool, double, string, ptime and unsigned int.
00144    */
00145   BOOST_STATIC_ASSERT(util::IsBasic<InputType>::value);
00146 
00147 }; // class BuilderFrom
00148 
00149 /*--------------------------------------------------------------------------
00150  * getObject()
00151  *------------------------------------------------------------------------*/
00152 
00153 template <typename ObjectType, typename InputType>
00154 typename value::PtrTraits<ObjectType>::Type_
00155 BuilderFrom<ObjectType, InputType>::getObject(const value::Variant& data)
00156   const {
00157 
00158   using frontend::LexicalToolKit;
00159 
00160   if (data.is<InputType>(LexicalToolKit::input))
00161     return getObject(data.get<InputType>(LexicalToolKit::input));
00162 
00163   throw RuntimeError() & "Builder '" & Processor::getName() & "' cannot "
00164     "build from input '" & data & "'!";
00165 }
00166 
00167 } // namespace keyvalue
00168 
00169 #endif // KEYVALUE_MNGT_BUILDERFROM_H_