Convert space to tab indentation

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2023-03-15 21:51:39 +01:00
parent 308f63490c
commit aa233bd5f9
9 changed files with 1293 additions and 1293 deletions

View File

@ -1,6 +1,6 @@
/* /*
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Copyright © 2016-2023 Amebis Copyright © 2016-2023 Amebis
*/ */
#pragma once #pragma once
@ -14,269 +14,269 @@
namespace stdex namespace stdex
{ {
/// ///
/// Base64 encoding session /// Base64 encoding session
/// ///
class base64_enc class base64_enc
{ {
public: public:
/// ///
/// Constructs blank encoding session /// Constructs blank encoding session
/// ///
base64_enc() noexcept : num(0) base64_enc() noexcept : num(0)
{ {
buf[0] = 0; buf[0] = 0;
buf[1] = 0; buf[1] = 0;
buf[2] = 0; buf[2] = 0;
} }
/// ///
/// Encodes one block of information, and _appends_ it to the output /// Encodes one block of information, and _appends_ it to the output
/// ///
/// \param[out] out Output /// \param[out] out Output
/// \param[in ] data Data to encode /// \param[in ] data Data to encode
/// \param[in ] size Length of `data` in bytes /// \param[in ] size Length of `data` in bytes
/// \param[in ] is_last Is this the last block of data? /// \param[in ] is_last Is this the last block of data?
/// ///
template<class _Elem, class _Traits, class _Ax> template<class _Elem, class _Traits, class _Ax>
void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_bytecount_(size) const void *data, _In_ size_t size, _In_opt_ bool is_last = true) void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_bytecount_(size) const void *data, _In_ size_t size, _In_opt_ bool is_last = true)
{ {
assert(data || !size); assert(data || !size);
// Preallocate output // Preallocate output
out.reserve(out.size() + enc_size(size)); out.reserve(out.size() + enc_size(size));
// Convert data character by character. // Convert data character by character.
for (size_t i = 0;; i++) { for (size_t i = 0;; i++) {
if (num >= 3) { if (num >= 3) {
encode(out); encode(out);
num = 0; num = 0;
} }
if (i >= size) if (i >= size)
break; break;
buf[num++] = reinterpret_cast<const uint8_t*>(data)[i]; buf[num++] = reinterpret_cast<const uint8_t*>(data)[i];
} }
// If this is the last block, flush the buffer. // If this is the last block, flush the buffer.
if (is_last && num) { if (is_last && num) {
encode(out, num); encode(out, num);
num = 0; num = 0;
} }
} }
/// ///
/// Resets encoding session /// Resets encoding session
/// ///
void clear() noexcept void clear() noexcept
{ {
num = 0; num = 0;
} }
/// ///
/// Returns maximum encoded size /// Returns maximum encoded size
/// ///
/// \param[in] size Number of bytes to encode /// \param[in] size Number of bytes to encode
/// ///
/// \returns Maximum number of bytes for the encoded data of `size` length /// \returns Maximum number of bytes for the encoded data of `size` length
/// ///
size_t enc_size(_In_ size_t size) const noexcept size_t enc_size(_In_ size_t size) const noexcept
{ {
return ((num + size + 2)/3)*4; return ((num + size + 2)/3)*4;
} }
protected: protected:
/// ///
/// Encodes one complete internal buffer of data /// Encodes one complete internal buffer of data
/// ///
template<class _Elem, class _Traits, class _Ax> template<class _Elem, class _Traits, class _Ax>
void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out) void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out)
{ {
out += base64_enc_lookup[ buf[0] >> 2 ]; out += base64_enc_lookup[ buf[0] >> 2 ];
out += base64_enc_lookup[((buf[0] << 4) | (buf[1] >> 4)) & 0x3f]; out += base64_enc_lookup[((buf[0] << 4) | (buf[1] >> 4)) & 0x3f];
out += base64_enc_lookup[((buf[1] << 2) | (buf[2] >> 6)) & 0x3f]; out += base64_enc_lookup[((buf[1] << 2) | (buf[2] >> 6)) & 0x3f];
out += base64_enc_lookup[ buf[2] & 0x3f]; out += base64_enc_lookup[ buf[2] & 0x3f];
} }
/// ///
/// Encodes partial internal buffer of data /// Encodes partial internal buffer of data
/// ///
template<class _Elem, class _Traits, class _Ax> template<class _Elem, class _Traits, class _Ax>
void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_ size_t size) void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_ size_t size)
{ {
if (size > 0) { if (size > 0) {
out += base64_enc_lookup[buf[0] >> 2]; out += base64_enc_lookup[buf[0] >> 2];
if (size > 1) { if (size > 1) {
out += base64_enc_lookup[((buf[0] << 4) | (buf[1] >> 4)) & 0x3f]; out += base64_enc_lookup[((buf[0] << 4) | (buf[1] >> 4)) & 0x3f];
if (size > 2) { if (size > 2) {
out += base64_enc_lookup[((buf[1] << 2) | (buf[2] >> 6)) & 0x3f]; out += base64_enc_lookup[((buf[1] << 2) | (buf[2] >> 6)) & 0x3f];
out += base64_enc_lookup[buf[2] & 0x3f]; out += base64_enc_lookup[buf[2] & 0x3f];
} else { } else {
out += base64_enc_lookup[(buf[1] << 2) & 0x3f]; out += base64_enc_lookup[(buf[1] << 2) & 0x3f];
out += '='; out += '=';
} }
} else { } else {
out += base64_enc_lookup[(buf[0] << 4) & 0x3f]; out += base64_enc_lookup[(buf[0] << 4) & 0x3f];
out += '='; out += '=';
out += '='; out += '=';
} }
} else { } else {
out += '='; out += '=';
out += '='; out += '=';
out += '='; out += '=';
out += '='; out += '=';
} }
} }
protected: protected:
uint8_t buf[3]; ///< Internal buffer uint8_t buf[3]; ///< Internal buffer
size_t num; ///< Number of bytes used in `buf` size_t num; ///< Number of bytes used in `buf`
}; };
/// \cond internal /// \cond internal
static const char base64_enc_lookup[64] = { static const char base64_enc_lookup[64] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
}; };
/// \endcond /// \endcond
/// ///
/// Base64 decoding session /// Base64 decoding session
/// ///
class base64_dec class base64_dec
{ {
public: public:
/// ///
/// Constructs blank decoding session /// Constructs blank decoding session
/// ///
base64_dec() noexcept : num(0) base64_dec() noexcept : num(0)
{ {
buf[0] = 0; buf[0] = 0;
buf[1] = 0; buf[1] = 0;
buf[2] = 0; buf[2] = 0;
buf[3] = 0; buf[3] = 0;
} }
/// ///
/// Decodes one block of information, and _appends_ it to the output /// Decodes one block of information, and _appends_ it to the output
/// ///
/// \param[out] out Output /// \param[out] out Output
/// \param[in ] is_last Was this the last block of data? /// \param[in ] is_last Was this the last block of data?
/// \param[in ] data Data to decode /// \param[in ] data Data to decode
/// \param[in ] size Length of `data` in bytes /// \param[in ] size Length of `data` in bytes
/// ///
template<class _Ty, class _Ax, class _Tchr> template<class _Ty, class _Ax, class _Tchr>
void decode(_Inout_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size) void decode(_Inout_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size)
{ {
is_last = false; is_last = false;
// Trim data size to first terminator. // Trim data size to first terminator.
for (size_t k = 0; k < size; k++) for (size_t k = 0; k < size; k++)
if (!data[k]) { size = k; break; } if (!data[k]) { size = k; break; }
// Preallocate output // Preallocate output
out.reserve(out.size() + dec_size(size)); out.reserve(out.size() + dec_size(size));
for (size_t i = 0;; i++) { for (size_t i = 0;; i++) {
if (num >= 4) { if (num >= 4) {
// Buffer full; decode it. // Buffer full; decode it.
size_t nibbles = decode(out); size_t nibbles = decode(out);
num = 0; num = 0;
if (nibbles < 3) { if (nibbles < 3) {
is_last = true; is_last = true;
break; break;
} }
} }
if (i >= size) if (i >= size)
break; break;
int x = data[i]; int x = data[i];
if ((buf[num] = x < _countof(base64_dec_lookup) ? base64_dec_lookup[x] : 255) != 255) if ((buf[num] = x < _countof(base64_dec_lookup) ? base64_dec_lookup[x] : 255) != 255)
num++; num++;
} }
} }
/// ///
/// Resets decoding session /// Resets decoding session
/// ///
void clear() noexcept void clear() noexcept
{ {
num = 0; num = 0;
} }
/// ///
/// Returns maximum decoded size /// Returns maximum decoded size
/// ///
/// \param[in] size Number of bytes to decode /// \param[in] size Number of bytes to decode
/// ///
/// \returns Maximum number of bytes for the decoded data of `size` length /// \returns Maximum number of bytes for the decoded data of `size` length
/// ///
size_t dec_size(_In_ size_t size) const noexcept size_t dec_size(_In_ size_t size) const noexcept
{ {
return ((num + size + 3)/4)*3; return ((num + size + 3)/4)*3;
} }
protected: protected:
/// ///
/// Decodes one complete internal buffer of data /// Decodes one complete internal buffer of data
/// ///
template<class _Ty, class _Ax> template<class _Ty, class _Ax>
size_t decode(_Inout_ std::vector<_Ty, _Ax> &out) size_t decode(_Inout_ std::vector<_Ty, _Ax> &out)
{ {
out.push_back((_Ty)(((buf[0] << 2) | (buf[1] >> 4)) & 0xff)); out.push_back((_Ty)(((buf[0] << 2) | (buf[1] >> 4)) & 0xff));
if (buf[2] < 64) { if (buf[2] < 64) {
out.push_back((_Ty)(((buf[1] << 4) | (buf[2] >> 2)) & 0xff)); out.push_back((_Ty)(((buf[1] << 4) | (buf[2] >> 2)) & 0xff));
if (buf[3] < 64) { if (buf[3] < 64) {
out.push_back((_Ty)(((buf[2] << 6) | buf[3]) & 0xff)); out.push_back((_Ty)(((buf[2] << 6) | buf[3]) & 0xff));
return 3; return 3;
} else } else
return 2; return 2;
} else } else
return 1; return 1;
} }
protected: protected:
uint8_t buf[4]; ///< Internal buffer uint8_t buf[4]; ///< Internal buffer
size_t num; ///< Number of bytes used in `buf` size_t num; ///< Number of bytes used in `buf`
}; };
/// \cond internal /// \cond internal
static const uint8_t base64_dec_lookup[256] = { static const uint8_t base64_dec_lookup[256] = {
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, /* 0 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
/* 1 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, /* 1 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
/* 2 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63, /* 2 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
/* 3 */ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 64, 255, 255, /* 3 */ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 64, 255, 255,
/* 4 */ 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 4 */ 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
/* 5 */ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, /* 5 */ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
/* 6 */ 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 6 */ 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
/* 7 */ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255, /* 7 */ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255,
/* 8 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, /* 8 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
/* 9 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, /* 9 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
/* A */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, /* A */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
/* B */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, /* B */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
/* C */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, /* C */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
/* D */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, /* D */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
/* E */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, /* E */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
/* F */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 /* F */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
}; };
/// \endcond /// \endcond
} }

