Main MRPT website > C++ reference
MRPT logo
MT_buffer.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef mrpt_synch_mt_buffer_H
29 #define mrpt_synch_mt_buffer_H
30 
32 
33 namespace mrpt
34 {
35 namespace synch
36 {
37 
38 /** This class is a bulk sequence of bytes with MultiThread (MT)-safe read and write operations.
39  * \ingroup synch_grp
40  */
41 class MT_buffer
42 {
43 private:
46 
47 public:
48  MT_buffer() //!< Default constructor
49  {}
50  virtual ~MT_buffer() //!< Destructor
51  {}
52 
53  void clear() //!< Empty the buffer
54  {
55  m_cs.enter();
56  m_data.clear();
57  m_cs.leave();
58  }
59 
60  size_t size() //!< Return the number of available bytes at this moment.
61  {
62  size_t s;
63  m_cs.enter();
64  s = m_data.size();
65  m_cs.leave();
66  return s;
67  }
68 
69  void appendData(const vector_byte &d) //!< Append new data to the stream
70  {
71  m_cs.enter();
72  m_data.insert( m_data.begin(), d.begin(),d.end() );
73  m_cs.leave();
74  }
75 
76  void readAndClear(vector_byte &d) //!< Read the whole buffer and empty it.
77  {
78  m_cs.enter();
79  d.clear();
80  m_data.swap(d);
81  m_cs.leave();
82  }
83 
84  void read(vector_byte &d) //!< Read the whole buffer.
85  {
86  m_cs.enter();
87  d = m_data;
88  m_cs.leave();
89  }
90 
91 }; // end of MT_buffer
92 
93 
94 } // End of namespace
95 } // End of namespace
96 
97 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013