acme
Foundational cross-platform layer of the ca2 framework
Loading...
Searching...
No Matches
targeted_unit.h
1// Created by camilo on 2023-06-28 BRT <3ThomasBorregaardSorensen!!
2#pragma once
3
4
5#include "acme/exception/bad_unit_exception.h"
6
8
9
10template < typename ENUM, typename allowed_units >
11class targeted_unit :
12 public unit_base < ENUM >
13{
14public:
15
16
17 constexpr targeted_unit() :
18 unit_base<ENUM>(0, allowed_units::allowed_units()[0])
19 {
20
21 }
22
23 template < prototype_integral INTEGRAL >
24 constexpr targeted_unit(INTEGRAL i, ENUM eunit) :
25 unit_base<ENUM>(i, eunit)
26 {
27 assert_allowed(eunit);
28 }
29
30 template < prototype_floating FLOATING >
31 constexpr targeted_unit(FLOATING f, ENUM eunit) :
32 unit_base<ENUM>(f, eunit)
33 {
34 assert_allowed(eunit);
35 }
36
37 constexpr targeted_unit(const unit_base <ENUM > & unit) :
38 unit_base<ENUM>(unit)
39 {
40 assert_allowed(this->m_eunit);
41 }
42
43
44 constexpr static void assert_allowed(ENUM eunit)
45 {
46
47 if (!is_allowed(eunit))
48 {
49
50 throw ::bad_unit_exception < ENUM >(eunit);
51
52 }
53
54 }
55
56 constexpr static bool is_allowed(ENUM eunit)
57 {
58
59 //::collection::index i = 0;
60
61 auto pallowed = allowed_units::allowed_units();
62
63 ENUM eallowed;
64
65 while ((eallowed = *pallowed) != e_unit_none)
66 {
67
68 if (eallowed == eunit)
69 {
70
71 return true;
72
73 }
74
75 pallowed++;
76
77 }
78
79 return false;
80
81 }
82
83
84 template < prototype_floating FLOATING >
85 constexpr targeted_unit & operator = (FLOATING f)
86 {
87
88 unit_base <ENUM >::operator = (f);
89
90 assert_allowed(this->m_eunit);
91
92 return *this;
93
94 }
95
96
97 constexpr targeted_unit & operator = (const unit_base <ENUM> & unit)
98 {
99
100 unit_base <ENUM >::operator =(unit);
101
102 assert_allowed(this->m_eunit);
103
104 return *this;
105
106 }
107
108 constexpr void set_unit(ENUM eunit)
109 {
110
111 assert_allowed(eunit);
112
113 this->m_eunit = eunit;
114
115 }
116
117
118 template < prototype_floating FLOATING >
119 targeted_unit & operator *= (FLOATING f)
120 {
121
122 unit_base < ENUM >::operator *=(f);
123
124 return *this;
125
126 }
127
128
129 template < prototype_floating FLOATING >
130 targeted_unit & operator /= (FLOATING f)
131 {
132
133 unit_base < ENUM >::operator /=(f);
134
135 return *this;
136
137 }
138
139
140 template < prototype_floating FLOATING >
141 targeted_unit operator * (FLOATING f) const
142 {
143
144 auto unit = *this;
145
146 unit *= f;
147
148 return unit;
149
150 }
151
152
153 template < prototype_floating FLOATING >
154 targeted_unit operator / (FLOATING f) const
155 {
156
157 auto unit = *this;
158
159 unit /= f;
160
161 return *this;
162
163 }
164
165
166};
167
168
169