srs-control 0.2.2
Loading...
Searching...
No Matches
CommonFunctions.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "EnumConvertFunctions.hpp" // IWYU pragma: export
4#include "srs/utils/CommonAlias.hpp" // IWYU pragma: keep
6#include <asio/any_io_executor.hpp>
7#include <asio/use_awaitable.hpp>
8#include <asio/use_future.hpp>
9#include <bit>
10#include <bitset>
11#include <cstddef>
12#include <cstdint>
13#include <cstdlib>
14#include <expected>
15#include <filesystem>
16#include <fmt/format.h>
17#include <format>
18#include <magic_enum/magic_enum.hpp>
19#include <string>
20#include <string_view>
21#include <type_traits>
22
23namespace srs::common
24{
25 // subbits from a half open range [min, max)
26 template <std::size_t bit_size, std::size_t max, std::size_t min = 0>
27 constexpr auto subset(const std::bitset<bit_size>& bits) -> std::bitset<max - min>
28 {
29 constexpr auto max_size = 64;
30 static_assert(max > min);
31 static_assert(max_size >= (max - min));
32 constexpr auto ignore_high = bit_size - max;
33
34 auto new_bits = (bits << ignore_high) >> (ignore_high + min);
35 return std::bitset<max - min>{ new_bits.to_ullong() };
36 }
37
38 template <std::size_t high_size, std::size_t low_size>
39 constexpr auto merge_bits(const std::bitset<high_size>& high_bits, const std::bitset<low_size>& low_bits)
40 -> std::bitset<high_size + low_size>
41 {
42 using NewBit = std::bitset<high_size + low_size>;
43 constexpr auto max_size = 64;
44 static_assert(max_size >= high_size + low_size);
45
46 auto high_bits_part = NewBit(high_bits.to_ullong());
47 auto low_bits_part = NewBit(low_bits.to_ullong());
48 auto new_bits = (high_bits_part << low_size) | low_bits_part;
49 return std::bitset<high_size + low_size>(new_bits.to_ullong());
50 }
51
52 template <std::size_t low_size, std::size_t total_size>
53 requires(total_size <= sizeof(std::uint64_t) * common::BYTE_BIT_LENGTH and total_size >= low_size)
54 constexpr auto get_low_bits(const std::bitset<total_size>& bits) -> std::bitset<low_size>
55 {
56 constexpr auto high_size = total_size - low_size;
57 const auto low_bits = std::bitset<low_size>{ ((bits << high_size) >> high_size).to_ullong() };
58 return low_bits;
59 }
60
61 template <std::size_t high_size, std::size_t total_size>
62 requires(total_size <= sizeof(std::uint64_t) * common::BYTE_BIT_LENGTH and total_size >= high_size)
63 constexpr auto get_high_bits(const std::bitset<total_size>& bits) -> std::bitset<high_size>
64 {
65 constexpr auto low_size = total_size - high_size;
66 const auto high_bits = std::bitset<high_size>{ (bits >> low_size).to_ullong() };
67 return high_bits;
68 }
69
70 template <std::size_t bit_size>
71 constexpr auto byte_swap(const std::bitset<bit_size>& bits)
72 {
73 auto val = bits.to_ullong();
74 val = val << ((sizeof(uint64_t) * common::BYTE_BIT_LENGTH) - bit_size);
75 val = std::byteswap(val);
76 return std::bitset<bit_size>(val);
77 }
78
79 template <typename T>
80 constexpr auto gray_to_binary(T gray_val)
81 {
82 auto bin_val = T{ gray_val };
83 while (gray_val > 0)
84 {
85 gray_val >>= 1U;
86 bin_val ^= gray_val;
87 }
88 return bin_val;
89 }
90
91 template <typename T>
92 constexpr auto binary_to_gray(T bin_val)
93 {
94 return bin_val ^ (bin_val >> 1U);
95 }
96
97 constexpr auto get_shared_from_this(auto&& obj)
98 {
99 return std::static_pointer_cast<std::remove_cvref_t<decltype(obj)>>(obj.shared_from_this());
100 }
101
102 template <typename Enum>
103 consteval auto get_enum_names()
104 {
105 auto names = magic_enum::enum_names<srs::common::ActionMode>();
106 return names;
107 }
108
109 constexpr auto insert_index_to_filename(std::string_view native_name, int idx) -> std::string
110 {
111 auto filepath = std::filesystem::path{ native_name };
112 auto extension = filepath.extension().string();
113 auto file_basename = filepath.replace_extension().string();
114 return std::format("{}_{}{}", file_basename, idx, extension);
115 }
116
117 inline auto get_default_log_path() -> std::filesystem::path
118 {
119 using path = std::filesystem::path;
120 const auto* xdg_state_home = std::getenv("XDG_STATE_HOME");
121 const auto state_dir =
122 xdg_state_home == nullptr ? path{ std::getenv("HOME") } / path{ ".local/state" } : path{ xdg_state_home };
123 return state_dir / path{ DEFAULT_LOG_FILE };
124 }
125
126 inline auto get_default_config_path() -> std::filesystem::path
127 {
128 using path = std::filesystem::path;
129 const auto* xdg_state_home = std::getenv("XDG_CONFIG_HOME");
130 const auto state_dir =
131 xdg_state_home == nullptr ? path{ std::getenv("HOME") } / path{ ".config" } : path{ xdg_state_home };
132 return state_dir / path{ DEFAULT_CONFIG_FILE };
133 }
134
135 inline auto get_file_size(std::string_view filename) -> std::expected<std::size_t, std::string>
136 {
137 namespace fs = std::filesystem;
138 auto file_path = fs::path{ filename };
139 if (fs::exists(file_path))
140 {
141 return std::filesystem::file_size(file_path);
142 }
143 return std::unexpected{ fmt::format("File with the name {:?} doesn't exist!", filename) };
144 }
145} // namespace srs::common
constexpr auto binary_to_gray(T bin_val)
constexpr auto DEFAULT_CONFIG_FILE
constexpr auto get_high_bits(const std::bitset< total_size > &bits) -> std::bitset< high_size >
constexpr auto gray_to_binary(T gray_val)
constexpr auto subset(const std::bitset< bit_size > &bits) -> std::bitset< max - min >
constexpr auto merge_bits(const std::bitset< high_size > &high_bits, const std::bitset< low_size > &low_bits) -> std::bitset< high_size+low_size >
constexpr auto get_low_bits(const std::bitset< total_size > &bits) -> std::bitset< low_size >
constexpr auto BYTE_BIT_LENGTH
constexpr auto insert_index_to_filename(std::string_view native_name, int idx) -> std::string
auto get_file_size(std::string_view filename) -> std::expected< std::size_t, std::string >
constexpr auto byte_swap(const std::bitset< bit_size > &bits)
auto get_default_log_path() -> std::filesystem::path
constexpr auto DEFAULT_LOG_FILE
auto get_default_config_path() -> std::filesystem::path
consteval auto get_enum_names()
constexpr auto get_shared_from_this(auto &&obj)