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 **************************************************************************/ 00021 00029 #ifndef KEYVALUE_MNGT_CALCULATOR_H_ 00030 #define KEYVALUE_MNGT_CALCULATOR_H_ 00031 00032 #include "keyvalue/mngt/Processor.h" 00033 #include "keyvalue/mngt/ProcessorMngr.h" 00034 #include "keyvalue/sys/exception/Exception.h" 00035 #include "keyvalue/value/Variant.h" 00036 00037 namespace keyvalue { 00038 00056 template <typename Tag> 00057 class Calculator : public Processor { 00058 00059 public: 00060 00071 static Calculator& 00072 getInstance(); 00073 00082 const char* 00083 getName() const; 00084 00094 value::Result 00095 getResult(const DataSet& data) const; 00096 00106 value::Result 00107 getResult(const value::Variant& data) const; 00108 00119 virtual value::Value 00120 getValue(const DataSet& data) const; 00121 00133 virtual value::Value 00134 getValue(const value::Variant& data) const; 00135 00136 protected: 00137 00138 // Constructor registers this into the ProcessorMngr 00139 Calculator(); 00140 00163 static void 00164 setGetter(Calculator& (*getter)()); 00165 00166 private: 00167 00178 static Calculator& 00179 getSingleton(); 00180 00181 static Calculator& (*getter_)(); 00182 00183 }; // class Calculator 00184 00185 /*-------------------------------------------------------------------------- 00186 * getInstance() 00187 *------------------------------------------------------------------------*/ 00188 00189 template <typename Tag> 00190 Calculator<Tag>& 00191 Calculator<Tag>::getInstance() { 00192 return getter_(); 00193 } 00194 00195 template <typename Tag> 00196 Calculator<Tag>& (*Calculator<Tag>::getter_)() = 00197 Calculator<Tag>::getSingleton; 00198 00199 /*-------------------------------------------------------------------------- 00200 * getResult() 00201 *------------------------------------------------------------------------*/ 00202 00203 template <typename Tag> 00204 value::Result 00205 Calculator<Tag>::getResult(const DataSet& data) const { 00206 return getValue(data); 00207 } 00208 00209 template <typename Tag> 00210 value::Result 00211 Calculator<Tag>::getResult(const value::Variant& data) const { 00212 return getValue(data); 00213 } 00214 00215 /*-------------------------------------------------------------------------- 00216 * getValue() 00217 *------------------------------------------------------------------------*/ 00218 00219 template <typename Tag> 00220 value::Value 00221 Calculator<Tag>::getValue(const value::Variant& data) const { 00222 throw RuntimeError() & "Calculator '" & getName() & "' cannot build from " 00223 "input '" & data & "'!"; 00224 } 00225 00226 /*-------------------------------------------------------------------------- 00227 * Calculator() 00228 *------------------------------------------------------------------------*/ 00229 00230 template <typename Tag> 00231 Calculator<Tag>::Calculator() { 00232 ProcessorMngr::getInstance().add(this); 00233 } 00234 00235 /*-------------------------------------------------------------------------- 00236 * setGetter() 00237 *------------------------------------------------------------------------*/ 00238 00239 template <typename Tag> 00240 void 00241 Calculator<Tag>::setGetter(Calculator& (*getter)()) { 00242 getter_ = getter; 00243 } 00244 00245 /*-------------------------------------------------------------------------- 00246 * getSingleton() 00247 *------------------------------------------------------------------------*/ 00248 00249 template <typename Tag> 00250 Calculator<Tag>& 00251 Calculator<Tag>::getSingleton() { 00252 static Calculator<Tag> singleton; 00253 return singleton; 00254 } 00255 00256 } // namespace keyvalue 00257 00258 #endif // KEYVALUE_MNGT_CALCULATOR_H_