SXEval 1.0.3
A generic s-expression interpreter library.
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#ifndef SXEVAL_UTILS_HPP
2#define SXEVAL_UTILS_HPP
3
4#include <type_traits>
5#include <cmath>
6#include <stdexcept>
7#include <cstdio>
8#include <limits>
9
10#define SXEVAL_PI 3.14159265358979323846
11
12
13/* DEFINITIONS */
14
15namespace sxeval {
16
24template<typename T>
25T StringToType(const std::string& s);
26
34template<typename T>
35inline bool Greater(const T& a, const T& b) {
36 return a > b;
37}
38
46template<typename T>
47inline typename std::enable_if<std::is_integral<T>::value, bool>::type
48Equal(const T& a, const T& b) {
49 return a == b;
50}
51
59template<typename T>
60inline typename std::enable_if<std::is_floating_point<T>::value, bool>::type
61Equal(const T& a, const T& b) {
62 return std::abs(a - b) <= std::numeric_limits<T>::epsilon();
63}
64
72template<typename T>
73inline bool NotEqual(const T& a, const T& b) {
74 return !Equal<T>(a, b);
75}
76
84template<typename T>
85inline bool GreaterOrEqual(const T& a, const T& b) {
86 return Greater<T>(a, b) || Equal<T>(a, b);
87}
88
96template<typename T>
97inline bool Less(const T& a, const T& b) {
98 return !GreaterOrEqual<T>(a, b);
99}
100
108template<typename T>
109inline bool LessOrEqual(const T& a, const T& b) {
110 return Less<T>(a, b) || Equal<T>(a, b);
111}
112
121template<typename T>
122inline typename std::enable_if<std::is_integral<T>::value, bool>::type
123TypeToBool(const T& val) {
124 return val != 0;
125}
126
135template<typename T>
136inline typename std::enable_if<std::is_floating_point<T>::value, bool>::type
137TypeToBool(const T& val) {
138 return std::abs(val) > std::numeric_limits<T>::epsilon();
139}
140
148template<typename T>
149inline bool LogicalAnd(const T& a, const T& b) {
150 return TypeToBool<T>(a) && TypeToBool<T>(b);
151}
152
160template<typename T>
161inline bool LogicalOr(const T& a, const T& b) {
162 return TypeToBool<T>(a) || TypeToBool<T>(b);
163}
164
171template<typename T>
172inline bool LogicalNot(const T& a) {
173 return !TypeToBool<T>(a);
174}
175
183template<typename T>
184inline bool LogicalXor(const T& a, const T& b) {
185 return TypeToBool<T>(a) != TypeToBool<T>(b);
186}
187
195template<typename T>
196inline bool LogicalNand(const T& a, const T& b) {
197 return !LogicalAnd<T>(a, b);
198}
199
207template<typename T>
208inline bool LogicalNor(const T& a, const T& b) {
209 return !LogicalOr<T>(a, b);
210}
211
219template<typename T>
220inline bool LogicalXnor(const T& a, const T& b) {
221 return !LogicalXor<T>(a, b);
222}
223
232template<typename T>
233inline typename std::enable_if<std::is_integral<T>::value, T>::type
234Modulo(const T& a, const T& b) {
235 return a % b;
236}
237
246template<typename T>
247inline typename std::enable_if<std::is_floating_point<T>::value, T>::type
248Modulo(const T& a, const T& b) {
249 return std::fmod(a, b);
250}
251
260template<typename T>
261inline typename std::enable_if<std::is_unsigned<T>::value, T>::type
262Absolute(const T& a) {
263 return a;
264}
265
274template<typename T>
275inline typename std::enable_if<
276 std::is_integral<T>::value && !std::is_unsigned<T>::value, T>::type
277Absolute(const T& a) {
278 return std::abs(a);
279}
280
289template<typename T>
290inline typename std::enable_if<
291 std::is_floating_point<T>::value && !std::is_unsigned<T>::value, T>::type
292Absolute(const T& a) {
293 return std::fabs(a);
294}
295
296} /* namespace sxeval */
297
298
299/* IMPLEMENTATIONS */
300
301template<>
302inline int sxeval::StringToType<int>(const std::string& s) {
303 int res;
304 if (sscanf(s.c_str(), "%d", &res) != 1) {
305 throw std::invalid_argument("Invalid int string");
306 }
307 return res;
308}
309
310template<>
311inline signed char sxeval::StringToType<signed char>(const std::string& s) {
312 signed char res;
313 if (sscanf(s.c_str(), "%hhd", &res) != 1) {
314 throw std::invalid_argument("Invalid signed char string");
315 }
316 return res;
317}
318
319template<>
320inline short int sxeval::StringToType<short int>(const std::string& s) {
321 short int res;
322 if (sscanf(s.c_str(), "%hd", &res) != 1) {
323 throw std::invalid_argument("Invalid short int string");
324 }
325 return res;
326}
327
328template<>
329inline long int sxeval::StringToType<long int>(const std::string& s) {
330 long int res;
331 if (sscanf(s.c_str(), "%ld", &res) != 1) {
332 throw std::invalid_argument("Invalid long int string");
333 }
334 return res;
335}
336
337template<>
338inline unsigned int sxeval::StringToType<unsigned int>(const std::string& s) {
339 unsigned int res;
340 if (sscanf(s.c_str(), "%u", &res) != 1) {
341 throw std::invalid_argument("Invalid unsigned int string");
342 }
343 return res;
344}
345
346template<>
347inline unsigned char sxeval::StringToType<unsigned char>(const std::string& s) {
348 unsigned char res;
349 if (sscanf(s.c_str(), "%hhu", &res) != 1) {
350 throw std::invalid_argument("Invalid unsigned char string");
351 }
352 return res;
353}
354
355template<>
356inline unsigned long int sxeval::StringToType<unsigned long int>(
357 const std::string& s)
358{
359 unsigned long int res;
360 if (sscanf(s.c_str(), "%lu", &res) != 1) {
361 throw std::invalid_argument("Invalid unsigned long int string");
362 }
363 return res;
364}
365
366template<>
367inline float sxeval::StringToType<float>(const std::string& s) {
368 float res;
369 if (sscanf(s.c_str(), "%f", &res) != 1) {
370 throw std::invalid_argument("Invalid float string");
371 }
372 return res;
373}
374
375template<>
376inline double sxeval::StringToType<double>(const std::string& s) {
377 double res;
378 if (sscanf(s.c_str(), "%lf", &res) != 1) {
379 throw std::invalid_argument("Invalid double string");
380 }
381 return res;
382}
383
384template<>
385inline long double sxeval::StringToType<long double>(const std::string& s)
386{
387 long double res;
388 if (sscanf(s.c_str(), "%Lf", &res) != 1) {
389 throw std::invalid_argument("Invalid long double string");
390 }
391 return res;
392}
393
394#endif /* SXEVAL_UTILS_HPP */
Definition AOperation.hpp:8
std::enable_if< std::is_integral< T >::value, T >::type Modulo(const T &a, const T &b)
Perform a modulo operation on types T.
Definition utils.hpp:234
bool LogicalXnor(const T &a, const T &b)
Perform a logical xnor operation on types T.
Definition utils.hpp:220
bool Less(const T &a, const T &b)
Check if a is less than b.
Definition utils.hpp:97
std::enable_if< std::is_integral< T >::value, bool >::type Equal(const T &a, const T &b)
Check if a is equal to b.
Definition utils.hpp:48
bool LogicalNot(const T &a)
Perform a logical not operation on type T.
Definition utils.hpp:172
bool LogicalXor(const T &a, const T &b)
Perform a logical xor operation on types T.
Definition utils.hpp:184
bool Greater(const T &a, const T &b)
Check if a is greater than b.
Definition utils.hpp:35
bool LogicalOr(const T &a, const T &b)
Perform a logical or operation on types T.
Definition utils.hpp:161
bool LogicalAnd(const T &a, const T &b)
Perform a logical and operations on types T.
Definition utils.hpp:149
bool LogicalNand(const T &a, const T &b)
Perform a logical nand operation on types T.
Definition utils.hpp:196
bool GreaterOrEqual(const T &a, const T &b)
Check if a is greater than or equal to b.
Definition utils.hpp:85
bool NotEqual(const T &a, const T &b)
Check if a is not equal to b.
Definition utils.hpp:73
std::enable_if< std::is_unsigned< T >::value, T >::type Absolute(const T &a)
Perform an absolute value operation on types T.
Definition utils.hpp:262
bool LessOrEqual(const T &a, const T &b)
Check if a is less than or equal to b.
Definition utils.hpp:109
T StringToType(const std::string &s)
Convert a string to a type T.
std::enable_if< std::is_integral< T >::value, bool >::type TypeToBool(const T &val)
Convert a type T to a boolean value.
Definition utils.hpp:123
bool LogicalNor(const T &a, const T &b)
Perform a logical nor operation on types T.
Definition utils.hpp:208