1 #ifndef DOIP_IDENTIFIERS_H
2 #define DOIP_IDENTIFIERS_H
21 template <
size_t IdLength,
bool zeroPadding = false,
char padChar = 0>
35 using iterator =
typename std::array<uint8_t, IdLength>::iterator;
36 using const_iterator =
typename std::array<uint8_t, IdLength>::const_iterator;
49 const size_t copy_length = std::min(id_string.length(),
ID_LENGTH);
50 std::copy_n(id_string.c_str(), copy_length, m_id.data());
60 if (id_cstr !=
nullptr) {
61 const size_t copy_length = std::min(std::strlen(id_cstr),
ID_LENGTH);
62 std::copy_n(id_cstr, copy_length, m_id.data());
76 if (
data !=
nullptr) {
77 const size_t copy_length = std::min(length,
ID_LENGTH);
78 std::copy_n(
data, copy_length, m_id.data());
91 const size_t copy_length = std::min(byte_array.size(),
ID_LENGTH);
92 std::copy_n(byte_array.data(), copy_length, m_id.data());
102 template <
typename integral,
103 typename = std::enable_if_t<std::is_integral_v<integral>>>
107 constexpr
size_t sizeof_integral =
sizeof(integral);
108 constexpr
size_t len = std::min(sizeof_integral,
ID_LENGTH);
110 for (
size_t i = 0; i < len; ++i) {
111 m_id[len - 1 - i] = (id_value >> (i * 8)) & 0xFF;
147 size_t effective_length = 0;
152 effective_length = i + 1;
154 return std::string(
reinterpret_cast<const char *
>(m_id.data()), effective_length);
163 std::ostringstream oss;
164 oss << std::uppercase << std::hex << std::setfill(
'0');
166 oss << std::setw(2) << static_cast<int>(m_id[i]);
188 const std::array<uint8_t, ID_LENGTH> &
getArray()
const {
208 bytes.insert(bytes.end(), m_id.begin(), m_id.end());
217 constexpr
size_t size()
const {
227 if constexpr (zeroPadding) {
229 return std::all_of(m_id.begin(), m_id.end(), [](uint8_t
byte) { return byte == static_cast<uint8_t>(padChar); });
232 return std::all_of(m_id.begin(), m_id.end(), [](uint8_t
byte) { return byte == 0; });
243 return m_id == other.m_id;
253 return m_id != other.m_id;
263 return m_id.at(index);
281 return static_cast<uint8_t
>(padChar);
289 auto begin() noexcept {
return m_id.begin(); }
296 auto begin() const noexcept {
return m_id.begin(); }
303 auto cbegin() const noexcept {
return m_id.cbegin(); }
310 auto end() noexcept {
return m_id.end(); }
317 auto end() const noexcept {
return m_id.end(); }
324 auto cend() const noexcept {
return m_id.cend(); }
332 std::array<uint8_t, ID_LENGTH> m_id;
334 void pad(
size_t start_index) {
335 if (zeroPadding && start_index <
ID_LENGTH) {
336 std::fill(m_id.begin() + start_index, m_id.end(), padChar);
342 template <
size_t IdLength,
bool zeroPadding,
char padChar>
Defines the ByteArray type and utility functions for byte manipulation.
Generic fixed-length identifier class template.
constexpr size_t size() const
Get the size (always IdLength)
auto cbegin() const noexcept
Iterator support - cbegin.
GenericFixedId(const std::string &id_string)
Construct from string.
auto end() const noexcept
Iterator support - end (const)
GenericFixedId()
Default constructor - creates an identifier filled with zeros.
bool operator!=(const GenericFixedId &other) const
Inequality operator.
GenericFixedId & operator=(GenericFixedId &&other) noexcept=default
Move assignment operator.
~GenericFixedId()=default
Destructor.
const uint8_t & const_reference
static const GenericFixedId Zero
Static instance containing only zeros.
const uint8_t & operator[](size_t index) const
Array subscript operator (const)
constexpr char getPadChar()
Get the char used to pad shorter identifiers.
GenericFixedId(const GenericFixedId &other)=default
Copy constructor.
std::string toHexString() const
Get identifier as hex string, bytes separated by '.
static constexpr size_t ID_LENGTH
Length of the identifier in bytes.
auto end() noexcept
Iterator support - end.
typename std::array< uint8_t, IdLength >::iterator iterator
auto begin() const noexcept
Iterator support - begin (const)
const uint8_t * data() const
Get pointer to raw data.
typename std::array< uint8_t, IdLength >::const_iterator const_iterator
GenericFixedId(const ByteArray &byte_array)
Construct from ByteArray.
GenericFixedId & operator=(const GenericFixedId &other)=default
Copy assignment operator.
std::ptrdiff_t difference_type
bool isEmpty() const
Check if identifier is all zeros or all padding characters.
GenericFixedId(GenericFixedId &&other) noexcept=default
Move constructor.
GenericFixedId(integral id_value)
Construct a new Generic Fixed Id object.
ByteArrayRef asByteArray() const
Get identifier as ByteArray with exactly IdLength bytes.
ByteArray & appendTo(ByteArray &bytes) const
Appends this identifier to the given byte array.
bool operator==(const GenericFixedId &other) const
Equality operator.
constexpr uint8_t getPadByte()
Get the byte used to pad shorter identifiers.
GenericFixedId(const char *id_cstr)
Construct from C-style string.
const std::array< uint8_t, ID_LENGTH > & getArray() const
Get direct access to the underlying array.
auto begin() noexcept
Iterator support - begin.
GenericFixedId(const uint8_t *data, size_t length)
Construct from byte sequence.
std::string toString() const
Get identifier as string.
auto cend() const noexcept
Iterator support - cend.
std::pair< const uint8_t *, size_t > ByteArrayRef
Reference to raw array of bytes.
std::ostream & operator<<(std::ostream &os, const ByteArray &arr)
Stream operator for ByteArray.
A dynamic array of bytes with utility methods for network protocol handling.