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 /**
24  * \defgroup drivers_ByteBuffer ByteBuffer class
25  * \ingroup drivers-internal-api-usb
26  * @{
27  */
28 class ByteBuffer {
29 public:
30 
31  /**
32  * Create a byte buffer of the given size
33  *
34  * @param size Number of bytes this buffer can hold
35  */
36  ByteBuffer(uint32_t size = 0);
37 
38  /**
39  * Delete this byte buffer
40  */
41  ~ByteBuffer();
42 
43  /**
44  * Set the size of the buffer
45  *
46  * Buffer contents are reset.
47  *
48  * @param size New buffer size
49  */
50  void resize(uint32_t size);
51 
52  /**
53  * Add a single byte to this buffer
54  *
55  * There must be enough space in the buffer or the behavior is undefined.
56  *
57  * @param data byte to add
58  */
59  void push(uint8_t data);
60 
61  /**
62  * Write a block of data to this ByteBuffer
63  *
64  * There must be enough space in the ByteBuffer or the behavior is undefined.
65  *
66  * @param data Block of data to write
67  * @param size Size of data to write
68  */
69  void write(uint8_t *data, uint32_t size);
70 
71  /**
72  * Remove a byte from this buffer
73  *
74  * @return data byte
75  */
76  uint8_t pop();
77 
78  /**
79  * Read a block of data from this ByteBuffer into a buffer pointed by 'data'
80  *
81  * There must be enough data in the ByteBuffer or the behavior is undefined.
82  *
83  * @param data Block of data to read
84  * @param size Size of data to read
85  */
86  void read(uint8_t *data, uint32_t size);
87 
88  /**
89  * Return the number bytes in this byte buffer
90  *
91  * @return Number of used bytes
92  */
93  uint32_t size();
94 
95  /**
96  * Return the number of additional bytes this buffer can hold
97  *
98  * @return Number of free bytes
99  */
100  uint32_t free();
101 
102  /**
103  * Check if this byte buffer is full
104  *
105  * @return true if full, false otherwise
106  */
107  bool full();
108 
109  /**
110  * Check if this byte buffer is empty
111  *
112  * @return true if empty, false otherwise
113  */
114  bool empty();
115 
116 private:
117 
118  uint32_t _head;
119  uint32_t _tail;
120  uint32_t _size;
121  uint8_t *_buf;
122 };
123 
124 /** @}*/
125 
126 #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.