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