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  **************************************************************************/
00021 
00029 #ifndef KEYVALUE_KEY_MAP_OBJECTMAP_H_
00030 #define KEYVALUE_KEY_MAP_OBJECTMAP_H_
00031 
00032 #include "keyvalue/extern/String.h"
00033 #include "keyvalue/frontend/LexicalToolKit.h"
00034 #include "keyvalue/mngt/Builder.h"
00035 #include "keyvalue/mngt/DataSet.h"
00036 #include "keyvalue/mngt/Repository.h"
00037 #include "keyvalue/sys/exception/KV_ASSERT.h"
00038 #include "keyvalue/value/Variant.h"
00039 
00040 namespace keyvalue {
00041 namespace key {
00042 
00063 template <typename OutputType>
00064 class ObjectMap {
00065 
00066 public:
00067 
00068   typedef shared_ptr<OutputType> OutputType_;
00069 
00070   ObjectMap();
00071 
00079   shared_ptr<OutputType>
00080   map(const value::Variant& variant) const;
00081 
00082 private:
00083 
00089   virtual string
00090   getName() const = 0;
00091 
00092 }; // class ObjectMap
00093 
00094 /*--------------------------------------------------------------------------
00095  * ObjectMap()
00096  *------------------------------------------------------------------------*/
00097 
00098 template<typename OutputType>
00099 ObjectMap<OutputType>::ObjectMap() {
00100 }
00101 
00102 /*--------------------------------------------------------------------------
00103  * map()
00104  *------------------------------------------------------------------------*/
00105 
00106 template<typename OutputType>
00107 shared_ptr<OutputType>
00108 ObjectMap<OutputType>::map(const value::Variant& variant) const {
00109 
00110   Builder<OutputType>& builder(Builder<OutputType>::getInstance());
00111 
00112   value::Result result;
00113 
00114   // Try to get result from a DataSet (whose name is stored by variant)
00115   if (variant.is<string>(frontend::LexicalToolKit::input)) {
00116 
00117     const string&
00118       name(variant.get<string>(frontend::LexicalToolKit::input));
00119 
00120     try {
00121 
00122       shared_ptr<const DataSet>
00123         data(Repository::getInstance().getDataSet(name));
00124 
00125       result = data->process(builder);
00126 
00127       const value::ObjectPtr object(result.getObjectPtr());
00128       KV_ASSERT(object, "value::Result must be non null ObjectPtr!");
00129       return static_pointer_cast<OutputType>(object);
00130     }
00131 
00132     catch(Repository::NotFound& error) {
00133       if (error.getName() != name)
00134         throw;
00135     }
00136   }
00137 
00138   // Try to get result from variant
00139   return builder.getObject(variant);
00140 }
00141 
00142 } // namespace key
00143 } // namespace keyvalue
00144 
00145 #endif // KEYVALUE_KEY_MAP_OBJECTMAP_H_

Generated on Sat Mar 20 15:08:29 2010 for KeyValue by  doxygen 1.6.1