Some checks are pending
BlockStorage/repertory/pipeline/head Build queued...
116 lines
4.0 KiB
C++
116 lines
4.0 KiB
C++
/*
|
|
Copyright <2018-2024> <scott.e.graves@protonmail.com>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/
|
|
#ifndef REPERTORY_INCLUDE_UTILS_COMMON_HPP_
|
|
#define REPERTORY_INCLUDE_UTILS_COMMON_HPP_
|
|
|
|
#include "utils/config.hpp"
|
|
|
|
namespace repertory::utils {
|
|
struct result final {
|
|
std::string_view function_name;
|
|
bool ok{false};
|
|
std::string reason{};
|
|
|
|
[[nodiscard]] operator bool() const { return ok; }
|
|
};
|
|
|
|
[[nodiscard]] inline auto
|
|
calculate_read_size(std::uint64_t total_size, std::size_t read_size,
|
|
std::uint64_t offset) -> std::size_t {
|
|
return static_cast<std::size_t>(
|
|
((offset + read_size) > total_size)
|
|
? ((offset < total_size) ? total_size - offset : 0U)
|
|
: read_size);
|
|
}
|
|
|
|
[[nodiscard]] auto
|
|
compare_version_strings(std::string version1,
|
|
std::string version2) -> std::int32_t;
|
|
|
|
[[nodiscard]] auto
|
|
compare_version_strings(std::wstring_view version1,
|
|
std::wstring_view version2) -> std::int32_t;
|
|
|
|
#if defined(PROJECT_ENABLE_STDUUID)
|
|
[[nodiscard]] auto create_uuid_string() -> std::string;
|
|
|
|
[[nodiscard]] auto create_uuid_wstring() -> std::wstring;
|
|
#endif // defined(PROJECT_ENABLE_STDUUID)
|
|
|
|
#if defined(PROJECT_ENABLE_LIBSODIUM)
|
|
template <typename data_type>
|
|
[[nodiscard]] inline auto generate_random() -> data_type;
|
|
|
|
template <typename data_type>
|
|
[[nodiscard]] inline auto
|
|
generate_random_between(const data_type &begin,
|
|
const data_type &end) -> data_type;
|
|
|
|
[[nodiscard]] auto generate_random_string(std::uint16_t length) -> std::string;
|
|
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
|
|
|
|
[[nodiscard]] auto
|
|
get_environment_variable(std::string_view variable) -> std::string;
|
|
|
|
[[nodiscard]] auto
|
|
get_environment_variable(std::wstring_view variable) -> std::wstring;
|
|
|
|
#if defined(PROJECT_ENABLE_BOOST)
|
|
[[nodiscard]] auto
|
|
get_next_available_port(std::uint16_t first_port,
|
|
std::uint16_t &available_port) -> bool;
|
|
#endif // defined(PROJECT_ENABLE_BOOST)
|
|
|
|
[[nodiscard]] auto resolve_variables(std::string str) -> std::string;
|
|
|
|
[[nodiscard]] auto resolve_variables(std::wstring_view str) -> std::wstring;
|
|
|
|
template <typename result_t, typename data_t>
|
|
[[nodiscard]] inline constexpr auto
|
|
divide_with_ceiling(result_t numerator, data_t denominator) -> result_t {
|
|
static_assert(std::is_integral_v<std::remove_cv_t<data_t>>,
|
|
"denominator must be an integral type");
|
|
|
|
return denominator == 0
|
|
? 0
|
|
: (numerator / denominator) + (numerator % denominator != 0);
|
|
}
|
|
|
|
#if defined(PROJECT_ENABLE_LIBSODIUM)
|
|
template <typename data_type>
|
|
[[nodiscard]] inline auto generate_random() -> data_type {
|
|
data_type ret{};
|
|
randombytes_buf(&ret, sizeof(ret));
|
|
return ret;
|
|
}
|
|
|
|
template <typename data_type>
|
|
[[nodiscard]] inline auto
|
|
generate_random_between(const data_type &begin,
|
|
const data_type &end) -> data_type {
|
|
return begin + generate_random<data_type>() % ((end + data_type{1}) - begin);
|
|
}
|
|
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
|
|
} // namespace repertory::utils
|
|
|
|
#endif // REPERTORY_INCLUDE_UTILS_COMMON_HPP_
|