View File

@ -1,6 +1,6 @@
/* /*
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Copyright © 2023 Amebis Copyright © 2023 Amebis
*/ */
#pragma once #pragma once
@ -11,67 +11,67 @@
namespace stdex namespace stdex
{ {
/// ///
/// Standard C runtime library error /// Standard C runtime library error
/// ///
class errno_error : public std::runtime_error class errno_error : public std::runtime_error
{ {
public: public:
/// ///
/// Constructs an exception /// Constructs an exception
/// ///
/// \param[in] num Numeric error code /// \param[in] num Numeric error code
/// \param[in] msg Error message /// \param[in] msg Error message
/// ///
errno_error(_In_ errno_t num, _In_ const std::string& msg) : errno_error(_In_ errno_t num, _In_ const std::string& msg) :
m_num(num), m_num(num),
runtime_error(msg) runtime_error(msg)
{ {
} }
/// ///
/// Constructs an exception /// Constructs an exception
/// ///
/// \param[in] num Numeric error code /// \param[in] num Numeric error code
/// \param[in] msg Error message /// \param[in] msg Error message
/// ///
errno_error(_In_ errno_t num, _In_opt_z_ const char *msg = nullptr) : errno_error(_In_ errno_t num, _In_opt_z_ const char *msg = nullptr) :
m_num(num), m_num(num),
runtime_error(msg) runtime_error(msg)
{ {
} }
/// ///
/// Constructs an exception using `GetLastError()` /// Constructs an exception using `GetLastError()`
/// ///
/// \param[in] msg Error message /// \param[in] msg Error message
/// ///
errno_error(_In_ const std::string& msg) : errno_error(_In_ const std::string& msg) :
m_num(errno), m_num(errno),
runtime_error(msg) runtime_error(msg)
{ {
} }
/// ///
/// Constructs an exception using `GetLastError()` /// Constructs an exception using `GetLastError()`
/// ///
/// \param[in] msg Error message /// \param[in] msg Error message
/// ///
errno_error(_In_opt_z_ const char *msg = nullptr) : errno_error(_In_opt_z_ const char *msg = nullptr) :
m_num(errno), m_num(errno),
runtime_error(msg) runtime_error(msg)
{ {
} }
/// ///
/// Returns the error number /// Returns the error number
/// ///
errno_t number() const errno_t number() const
{ {
return m_num; return m_num;
} }
protected: protected:
errno_t m_num; ///< Numeric error code errno_t m_num; ///< Numeric error code
}; };
} }

View File

@ -1,6 +1,6 @@
/* /*
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Copyright © 2023 Amebis Copyright © 2023 Amebis
*/ */
#pragma once #pragma once
@ -10,19 +10,19 @@
namespace stdex namespace stdex
{ {
/// ///
/// User cancelled exception /// User cancelled exception
/// ///
class user_cancelled : public std::exception class user_cancelled : public std::exception
{ {
public: public:
/// ///
/// Constructs an exception /// Constructs an exception
/// ///
/// \param[in] msg Error message /// \param[in] msg Error message
/// ///
user_cancelled(_In_opt_z_ const char *msg = nullptr) : exception(msg) user_cancelled(_In_opt_z_ const char *msg = nullptr) : exception(msg)
{ {
} }
}; };
} }

View File

