66
77#include < limits>
88#include < atomic>
9+ #include " Common/Cpp/Exceptions.h"
910#include " Common/Cpp/Containers/Pimpl.tpp"
1011#include " Common/Cpp/Json/JsonValue.h"
1112#include " SimpleIntegerOption.h"
1213
1314namespace PokemonAutomation {
1415
1516
16- template <typename Type>
17- struct SimpleIntegerCell <Type>::Data{
18- const Type m_min_value;
19- const Type m_max_value;
20- const Type m_default;
21- std::atomic<Type> m_current;
2217
23- Data (Type min_value, Type max_value, Type default_value, Type value)
18+ uint64_t SimpleIntegerCellBase::sanitize (uint64_t x){
19+ if (x > (uint64_t )std::numeric_limits<NativeType>::max ()){
20+ throw InternalProgramError (nullptr , PA_CURRENT_FUNCTION, " Integer Overflow" );
21+ }
22+ return x;
23+ }
24+
25+
26+
27+
28+
29+ struct SimpleIntegerCellBase ::Data{
30+ const NativeType m_min_value;
31+ const NativeType m_max_value;
32+ const NativeType m_default;
33+ std::atomic<NativeType> m_current;
34+
35+ Data (
36+ NativeType min_value, NativeType max_value,
37+ NativeType default_value,
38+ NativeType value
39+ )
2440 : m_min_value(min_value)
2541 , m_max_value(max_value)
2642 , m_default(default_value)
@@ -31,96 +47,93 @@ struct SimpleIntegerCell<Type>::Data{
3147
3248
3349
34- template <typename Type>
35- SimpleIntegerCell<Type>::~SimpleIntegerCell () = default ;
36- template <typename Type>
37- SimpleIntegerCell<Type>::SimpleIntegerCell(const SimpleIntegerCell& x)
38- : ConfigOptionImpl<SimpleIntegerCell<Type>>(x)
50+ SimpleIntegerCellBase::~SimpleIntegerCellBase () = default ;
51+ SimpleIntegerCellBase::SimpleIntegerCellBase (const SimpleIntegerCellBase& x)
52+ : ConfigOptionImpl<SimpleIntegerCellBase>(x)
3953 , m_data(CONSTRUCT_TOKEN, x.min_value(), x.max_value(), x.default_value(), x.current_value())
4054{}
41- template <typename Type>
42- SimpleIntegerCell<Type>::SimpleIntegerCell(
55+ SimpleIntegerCellBase::SimpleIntegerCellBase (
4356 LockMode lock_while_running,
44- Type min_value, Type max_value,
45- Type default_value, Type current_value
57+ NativeType min_value, NativeType max_value,
58+ NativeType default_value, NativeType current_value
4659)
47- : ConfigOptionImpl<SimpleIntegerCell<Type> >(lock_while_running)
60+ : ConfigOptionImpl<SimpleIntegerCellBase >(lock_while_running)
4861 , m_data(CONSTRUCT_TOKEN, min_value, max_value, default_value, current_value)
4962{}
5063
51- template <typename Type>
52- SimpleIntegerCell<Type>::SimpleIntegerCell(
64+ SimpleIntegerCellBase::SimpleIntegerCellBase (
5365 LockMode lock_while_running,
54- Type default_value
66+ NativeType default_value
5567)
56- : ConfigOptionImpl<SimpleIntegerCell<Type>>(lock_while_running)
57- , m_data(CONSTRUCT_TOKEN, std::numeric_limits<Type>::min(), std::numeric_limits<Type>::max(), default_value, default_value)
68+ : ConfigOptionImpl<SimpleIntegerCellBase>(lock_while_running)
69+ , m_data(
70+ CONSTRUCT_TOKEN,
71+ std::numeric_limits<NativeType>::min(),
72+ std::numeric_limits<NativeType>::max(),
73+ default_value,
74+ default_value
75+ )
5876{}
59- template <typename Type>
60- SimpleIntegerCell<Type>::SimpleIntegerCell(
77+ SimpleIntegerCellBase::SimpleIntegerCellBase (
6178 LockMode lock_while_running,
62- Type default_value, Type min_value
79+ NativeType default_value, NativeType min_value
6380)
64- : ConfigOptionImpl<SimpleIntegerCell<Type>>(lock_while_running)
65- , m_data(CONSTRUCT_TOKEN, min_value, std::numeric_limits<Type>::max(), default_value, default_value)
81+ : ConfigOptionImpl<SimpleIntegerCellBase>(lock_while_running)
82+ , m_data(
83+ CONSTRUCT_TOKEN,
84+ min_value,
85+ std::numeric_limits<NativeType>::max(),
86+ default_value,
87+ default_value
88+ )
6689{}
67- template <typename Type>
68- SimpleIntegerCell<Type>::SimpleIntegerCell(
90+ SimpleIntegerCellBase::SimpleIntegerCellBase (
6991 LockMode lock_while_running,
70- Type default_value, Type min_value, Type max_value
92+ NativeType default_value, NativeType min_value, NativeType max_value
7193)
72- : ConfigOptionImpl<SimpleIntegerCell<Type> >(lock_while_running)
94+ : ConfigOptionImpl<SimpleIntegerCellBase >(lock_while_running)
7395 , m_data(CONSTRUCT_TOKEN, min_value, max_value, default_value, default_value)
7496{}
7597
76- template <typename Type>
77- Type SimpleIntegerCell<Type>::min_value() const {
98+ SimpleIntegerCellBase::NativeType SimpleIntegerCellBase::min_value () const {
7899 return m_data->m_min_value ;
79100}
80- template <typename Type>
81- Type SimpleIntegerCell<Type>::max_value() const {
101+ SimpleIntegerCellBase::NativeType SimpleIntegerCellBase::max_value () const {
82102 return m_data->m_max_value ;
83103}
84- template <typename Type>
85- Type SimpleIntegerCell<Type>::default_value() const {
104+ SimpleIntegerCellBase::NativeType SimpleIntegerCellBase::default_value () const {
86105 return m_data->m_default ;
87106}
88- template <typename Type>
89- Type SimpleIntegerCell<Type>::current_value() const {
107+ SimpleIntegerCellBase::NativeType SimpleIntegerCellBase::current_value () const {
90108 return m_data->m_current .load (std::memory_order_relaxed);
91109}
92- template <typename Type>
93- SimpleIntegerCell<Type>::operator Type () const {
94- return m_data->m_current .load (std::memory_order_relaxed);
95- }
96- template <typename Type>
97- std::string SimpleIntegerCell<Type>::set(Type x){
110+ // SimpleIntegerCellBase::operator NativeType() const{
111+ // return m_data->m_current.load(std::memory_order_relaxed);
112+ // }
113+ std::string SimpleIntegerCellBase::set (NativeType x){
98114 std::string err = check_validity (x);
99115 if (!err.empty ()){
100116 return err;
101117 }
102- if ((Type)* this == x){
118+ if (current_value () == x){
103119 return std::string ();
104120 }
105121 if (x != m_data->m_current .exchange (x, std::memory_order_relaxed)){
106122 this ->report_value_changed (this );
107123 }
108124 return std::string ();
109125}
110- template <typename Type>
111- void SimpleIntegerCell<Type>::load_json(const JsonValue& json){
112- Type value;
126+ void SimpleIntegerCellBase::load_json (const JsonValue& json){
127+ NativeType value;
113128 if (json.read_integer (value, m_data->m_min_value , m_data->m_max_value )){
114129 set (value);
115130 }
116131}
117- template <typename Type>
118- JsonValue SimpleIntegerCell<Type>::to_json() const {
119- return (Type)*this ;
132+ JsonValue SimpleIntegerCellBase::to_json () const {
133+ return current_value ();
120134}
121135
122- template <typename Type>
123- std::string SimpleIntegerCell<Type>::check_validity(Type x) const {
136+ std::string SimpleIntegerCellBase::check_validity (NativeType x) const {
124137 if (x < m_data->m_min_value ){
125138 return " Value too small: min = " + std::to_string (m_data->m_min_value ) + " , value = " + std::to_string (x);
126139 }
@@ -129,27 +142,26 @@ std::string SimpleIntegerCell<Type>::check_validity(Type x) const{
129142 }
130143 return std::string ();
131144}
132- template <typename Type>
133- std::string SimpleIntegerCell<Type>::check_validity() const {
134- return check_validity (*this );
145+ std::string SimpleIntegerCellBase::check_validity () const {
146+ return check_validity (current_value ());
135147}
136- template <typename Type>
137- void SimpleIntegerCell<Type>::restore_defaults(){
148+ void SimpleIntegerCellBase::restore_defaults (){
138149 set (m_data->m_default );
139150}
140151
141152
142153
143154
144155
145- template <typename Type>
146- SimpleIntegerOption<Type>::SimpleIntegerOption(
156+
157+
158+ SimpleIntegerOptionBase::SimpleIntegerOptionBase (
147159 std::string label,
148160 LockMode lock_while_running,
149- Type min_value, Type max_value,
150- Type default_value, Type current_value
161+ NativeType min_value, NativeType max_value,
162+ NativeType default_value, NativeType current_value
151163)
152- : ConfigOptionImpl<SimpleIntegerOption<Type>, SimpleIntegerCell<Type> >(
164+ : ConfigOptionImpl<SimpleIntegerOptionBase, SimpleIntegerCellBase >(
153165 lock_while_running,
154166 min_value,
155167 max_value,
@@ -158,38 +170,35 @@ SimpleIntegerOption<Type>::SimpleIntegerOption(
158170 )
159171 , m_label(std::move(label))
160172{}
161- template <typename Type>
162- SimpleIntegerOption<Type>::SimpleIntegerOption(
173+ SimpleIntegerOptionBase::SimpleIntegerOptionBase (
163174 std::string label,
164175 LockMode lock_while_running,
165- Type default_value
176+ NativeType default_value
166177)
167- : ConfigOptionImpl<SimpleIntegerOption<Type>, SimpleIntegerCell<Type> >(
178+ : ConfigOptionImpl<SimpleIntegerOptionBase, SimpleIntegerCellBase >(
168179 lock_while_running,
169180 default_value
170181 )
171182 , m_label(std::move(label))
172183{}
173- template <typename Type>
174- SimpleIntegerOption<Type>::SimpleIntegerOption(
184+ SimpleIntegerOptionBase::SimpleIntegerOptionBase (
175185 std::string label,
176186 LockMode lock_while_running,
177- Type default_value, Type min_value
187+ NativeType default_value, NativeType min_value
178188)
179- : ConfigOptionImpl<SimpleIntegerOption<Type>, SimpleIntegerCell<Type> >(
189+ : ConfigOptionImpl<SimpleIntegerOptionBase, SimpleIntegerCellBase >(
180190 lock_while_running,
181191 default_value,
182192 min_value
183193 )
184194 , m_label(std::move(label))
185195{}
186- template <typename Type>
187- SimpleIntegerOption<Type>::SimpleIntegerOption(
196+ SimpleIntegerOptionBase::SimpleIntegerOptionBase (
188197 std::string label,
189198 LockMode lock_while_running,
190- Type default_value, Type min_value, Type max_value
199+ NativeType default_value, NativeType min_value, NativeType max_value
191200)
192- : ConfigOptionImpl<SimpleIntegerOption<Type>, SimpleIntegerCell<Type> >(
201+ : ConfigOptionImpl<SimpleIntegerOptionBase, SimpleIntegerCellBase >(
193202 lock_while_running,
194203 default_value,
195204 min_value,
@@ -202,31 +211,4 @@ SimpleIntegerOption<Type>::SimpleIntegerOption(
202211
203212
204213
205-
206- template class SimpleIntegerCell <uint8_t >;
207- template class SimpleIntegerCell <uint16_t >;
208- template class SimpleIntegerCell <uint32_t >;
209- template class SimpleIntegerCell <uint64_t >;
210- template class SimpleIntegerCell <int8_t >;
211- template class SimpleIntegerCell <int16_t >;
212- template class SimpleIntegerCell <int32_t >;
213- template class SimpleIntegerCell <int64_t >;
214-
215- template class SimpleIntegerOption <uint8_t >;
216- template class SimpleIntegerOption <uint16_t >;
217- template class SimpleIntegerOption <uint32_t >;
218- template class SimpleIntegerOption <uint64_t >;
219- template class SimpleIntegerOption <int8_t >;
220- template class SimpleIntegerOption <int16_t >;
221- template class SimpleIntegerOption <int32_t >;
222- template class SimpleIntegerOption <int64_t >;
223-
224-
225- // This is stupid.
226- #ifdef __APPLE__
227- template class SimpleIntegerCell <size_t >;
228- template class SimpleIntegerOption <size_t >;
229- #endif
230-
231-
232214}
0 commit comments