Orocos Real-Time Toolkit
2.5.0
|
00001 /*************************************************************************** 00002 tag: Peter Soetens Thu Oct 22 11:59:07 CEST 2009 BufferUnSync.hpp 00003 00004 BufferUnSync.hpp - description 00005 ------------------- 00006 begin : Thu October 22 2009 00007 copyright : (C) 2009 Peter Soetens 00008 email : peter@thesourcworks.com 00009 00010 *************************************************************************** 00011 * This library is free software; you can redistribute it and/or * 00012 * modify it under the terms of the GNU General Public * 00013 * License as published by the Free Software Foundation; * 00014 * version 2 of the License. * 00015 * * 00016 * As a special exception, you may use this file as part of a free * 00017 * software library without restriction. Specifically, if other files * 00018 * instantiate templates or use macros or inline functions from this * 00019 * file, or you compile this file and link it with other files to * 00020 * produce an executable, this file does not by itself cause the * 00021 * resulting executable to be covered by the GNU General Public * 00022 * License. This exception does not however invalidate any other * 00023 * reasons why the executable file might be covered by the GNU General * 00024 * Public License. * 00025 * * 00026 * This library is distributed in the hope that it will be useful, * 00027 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00028 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00029 * Lesser General Public License for more details. * 00030 * * 00031 * You should have received a copy of the GNU General Public * 00032 * License along with this library; if not, write to the Free Software * 00033 * Foundation, Inc., 59 Temple Place, * 00034 * Suite 330, Boston, MA 02111-1307 USA * 00035 * * 00036 ***************************************************************************/ 00037 00038 00039 00040 #ifndef ORO_CORELIB_BUFFER_UNSYNC_HPP 00041 #define ORO_CORELIB_BUFFER_UNSYNC_HPP 00042 00043 #include "BufferInterface.hpp" 00044 #include <deque> 00045 00046 namespace RTT 00047 { namespace base { 00048 00049 00057 template<class T> 00058 class BufferUnSync 00059 :public BufferInterface<T> 00060 { 00061 public: 00062 typedef typename BufferInterface<T>::reference_t reference_t; 00063 typedef typename BufferInterface<T>::param_t param_t; 00064 typedef typename BufferInterface<T>::size_type size_type; 00065 typedef T value_t; 00066 00070 BufferUnSync( size_type size, const T& initial_value = T() ) 00071 : cap(size), buf() 00072 { 00073 data_sample(initial_value); 00074 } 00075 00076 virtual void data_sample( const T& sample ) 00077 { 00078 buf.resize(cap, sample); 00079 buf.resize(0); 00080 } 00081 00085 ~BufferUnSync() {} 00086 00087 bool Push( param_t item ) 00088 { 00089 if (cap == (size_type)buf.size() ) { 00090 return false; 00091 } 00092 buf.push_back( item ); 00093 return true; 00094 } 00095 00096 size_type Push(const std::vector<T>& items) 00097 { 00098 typename std::vector<T>::const_iterator itl( items.begin() ); 00099 while ( ((size_type)buf.size() != cap) && (itl != items.end()) ) { 00100 buf.push_back( *itl ); 00101 ++itl; 00102 } 00103 return (itl - items.begin()); 00104 } 00105 bool Pop( reference_t item ) 00106 { 00107 if ( buf.empty() ) { 00108 return false; 00109 } 00110 item = buf.front(); 00111 buf.pop_front(); 00112 return true; 00113 } 00114 00115 size_type Pop(std::vector<T>& items ) 00116 { 00117 int quant = 0; 00118 while ( !buf.empty() ) { 00119 items.push_back( buf.front() ); 00120 buf.pop_front(); 00121 ++quant; 00122 } 00123 return quant; 00124 } 00125 00126 value_t* PopWithoutRelease() 00127 { 00128 if(buf.empty()) 00129 return 0; 00130 00131 //note we need to copy the sample, as 00132 //front is not garanteed to be valid after 00133 //any other operation on the deque 00134 lastSample = buf.front(); 00135 buf.pop_front(); 00136 return &lastSample; 00137 00138 return 0; 00139 } 00140 00141 void Release(value_t *item) 00142 { 00143 //we do not need to release any memory, but we can check 00144 //if the other side messed up 00145 assert(item == &lastSample && "Wrong pointer given back to buffer"); 00146 } 00147 00148 size_type capacity() const { 00149 return cap; 00150 } 00151 00152 size_type size() const { 00153 return buf.size(); 00154 } 00155 00156 void clear() { 00157 buf.clear(); 00158 } 00159 00160 bool empty() const { 00161 return buf.empty(); 00162 } 00163 00164 bool full() const { 00165 return (size_type)buf.size() == cap; 00166 } 00167 private: 00168 size_type cap; 00169 std::deque<T> buf; 00170 value_t lastSample; 00171 }; 00172 }} 00173 00174 #endif // BUFFERSIMPLE_HPP