ocra-recipes
Doxygen documentation for the ocra-recipes repository
Buffer.h
Go to the documentation of this file.
1 #ifndef _OCRABASE_BUFFER_H_
2 #define _OCRABASE_BUFFER_H_
3 
4 #ifdef WIN32
5 # pragma once
6 #endif
7 
8 namespace ocra
9 {
17  template<class T>
18  class Buffer
19  {
20  public:
21  typedef T value_type;
22  typedef T* ptr_type;
23 
24  private:
27  Buffer(const Buffer&);
28  Buffer& operator= (const Buffer&);
30 
31  public:
33  Buffer(size_t size): buf_(new value_type[size]), size_(size), index_(0) {}
34  ~Buffer() { delete[] buf_; }
35 
40  void resize(size_t size)
41  {
42  index_ = 0;
43 
44  if(size <= size_)
45  return;
46 
47  size_t nsize = size_ + 1;
48  while(nsize <= size)
49  nsize *= 2;
50 
51  delete[] buf_;
52  buf_ = new value_type[nsize];
53  size_ = nsize;
54  }
55 
59  void reset()
60  {
61  index_ = 0;
62  }
63 
68  ptr_type allocate(size_t n)
69  {
70  if((index_ + n) > size_)
71  throw std::runtime_error("[ocra::Buffer::allocate] Insufficient memory, please resize before allocation");
72  ptr_type result = buf_ + index_;
73  index_ += n;
74  return result;
75  }
76 
78  size_t size() const
79  {
80  return size_;
81  }
82 
84  size_t freeSize() const
85  {
86  return size_ - index_;
87  }
88 
89  private:
90  ptr_type buf_; //< the memory pool
91  size_t size_; //< its size
92  size_t index_; //< the index of the first free element in the pool
93  };
94 }
95 
96 #endif //_OCRABASE_BUFFER_H_
97 
98 // cmake:sourcegroup=utils
size_t size() const
Definition: Buffer.h:78
size_t freeSize() const
Definition: Buffer.h:84
void resize(size_t size)
Definition: Buffer.h:40
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
void reset()
Definition: Buffer.h:59
T value_type
Definition: Buffer.h:21
Buffer(size_t size)
Definition: Buffer.h:33
T * ptr_type
Definition: Buffer.h:22
ptr_type allocate(size_t n)
Definition: Buffer.h:68