YARP
Yet Another Robot Platform
FixedSizeBuffersManager.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_DEV_IMPL_FIXEDSIZEBUFFERSMANAGER_H
10 #define YARP_DEV_IMPL_FIXEDSIZEBUFFERSMANAGER_H
11 
12 #include <vector>
13 #include <mutex>
14 
15 
16 namespace yarp {
17 namespace dev {
18 namespace impl {
19 
20 
25 template <typename T>
26 class Buffer
27 {
28 public:
29  uint32_t key;
30  T* dataPtr;
31  uint32_t numOfElements;
32 
36  Buffer();
37 
41  ~Buffer();
42 
46  T* getData();
47 
51  uint32_t getSize();
52 
59  T getValue(uint32_t index);
60 
67  void setValue(uint32_t index, T value);
68 
72  T& operator[](uint32_t index);
73 };
74 
75 
86 template <typename T>
88 {
89 public:
95  explicit FixedSizeBuffersManager(uint32_t sizeOfBuffers, std::size_t initialNumOfBuffers = 3);
96 
103 
112 
116  std::size_t getBufferSize();
117 
126 
132  void printBuffers();
133 
134 private:
135  std::mutex m_mutex;
136  std::vector<T*> m_buffers;
137  std::vector<bool> m_usedBuffers;
138  std::size_t m_numElem;
139  uint32_t m_firstFreeBuff; //euristic
140 };
141 
142 
143 } // namespace impl
144 } // namespace dev
145 } // namespace yarp
146 
148 
149 #endif // YARP_DEV_IMPL_FIXEDSIZEBUFFERSMANAGER_H
Buffer contains info about a buffer of type T and it is used to exchange information with yarp::dev::...
T * getData()
Return the data pointer.
~Buffer()
Destructor.Note that the memory will not deallocated.
T & operator[](uint32_t index)
Access specified element.
T getValue(uint32_t index)
Return the value contained in the buffer at index index.
uint32_t getSize()
Return the number of element of buffer.
void setValue(uint32_t index, T value)
Set the value @value in the buffer at index index.
A manager of fixed size buffers in multi-thread environment.
FixedSizeBuffersManager(uint32_t sizeOfBuffers, std::size_t initialNumOfBuffers=3)
Constructor.
std::size_t getBufferSize()
Get the number of elements of a buffer.
Buffer< T > getBuffer()
Get a buffer and fill its information in @buffer.
void releaseBuffer(Buffer< T > &buffer)
Release a buffer.
The main, catch-all namespace for YARP.
Definition: environment.h:18