10Duke Enterprise C++ SDK
Loading...
Searching...
No Matches
BinaryView.h
1#ifndef TENDUKE_CORE_BINARYVIEW_H
2#define TENDUKE_CORE_BINARYVIEW_H
3
4#include "./DataSpan.h"
5
6#include <cstring>
7#include <limits>
8#include <stdexcept>
9#include <string>
10
11namespace tenduke { namespace utl {
12
42{
43public:
50 const unsigned char *data,
51 const std::size_t size
52 ) : data_(data)
53 , size_(size)
54 {
55 }
56
60 const unsigned char * data() const override
61 {
62 return data_;
63 }
64
68 std::size_t size_bytes() const override
69 {
70 return size_;
71 }
72
73private:
74 const unsigned char *data_;
75 std::size_t size_;
76};
77
83{
84 return {nullptr, 0};
85}
86
94 const unsigned char* data,
95 const std::size_t size
96) {
97 return {data, size};
98}
99
105template <std::size_t N>
106inline BinaryView make_view(unsigned char (&arr)[N])
107{
108 return BinaryView(arr, N);
109}
110
116inline BinaryView make_view(const char *value)
117{
118 return {
119 reinterpret_cast<const unsigned char*>(value),
120 std::strlen((const char*)value)
121 };
122}
123
129inline BinaryView make_view(const std::string &value)
130{
131 return {
132 reinterpret_cast<const unsigned char*>(value.data()),
133 value.size()
134 };
135}
136
144 const ::tenduke::utl::DataSpan &value,
145 const std::size_t offset,
146 const std::size_t length = (std::numeric_limits<std::size_t>::max)()
147)
148{
149 if (offset > value.size_bytes()) {
150 throw std::out_of_range("offset is out of range");
151 }
152 const std::size_t remaining = value.size_bytes() - offset;
153 const std::size_t actual = (length == (std::numeric_limits<std::size_t>::max)())
154 ? remaining
155 : length;
156 if (actual > remaining) {
157 throw std::out_of_range("length is out of range");
158 }
159 return {value.data() + offset, actual};
160}
161
162}}
163
164#endif //TENDUKE_CORE_BINARYVIEW_H
A read-only view of binary data.
Definition BinaryView.h:42
BinaryView(const unsigned char *data, const std::size_t size)
Constructs a new instance.
Definition BinaryView.h:49
const unsigned char * data() const override
Returns a pointer to the beginning of the underlying sequence.This provides a read-only view of the d...
Definition BinaryView.h:60
std::size_t size_bytes() const override
Returns the size of the span in bytes.size of the span in bytes
Definition BinaryView.h:68
A span of binary data.
Definition DataSpan.h:16
Utilities.
Definition Base64Decoder.h:10
BinaryView empty_view()
Creates an empty tenduke::utl::BinaryView.
Definition BinaryView.h:82
BinaryView make_view(const unsigned char *data, const std::size_t size)
A helper function to create a tenduke::utl::BinaryView from a const pointer.
Definition BinaryView.h:93
Root for classes, functions and globals of 10Duke C++ Client.
Definition APIRequest.h:4