OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
buffer.hpp
Go to the documentation of this file.
1// Copyright (c) 2021 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef OPENCBDC_TX_SRC_COMMON_BUFFER_H_
7#define OPENCBDC_TX_SRC_COMMON_BUFFER_H_
8
9#include <optional>
10#include <string>
11#include <vector>
12
13namespace cbdc {
15 class buffer {
16 public:
17 buffer() = default;
18
21 [[nodiscard]] auto size() const -> size_t;
22
25 [[nodiscard]] auto data() -> void*;
26
29 [[nodiscard]] auto data() const -> const void*;
30
34 [[nodiscard]] auto data_at(size_t offset) -> void*;
35
39 [[nodiscard]] auto data_at(size_t offset) const -> const void*;
40
45 void append(const void* data, size_t len);
46
48 void clear();
49
50 auto operator==(const buffer& other) const -> bool;
51
54 void extend(size_t len);
55
58 [[nodiscard]] auto c_ptr() const -> const unsigned char*;
59
62 [[nodiscard]] auto c_str() const -> const char*;
63
67 static auto from_hex(const std::string& hex)
68 -> std::optional<cbdc::buffer>;
69
72 [[nodiscard]] auto to_hex() const -> std::string;
73
79 static auto from_hex_prefixed(const std::string& hex,
80 const std::string& prefix = "0x")
81 -> std::optional<buffer>;
82
86 [[nodiscard]] auto to_hex_prefixed(const std::string& prefix
87 = "0x") const -> std::string;
88
89 private:
90 std::vector<std::byte> m_data{};
91 };
92}
93
94#endif // OPENCBDC_TX_SRC_COMMON_BUFFER_H_
Buffer to store and retrieve byte data.
Definition buffer.hpp:15
auto data() -> void *
Returns a raw pointer to the start of the buffer data.
Definition buffer.cpp:28
void clear()
Removes any existing content in the buffer making its size 0.
Definition buffer.cpp:14
static auto from_hex(const std::string &hex) -> std::optional< cbdc::buffer >
Creates a new buffer from the provided hex string.
Definition buffer.cpp:85
auto to_hex_prefixed(const std::string &prefix="0x") const -> std::string
Returns a hex string representation of the contents of the buffer prefixed with a prefix sequence.
Definition buffer.cpp:77
auto c_str() const -> const char *
Returns a pointer to the data, cast to a char*.
Definition buffer.cpp:57
void append(const void *data, size_t len)
Adds the given number of bytes from the given pointer to the end of the buffer.
Definition buffer.cpp:18
void extend(size_t len)
Extends the size of the buffer by the given length.
Definition buffer.cpp:48
auto c_ptr() const -> const unsigned char *
Returns a pointer to the data, cast to an unsigned char*.
Definition buffer.cpp:52
buffer()=default
static auto from_hex_prefixed(const std::string &hex, const std::string &prefix="0x") -> std::optional< buffer >
Creates a new buffer from the provided hex string optionally prefixed with a prefix sequence.
Definition buffer.cpp:108
auto to_hex() const -> std::string
Returns a hex string representation of the contents of the buffer.
Definition buffer.cpp:62
auto size() const -> size_t
Returns the number of bytes contained in the buffer.
Definition buffer.cpp:24
auto data_at(size_t offset) -> void *
Returns a raw pointer to the start of the buffer data.
Definition buffer.cpp:36