summaryrefslogtreecommitdiff
path: root/lib/helpers.h
blob: 2c7f4c2778c247857afbe08fa4efd992b18be461 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef MYGRATE_HELPERS_H
#define MYGRATE_HELPERS_H

#include <boost/numeric/conversion/cast.hpp>
#include <concepts>
#include <cstdint>
#include <cstdlib>
#include <string>
#include <utility>

namespace MyGrate {
	template<uint8_t size>
	using SmallestUInt = std::conditional_t<size <= 8, uint8_t,
			std::conditional_t<size <= 16, uint16_t, std::conditional_t<size <= 32, uint32_t, uint64_t>>>;

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

	template<typename X, typename R, typename... P>
	constexpr inline auto
	verify(R expr, P &&... p)
	{
		if (!expr) [[unlikely]] {
			throw X(std::forward<P>(p)...);
		}
		return expr;
	}

	constexpr inline auto
	mod100_extract(std::integral auto & i)
	{
		using R = std::conditional_t<std::is_signed_v<decltype(i)>, int8_t, uint8_t>;
		const auto r {boost::numeric_cast<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;
	};

	inline const char *
	getenv(const char * env, const char * dfl)
	{
		if (const auto e {::getenv(env)}) {
			return e;
		}
		return dfl;
	}
}

#endif