summaryrefslogtreecommitdiff
path: root/lib/helpers.h
blob: a8a630e04a92e193506265fcc39f61bcc5b67e1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef MYGRATE_HELPERS_H
#define MYGRATE_HELPERS_H

#include <concepts>
#include <cstdint>
#include <string>
#include <utility>

namespace MyGrate {
	template<std::integral I>
	constexpr inline auto
	bitslice(const I i, uint8_t offset, uint8_t size)
	{
		return (i >> offset) & ((1U << size) - 1U);
	}

	template<typename X, typename... P>
	constexpr inline void
	verify(bool expr, P &&... p)
	{
		if (!expr) {
			throw X(std::forward<P...>(p)...);
		}
	}

	template<typename T>
	constexpr inline auto
	mod100_extract(T & i)
	{
		const auto r {i % 100};
		i /= 100;
		return r;
	}

	template<typename T>
	concept Stringable = requires(T a)
	{
		{
			std::to_string(a)
			} -> std::same_as<std::string>;
	};
	template<typename T>
	concept Viewable = requires(T a)
	{
		{
			a.data()
			} -> std::convertible_to<const char *>;
		{
			a.size()
			} -> std::integral;
	};
}

#endif