SRS-control 0.1.4
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 <bit>
7#include <bitset>
8#include <boost/asio.hpp>
9#include <boost/asio/any_io_executor.hpp>
10#include <boost/asio/use_awaitable.hpp>
11#include <boost/asio/use_future.hpp>
12#include <boost/thread/future.hpp>
13#include <cstddef>
14#include <cstdint>
15#include <filesystem>
16#include <format>
17#include <iterator>
18#include <magic_enum/magic_enum.hpp>
19#include <string>
20#include <string_view>
21#include <type_traits>
22#include <utility>
23
24namespace srs::common
25{
26 // subbits from a half open range [min, max)
27 template <std::size_t bit_size, std::size_t max, std::size_t min = 0>
28 constexpr auto subset(const std::bitset<bit_size>& bits) -> std::bitset<max - min>
29 {
30 constexpr auto max_size = 64;
31 static_assert(max > min);
32 static_assert(max_size >= (max - min));
33 constexpr auto ignore_high = bit_size - max;
34
35 auto new_bits = (bits << ignore_high) >> (ignore_high + min);
36 return std::bitset<max - min>{ new_bits.to_ullong() };
37 }
38
39 template <std::size_t high_size, std::size_t low_size>
40 constexpr auto merge_bits(const std::bitset<high_size>& high_bits, const std::bitset<low_size>& low_bits)
41 -> std::bitset<high_size + low_size>
42 {
43 using NewBit = std::bitset<high_size + low_size>;
44 constexpr auto max_size = 64;
45 static_assert(max_size >= high_size + low_size);
46
47 auto high_bits_part = NewBit(high_bits.to_ullong());
48 auto low_bits_part = NewBit(low_bits.to_ullong());
49 auto new_bits = (high_bits_part << low_size) | low_bits_part;
50 return std::bitset<high_size + low_size>(new_bits.to_ullong());
51 }
52
53 template <std::size_t low_size, std::size_t total_size>
54 requires(total_size <= sizeof(std::uint64_t) * common::BYTE_BIT_LENGTH and total_size >= low_size)
55 constexpr auto get_low_bits(const std::bitset<total_size>& bits) -> std::bitset<low_size>
56 {
57 constexpr auto high_size = total_size - low_size;
58 const auto low_bits = std::bitset<low_size>{ ((bits << high_size) >> high_size).to_ullong() };
59 return low_bits;
60 }
61
62 template <std::size_t high_size, std::size_t total_size>
63 requires(total_size <= sizeof(std::uint64_t) * common::BYTE_BIT_LENGTH and total_size >= high_size)
64 constexpr auto get_high_bits(const std::bitset<total_size>& bits) -> std::bitset<high_size>
65 {
66 constexpr auto low_size = total_size - high_size;
67 const auto high_bits = std::bitset<high_size>{ (bits >> low_size).to_ullong() };
68 return high_bits;
69 }
70
71 template <std::size_t bit_size>
72 constexpr auto byte_swap(const std::bitset<bit_size>& bits)
73 {
74 auto val = bits.to_ullong();
75 val = val << ((sizeof(uint64_t) * common::BYTE_BIT_LENGTH) - bit_size);
76 val = std::byteswap(val);
77 return std::bitset<bit_size>(val);
78 }
79
80 template <typename T>
81 constexpr auto gray_to_binary(T gray_val)
82 {
83 auto bin_val = T{ gray_val };
84 while (gray_val > 0)
85 {
86 gray_val >>= 1U;
87 bin_val ^= gray_val;
88 }
89 return bin_val;
90 }
91
92 template <typename T>
93 constexpr auto binary_to_gray(T bin_val)
94 {
95 return bin_val ^ (bin_val >> 1U);
96 }
97
98 constexpr auto get_shared_from_this(auto&& obj)
99 {
100 return std::static_pointer_cast<std::remove_cvref_t<decltype(obj)>>(obj.shared_from_this());
101 }
102
103 template <typename Enum>
104 consteval auto get_enum_names()
105 {
106 auto names = magic_enum::enum_names<srs::common::ActionMode>();
107 return names;
108 }
109
110 constexpr auto insert_index_to_filename(std::string_view native_name, int idx) -> std::string
111 {
112 auto filepath = std::filesystem::path{ native_name };
113 auto extension = filepath.extension().string();
114 auto file_basename = filepath.replace_extension().string();
115 return std::format("{}_{}{}", file_basename, idx, extension);
116 }
117
118 // TODO: completely get rid of asio::coro
119 auto create_coro_task(auto task, const asio::any_io_executor& executor)
120 {
121 auto task_handle = task();
122 using input_type = decltype(task_handle)::input_type;
123 asio::co_spawn(executor, task_handle.async_resume(input_type{}, asio::use_awaitable), asio::use_future).get();
124 return task_handle;
125 }
126
127} // namespace srs::common
constexpr auto binary_to_gray(T bin_val)
constexpr auto get_low_bits(const std::bitset< total_size > &bits) -> std::bitset< low_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 BYTE_BIT_LENGTH
constexpr auto insert_index_to_filename(std::string_view native_name, int idx) -> std::string
constexpr auto byte_swap(const std::bitset< bit_size > &bits)
auto create_coro_task(auto task, const asio::any_io_executor &executor)
consteval auto get_enum_names()
constexpr auto get_shared_from_this(auto &&obj)
constexpr auto get_high_bits(const std::bitset< total_size > &bits) -> std::bitset< high_size >