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_CONVERTER_STDSINGLE_H_ 00030 #define KEYVALUE_KEY_CONVERTER_STDSINGLE_H_ 00031 00032 #include "keyvalue/sys/exception/KV_ASSERT.h" 00033 #include "keyvalue/value/Single.h" 00034 #include "keyvalue/value/Variant.h" 00035 00036 namespace keyvalue { 00037 namespace key { 00038 00066 template <typename ElementType> 00067 class StdSingle { 00068 00069 public: 00070 00074 typedef value::Single InputType_; 00075 00082 typedef ElementType OutputType_; 00083 00096 StdSingle(const InputType_& input, OutputType_* cache); 00097 00104 bool 00105 isEmpty() const; 00106 00112 value::Variant 00113 pop(); 00114 00121 void 00122 insert(const ElementType& element); 00123 00129 OutputType_ 00130 getOutput() const; 00131 00138 bool 00139 mustUpdate() const; 00140 00141 private: 00142 00143 const InputType_& input_; 00144 OutputType_* const cache_; 00145 OutputType_ output_; 00146 bool empty_; 00147 bool mustUpdate_; 00148 00149 }; 00150 00151 /*-------------------------------------------------------------------------- 00152 * StdSingle() 00153 *------------------------------------------------------------------------*/ 00154 00155 template <typename ElementType> 00156 StdSingle<ElementType>::StdSingle(const InputType_& input, 00157 OutputType_* const cache) : input_(input), cache_(cache), empty_(false), 00158 mustUpdate_(!cache) { 00159 } 00160 00161 /*-------------------------------------------------------------------------- 00162 * isEmpty() 00163 *------------------------------------------------------------------------*/ 00164 00165 template <typename ElementType> 00166 bool 00167 StdSingle<ElementType>::isEmpty() const { 00168 return empty_; 00169 } 00170 00171 /*-------------------------------------------------------------------------- 00172 * pop() 00173 *------------------------------------------------------------------------*/ 00174 00175 template <typename ElementType> 00176 value::Variant 00177 StdSingle<ElementType>::pop() { 00178 KV_ASSERT(!isEmpty(), "Element has been processed!"); 00179 empty_ = true; 00180 return input_; 00181 } 00182 00183 /*-------------------------------------------------------------------------- 00184 * insert() 00185 *------------------------------------------------------------------------*/ 00186 00187 template <typename ElementType> 00188 void 00189 StdSingle<ElementType>::insert(const ElementType& element) { 00190 output_ = element; 00191 mustUpdate_ = mustUpdate_ || output_ == *cache_; 00192 } 00193 00194 /*-------------------------------------------------------------------------- 00195 * getOutput() 00196 *------------------------------------------------------------------------*/ 00197 00198 template <typename ElementType> 00199 typename StdSingle<ElementType>::OutputType_ 00200 StdSingle<ElementType>::getOutput() const { 00201 return output_; 00202 } 00203 00204 /*-------------------------------------------------------------------------- 00205 * getOutput() 00206 *------------------------------------------------------------------------*/ 00207 00208 template <typename ElementType> 00209 bool 00210 StdSingle<ElementType>::mustUpdate() const { 00211 return mustUpdate_; 00212 } 00213 00214 } // namespace key 00215 } // namespace keyvalue 00216 00217 #endif // KEYVALUE_KEY_CONVERTER_STDSINGLE_H_