10Duke Enterprise C++ SDK
Toggle main menu visibility
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
11
namespace
tenduke
{
namespace
utl
{
12
41
class
BinaryView
:
public
virtual
::tenduke::utl::DataSpan
42
{
43
public
:
49
BinaryView
(
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
73
private
:
74
const
unsigned
char
*data_;
75
std::size_t size_;
76
};
77
82
inline
BinaryView
empty_view
()
83
{
84
return
{
nullptr
, 0};
85
}
86
93
inline
BinaryView
make_view
(
94
const
unsigned
char
* data,
95
const
std::size_t size
96
) {
97
return
{data, size};
98
}
99
105
template
<std::
size_t
N>
106
inline
BinaryView
make_view
(
unsigned
char
(&arr)[N])
107
{
108
return
BinaryView
(arr, N);
109
}
110
116
inline
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
129
inline
BinaryView
make_view
(
const
std::string &value)
130
{
131
return
{
132
reinterpret_cast<
const
unsigned
char
*
>
(value.data()),
133
value.size()
134
};
135
}
136
143
inline
BinaryView
make_view
(
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
tenduke::utl::BinaryView
A read-only view of binary data.
Definition
BinaryView.h:42
tenduke::utl::BinaryView::BinaryView
BinaryView(const unsigned char *data, const std::size_t size)
Constructs a new instance.
Definition
BinaryView.h:49
tenduke::utl::BinaryView::data
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
tenduke::utl::BinaryView::size_bytes
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
tenduke::utl::DataSpan
A span of binary data.
Definition
DataSpan.h:16
tenduke::utl
Utilities.
Definition
Base64Decoder.h:10
tenduke::utl::empty_view
BinaryView empty_view()
Creates an empty tenduke::utl::BinaryView.
Definition
BinaryView.h:82
tenduke::utl::make_view
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
tenduke
Root for classes, functions and globals of 10Duke C++ Client.
Definition
APIRequest.h:4
core
core
src
utl
BinaryView.h
Generated by
1.17.0