VitroIO SDK
Software development kit for Vitro Shard.
Loading...
Searching...
No Matches
buffer.h
Go to the documentation of this file.
1#ifndef VITROIO_SDK_BUFFER_H
2#define VITROIO_SDK_BUFFER_H
3
4#include <mbed.h>
5
6namespace vitroio
7{
8
9namespace sdk
10{
11
12namespace impl
13{
14
15class Buffer
16{
17public:
19 buff_(NULL),
20 availableSize_(0),
21 workingSize_(0),
22 usedSize_(0),
23 freeOffset_(0)
24 {}
25
27 {
28 free();
29 }
30
31 bool alloc(uint32_t size, uint8_t defaultValue = 0)
32 {
33 if( (buff_ = new (nothrow) uint8_t[size]) ){
34 availableSize_ = size;
35 workingSize_ = size;
36 usedSize_ = 0;
37 memset(buff_, defaultValue, size);
38 return true;
39 }
40 return false;
41 }
42
43 void free()
44 {
45 if(buff_){
46 delete buff_;
47 buff_ = NULL;
48 }
49 availableSize_ = 0;
50 workingSize_ = 0;
51 usedSize_ = 0;
52 freeOffset_ = 0;
53 }
54
55 uint32_t availableSize() const
56 {
57 return availableSize_;
58 }
59
60 uint32_t size() const
61 {
62 return workingSize_;
63 }
64
65 uint32_t freeSize() const
66 {
67 return workingSize_ - usedSize_;
68 }
69
70 bool resize(uint32_t size)
71 {
72 if(size > availableSize_){
73 return false;
74 }
75 workingSize_ = size;
76
77 usedSize_ = (usedSize_ > workingSize_) ? workingSize_ : usedSize_;
78
79 return true;
80 }
81
82 uint8_t* get()
83 {
84 return buff_;
85 }
86
87 const uint8_t* get() const
88 {
89 return buff_;
90 }
91
92 uint32_t set(uint8_t* data, uint32_t size)
93 {
94 uint32_t bytesToSet = (size > freeSize()) ? freeSize() : size;
95
96 memcpy(buff_ + freeOffset_, data, bytesToSet);
97 usedSize_ += bytesToSet;
98 freeOffset_ += bytesToSet;
99
100 return bytesToSet;
101 }
102
103 void clear(uint32_t newSize = 0, uint8_t defaultValue = 0)
104 {
105 usedSize_ = 0;
106 freeOffset_ = 0;
107 memset(buff_, defaultValue, availableSize_);
108 }
109
110 bool isEmpty() const
111 {
112 return !usedSize_;
113 }
114
115 bool isFull() const
116 {
117 return (usedSize_ == workingSize_) ? true : false;
118 }
119
120 operator bool() const
121 {
122 return buff_;
123 }
124
125private:
126 uint8_t* buff_;
127 uint32_t availableSize_;
128 uint32_t workingSize_;
129 uint32_t usedSize_;
130 uint32_t freeOffset_;
131};
132
133} // namespace impl
134
135} // namespace sdk
136
137} // namespace vitroio
138
139#endif // VITROIO_SDK_BUFFER_H
Definition: buffer.h:16
const uint8_t * get() const
Definition: buffer.h:87
void free()
Definition: buffer.h:43
uint32_t freeSize() const
Definition: buffer.h:65
bool isFull() const
Definition: buffer.h:115
uint32_t set(uint8_t *data, uint32_t size)
Definition: buffer.h:92
bool isEmpty() const
Definition: buffer.h:110
bool alloc(uint32_t size, uint8_t defaultValue=0)
Definition: buffer.h:31
uint32_t size() const
Definition: buffer.h:60
uint8_t * get()
Definition: buffer.h:82
bool resize(uint32_t size)
Definition: buffer.h:70
uint32_t availableSize() const
Definition: buffer.h:55
~Buffer()
Definition: buffer.h:26
Buffer()
Definition: buffer.h:18
void clear(uint32_t newSize=0, uint8_t defaultValue=0)
Definition: buffer.h:103
The namespace contains all of components of vitro-shard-sdk and components based on the sdk....
Definition: can_layer.h:9