Class to hold an array of bytes. Contains an internal buffer that may be larger than what is reported by size(), to avoid repeated allocations when a ByteArray grows.
| 12 | // larger than what is reported by size(), to avoid repeated allocations when a |
| 13 | // ByteArray grows. |
| 14 | class ByteArray { |
| 15 | public: |
| 16 | typedef char value_type; |
| 17 | typedef char* iterator; |
| 18 | typedef char const* const_iterator; |
| 19 | |
| 20 | // Constructs a byte array from a given c string WITHOUT including the |
| 21 | // trailing '\0' |
| 22 | static ByteArray fromCString(char const* str); |
| 23 | // Same, but includes the trailing '\0' |
| 24 | static ByteArray fromCStringWithNull(char const* str); |
| 25 | static ByteArray withReserve(size_t capacity); |
| 26 | |
| 27 | ByteArray(); |
| 28 | ByteArray(size_t dataSize, char c); |
| 29 | ByteArray(char const* data, size_t dataSize); |
| 30 | ByteArray(ByteArray const& b); |
| 31 | ByteArray(ByteArray&& b) noexcept; |
| 32 | ~ByteArray(); |
| 33 | |
| 34 | ByteArray& operator=(ByteArray const& b); |
| 35 | ByteArray& operator=(ByteArray&& b) noexcept; |
| 36 | |
| 37 | char const* ptr() const; |
| 38 | char* ptr(); |
| 39 | |
| 40 | size_t size() const; |
| 41 | // Maximum size before realloc |
| 42 | size_t capacity() const; |
| 43 | // Is zero size |
| 44 | bool empty() const; |
| 45 | |
| 46 | // Sets size to 0. |
| 47 | void clear(); |
| 48 | // Clears and resets buffer to empty. |
| 49 | void reset(); |
| 50 | |
| 51 | void reserve(size_t capacity); |
| 52 | |
| 53 | void resize(size_t size); |
| 54 | // resize, filling new space with given byte if it exists. |
| 55 | void resize(size_t size, char f); |
| 56 | |
| 57 | // fill array with byte. |
| 58 | void fill(char c); |
| 59 | // fill array and resize to new size. |
| 60 | void fill(size_t size, char c); |
| 61 | |
| 62 | void append(ByteArray const& b); |
| 63 | void append(char const* data, size_t len); |
| 64 | void appendByte(char b); |
| 65 | |
| 66 | void copyTo(char* data, size_t len) const; |
| 67 | void copyTo(char* data) const; |
| 68 | |
| 69 | // Copy from ByteArray starting at pos, to data, with size len. |
| 70 | void copyTo(char* data, size_t pos, size_t len) const; |
| 71 | // Copy from data pointer to ByteArray at pos with size len. |
no outgoing calls