libbio
Loading...
Searching...
No Matches
Bytes.h
Go to the documentation of this file.
1/*
2 * This file is a part of the Biology project by eons LLC.
3 * Biology (aka Develop Biology) is a framework for approaching software
4 * development from a natural sciences perspective.
5 *
6 * Copyright (C) 2026 Séon O'Shannon & eons LLC
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include <stddef.h>
25#include <iterator>
26#include <vector>
27
28namespace bio
29{
30
35{
36public:
38 mData(NULL),
39 mSize(0)
40 {
41 }
42
43 BytesView(const void* data, size_t size) :
45 mSize(size)
46 {
47 }
48
49 const unsigned char* Data() const
50 {
51 return mData;
52 }
53
54 size_t Size() const
55 {
56 return mSize;
57 }
58
59 bool Empty() const
60 {
61 return mSize == 0;
62 }
63
64private:
65 const unsigned char* mData;
66 size_t mSize;
67};
68
72class Bytes
73{
74public:
76 {
77 }
78
79 explicit Bytes(size_t size)
80 {
81 Resize(size);
82 }
83
84 Bytes(const void* data, size_t size)
85 {
87 }
88
89 // Range constructor — stdlib-interop. SFINAE-guarded on
90 // iterator_category so it doesn't overload-collide with
91 // Bytes(const void*, size_t) on integral arguments
92 // (e.g. Bytes(0, 0) would otherwise be ambiguous). The
93 // trailing pointer parameter form is C++98-compatible;
94 // default-template-args on function templates require C++11.
95 template < typename InputIt >
99 typename ::std::iterator_traits< InputIt >::iterator_category* = 0
100 ) :
101 mData(first, last)
102 {
103 }
104
105 const unsigned char* Data() const
106 {
107 if (mData.empty())
108 {
109 return NULL;
110 }
111 return &mData[0];
112 }
113
114 unsigned char* Data()
115 {
116 if (mData.empty())
117 {
118 return NULL;
119 }
120 return &mData[0];
121 }
122
123 size_t Size() const
124 {
125 return mData.size();
126 }
127
128 bool Empty() const
129 {
130 return mData.empty();
131 }
132
133 void Clear()
134 {
135 mData.clear();
136 }
137
138 void Resize(size_t size)
139 {
140 mData.resize(size, 0);
141 }
142
143 void Assign(const void* data, size_t size)
144 {
145 const unsigned char* bytes = static_cast< const unsigned char* >(data);
146 if (!bytes || size == 0)
147 {
148 mData.clear();
149 return;
150 }
151 mData.assign(bytes, bytes + size);
152 }
153
154 void Append(const void* data, size_t size)
155 {
156 const unsigned char* bytes = static_cast< const unsigned char* >(data);
157 if (!bytes || size == 0)
158 {
159 return;
160 }
161 mData.insert(mData.end(), bytes, bytes + size);
162 }
163
165 {
166 return BytesView(Data(), Size());
167 }
168
169 bool operator==(const Bytes& other) const
170 {
171 return mData == other.mData;
172 }
173
174 bool operator!=(const Bytes& other) const
175 {
176 return !(*this == other);
177 }
178
179 // ----------------------------------------------------------------------
180 // Stdlib-interop API
181 //
182 // Naming exception to bio's house style (capitalized methods): these
183 // must be lowercase because they are compile-time hooks for the C++
184 // standard library — range-for resolves `for (x : c)` to `c.begin()`
185 // / `c.end()` by EXACT name, and std::iterator_traits / std-algorithms
186 // look for `value_type`, `iterator`, etc. by exact name. Capitalized
187 // aliases would not satisfy these resolutions, defeating the purpose.
188 //
189 // The capitalized API above stays canonical for bio's own consumers;
190 // these are purely stdlib drop-in compatibility.
191 // ----------------------------------------------------------------------
192
193 typedef unsigned char value_type;
194 typedef unsigned char* iterator;
195 typedef const unsigned char* const_iterator;
196 typedef size_t size_type;
197 typedef ::std::ptrdiff_t difference_type;
198
199 iterator begin() { return mData.data(); }
200 iterator end() { return mData.data() + mData.size(); }
201 const_iterator begin() const { return mData.data(); }
202 const_iterator end() const { return mData.data() + mData.size(); }
203 const_iterator cbegin() const { return mData.data(); }
204 const_iterator cend() const { return mData.data() + mData.size(); }
205
206 unsigned char& operator[](size_type i) { return mData[i]; }
207 const unsigned char& operator[](size_type i) const { return mData[i]; }
208
209 void reserve(size_type n) { mData.reserve(n); }
210 void push_back(unsigned char b) { mData.push_back(b); }
211
212private:
213 ::std::vector< unsigned char > mData;
214};
215
216} //bio namespace
std::size_t size
Definition CborAxis.cpp:470
const uint8_t * data
Definition CborAxis.cpp:469
Definition Bytes.h:35
BytesView()
Definition Bytes.h:37
bool Empty() const
Definition Bytes.h:59
size_t Size() const
Definition Bytes.h:54
const unsigned char * Data() const
Definition Bytes.h:49
BytesView(const void *data, size_t size)
Definition Bytes.h:43
Definition Bytes.h:73
const_iterator begin() const
Definition Bytes.h:201
void Assign(const void *data, size_t size)
Definition Bytes.h:143
const_iterator end() const
Definition Bytes.h:202
const unsigned char * const_iterator
Definition Bytes.h:195
unsigned char value_type
Definition Bytes.h:193
Bytes(size_t size)
Definition Bytes.h:79
Bytes(const void *data, size_t size)
Definition Bytes.h:84
unsigned char & operator[](size_type i)
Definition Bytes.h:206
iterator end()
Definition Bytes.h:200
unsigned char * iterator
Definition Bytes.h:194
::std::ptrdiff_t difference_type
Definition Bytes.h:197
unsigned char * Data()
Definition Bytes.h:114
void push_back(unsigned char b)
Definition Bytes.h:210
iterator begin()
Definition Bytes.h:199
const_iterator cend() const
Definition Bytes.h:204
void Clear()
Definition Bytes.h:133
bool operator==(const Bytes &other) const
Definition Bytes.h:169
void reserve(size_type n)
Definition Bytes.h:209
BytesView View() const
Definition Bytes.h:164
size_t size_type
Definition Bytes.h:196
Bytes()
Definition Bytes.h:75
const_iterator cbegin() const
Definition Bytes.h:203
bool operator!=(const Bytes &other) const
Definition Bytes.h:174
const unsigned char * Data() const
Definition Bytes.h:105
void Append(const void *data, size_t size)
Definition Bytes.h:154
Bytes(InputIt first, InputIt last, typename ::std::iterator_traits< InputIt >::iterator_category *=0)
Definition Bytes.h:96
bool Empty() const
Definition Bytes.h:128
const unsigned char & operator[](size_type i) const
Definition Bytes.h:207
size_t Size() const
Definition Bytes.h:123
void Resize(size_t size)
Definition Bytes.h:138
Definition Pointer.h:115
Definition FinalCell.h:29