Mistake on this page?
Report an issue in GitHub or email us
ByteBuffer.h
1 /*
2  * Copyright (c) 2018-2019, Arm Limited and affiliates.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef BYTE_BUFFER_H
19 #define BYTE_BUFFER_H
20 
21 #include <stdint.h>
22 
23 class ByteBuffer {
24 public:
25 
26  /**
27  * Create a byte buffer of the given size
28  *
29  * @param size Number of bytes this buffer can hold
30  */
31  ByteBuffer(uint32_t size = 0);
32 
33  /**
34  * Delete this byte buffer
35  */
36  ~ByteBuffer();
37 
38  /**
39  * Set the size of the buffer
40  *
41  * Buffer contents are reset.
42  *
43  * @param size New buffer size
44  */
45  void resize(uint32_t size);
46 
47  /**
48  * Add a single byte to this buffer
49  *
50  * There must be enough space in the buffer or the behavior is undefined.
51  *
52  * @param data byte to add
53  */
54  void push(uint8_t data);
55 
56  /**
57  * Write a block of data to this ByteBuffer
58  *
59  * There must be enough space in the ByteBuffer or the behavior is undefined.
60  *
61  * @param data Block of data to write
62  * @param size Size of data to write
63  */
64  void write(uint8_t *data, uint32_t size);
65 
66  /**
67  * Remove a byte from this buffer
68  *
69  * @return data byte
70  */
71  uint8_t pop();
72 
73  /**
74  * Read a block of data from this ByteBuffer into a buffer pointed by 'data'
75  *
76  * There must be enough data in the ByteBuffer or the behavior is undefined.
77  *
78  * @param data Block of data to read
79  * @param size Size of data to read
80  */
81  void read(uint8_t *data, uint32_t size);
82 
83  /**
84  * Return the number bytes in this byte buffer
85  *
86  * @return Number of used bytes
87  */
88  uint32_t size();
89 
90  /**
91  * Return the number of additional bytes this buffer can hold
92  *
93  * @return Number of free bytes
94  */
95  uint32_t free();
96 
97  /**
98  * Check if this byte buffer is full
99  *
100  * @return true if full, false otherwise
101  */
102  bool full();
103 
104  /**
105  * Check if this byte buffer is empty
106  *
107  * @return true if empty, false otherwise
108  */
109  bool empty();
110 
111 private:
112 
113  uint32_t _head;
114  uint32_t _tail;
115  uint32_t _size;
116  uint8_t *_buf;
117 };
118 
119 
120 #endif
uint32_t free()
Return the number of additional bytes this buffer can hold.
void push(uint8_t data)
Add a single byte to this buffer.
bool empty()
Check if this byte buffer is empty.
void write(uint8_t *data, uint32_t size)
Write a block of data to this ByteBuffer.
~ByteBuffer()
Delete this byte buffer.
uint8_t pop()
Remove a byte from this buffer.
ByteBuffer(uint32_t size=0)
Create a byte buffer of the given size.
void resize(uint32_t size)
Set the size of the buffer.
bool full()
Check if this byte buffer is full.
uint32_t size()
Return the number bytes in this byte buffer.
void read(uint8_t *data, uint32_t size)
Read a block of data from this ByteBuffer into a buffer pointed by &#39;data&#39;.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.