summaryrefslogtreecommitdiff
path: root/lib/helpers.h
blob: 3a94eaddcc997c2a6e4536d9a03a219dd05658af (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
#ifndef MYGRATE_HELPERS_H
#define MYGRATE_HELPERS_H

#include <concepts>
#include <cstdint>
#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;
	}
}

#endif