@ -1,6 +1,6 @@
/* /*
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Copyright © 2016-2023 Amebis Copyright © 2016-2023 Amebis
*/ */
#pragma once #pragma once
@ -14,149 +14,149 @@
namespace stdex namespace stdex
{ {
/// ///
/// Hexadecimal encoding session /// Hexadecimal encoding session
/// ///
class hex_enc class hex_enc
{ {
public: public:
/// ///
/// Constructs blank encoding session /// Constructs blank encoding session
/// ///
hex_enc() noexcept hex_enc() noexcept
{ {
} }
/// ///
/// Encodes one block of information, and _appends_ it to the output /// Encodes one block of information, and _appends_ it to the output
/// ///
/// \param[out] out Output /// \param[out] out Output
/// \param[in ] data Data to encode /// \param[in ] data Data to encode
/// \param[in ] size Length of `data` in bytes /// \param[in ] size Length of `data` in bytes
/// ///
template<class _Elem, class _Traits, class _Ax> template<class _Elem, class _Traits, class _Ax>
void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_bytecount_(size) const void *data, _In_ size_t size) void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_bytecount_(size) const void *data, _In_ size_t size)
{ {
assert(data || !size); assert(data || !size);
// Preallocate output // Preallocate output
out.reserve(out.size() + enc_size(size)); out.reserve(out.size() + enc_size(size));
// Convert data character by character. // Convert data character by character.
for (size_t i = 0; i < size; i++) { for (size_t i = 0; i < size; i++) {
uint8_t uint8_t
x = reinterpret_cast<const uint8_t*>(data)[i], x = reinterpret_cast<const uint8_t*>(data)[i],
x_h = ((x & 0xf0) >> 4), x_h = ((x & 0xf0) >> 4),
x_l = ((x & 0x0f) ); x_l = ((x & 0x0f) );
out += x_h < 10 ? '0' + x_h : 'A' - 10 + x_h; out += x_h < 10 ? '0' + x_h : 'A' - 10 + x_h;
out += x_l < 10 ? '0' + x_l : 'A' - 10 + x_l; out += x_l < 10 ? '0' + x_l : 'A' - 10 + x_l;
} }
} }
/// ///
/// Returns maximum encoded size /// Returns maximum encoded size
/// ///
/// \param[in] size Number of bytes to encode /// \param[in] size Number of bytes to encode
/// ///
/// \returns Maximum number of bytes for the encoded data of `size` length /// \returns Maximum number of bytes for the encoded data of `size` length
/// ///
size_t enc_size(_In_ size_t size) const noexcept size_t enc_size(_In_ size_t size) const noexcept
{ {
return size*2; return size*2;
} }
}; };
/// ///
/// Hexadecimal decoding session /// Hexadecimal decoding session
/// ///
class hex_dec class hex_dec
{ {
public: public:
/// ///
/// Constructs blank decoding session /// Constructs blank decoding session
/// ///
hex_dec() noexcept : hex_dec() noexcept :
buf(0), buf(0),
num(0) num(0)
{ {
} }
/// ///
/// Decodes one block of information, and _appends_ it to the output /// Decodes one block of information, and _appends_ it to the output
/// ///
/// \param[inout] out Output /// \param[inout] out Output
/// \param[out ] is_last Was this the last block of data? Actually, is this block of data complete? /// \param[out ] is_last Was this the last block of data? Actually, is this block of data complete?
/// \param[in ] data Data to decode /// \param[in ] data Data to decode
/// \param[in ] size Length of `data` in bytes /// \param[in ] size Length of `data` in bytes
/// ///
template<class _Ty, class _Ax, class _Tchr> template<class _Ty, class _Ax, class _Tchr>
void decode(_Inout_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size) void decode(_Inout_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size)
{ {
is_last = false; is_last = false;
// Trim data size to first terminator. // Trim data size to first terminator.
for (size_t k = 0; k < size; k++) for (size_t k = 0; k < size; k++)
if (!data[k]) { size = k; break; } if (!data[k]) { size = k; break; }
// Preallocate output // Preallocate output
out.reserve(out.size() + dec_size(size)); out.reserve(out.size() + dec_size(size));
for (size_t i = 0;; i++) { for (size_t i = 0;; i++) {
if (num >= 2) { if (num >= 2) {
// Buffer full. // Buffer full.
out.push_back(buf); out.push_back(buf);
num = 0; num = 0;
is_last = true; is_last = true;
} else } else
is_last = false; is_last = false;
if (i >= size) if (i >= size)
break; break;
int x = data[i]; int x = data[i];
if ('0' <= x && x <= '9') { if ('0' <= x && x <= '9') {
buf = ((buf & 0xf) << 4) | (uint8_t)(x - '0'); buf = ((buf & 0xf) << 4) | (uint8_t)(x - '0');
num++; num++;
} else if ('A' <= x && x <= 'F') { } else if ('A' <= x && x <= 'F') {
buf = ((buf & 0xf) << 4) | (uint8_t)(x - ('A' - 10)); buf = ((buf & 0xf) << 4) | (uint8_t)(x - ('A' - 10));
num++; num++;
} else if ('a' <= x && x <= 'f') { } else if ('a' <= x && x <= 'f') {
buf = ((buf & 0xf) << 4) | (uint8_t)(x - ('a' - 10)); buf = ((buf & 0xf) << 4) | (uint8_t)(x - ('a' - 10));
num++; num++;
} }
} }
} }
/// ///
/// Resets decoding session /// Resets decoding session
/// ///
void clear() noexcept void clear() noexcept
{ {
num = 0; num = 0;
} }
/// ///
/// Returns maximum decoded size /// Returns maximum decoded size
/// ///
/// \param[in] size Number of bytes to decode /// \param[in] size Number of bytes to decode
/// ///
/// \returns Maximum number of bytes for the decoded data of `size` length /// \returns Maximum number of bytes for the decoded data of `size` length
/// ///
size_t dec_size(_In_ size_t size) const noexcept size_t dec_size(_In_ size_t size) const noexcept
{ {
return (size + 1)/2; return (size + 1)/2;
} }
protected: protected:
uint8_t buf; ///< Internal buffer uint8_t buf; ///< Internal buffer
size_t num; ///< Number of nibbles used in `buf` size_t num; ///< Number of nibbles used in `buf`
}; };
} }

View File

