ObjectMap.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 
00035 #ifndef KEYVALUE_KEY_MAP_OBJECTMAP_H_
00036 #define KEYVALUE_KEY_MAP_OBJECTMAP_H_
00037 
00038 #include "keyvalue/extern/String.h"
00039 #include "keyvalue/frontend/LexicalToolKit.h"
00040 #include "keyvalue/mngt/BuilderFrom.h"
00041 #include "keyvalue/mngt/DataSet.h"
00042 #include "keyvalue/mngt/ProcessorInstantiator.h"
00043 #include "keyvalue/mngt/Repository.h"
00044 #include "keyvalue/sys/exception/KV_ASSERT.h"
00045 #include "keyvalue/util/Global.h"
00046 #include "keyvalue/value/Variant.h"
00047 
00048 namespace keyvalue {
00049 namespace key {
00050 
00072 template <typename ObjectType>
00073 class ObjectMap {
00074 
00075 public:
00076 
00077   typedef shared_ptr<ObjectType> OutputType_;
00078 
00086   shared_ptr<ObjectType>
00087   map(const value::Variant& variant) const;
00088 
00089 private:
00090 
00096   virtual string
00097   getName() const = 0;
00098 
00099 }; // class ObjectMap
00100 
00101 /*--------------------------------------------------------------------------
00102  * map()
00103  *------------------------------------------------------------------------*/
00104 
00105 template<typename ObjectType>
00106 shared_ptr<ObjectType>
00107 ObjectMap<ObjectType>::map(const value::Variant& variant) const {
00108 
00109   Processor& builder(ProcessorInstantiator<ObjectType>::getInstance());
00110 
00111   string name;
00112 
00113   // Try to get result from a DataSet (whose name is stored by variant)
00114   if (variant.is<string>(frontend::LexicalToolKit::input)) {
00115 
00116     name = variant.get<string>(frontend::LexicalToolKit::input);
00117 
00118     try {
00119 
00120       shared_ptr<const DataSet>
00121         data(util::Global<Repository>::get()->getDataSet(name));
00122 
00123       value::Result result(data->process(builder));
00124 
00125       const value::ObjectPtr object(result.getObjectPtr());
00126       KV_ASSERT(object, "value::Result must be non null ObjectPtr!");
00127       return static_pointer_cast<ObjectType>(object);
00128     }
00129 
00130     catch(Repository::NotFound& error) {
00131       if (error.getName() != name)
00132         throw;
00133     }
00134   }
00135 
00136   // Try to get result from variant
00137   BuilderFromVariant<ObjectType>*
00138     ptr(dynamic_cast<BuilderFromVariant<ObjectType>*>(&builder));
00139   if (ptr)
00140     return ptr->getObject(variant);
00141 
00142   if (!name.empty())
00143     throw RuntimeError() & "Object '" & name & "' not found and builder " &
00144       builder.getName() & "cannot build from this input!";
00145 
00146   throw RuntimeError() & "Builder '" & builder.getName() & "'cannot build"
00147     "from input " & variant & "'!";
00148 }
00149 
00150 } // namespace key
00151 } // namespace keyvalue
00152 
00153 #endif // KEYVALUE_KEY_MAP_OBJECTMAP_H_