core/ValuesBuffer.hpp

Go to the documentation of this file.
00001 /*
00002   This file is part of OpenFLUID software
00003   Copyright (c) 2007-2010 INRA-Montpellier SupAgro
00004 
00005 
00006  == GNU General Public License Usage ==
00007 
00008   OpenFLUID is free software: you can redistribute it and/or modify
00009   it under the terms of the GNU General Public License as published by
00010   the Free Software Foundation, either version 3 of the License, or
00011   (at your option) any later version.
00012 
00013   OpenFLUID is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016   GNU General Public License for more details.
00017 
00018   You should have received a copy of the GNU General Public License
00019   along with OpenFLUID.  If not, see <http://www.gnu.org/licenses/>.
00020 
00021   In addition, as a special exception, INRA gives You the additional right
00022   to dynamically link the code of OpenFLUID with code not covered
00023   under the GNU General Public License ("Non-GPL Code") and to distribute
00024   linked combinations including the two, subject to the limitations in this
00025   paragraph. Non-GPL Code permitted under this exception must only link to
00026   the code of OpenFLUID dynamically through the OpenFLUID libraries
00027   interfaces, and only for building OpenFLUID plugins. The files of
00028   Non-GPL Code may be link to the OpenFLUID libraries without causing the
00029   resulting work to be covered by the GNU General Public License. You must
00030   obey the GNU General Public License in all respects for all of the
00031   OpenFLUID code and other code used in conjunction with OpenFLUID
00032   except the Non-GPL Code covered by this exception. If you modify
00033   this OpenFLUID, you may extend this exception to your version of the file,
00034   but you are not obligated to do so. If you do not wish to provide this
00035   exception without modification, you must delete this exception statement
00036   from your version and license this OpenFLUID solely under the GPL without
00037   exception.
00038 
00039 
00040  == Other Usage ==
00041 
00042   Other Usage means a use of OpenFLUID that is inconsistent with the GPL
00043   license, and requires a written agreement between You and INRA.
00044   Licensees for Other Usage of OpenFLUID may use this file in accordance
00045   with the terms contained in the written agreement between You and INRA.
00046 */
00047 
00055 #ifndef __VALUESBUFFER_HPP__
00056 #define __VALUESBUFFER_HPP__
00057 
00058 #include <openfluid/dllexport.hpp>
00059 #include <openfluid/core/ValuesBufferProperties.hpp>
00060 #include <boost/circular_buffer.hpp>
00061 
00062 #include <iostream>
00063 
00064 namespace openfluid { namespace core {
00065 
00066 
00067 template <class T>
00068 class DLLEXPORT ValuesBuffer : public ValuesBufferProperties
00069 {
00070 
00071   private:
00072     boost::circular_buffer<T> m_Data;
00073 
00074     unsigned int m_NextStep;
00075 
00076     bool TranslateStepNbrToIndex(const unsigned int& StepNbr,
00077                                  unsigned int& Index) const;
00078 
00079   public:
00080 
00081 
00082     ValuesBuffer();
00083 
00084     ~ValuesBuffer();
00085 
00086     bool getValue(const unsigned int StepNbr,T* Value) const;
00087 
00088     bool getCurrentValue(T* Value) const;
00089 
00090     bool modifyValue(const unsigned int StepNbr, T Value);
00091 
00092     bool appendValue(const T Value);
00093 
00094     unsigned int getNextStep() const;
00095 
00096     void displayStatus(std::ostream& OStream);
00097 
00098 };
00099 
00100 
00101 // =====================================================================
00102 // =====================================================================
00103 
00104 template <class T>
00105 ValuesBuffer<T>::ValuesBuffer()
00106 {
00107 
00108   m_NextStep = 0;
00109 
00110   m_Data.set_capacity(BufferSize);
00111   m_Data.clear();
00112 
00113 }
00114 
00115 // =====================================================================
00116 // =====================================================================
00117 
00118 template <class T>
00119 ValuesBuffer<T>::~ValuesBuffer()
00120 {
00121 
00122 }
00123 
00124 
00125 // =====================================================================
00126 // =====================================================================
00127 
00128 template <class T>
00129 bool ValuesBuffer<T>::TranslateStepNbrToIndex(const unsigned int& StepNbr,
00130                                               unsigned int& Index ) const
00131 {
00132   if (m_NextStep < BufferSize)
00133   {
00134     if (StepNbr < m_NextStep)
00135     {
00136       Index = StepNbr;
00137       return true;
00138     }
00139   }
00140   else
00141   {
00142     int StepsDelta = m_NextStep - StepNbr -1;
00143 
00144     if (StepsDelta < BufferSize && StepsDelta >= 0)
00145     {
00146       Index = BufferSize - StepsDelta -1;
00147       return true;
00148     }
00149   }
00150 
00151   return false;
00152 }
00153 
00154 
00155 // =====================================================================
00156 // =====================================================================
00157 
00158 
00159 template <class T>
00160 bool ValuesBuffer<T>::getValue(const unsigned int StepNbr,T* Value) const
00161 {
00162   unsigned int Index;
00163 
00164   if (TranslateStepNbrToIndex(StepNbr,Index))
00165   {
00166     *Value = m_Data[Index];
00167     return true;
00168   }
00169   return false;
00170 }
00171 
00172 
00173 // =====================================================================
00174 // =====================================================================
00175 
00176 template <class T>
00177 bool ValuesBuffer<T>::getCurrentValue(T* Value) const
00178 {
00179 
00180   *Value = m_Data.back();
00181 
00182   return true;
00183 }
00184 
00185 
00186 // =====================================================================
00187 // =====================================================================
00188 
00189 template <class T>
00190 bool ValuesBuffer<T>::modifyValue(const unsigned int StepNbr, T Value)
00191 {
00192   unsigned int Index;
00193 
00194   if (TranslateStepNbrToIndex(StepNbr,Index))
00195   {
00196     m_Data[Index] = Value;
00197     return true;
00198   }
00199   return false;
00200 }
00201 
00202 
00203 // =====================================================================
00204 // =====================================================================
00205 
00206 template <class T>
00207 bool ValuesBuffer<T>::appendValue(const T Value)
00208 {
00209 
00210   m_Data.push_back(Value);
00211   m_NextStep++;
00212 
00213   return true;
00214 }
00215 
00216 
00217 // =====================================================================
00218 // =====================================================================
00219 
00220 
00221 template <class T>
00222 unsigned int ValuesBuffer<T>::getNextStep() const
00223 {
00224   return m_NextStep;
00225 }
00226 
00227 
00228 // =====================================================================
00229 // =====================================================================
00230 
00231 
00232 template <class T>
00233 void ValuesBuffer<T>::displayStatus(std::ostream& OStream)
00234 {
00235   OStream << "-- ValuesBuffer status --"<< std::endl;
00236   OStream << "   BufferSize : " << BufferSize << std::endl;
00237   OStream << "   Size : " << m_Data.size() << std::endl;
00238   OStream << "   Element size : " << sizeof(T) << std::endl;
00239   OStream << "   Current storage step : " << m_NextStep-1 << std::endl;
00240   OStream << "------------------------------"<< std::endl;
00241 }
00242 
00243 
00244 // =====================================================================
00245 // =====================================================================
00246 
00247 
00248 
00249 } } // namespaces
00250 
00251 
00252 
00253 #endif /* __VALUESBUFFER_HPP__ */

Generated using Doxygen 1.7.3
Creative Commons License Creative Commons By-NC-ND license