@ -1,6 +1,6 @@
/* /*
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Copyright © 2016-2023 Amebis Copyright © 2016-2023 Amebis
*/ */
#pragma once #pragma once
@ -12,237 +12,237 @@
namespace stdex { namespace stdex {
namespace idrec { namespace idrec {
/// ///
/// Reads record ID /// Reads record ID
/// ///
/// \param[in] stream Input stream /// \param[in] stream Input stream
/// \param[out] id Record ID /// \param[out] id Record ID
/// \param[in] end Position limit. Default is -1 (no limit). /// \param[in] end Position limit. Default is -1 (no limit).
/// ///
/// \returns /// \returns
/// - \c true when succeeded /// - \c true when succeeded
/// - \c false otherwise /// - \c false otherwise
/// ///
template <class T_ID> template <class T_ID>
_Success_(return) bool read_id(_In_ std::istream& stream, _Out_ T_ID &id, _In_opt_ std::streamoff end = (std::streamoff)-1) _Success_(return) bool read_id(_In_ std::istream& stream, _Out_ T_ID &id, _In_opt_ std::streamoff end = (std::streamoff)-1)
{ {
if (end == (std::streamoff)-1 || stream.tellg() < end) { if (end == (std::streamoff)-1 || stream.tellg() < end) {
stream.read((char*)&id, sizeof(id)); stream.read((char*)&id, sizeof(id));
return stream.good(); return stream.good();
} else } else
return false; return false;
} }
/// ///
/// Skips current record data /// Skips current record data
/// ///
/// \param[in] stream Input stream /// \param[in] stream Input stream
/// ///
/// \returns /// \returns
/// - \c true when successful /// - \c true when successful
/// - \c false otherwise /// - \c false otherwise
/// ///
template <class T_SIZE, unsigned int ALIGN> template <class T_SIZE, unsigned int ALIGN>
bool ignore(_In_ std::istream& stream) bool ignore(_In_ std::istream& stream)
{ {
// Read record size. // Read record size.
T_SIZE size; T_SIZE size;
stream.read((char*)&size, sizeof(size)); stream.read((char*)&size, sizeof(size));
if (!stream.good()) return false; if (!stream.good()) return false;
// Skip the record data. // Skip the record data.
size += (T_SIZE)(ALIGN - size) % ALIGN; size += (T_SIZE)(ALIGN - size) % ALIGN;
stream.ignore(size); stream.ignore(size);
if (!stream.good()) return false; if (!stream.good()) return false;
return true; return true;
} }
/// ///
/// Finds record data /// Finds record data
/// ///
/// \param[in] stream Input stream /// \param[in] stream Input stream
/// \param[in] id Record ID /// \param[in] id Record ID
/// \param[in] end Position limit. Default is -1 (no limit). /// \param[in] end Position limit. Default is -1 (no limit).
/// ///
/// \returns /// \returns
/// - \c true when found /// - \c true when found
/// - \c false otherwise /// - \c false otherwise
/// ///
template <class T_ID, class T_SIZE, unsigned int ALIGN> template <class T_ID, class T_SIZE, unsigned int ALIGN>
bool find(_In_ std::istream& stream, _In_ T_ID id, _In_opt_ std::streamoff end = (std::streamoff)-1) bool find(_In_ std::istream& stream, _In_ T_ID id, _In_opt_ std::streamoff end = (std::streamoff)-1)
{ {
T_ID _id; T_ID _id;
while (end == (std::streamoff)-1 || stream.tellg() < end) { while (end == (std::streamoff)-1 || stream.tellg() < end) {
stream.read((char*)&_id, sizeof(_id)); stream.read((char*)&_id, sizeof(_id));
if (!stream.good()) return false; if (!stream.good()) return false;
if (_id == id) { if (_id == id) {
// The record was found. // The record was found.
return true; return true;
} else } else
ignore<T_SIZE, ALIGN>(stream); ignore<T_SIZE, ALIGN>(stream);
} }
return false; return false;
} }
/// ///
/// Writes record header /// Writes record header
/// ///
/// \param[in] stream Output stream /// \param[in] stream Output stream
/// \param[in] id Record ID /// \param[in] id Record ID
/// ///
/// \returns Position of the record header start in \p stream. Save for later \c close call. /// \returns Position of the record header start in \p stream. Save for later \c close call.
/// ///
template <class T_ID, class T_SIZE> template <class T_ID, class T_SIZE>
std::streamoff open(_In_ std::ostream& stream, _In_ T_ID id) std::streamoff open(_In_ std::ostream& stream, _In_ T_ID id)
{ {
std::streamoff start = stream.tellp(); std::streamoff start = stream.tellp();
// Write ID. // Write ID.
if (stream.fail()) return (std::streamoff)-1; if (stream.fail()) return (std::streamoff)-1;
stream.write((const char*)&id, sizeof(id)); stream.write((const char*)&id, sizeof(id));
// Write 0 as a placeholder for data size. // Write 0 as a placeholder for data size.
if (stream.fail()) return (std::streamoff)-1; if (stream.fail()) return (std::streamoff)-1;
T_SIZE size = 0; T_SIZE size = 0;
stream.write((const char*)&size, sizeof(size)); stream.write((const char*)&size, sizeof(size));
return start; return start;
} }
/// ///
/// Updates record header /// Updates record header
/// ///
/// \param[in] stream Output stream /// \param[in] stream Output stream
/// \param[in] start Start position of the record in \p stream /// \param[in] start Start position of the record in \p stream
/// ///
/// \returns Position of the record end in \p stream /// \returns Position of the record end in \p stream
/// ///
template <class T_ID, class T_SIZE, unsigned int ALIGN> template <class T_ID, class T_SIZE, unsigned int ALIGN>
std::streamoff close(_In_ std::ostream& stream, _In_ std::streamoff start) std::streamoff close(_In_ std::ostream& stream, _In_ std::streamoff start)
{ {
std::streamoff end = stream.tellp(); std::streamoff end = stream.tellp();
T_SIZE T_SIZE
size = (T_SIZE)(end - start - sizeof(T_ID) - sizeof(T_SIZE)), size = (T_SIZE)(end - start - sizeof(T_ID) - sizeof(T_SIZE)),
remainder = (T_SIZE)(ALIGN - size) % ALIGN; // Number of bytes we need to add, to keep the data integral number of ALIGN blocks long remainder = (T_SIZE)(ALIGN - size) % ALIGN; // Number of bytes we need to add, to keep the data integral number of ALIGN blocks long
if (remainder) { if (remainder) {
// Append padding. // Append padding.
static const char padding[ALIGN] = {}; static const char padding[ALIGN] = {};
stream.write(padding, remainder); stream.write(padding, remainder);
end += remainder; end += remainder;
} }
// Update the data size. // Update the data size.
if (stream.fail()) return (std::streamoff)-1; if (stream.fail()) return (std::streamoff)-1;
stream.seekp(start + sizeof(T_ID)); stream.seekp(start + sizeof(T_ID));
stream.write((const char*)&size, sizeof(size)); stream.write((const char*)&size, sizeof(size));
stream.seekp(end); stream.seekp(end);
return end; return end;
} }
/// ///
/// Helper class for read/write of records to/from memory /// Helper class for read/write of records to/from memory
/// ///
template <class T, class T_ID, const T_ID ID, class T_SIZE, unsigned int ALIGN> template <class T, class T_ID, const T_ID ID, class T_SIZE, unsigned int ALIGN>
class record class record
{ {
public: public:
/// ///
/// Constructs the class /// Constructs the class
/// ///
/// \param[in] d Reference to record data /// \param[in] d Reference to record data
/// ///
record(_In_ T &d) : data(d) {} record(_In_ T &d) : data(d) {}
/// ///
/// Constructs the class /// Constructs the class
/// ///
/// \param[in] d Reference to record data /// \param[in] d Reference to record data
/// ///
record(_In_ const T &d) : data((T&)d) {} record(_In_ const T &d) : data((T&)d) {}
/// ///
/// Returns record id /// Returns record id
/// ///
static const T_ID id() static const T_ID id()
{ {
return ID; return ID;
} }
/// ///
/// Assignment operator /// Assignment operator
/// ///
/// \param[in] r Source record /// \param[in] r Source record
/// ///
/// \returns A const reference to this struct /// \returns A const reference to this struct
/// ///
const record<T, T_ID, ID, T_SIZE, ALIGN>& operator =(_In_ const record<T, T_ID, ID, T_SIZE, ALIGN> &r) const record<T, T_ID, ID, T_SIZE, ALIGN>& operator =(_In_ const record<T, T_ID, ID, T_SIZE, ALIGN> &r)
{ {
data = r.data; data = r.data;
return *this; return *this;
} }
/// ///
/// Writes record header /// Writes record header
/// ///
/// \param[in] stream Output stream /// \param[in] stream Output stream
/// ///
/// \returns Position of the record header start in \p stream. Save for later \c close call. /// \returns Position of the record header start in \p stream. Save for later \c close call.
/// ///
static std::streamoff open(_In_ std::ostream& stream) static std::streamoff open(_In_ std::ostream& stream)
{ {
return stdex::idrec::open<T_ID, T_SIZE>(stream, ID); return stdex::idrec::open<T_ID, T_SIZE>(stream, ID);
} }
/// ///
/// Updates record header /// Updates record header
/// ///
/// \param[in] stream Output stream /// \param[in] stream Output stream
/// \param[in] start Start position of the record in \p stream /// \param[in] start Start position of the record in \p stream
/// ///
/// \returns Position of the record end in \p stream /// \returns Position of the record end in \p stream
/// ///
static std::streamoff close(_In_ std::ostream& stream, _In_ std::streamoff start) static std::streamoff close(_In_ std::ostream& stream, _In_ std::streamoff start)
{ {
return stdex::idrec::close<T_ID, T_SIZE, ALIGN>(stream, start); return stdex::idrec::close<T_ID, T_SIZE, ALIGN>(stream, start);
} }
/// ///
/// Finds record data /// Finds record data
/// ///
/// \param[in] stream Input stream /// \param[in] stream Input stream
/// \param[in] end Position limit. Default is -1 (no limit). /// \param[in] end Position limit. Default is -1 (no limit).
/// ///
/// \returns /// \returns
/// - \c true when found /// - \c true when found
/// - \c false otherwise /// - \c false otherwise
/// ///
static bool find(_In_ std::istream& stream, _In_opt_ std::streamoff end = (std::streamoff)-1) static bool find(_In_ std::istream& stream, _In_opt_ std::streamoff end = (std::streamoff)-1)
{ {
return stdex::idrec::find<T_ID, T_SIZE, ALIGN>(stream, ID, end); return stdex::idrec::find<T_ID, T_SIZE, ALIGN>(stream, ID, end);
} }
T &data; ///< Record data reference T &data; ///< Record data reference
}; };
}; };
}; };
@ -257,14 +257,14 @@ namespace stdex {
template <class T, class T_ID, T_ID ID, class T_SIZE, unsigned int ALIGN> template <class T, class T_ID, T_ID ID, class T_SIZE, unsigned int ALIGN>
std::ostream& operator <<(_In_ std::ostream& stream, _In_ const stdex::idrec::record<T, T_ID, ID, T_SIZE, ALIGN> r) std::ostream& operator <<(_In_ std::ostream& stream, _In_ const stdex::idrec::record<T, T_ID, ID, T_SIZE, ALIGN> r)
{ {
// Parameter r does not need to be passed by reference. It has only one field (data), which is a reference itself already. // Parameter r does not need to be passed by reference. It has only one field (data), which is a reference itself already.
std::streamoff start = r.open(stream); std::streamoff start = r.open(stream);
if (stream.fail()) return stream; if (stream.fail()) return stream;
stream << r.data; stream << r.data;
r.close(stream, start); r.close(stream, start);
return stream; return stream;
} }
@ -279,19 +279,19 @@ std::ostream& operator <<(_In_ std::ostream& stream, _In_ const stdex::idrec::re
template <class T, class T_ID, T_ID ID, class T_SIZE, unsigned int ALIGN> template <class T, class T_ID, T_ID ID, class T_SIZE, unsigned int ALIGN>
std::istream& operator >>(_In_ std::istream& stream, _In_ stdex::idrec::record<T, T_ID, ID, T_SIZE, ALIGN> r) std::istream& operator >>(_In_ std::istream& stream, _In_ stdex::idrec::record<T, T_ID, ID, T_SIZE, ALIGN> r)
{ {
// Parameter r does not need to be passed by reference. It has only one field (data), which is a reference itself already. // Parameter r does not need to be passed by reference. It has only one field (data), which is a reference itself already.
// Read data size. // Read data size.
T_SIZE size; T_SIZE size;
stream.read((char*)&size, sizeof(size)); stream.read((char*)&size, sizeof(size));
if (!stream.good()) return stream; if (!stream.good()) return stream;
// Read data. // Read data.
std::streamoff start = stream.tellg(); std::streamoff start = stream.tellg();
stream >> r.data; // TODO: operator >> should not read past the record data! Make a size limited stream and read from it instead. stream >> r.data; // TODO: operator >> should not read past the record data! Make a size limited stream and read from it instead.
size += (T_SIZE)(ALIGN - size) % ALIGN; size += (T_SIZE)(ALIGN - size) % ALIGN;
stream.seekg(start + size); stream.seekg(start + size);
return stream; return stream;
} }

View File

@ -1,6 +1,6 @@
/* /*
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Copyright © 2023 Amebis Copyright © 2023 Amebis
*/ */
#pragma once #pragma once
@ -9,54 +9,54 @@
namespace stdex namespace stdex
{ {
/// ///
/// Numerical interval /// Numerical interval
/// ///
template <class T> template <class T>
struct interval struct interval
{ {
T start; ///< interval start T start; ///< interval start
T end; ///< interval end T end; ///< interval end
/// ///
/// Constructs an invalid interval /// Constructs an invalid interval
/// ///
inline interval() noexcept : start(1), end(0) {} inline interval() noexcept : start(1), end(0) {}
/// ///
/// Constructs a zero-size interval /// Constructs a zero-size interval
/// ///
/// \param[in] x Interval start and end value /// \param[in] x Interval start and end value
/// ///
inline interval(_In_ T x) noexcept : start(x), end(x) {} inline interval(_In_ T x) noexcept : start(x), end(x) {}
/// ///
/// Constructs an interval /// Constructs an interval
/// ///
/// \param[in] _start Interval start value /// \param[in] _start Interval start value
/// \param[in] _end Interval end value /// \param[in] _end Interval end value
/// ///
inline interval(_In_ T _start, _In_ T _end) noexcept : start(_start), end(_end) {} inline interval(_In_ T _start, _In_ T _end) noexcept : start(_start), end(_end) {}
/// ///
/// Returns interval size /// Returns interval size
/// ///
/// \returns Interval size or 0 if interval is invalid /// \returns Interval size or 0 if interval is invalid
/// ///
inline T size() const { return start <= end ? end - start : 0; } inline T size() const { return start <= end ? end - start : 0; }
/// ///
/// Is interval empty? /// Is interval empty?
/// ///
/// \returns true if interval is empty or false otherwise /// \returns true if interval is empty or false otherwise
/// ///
inline bool empty() const { return start >= end; } inline bool empty() const { return start >= end; }
/// ///
/// Is interval valid? /// Is interval valid?
/// ///
/// \returns true if interval is valid or false otherwise /// \returns true if interval is valid or false otherwise
/// ///
inline operator bool() const { return start <= end; } inline operator bool() const { return start <= end; }
}; };
} }

View File

@ -1,6 +1,6 @@
/* /*
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Copyright © 2023 Amebis Copyright © 2023 Amebis
*/ */
#pragma once #pragma once
@ -11,254 +11,254 @@
namespace stdex namespace stdex
{ {
/// ///
/// Progress indicator base class /// Progress indicator base class
/// ///
template <class T> template <class T>
class progress class progress
{ {
public: public:
/// ///
/// Set progress indicator text /// Set progress indicator text
/// ///
/// \param[in] msg Text to display /// \param[in] msg Text to display
/// ///
virtual void set_text(_In_z_ const char* msg) virtual void set_text(_In_z_ const char* msg)
{ {
msg; msg;
} }
/// ///
/// Set progress range extent /// Set progress range extent
/// ///
/// \param[in] start Minimum value of the progress /// \param[in] start Minimum value of the progress
/// \param[in] end Maximum value of the progress /// \param[in] end Maximum value of the progress
/// ///
virtual void set_range(_In_ T start, _In_ T end) virtual void set_range(_In_ T start, _In_ T end)
{ {
start; end; start; end;
} }
/// ///
/// Set current progress /// Set current progress
/// ///
/// \param[in] value Current value of the progress. Must be between start and end parameters provided in set_range() call. /// \param[in] value Current value of the progress. Must be between start and end parameters provided in set_range() call.
/// ///
virtual void set(_In_ T value) virtual void set(_In_ T value)
{ {
value; value;
} }
/// ///
/// Show or hide progress /// Show or hide progress
/// ///
/// \param[in] show Shows or hides progress indicator /// \param[in] show Shows or hides progress indicator
/// ///
virtual void show(_In_ bool show = true) virtual void show(_In_ bool show = true)
{ {
show; show;
} }
/// ///
/// Query whether user requested abort /// Query whether user requested abort
/// ///
virtual bool cancel() virtual bool cancel()
{ {
return false; return false;
} }
}; };
/// ///
/// Lazy progress indicator base class /// Lazy progress indicator base class
/// ///
/// Use with expensive progress reporting to suppress progress indication for a period of time. /// Use with expensive progress reporting to suppress progress indication for a period of time.
/// ///
template <class T> template <class T>
class lazy_progress : public progress<T> class lazy_progress : public progress<T>
{ {
public: public:
/// ///
/// Constructs a lazy progress indicator /// Constructs a lazy progress indicator
/// ///
/// \param[in] timeout Timeout to wait before forwarding progress /// \param[in] timeout Timeout to wait before forwarding progress
/// ///
lazy_progress(_In_ const std::chrono::nanoseconds& timeout = std::chrono::nanoseconds(500000)) : lazy_progress(_In_ const std::chrono::nanoseconds& timeout = std::chrono::nanoseconds(500000)) :
m_timeout(timeout), m_timeout(timeout),
m_start(0), m_start(0),
m_end(0), m_end(0),
m_value(-1) m_value(-1)
{} {}
/// ///
/// Set progress range extent /// Set progress range extent
/// ///
/// \param[in] start Minimum value of the progress /// \param[in] start Minimum value of the progress
/// \param[in] end Maximum value of the progress /// \param[in] end Maximum value of the progress
/// ///
virtual void set_range(_In_ T start, _In_ T end) virtual void set_range(_In_ T start, _In_ T end)
{ {
m_start = start; m_start = start;
m_end = end; m_end = end;
} }
/// ///
/// Set current progress /// Set current progress
/// ///
/// \param[in] value Current value of the progress. Must be between start and end parameters provided in set_range() call. /// \param[in] value Current value of the progress. Must be between start and end parameters provided in set_range() call.
/// ///
virtual void set(_In_ T value) virtual void set(_In_ T value)
{ {
if (value == m_start || value == m_end) if (value == m_start || value == m_end)
m_last = std::chrono::high_resolution_clock::now(); m_last = std::chrono::high_resolution_clock::now();
else if (value == m_value) else if (value == m_value)
return; return;
else { else {
auto now = std::chrono::high_resolution_clock::now(); auto now = std::chrono::high_resolution_clock::now();
if (now - m_last < m_timeout) if (now - m_last < m_timeout)
return; return;
m_last = now; m_last = now;
} }
m_value = value; m_value = value;
do_set(); do_set();
} }
protected: protected:
/// ///
/// Called when progress reporting is due. Should override this method to implement actual progress refresh. /// Called when progress reporting is due. Should override this method to implement actual progress refresh.
/// ///
virtual void do_set() {} virtual void do_set() {}
protected: protected:
std::chrono::nanoseconds m_timeout; std::chrono::nanoseconds m_timeout;
std::chrono::steady_clock::time_point m_last; std::chrono::steady_clock::time_point m_last;
T m_start, m_end, m_value; T m_start, m_end, m_value;
}; };
/// ///
/// Global progress indicator base class /// Global progress indicator base class
/// ///
/// Use to report progress of a phase or section as a part of a whole progress. /// Use to report progress of a phase or section as a part of a whole progress.
/// ///
template <class T> template <class T>
class global_progress : public progress<T> class global_progress : public progress<T>
{ {
public: public:
/// ///
/// Constructs a progress indicator /// Constructs a progress indicator
/// ///
/// \param[in] host Host progress indicator /// \param[in] host Host progress indicator
/// ///
global_progress(_In_opt_ progress<T>* host = NULL) : m_host(host) global_progress(_In_opt_ progress<T>* host = NULL) : m_host(host)
{} {}
/// ///
/// Attach to a host progress indicator /// Attach to a host progress indicator
/// ///
/// \param[in] host Host progress indicator /// \param[in] host Host progress indicator
/// ///
inline void attach(_In_opt_ progress<T>* host) inline void attach(_In_opt_ progress<T>* host)
{ {
m_host = host; m_host = host;
} }
/// ///
/// Detach host progress indicator /// Detach host progress indicator
/// ///
/// \returns Old host progress indicator /// \returns Old host progress indicator
/// ///
inline progress<T>* detach() inline progress<T>* detach()
{ {
progress* k = m_host; progress* k = m_host;
m_host = NULL; m_host = NULL;
return k; return k;
} }
/// ///
/// Set global extend of the progress indicator /// Set global extend of the progress indicator
/// ///
/// \param[in] start Minimum value of the progress /// \param[in] start Minimum value of the progress
/// \param[in] end Maximum value of the progress /// \param[in] end Maximum value of the progress
/// ///
inline void set_global_range(_In_ T start, _In_ T end) inline void set_global_range(_In_ T start, _In_ T end)
{ {
m_glob.start = start; m_glob.start = start;
m_glob.end = end; m_glob.end = end;
if (m_host) if (m_host)
m_host->set_range(m_glob.start, m_glob.end); m_host->set_range(m_glob.start, m_glob.end);
} }
/// ///
/// Set section extend of the progress indicator /// Set section extend of the progress indicator
/// ///
/// \param[in] start Minimum value of the progress /// \param[in] start Minimum value of the progress
/// \param[in] end Maximum value of the progress /// \param[in] end Maximum value of the progress
/// ///
inline void set_section_range(_In_ T start, _In_ T end) inline void set_section_range(_In_ T start, _In_ T end)
{ {
m_odsek.start = start; m_odsek.start = start;
m_odsek.end = end; m_odsek.end = end;
} }
/// ///
/// Set progress indicator text /// Set progress indicator text
/// ///
/// \param[in] msg Text to display /// \param[in] msg Text to display
/// ///
virtual void set_text(_In_ const char* msg) virtual void set_text(_In_ const char* msg)
{ {
if (m_host) if (m_host)
m_host->set_text(msg); m_host->set_text(msg);
} }
/// ///
/// Set local extend of the progress indicator /// Set local extend of the progress indicator
/// ///
/// \param[in] start Minimum value of the progress /// \param[in] start Minimum value of the progress
/// \param[in] end Maximum value of the progress /// \param[in] end Maximum value of the progress
/// ///
virtual void set_range(_In_ T start, _In_ T end) virtual void set_range(_In_ T start, _In_ T end)
{ {
m_kaz.start = start; m_kaz.start = start;
m_kaz.end = end; m_kaz.end = end;
} }
/// ///
/// Set local current progress /// Set local current progress
/// ///
/// \param[in] value Current value of the progress. Must be between start and end parameters provided in set_range() call. /// \param[in] value Current value of the progress. Must be between start and end parameters provided in set_range() call.
/// ///
virtual void set(_In_ T value) virtual void set(_In_ T value)
{ {
if (m_host) { if (m_host) {
T dolzina = m_kaz.size(); T dolzina = m_kaz.size();
if (dolzina != 0) { if (dolzina != 0) {
// TODO: Implement with muldiv. // TODO: Implement with muldiv.
m_host->set(((value - m_kaz.start) * m_odsek.size() / dolzina) + m_odsek.start); m_host->set(((value - m_kaz.start) * m_odsek.size() / dolzina) + m_odsek.start);
} }
} }
} }
/// ///
/// Show or hide progress /// Show or hide progress
/// ///
/// \param[in] show Shows or hides progress indicator /// \param[in] show Shows or hides progress indicator
/// ///
virtual void show(_In_ bool show = true) virtual void show(_In_ bool show = true)
{ {
if (m_host) if (m_host)
m_host->show(show); m_host->show(show);
} }
/// ///
/// Query whether user requested abort /// Query whether user requested abort
/// ///
virtual bool cancel() virtual bool cancel()
{ {
return m_host && m_host->cancel(); return m_host && m_host->cancel();
} }
protected: protected:
progress* m_host; progress* m_host;
interval<T> m_kaz, m_glob, m_odsek; interval<T> m_kaz, m_glob, m_odsek;
}; };
} }

View File

@ -1,6 +1,6 @@
/* /*
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Copyright © 2022-2023 Amebis Copyright © 2022-2023 Amebis
*/ */
#pragma once #pragma once

View File

@ -1,6 +1,6 @@
/* /*
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Copyright © 2016-2023 Amebis Copyright © 2016-2023 Amebis
*/ */
#pragma once #pragma once
@ -9,408 +9,408 @@
namespace stdex namespace stdex
{ {
/// ///
/// Helper class to allow limited size FIFO queues implemented as vector of elements /// Helper class to allow limited size FIFO queues implemented as vector of elements
/// ///
template <class T> template <class T>
class vector_queue class vector_queue
{ {
public: public:
/// ///
/// Type to measure element count and indices in /// Type to measure element count and indices in
/// ///
typedef size_t size_type; typedef size_t size_type;
/// ///
/// Element type /// Element type
/// ///
typedef T value_type; typedef T value_type;
/// ///
/// Reference to element type /// Reference to element type
/// ///
typedef T& reference; typedef T& reference;
/// ///
/// Constant reference to element type /// Constant reference to element type
/// ///
typedef const T& const_reference; typedef const T& const_reference;
/// ///
/// Pointer to element /// Pointer to element
/// ///
typedef T* pointer; typedef T* pointer;
/// ///
/// Constant pointer to element /// Constant pointer to element
/// ///
typedef const T* const_pointer; typedef const T* const_pointer;
public: public:
/// ///
/// Construct queue of fixed size. /// Construct queue of fixed size.
/// ///
/// \param[in] size_max Maximum number of elements. Please note this cannot be changed later. /// \param[in] size_max Maximum number of elements. Please note this cannot be changed later.
/// ///
vector_queue(_In_ size_type size_max) : vector_queue(_In_ size_type size_max) :
m_data(new value_type[size_max]), m_data(new value_type[size_max]),
m_head(0), m_head(0),
m_count(0), m_count(0),
m_size_max(size_max) m_size_max(size_max)
{ {
} }
/// ///
/// Copies existing queue. /// Copies existing queue.
/// ///
/// \param[in] other Queue to copy from /// \param[in] other Queue to copy from
/// ///
vector_queue(_In_ const vector_queue<value_type> &other) : vector_queue(_In_ const vector_queue<value_type> &other) :
m_data(new value_type[other.m_size_max]), m_data(new value_type[other.m_size_max]),
m_head(other.m_head), m_head(other.m_head),
m_count(other.m_count), m_count(other.m_count),
m_size_max(other.m_size_max) m_size_max(other.m_size_max)
{ {
// Copy elements. // Copy elements.
for (size_type i = 0; i < m_count; i++) { for (size_type i = 0; i < m_count; i++) {
size_type i_l = abs(i); size_type i_l = abs(i);
m_data[i_l] = other.m_data[i_l]; m_data[i_l] = other.m_data[i_l];
} }
} }
/// ///
/// Destroys the queue /// Destroys the queue
/// ///
virtual ~vector_queue() virtual ~vector_queue()
{ {
if (m_data) delete [] m_data; if (m_data) delete [] m_data;
} }
/// ///
/// Moves existing queue. /// Moves existing queue.
/// ///
/// \param[inout] other Queue to move /// \param[inout] other Queue to move
/// ///
vector_queue(_Inout_ vector_queue<value_type> &&other) : vector_queue(_Inout_ vector_queue<value_type> &&other) :
m_data (std::move(other.m_data )), m_data (std::move(other.m_data )),
m_head (std::move(other.m_head )), m_head (std::move(other.m_head )),
m_count (std::move(other.m_count )), m_count (std::move(other.m_count )),
m_size_max(std::move(other.m_size_max)) m_size_max(std::move(other.m_size_max))
{ {
// Reset other to consistent state. // Reset other to consistent state.
other.m_data = NULL; other.m_data = NULL;
other.m_head = 0; other.m_head = 0;
other.m_count = 0; other.m_count = 0;
other.m_size_max = 0; other.m_size_max = 0;
} }
/// ///
/// Copies existing queue. /// Copies existing queue.
/// ///
/// \param[in] other Queue to copy from /// \param[in] other Queue to copy from
/// ///
vector_queue<value_type>& operator=(_In_ const vector_queue<value_type> &other) vector_queue<value_type>& operator=(_In_ const vector_queue<value_type> &other)
{ {
if (this != std::addressof(other)) { if (this != std::addressof(other)) {
m_head = other.m_head; m_head = other.m_head;
m_count = other.m_count; m_count = other.m_count;
m_size_max = other.m_size_max; m_size_max = other.m_size_max;
// Copy elements. // Copy elements.
if (m_data) delete [] m_data; if (m_data) delete [] m_data;
m_data = new value_type[other.m_size_max]; m_data = new value_type[other.m_size_max];
for (size_type i = 0; i < m_count; i++) { for (size_type i = 0; i < m_count; i++) {
size_type i_l = abs(i); size_type i_l = abs(i);
m_data[i_l] = other.m_data[i_l]; m_data[i_l] = other.m_data[i_l];
} }
} }
return *this; return *this;
} }
/// ///
/// Moves existing queue. /// Moves existing queue.
/// ///
/// \param[inout] other Queue to move /// \param[inout] other Queue to move
/// ///
vector_queue<value_type>& operator=(_Inout_ vector_queue<value_type> &&other) vector_queue<value_type>& operator=(_Inout_ vector_queue<value_type> &&other)
{ {
if (this != std::addressof(other)) { if (this != std::addressof(other)) {
m_data = std::move(other.m_data ); m_data = std::move(other.m_data );
m_head = std::move(other.m_head ); m_head = std::move(other.m_head );
m_count = std::move(other.m_count ); m_count = std::move(other.m_count );
m_size_max = std::move(other.m_size_max); m_size_max = std::move(other.m_size_max);
// Reset other to consistent state. // Reset other to consistent state.
other.m_data = NULL; other.m_data = NULL;
other.m_head = 0; other.m_head = 0;
other.m_count = 0; other.m_count = 0;
other.m_size_max = 0; other.m_size_max = 0;
} }
return *this; return *this;
} }
/// ///
/// Returns the number of elements in the vector. /// Returns the number of elements in the vector.
/// ///
size_type size() const size_type size() const
{ {
return m_count; return m_count;
} }
/// ///
/// Returns the number of elements that the queue can contain before overwriting head ones. /// Returns the number of elements that the queue can contain before overwriting head ones.
/// ///
size_type capacity() const size_type capacity() const
{ {
return m_size_max; return m_size_max;
} }
/// ///
/// Erases the elements of the queue. /// Erases the elements of the queue.
/// ///
void clear() void clear()
{ {
m_count = 0; m_count = 0;
} }
/// ///
/// Tests if the queue is empty. /// Tests if the queue is empty.
/// ///
bool empty() const bool empty() const
{ {
return m_count == 0; return m_count == 0;
} }
/// ///
/// Returns a reference to the element at a specified location in the queue. /// Returns a reference to the element at a specified location in the queue.
/// ///
/// \param[in] pos The subscript or position number of the element to reference in the queue. /// \param[in] pos The subscript or position number of the element to reference in the queue.
/// ///
reference at(_In_ size_type pos) reference at(_In_ size_type pos)
{ {
if (pos >= m_count) throw std::invalid_argument("Invalid subscript"); if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
return m_data[abs(pos)]; return m_data[abs(pos)];
} }
/// ///
/// Returns a reference to the element at a specified location in the queue. /// Returns a reference to the element at a specified location in the queue.
/// ///
/// \param[in] pos The subscript or position number of the element to reference in the queue. /// \param[in] pos The subscript or position number of the element to reference in the queue.
/// ///
reference operator[](_In_ size_type pos) reference operator[](_In_ size_type pos)
{ {
if (pos >= m_count) throw std::invalid_argument("Invalid subscript"); if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
return m_data[abs(pos)]; return m_data[abs(pos)];
} }
/// ///
/// Returns a constant reference to the element at a specified location in the queue. /// Returns a constant reference to the element at a specified location in the queue.
/// ///
/// \param[in] pos The subscript or position number of the element to reference in the queue. /// \param[in] pos The subscript or position number of the element to reference in the queue.
/// ///
const_reference at(_In_ size_type pos) const const_reference at(_In_ size_type pos) const
{ {
if (pos >= m_count) throw std::invalid_argument("Invalid subscript"); if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
return m_data[abs(pos)]; return m_data[abs(pos)];
} }
/// ///
/// Returns a constant reference to the element at a specified location in the queue. /// Returns a constant reference to the element at a specified location in the queue.
/// ///
/// \param[in] pos The subscript or position number of the element to reference in the queue. /// \param[in] pos The subscript or position number of the element to reference in the queue.
/// ///
const_reference operator[](_In_ size_type pos) const const_reference operator[](_In_ size_type pos) const
{ {
if (pos >= m_count) throw std::invalid_argument("Invalid subscript"); if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
return m_data[abs(pos)]; return m_data[abs(pos)];
} }
/// ///
/// Returns a reference to the element at the absolute location in the queue. /// Returns a reference to the element at the absolute location in the queue.
/// ///
/// \note Absolute means "measured from the beginning of the storage". /// \note Absolute means "measured from the beginning of the storage".
/// ///
/// \param[in] pos The absolute subscript or position number of the element to reference in the queue. /// \param[in] pos The absolute subscript or position number of the element to reference in the queue.
/// ///
reference at_abs(_In_ size_type pos) reference at_abs(_In_ size_type pos)
{ {
if (pos >= m_size_max) throw std::invalid_argument("Invalid subscript"); if (pos >= m_size_max) throw std::invalid_argument("Invalid subscript");
return m_data[pos]; return m_data[pos];
} }
/// ///
/// Returns a constant reference to the element at the absolute location in the queue: measured from the beginning of the storage. /// Returns a constant reference to the element at the absolute location in the queue: measured from the beginning of the storage.
/// ///
/// \note Absolute means "measured from the beginning of the storage". /// \note Absolute means "measured from the beginning of the storage".
/// ///
/// \param[in] pos The absolute subscript or position number of the element to reference in the queue. /// \param[in] pos The absolute subscript or position number of the element to reference in the queue.
/// ///
const_reference at_abs(_In_ size_type pos) const const_reference at_abs(_In_ size_type pos) const
{ {
if (pos >= m_size_max) throw std::invalid_argument("Invalid subscript"); if (pos >= m_size_max) throw std::invalid_argument("Invalid subscript");
return m_data[pos]; return m_data[pos];
} }
/// ///
/// Copies an existing element to the end of the queue, overriding the first one when queue is out of space. /// Copies an existing element to the end of the queue, overriding the first one when queue is out of space.
/// ///
/// \param[in] v Element to copy to the end of the queue. /// \param[in] v Element to copy to the end of the queue.
/// ///
/// \returns The absolute subscript or position number the element was copied to. /// \returns The absolute subscript or position number the element was copied to.
/// ///
size_type push_back(_In_ const value_type &v) size_type push_back(_In_ const value_type &v)
{ {
if (m_count < m_size_max) { if (m_count < m_size_max) {
size_type pos = abs(m_count); size_type pos = abs(m_count);
m_data[pos] = v; m_data[pos] = v;
m_count++; m_count++;
return pos; return pos;
} else { } else {
size_type pos = m_head; size_type pos = m_head;
m_data[pos] = v; m_data[pos] = v;
m_head = abs(1); m_head = abs(1);
return pos; return pos;
} }
} }
/// ///
/// Moves the element to the end of the queue, overriding the first one when queue is out of space. /// Moves the element to the end of the queue, overriding the first one when queue is out of space.
/// ///
/// \param[in] v Element to move to the end of the queue. /// \param[in] v Element to move to the end of the queue.
/// ///
/// \returns The absolute subscript or position number the element was moved to. /// \returns The absolute subscript or position number the element was moved to.
/// ///
size_type push_back(_Inout_ value_type&&v) size_type push_back(_Inout_ value_type&&v)
{ {
if (m_count < m_size_max) { if (m_count < m_size_max) {
size_type pos = abs(m_count); size_type pos = abs(m_count);
m_data[pos] = std::move(v); m_data[pos] = std::move(v);
m_count++; m_count++;
return pos; return pos;
} else { } else {
size_type pos = m_head; size_type pos = m_head;
m_data[pos] = std::move(v); m_data[pos] = std::move(v);
m_head = abs(1); m_head = abs(1);
return pos; return pos;
} }
} }
/// ///
/// Removes (dequeues) the last element of the queue. /// Removes (dequeues) the last element of the queue.
/// ///
void pop_back() void pop_back()
{ {
if (!m_count) throw std::invalid_argument("Empty storage"); if (!m_count) throw std::invalid_argument("Empty storage");
m_count--; m_count--;
} }
/// ///
/// Copies an existing element to the head of the queue, overriding the last one when queue is out of space and moving all others one place right. /// Copies an existing element to the head of the queue, overriding the last one when queue is out of space and moving all others one place right.
/// ///
/// \param[in] v Element to copy to the head of the queue. /// \param[in] v Element to copy to the head of the queue.
/// ///
/// \returns The absolute subscript or position number the element was copied to. /// \returns The absolute subscript or position number the element was copied to.
/// ///
size_type push_front(_In_ const value_type &v) size_type push_front(_In_ const value_type &v)
{ {
m_head = abs(-1); m_head = abs(-1);
if (m_count < m_size_max) if (m_count < m_size_max)
m_count++; m_count++;
m_data[m_head] = v; m_data[m_head] = v;
return m_head; return m_head;
} }
/// ///
/// Moves the element to the head of the queue, overriding the last one when queue is out of space and moving all others one place right. /// Moves the element to the head of the queue, overriding the last one when queue is out of space and moving all others one place right.
/// ///
/// \param[in] v Element to move to the head of the queue. /// \param[in] v Element to move to the head of the queue.
/// ///
/// \returns The absolute subscript or position number the element was moved to. /// \returns The absolute subscript or position number the element was moved to.
/// ///
size_type push_front(_Inout_ value_type&&v) size_type push_front(_Inout_ value_type&&v)
{ {
m_head = abs(-1); m_head = abs(-1);
if (m_count < m_size_max) if (m_count < m_size_max)
m_count++; m_count++;
m_data[m_head] = std::move(v); m_data[m_head] = std::move(v);
return m_head; return m_head;
} }
/// ///
/// Removes (dequeues) the head element of the queue. /// Removes (dequeues) the head element of the queue.
/// ///
void pop_front() void pop_front()
{ {
if (!m_count) throw std::invalid_argument("Empty storage"); if (!m_count) throw std::invalid_argument("Empty storage");
m_head = abs(1); m_head = abs(1);
m_count--; m_count--;
} }
/// ///
/// Returns a reference to the head element in the queue. /// Returns a reference to the head element in the queue.
/// ///
reference front() reference front()
{ {
if (!m_count) throw std::invalid_argument("Empty storage"); if (!m_count) throw std::invalid_argument("Empty storage");
return m_data[m_head]; return m_data[m_head];
} }
/// ///
/// Returns a constant reference to the head element in the queue. /// Returns a constant reference to the head element in the queue.
/// ///
const_reference front() const const_reference front() const
{ {
if (!m_count) throw std::invalid_argument("Empty storage"); if (!m_count) throw std::invalid_argument("Empty storage");
return m_data[m_head]; return m_data[m_head];
} }
/// ///
/// Returns a reference to the last element in the queue. /// Returns a reference to the last element in the queue.
/// ///
reference back() reference back()
{ {
return m_data[tail()]; return m_data[tail()];
} }
/// ///
/// Returns a constant reference to the last element in the queue. /// Returns a constant reference to the last element in the queue.
/// ///
const_reference back() const const_reference back() const
{ {
return m_data[tail()]; return m_data[tail()];
} }
/// ///
/// Returns absolute subscript or position number of the head element in the queue. The element does not need to exist. /// Returns absolute subscript or position number of the head element in the queue. The element does not need to exist.
/// ///
size_type head() const size_type head() const
{ {
return m_head; return m_head;
} }
/// ///
/// Returns absolute subscript or position number of the last element in the queue. The element must exist. /// Returns absolute subscript or position number of the last element in the queue. The element must exist.
/// ///
size_type tail() const size_type tail() const
{ {
if (!m_count) throw std::invalid_argument("Empty storage"); if (!m_count) throw std::invalid_argument("Empty storage");
return abs(m_count - 1); return abs(m_count - 1);
} }
/// ///
/// Returns absolute subscript or position number of the given element in the queue. /// Returns absolute subscript or position number of the given element in the queue.
/// ///
size_type abs(_In_ size_type pos) const size_type abs(_In_ size_type pos) const
{ {
return (m_head + pos) % m_size_max; return (m_head + pos) % m_size_max;
} }
protected: protected:
value_type *m_data; ///< Underlying data container value_type *m_data; ///< Underlying data container
size_type m_head; ///< Index of the first element size_type m_head; ///< Index of the first element
size_type m_count; ///< Number of elements size_type m_count; ///< Number of elements
size_type m_size_max; ///< Maximum size size_type m_size_max; ///< Maximum size
}; };
} }