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     /**
00071       Default constructor
00072     */
00073     IndexedValue():
00074       m_Index(0),m_Value(boost::shared_ptr<Value>(new NullValue())) {};
00075 
00076     /**
00077       Constructor from a time index and a value
00078     */
00079     IndexedValue(const TimeIndex_t& Ind, const Value& Val):
00080       m_Index(Ind),m_Value(boost::shared_ptr<Value>(Val.clone())) {};
00081 
00082     /**
00083       Copy constructor
00084     */
00085     IndexedValue(const IndexedValue& IndValue):
00086           m_Index(IndValue.m_Index),m_Value(boost::shared_ptr<Value>(IndValue.m_Value.get()->clone())) {};
00087 
00088     /**
00089       Returns the time index of the indexed value
00090       @return the time index
00091     */
00092     inline TimeIndex_t getIndex() const { return m_Index; };
00093 
00094     /**
00095       Returns a pointer to the value of the indexed value
00096       @return a pointer to the value
00097     */
00098     inline Value* getValue() const { return m_Value.get(); };
00099 
00100     /**
00101       Returns a pointer to the value of the indexed value
00102       @return a pointer to the value
00103     */
00104     inline Value* getValue() { return m_Value.get(); };
00105 
00106     /**
00107       Clears the content of the indexed value. The time index is set to 0,
00108       and the value is set to an openfluid::core::NullValue.
00109     */
00110     inline void clear() { m_Index = 0; m_Value.reset(new NullValue()); };
00111 
00112 };
00113 
00114 
00115 /**
00116   Indexed value list, ordered from oldest (front) to more recent (back)
00117 */
00118 typedef std::list<IndexedValue> IndexedValueList;
00119 
00120 
00121 // =====================================================================
00122 // =====================================================================
00123 
00124 
00125 class DLLEXPORT ValuesBuffer: public ValuesBufferProperties
00126 {
00127 
00128   public:
00129 
00130     typedef  boost::circular_buffer<IndexedValue> DataContainer_t;
00131 
00132 
00133   private:
00134 
00135     DataContainer_t m_Data;
00136 
00137     DataContainer_t::iterator findAtIndex(const TimeIndex_t& anIndex);
00138 
00139     DataContainer_t::const_iterator findAtIndex(const TimeIndex_t& anIndex) const;
00140 
00141   public:
00142 
00143     ValuesBuffer();
00144 
00145     ~ValuesBuffer();
00146 
00147     bool getValue(const TimeIndex_t& anIndex, Value* aValue) const;
00148 
00149     Value* getValue(const TimeIndex_t& anIndex) const;
00150 
00151     Value* getCurrentValue() const;
00152 
00153     TimeIndex_t getCurrentIndex() const;
00154 
00155     bool isValueExist(const TimeIndex_t& anIndex) const
00156     {
00157       return (!m_Data.empty() && findAtIndex(anIndex) != m_Data.end());
00158     }
00159 
00160 
00161     bool getCurrentValue(Value* aValue) const;
00162 
00163     bool getLatestIndexedValue(IndexedValue& IndValue) const;
00164 
00165     bool getLatestIndexedValues(const TimeIndex_t& anIndex, IndexedValueList& IndValueList) const;
00166 
00167     bool getIndexedValues(const TimeIndex_t& aBeginIndex, const TimeIndex_t& anEndIndex,
00168                                 IndexedValueList& IndValueList) const;
00169 
00170     bool modifyValue(const TimeIndex_t& anIndex, const Value& aValue);
00171 
00172     bool modifyCurrentValue(const Value& aValue);
00173 
00174     bool appendValue(const TimeIndex_t& anIndex, const Value& aValue);
00175 
00176     unsigned int getValuesCount() const
00177     {
00178       return m_Data.size();
00179     }
00180 
00181     void displayStatus(std::ostream& OStream) const;
00182 
00183     void displayContent(std::ostream& OStream) const;
00184 
00185 
00186 };
00187 
00188 }
00189 } // namespaces
00190 
00191 
00192 #endif /* __VALUESBUFFER_HPP__ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines