core/ValuesBuffer.hpp
Go to the documentation of this file.
00001 /*
00002 
00003   This file is part of OpenFLUID software
00004   Copyright(c) 2007, INRA - Montpellier SupAgro
00005 
00006 
00007  == GNU General Public License Usage ==
00008 
00009   OpenFLUID is free software: you can redistribute it and/or modify
00010   it under the terms of the GNU General Public License as published by
00011   the Free Software Foundation, either version 3 of the License, or
00012   (at your option) any later version.
00013 
00014   OpenFLUID is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017   GNU General Public License for more details.
00018 
00019   You should have received a copy of the GNU General Public License
00020   along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
00021 
00022 
00023  == Other Usage ==
00024 
00025   Other Usage means a use of OpenFLUID that is inconsistent with the GPL
00026   license, and requires a written agreement between You and INRA.
00027   Licensees for Other Usage of OpenFLUID may use this file in accordance
00028   with the terms contained in the written agreement between You and INRA.
00029   
00030 */
00031 
00032 /**
00033  @file
00034 
00035  @author Jean-Christophe FABRE <fabrejc@supagro.inra.fr>
00036  */
00037 
00038 #ifndef __VALUESBUFFER_HPP__
00039 #define __VALUESBUFFER_HPP__
00040 
00041 #include <openfluid/dllexport.hpp>
00042 #include <openfluid/core/ValuesBufferProperties.hpp>
00043 #include <openfluid/core/Value.hpp>
00044 #include <openfluid/core/NullValue.hpp>
00045 #include <openfluid/core/DateTime.hpp>
00046 #include <boost/circular_buffer.hpp>
00047 
00048 #include <boost/shared_ptr.hpp>
00049 
00050 #include <iostream>
00051 #include <list>
00052 
00053 namespace openfluid {
00054 namespace core {
00055 
00056 
00057 class IndexedValue
00058 {
00059   friend class ValuesBuffer;
00060 
00061   private:
00062 
00063     TimeIndex_t m_Index;
00064 
00065     boost::shared_ptr<Value> m_Value;
00066 
00067 
00068   public:
00069 
00070     IndexedValue():
00071       m_Index(0),m_Value(boost::shared_ptr<Value>(new NullValue())) {};
00072 
00073     IndexedValue(const TimeIndex_t& Ind, const Value& Val):
00074       m_Index(Ind),m_Value(boost::shared_ptr<Value>(Val.clone())) {};
00075 
00076     IndexedValue(const IndexedValue& IndValue):
00077           m_Index(IndValue.m_Index),m_Value(boost::shared_ptr<Value>(IndValue.m_Value.get()->clone())) {};
00078 
00079     inline TimeIndex_t getIndex() const { return m_Index; };
00080 
00081     inline Value* getValue() const { return m_Value.get(); };
00082 
00083     inline Value* getValue() { return m_Value.get(); };
00084 
00085     inline void clear() { m_Index = 0; m_Value.reset(new NullValue()); };
00086 
00087 };
00088 
00089 
00090 /**
00091   Indexed value list, ordered from oldest (front) to more recent (back)
00092 */
00093 typedef std::list<IndexedValue> IndexedValueList;
00094 
00095 
00096 // =====================================================================
00097 // =====================================================================
00098 
00099 
00100 class DLLEXPORT ValuesBuffer: public ValuesBufferProperties
00101 {
00102 
00103   public:
00104 
00105     typedef  boost::circular_buffer<IndexedValue> DataContainer_t;
00106 
00107 
00108   private:
00109 
00110     DataContainer_t m_Data;
00111 
00112     DataContainer_t::iterator findAtIndex(const TimeIndex_t& anIndex);
00113 
00114     DataContainer_t::const_iterator findAtIndex(const TimeIndex_t& anIndex) const;
00115 
00116   public:
00117 
00118     ValuesBuffer();
00119 
00120     ~ValuesBuffer();
00121 
00122     bool getValue(const TimeIndex_t& anIndex, Value* aValue) const;
00123 
00124     Value* getValue(const TimeIndex_t& anIndex) const;
00125 
00126     Value* getCurrentValue() const;
00127 
00128     TimeIndex_t getCurrentIndex() const;
00129 
00130     bool isValueExist(const TimeIndex_t& anIndex) const
00131     {
00132       return (!m_Data.empty() && findAtIndex(anIndex) != m_Data.end());
00133     }
00134 
00135 
00136     bool getCurrentValue(Value* aValue) const;
00137 
00138     bool getLatestIndexedValue(IndexedValue& IndValue) const;
00139 
00140     bool getLatestIndexedValues(const TimeIndex_t& anIndex, IndexedValueList& IndValueList) const;
00141 
00142     bool getIndexedValues(const TimeIndex_t& aBeginIndex, const TimeIndex_t& anEndIndex,
00143                                 IndexedValueList& IndValueList) const;
00144 
00145     bool modifyValue(const TimeIndex_t& anIndex, const Value& aValue);
00146 
00147     bool modifyCurrentValue(const Value& aValue);
00148 
00149     bool appendValue(const TimeIndex_t& anIndex, const Value& aValue);
00150 
00151     unsigned int getValuesCount() const
00152     {
00153       return m_Data.size();
00154     }
00155 
00156     void displayStatus(std::ostream& OStream) const;
00157 
00158     void displayContent(std::ostream& OStream) const;
00159 
00160 
00161 };
00162 
00163 }
00164 } // namespaces
00165 
00166 
00167 #endif /* __VALUESBUFFER_HPP__ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines