00001 /*************************************************************************** 00002 * 00003 * Copyright (C) 2009-2011 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_CONVERTER_STDSINGLE_H_ 00036 #define KEYVALUE_KEY_CONVERTER_STDSINGLE_H_ 00037 00038 #include "keyvalue/sys/exception/KV_ASSERT.h" 00039 #include "keyvalue/value/Single.h" 00040 #include "keyvalue/value/Variant.h" 00041 00042 namespace keyvalue { 00043 namespace key { 00044 00072 template <typename ElementType> 00073 class StdSingle { 00074 00075 public: 00076 00080 typedef value::Single InputType_; 00081 00088 typedef ElementType OutputType_; 00089 00102 StdSingle(const InputType_& input, OutputType_* cache); 00103 00110 bool 00111 isEmpty() const; 00112 00125 value::Variant 00126 pop(); 00127 00134 void 00135 insert(const ElementType& element); 00136 00142 OutputType_ 00143 getOutput() const; 00144 00151 bool 00152 mustUpdate() const; 00153 00154 private: 00155 00156 const InputType_& input_; 00157 OutputType_* const cache_; 00158 OutputType_ output_; 00159 bool empty_; 00160 bool mustUpdate_; 00161 00162 }; 00163 00164 /*-------------------------------------------------------------------------- 00165 * StdSingle() 00166 *------------------------------------------------------------------------*/ 00167 00168 template <typename ElementType> 00169 StdSingle<ElementType>::StdSingle(const InputType_& input, 00170 OutputType_* const cache) : 00171 input_(input), 00172 cache_(cache), 00173 empty_(false), 00174 mustUpdate_(!cache) { 00175 } 00176 00177 /*-------------------------------------------------------------------------- 00178 * isEmpty() 00179 *------------------------------------------------------------------------*/ 00180 00181 template <typename ElementType> 00182 bool 00183 StdSingle<ElementType>::isEmpty() const { 00184 return empty_; 00185 } 00186 00187 /*-------------------------------------------------------------------------- 00188 * pop() 00189 *------------------------------------------------------------------------*/ 00190 00191 template <typename ElementType> 00192 value::Variant 00193 StdSingle<ElementType>::pop() { 00194 KV_ASSERT(!isEmpty(), "Element has been processed!"); 00195 empty_ = true; 00196 return input_; 00197 } 00198 00199 /*-------------------------------------------------------------------------- 00200 * insert() 00201 *------------------------------------------------------------------------*/ 00202 00203 template <typename ElementType> 00204 void 00205 StdSingle<ElementType>::insert(const ElementType& element) { 00206 output_ = element; 00207 mustUpdate_ = mustUpdate_ || !(output_ == *cache_); 00208 } 00209 00210 /*-------------------------------------------------------------------------- 00211 * getOutput() 00212 *------------------------------------------------------------------------*/ 00213 00214 template <typename ElementType> 00215 typename StdSingle<ElementType>::OutputType_ 00216 StdSingle<ElementType>::getOutput() const { 00217 return output_; 00218 } 00219 00220 /*-------------------------------------------------------------------------- 00221 * mustUpdate() 00222 *------------------------------------------------------------------------*/ 00223 00224 template <typename ElementType> 00225 bool 00226 StdSingle<ElementType>::mustUpdate() const { 00227 return mustUpdate_; 00228 } 00229 00230 } // namespace key 00231 } // namespace keyvalue 00232 00233 #endif // KEYVALUE_KEY_CONVERTER_STDSINGLE_H_