initial commit
[menupp.git] / src / common.hpp
blob2d8b6965ad60d971ccc728d3286c3a4ac076f5fb
1 #ifndef MENUPP_COMMON_HPP_
2 #define MENUPP_COMMON_HPP_
4 namespace Menu
7 template<typename B, typename... Ts>
8 using Extends = typename std::enable_if<(... && std::is_base_of<B, Ts>::value)>::type;
10 enum struct Move
12 Into, Up, Down, Left, Right
15 template<typename T = size_t, T Min = 0, T Max = 100>
16 struct Range
18 static_assert(Min <= Max);
20 using Type = T;
21 static constexpr Type min = Min;
22 static constexpr Type max = Max;
25 template<
26 typename R = Range<>,
27 typename R::Type Default = 0,
28 typename R::Type Inc = 1,
29 bool Loop = false
31 struct LevelConf
33 static_assert(Inc <= (R::max - R::min));
34 static_assert(Default >= R::min);
35 static_assert(Default <= R::max);
37 using Type = typename R::Type;
38 using Range = R;
39 static constexpr bool loop = Loop;
40 static constexpr Type inc = Inc;
41 static constexpr Type dflt = Default;
44 template<typename LevelConf>
45 typename LevelConf::Type inc(typename LevelConf::Type x)
47 typename LevelConf::Type ret = x + LevelConf::inc;
48 if constexpr(LevelConf::loop) {
49 if (ret < x)
50 ret = LevelConf::Range::min;
51 else if (ret > LevelConf::Range::max)
52 ret = LevelConf::Range::min;
53 else if (ret < LevelConf::Range::min)
54 ret = LevelConf::Range::max;
55 } else {
56 if (ret < x)
57 ret = LevelConf::Range::max;
58 else if (ret > LevelConf::Range::max)
59 ret = LevelConf::Range::max;
60 else if (ret < LevelConf::Range::min)
61 ret = LevelConf::Range::min;
63 return ret;
66 template<typename LevelConf>
67 typename LevelConf::Type dec(typename LevelConf::Type x)
69 typename LevelConf::Type ret = x - LevelConf::inc;
70 if constexpr(LevelConf::loop) {
71 if (ret > x)
72 ret = LevelConf::Range::max;
73 else if (ret > LevelConf::Range::max)
74 ret = LevelConf::Range::min;
75 else if (ret < LevelConf::Range::min)
76 ret = LevelConf::Range::max;
77 } else {
78 if (ret > x)
79 ret = LevelConf::Range::min;
80 else if (ret > LevelConf::Range::max)
81 ret = LevelConf::Range::max;
82 else if (ret < LevelConf::Range::min)
83 ret = LevelConf::Range::min;
85 return ret;
90 #endif // MENUPP_COMMON_HPP_