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 <magic_enum/magic_enum.hpp>
18#include <string>
19#include <string_view>
20#include <type_traits>
21
22namespace srs::common
23{
24 // subbits from a half open range [min, max)
25 template <std::size_t bit_size, std::size_t max, std::size_t min = 0>
26 constexpr auto subset(const std::bitset<bit_size>& bits) -> std::bitset<max - min>
27 {
28 constexpr auto max_size = 64;
29 static_assert(max > min);
30 static_assert(max_size >= (max - min));
31 constexpr auto ignore_high = bit_size - max;
32
33 auto new_bits = (bits << ignore_high) >> (ignore_high + min);
34 return std::bitset<max - min>{ new_bits.to_ullong() };
35 }
36
37 template <std::size_t high_size, std::size_t low_size>
38 constexpr auto merge_bits(const std::bitset<high_size>& high_bits, const std::bitset<low_size>& low_bits)
39 -> std::bitset<high_size + low_size>
40 {
41 using NewBit = std::bitset<high_size + low_size>;
42 constexpr auto max_size = 64;
43 static_assert(max_size >= high_size + low_size);
44
45 auto high_bits_part = NewBit(high_bits.to_ullong());
46 auto low_bits_part = NewBit(low_bits.to_ullong());
47 auto new_bits = (high_bits_part << low_size) | low_bits_part;
48 return std::bitset<high_size + low_size>(new_bits.to_ullong());
49 }
50
51 template <std::size_t bit_size>
52 constexpr auto byte_swap(const std::bitset<bit_size>& bits)
53 {
54 auto val = bits.to_ullong();
55 val = val << ((sizeof(uint64_t) * common::BYTE_BIT_LENGTH) - bit_size);
56 val = std::byteswap(val);
57 return std::bitset<bit_size>(val);
58 }
59
60 template <typename T>
61 constexpr auto gray_to_binary(T gray_val)
62 {
63 auto bin_val = T{ gray_val };
64 while (gray_val > 0)
65 {
66 gray_val >>= 1U;
67 bin_val ^= gray_val;
68 }
69 return bin_val;
70 }
71
72 constexpr auto get_shared_from_this(auto&& obj)
73 {
74 return std::static_pointer_cast<std::remove_cvref_t<decltype(obj)>>(obj.shared_from_this());
75 }
76
77 template <typename Enum>
78 consteval auto get_enum_names()
79 {
80 auto names = magic_enum::enum_names<srs::common::ActionMode>();
81 return names;
82 }
83
84 constexpr auto insert_index_to_filename(std::string_view native_name, int idx) -> std::string
85 {
86 auto filepath = std::filesystem::path{ native_name };
87 auto extension = filepath.extension().string();
88 auto file_basename = filepath.replace_extension().string();
89 return std::format("{}_{}{}", file_basename, idx, extension);
90 }
91
92 // TODO: completely get rid of asio::coro
93 auto create_coro_task(auto task, const asio::any_io_executor& executor)
94 {
95 auto task_handle = task();
96 using input_type = decltype(task_handle)::input_type;
97 asio::co_spawn(executor, task_handle.async_resume(input_type{}, asio::use_awaitable), asio::use_future).get();
98 return task_handle;
99 }
100
101} // namespace srs::common
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)