CCA
|
Buffer.h
Go to the documentation of this file.
00001 /* ============================================================ 00002 * 00003 * This file is a part of CCA project 00004 * 00005 * Copyright (C) 2011 by Arne Nordmann <anordman at cor-lab dot uni-bielefeld dot de> 00006 * 00007 * This file may be licensed under the terms of the 00008 * GNU Lesser General Public License Version 3 (the ``LGPL''), 00009 * or (at your option) any later version. 00010 * 00011 * Software distributed under the License is distributed 00012 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either 00013 * express or implied. See the LGPL for the specific language 00014 * governing rights and limitations. 00015 * 00016 * You should have received a copy of the LGPL along with this 00017 * program. If not, go to http://www.gnu.org/licenses/lgpl.html 00018 * or write to the Free Software Foundation, Inc., 00019 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00020 * 00021 * The development of this software was supported by: 00022 * CoR-Lab, Research Institute for Cognition and Robotics 00023 * Bielefeld University 00024 * 00025 * ============================================================ */ 00026 00027 #pragma once 00028 00029 #include <string> 00030 #include <sstream> 00031 #include <iostream> 00032 #include <exception> 00033 00034 #include <boost/shared_ptr.hpp> 00035 00036 #include "rsc/misc/langutils.h" 00037 00038 #include "cca/buffer/BufferException.h" 00039 00040 namespace cca { 00041 00042 using namespace rsc::misc; 00043 00047 template<class DATATYPE> 00048 class Buffering { 00049 00050 public: 00051 00053 typedef ::boost::shared_ptr<DATATYPE> DataPtr; 00054 00055 Buffering() { 00056 } 00057 virtual ~Buffering() { 00058 } 00059 00063 virtual DataPtr get() throw () = 0; 00064 00068 virtual void add(DataPtr item) = 0; 00069 00076 virtual bool empty() const = 0; 00077 00084 virtual bool newItem() const = 0; 00085 00092 virtual unsigned int size() const = 0; 00093 00097 virtual void purge() = 0; 00098 00099 protected: 00104 Buffering(Buffering &buffer); 00105 00110 void operator=(const Buffering&); 00111 }; 00112 00117 template<class DATATYPE> 00118 class Buffer: public Buffering<DATATYPE> { 00119 00120 public: 00121 00123 typedef ::boost::shared_ptr<Buffer<DATATYPE> > Ptr; 00124 00126 typedef ::boost::shared_ptr<DATATYPE> DataPtr; 00127 00128 Buffer(bool alwaysKeepLatest = true) : 00129 warm(false), numItems(0), keepLatest(alwaysKeepLatest), 00130 newitem(false) { 00131 } 00132 00133 virtual ~Buffer() { 00134 } 00135 00139 virtual std::string print() const { 00140 std::ostringstream outstream(std::ostringstream::out); 00141 outstream.precision(3); // Precision when printing double values 00142 outstream << "<Buffer>" << std::endl; 00143 return outstream.str(); 00144 } 00145 00149 virtual DataPtr get() throw () = 0; 00150 00154 virtual void add(DataPtr item) = 0; 00155 00162 virtual bool empty() const { 00163 if (this->keepLatest) { 00164 return (!this->warm); 00165 } else { 00166 return (this->size() == 0); 00167 } 00168 } 00169 00176 virtual bool newItem() const { 00177 return this->newitem; 00178 } 00179 00186 virtual unsigned int size() const { 00187 return this->numItems; 00188 } 00189 00193 virtual void purge() = 0; 00194 00195 protected: 00200 Buffer(Buffer &buffer); 00201 00206 void operator=(const Buffer&); 00207 00211 bool warm; 00212 00216 unsigned int numItems; 00217 00222 bool keepLatest; 00223 00227 bool newitem; 00228 }; 00229 00230 }