SRS-control 0.1.4
 
Loading...
Searching...
No Matches
CommonFunctions.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <bit>
4#include <bitset>
5#include <boost/asio.hpp>
6#include <boost/thread/future.hpp>
8
9namespace srs::common
10{
14
15 // subbits from a half open range [min, max)
16 template <std::size_t bit_size, std::size_t max, std::size_t min = 0>
17 constexpr auto subset(const std::bitset<bit_size>& bits) -> std::bitset<max - min>
18 {
19 constexpr auto max_size = 64;
20 static_assert(max > min);
21 static_assert(max_size >= (max - min));
22 constexpr auto ignore_high = bit_size - max;
23
24 auto new_bits = (bits << ignore_high) >> (ignore_high + min);
25 return std::bitset<max - min>{ new_bits.to_ullong() };
26 }
27
28 template <std::size_t high_size, std::size_t low_size>
29 constexpr auto merge_bits(const std::bitset<high_size>& high_bits, const std::bitset<low_size>& low_bits)
30 -> std::bitset<high_size + low_size>
31 {
32 using NewBit = std::bitset<high_size + low_size>;
33 constexpr auto max_size = 64;
34 static_assert(max_size >= high_size + low_size);
35
36 auto high_bits_part = NewBit(high_bits.to_ullong());
37 auto low_bits_part = NewBit(low_bits.to_ullong());
38 auto new_bits = (high_bits_part << low_size) | low_bits_part;
39 return std::bitset<high_size + low_size>(new_bits.to_ullong());
40 }
41
42 template <std::size_t bit_size>
43 constexpr auto byte_swap(const std::bitset<bit_size>& bits)
44 {
45 auto val = bits.to_ullong();
46 val = val << (sizeof(uint64_t) * common::BYTE_BIT_LENGTH - bit_size);
47 val = std::byteswap(val);
48 return std::bitset<bit_size>(val);
49 }
50
51 template <typename T>
52 constexpr auto gray_to_binary(T gray_val)
53 {
54 auto bin_val = T{ gray_val };
55 while (gray_val > 0)
56 {
57 gray_val >>= 1;
58 bin_val ^= gray_val;
59 }
60 return bin_val;
61 }
62
63 constexpr auto get_shared_from_this(auto&& obj)
64 {
65 return std::static_pointer_cast<std::remove_cvref_t<decltype(obj)>>(obj.shared_from_this());
66 }
67
68 auto create_coro_future(auto& coro, auto&& pre_fut)
69 {
70 if (not pre_fut.valid())
71 {
72 throw std::runtime_error("Previous future is not valid!");
73 }
74 return pre_fut.then(
75 [&coro](std::remove_cvref_t<decltype(pre_fut)> fut)
76 {
77 ;
78 return asio::co_spawn(
79 coro.get_executor(), coro.async_resume(fut.get(), asio::use_awaitable), asio::use_future)
80 .get();
81 });
82 }
83
84 auto create_coro_future(auto& coro, bool is_terminated)
85 {
86 return boost::async(
87 [&coro, is_terminated]()
88 {
89 return asio::co_spawn(
90 coro.get_executor(), coro.async_resume(is_terminated, asio::use_awaitable), asio::use_future)
91 .get();
92 });
93 }
94
95 void coro_sync_start(auto& coro, auto&&... args)
96 {
97 asio::co_spawn(coro.get_executor(), coro.async_resume(std::forward<decltype(args)>(args)...), asio::use_future)
98 .get();
99 }
100
102} // namespace srs
constexpr auto BYTE_BIT_LENGTH
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_swap(const std::bitset< bit_size > &bits)
auto create_coro_future(auto &coro, auto &&pre_fut)
constexpr auto get_shared_from_this(auto &&obj)
void coro_sync_start(auto &coro, auto &&... args)