Loading...
Searching...
No Matches
TypeUtils.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31
32#ifndef DRAIN_TYPE_UTILS
33#define DRAIN_TYPE_UTILS
34
35#include <cmath>
36#include <limits>
37#include <set>
38#include <typeinfo>
39
40
41#include "Type.h"
42#include "RegExp.h"
43
44namespace drain {
45
47
50template <class S>
51struct typeLimits {
52
53 static inline
54 long int getMinI(){
55 return static_cast<long int>(std::numeric_limits<S>::min());
56 }
57
58 static inline
59 unsigned long int getMaxI(){
60 return static_cast<unsigned long int>(std::numeric_limits<S>::max());
61 }
62
63 static inline
64 double getMaxF(){
65 return static_cast<double>(std::numeric_limits<S>::max());
66 }
67
68};
69
70
71template <> inline long int typeLimits<void>::getMinI(){ return 0l; }
72template <> inline unsigned long int typeLimits<void>::getMaxI(){ return 0l; }
73template <> inline double typeLimits<void>::getMaxF(){ return 0.0; }
74
75template <> inline long int typeLimits<std::string>::getMinI(){ return 0l; }
76template <> inline unsigned long int typeLimits<std::string>::getMaxI(){ return 0l; }
77template <> inline double typeLimits<std::string>::getMaxF(){ return 0.0; }
78
80
94// THIS WORKS WELL!
95template <class D>
97
98public:
99
101 typedef D (*value_t)(D);
102
107 template <class S, class T>
108 static inline
110 if (std::numeric_limits<S>::is_integer)
111 return &typeLimiter<D>::limitInteger<S,D>;
112 else
113 return &typeLimiter<D>::limitFloat<S,D>;
114 }
115
116
117protected:
118
120
126 template <class S, class T>
127 static
129
130 static const T minValue = static_cast<T>(typeLimits<S>::getMinI());
131 static const T maxValue = static_cast<T>(typeLimits<S>::getMaxI());
132 if (x < minValue)
133 return minValue;
134 else if (x > maxValue)
135 return maxValue;
136
137 return x;
138 }
139
141
145 template <class S, class T>
146 static
148
149 static const T maxValue = static_cast<T>(typeLimits<S>::getMaxF());
150 // notice minus sign
151 if (x < -maxValue)
152 return -maxValue;
153 else if (x > maxValue)
154 return maxValue;
155
156 return x;
157 }
158
159
160
161};
162
163struct TypeUtils {
164
165
167
170 static
171 const std::type_info & guessType(const std::string & value);
172
174
177 static
178 const std::type_info & guessType(double d, const std::type_info & type = typeid(unsigned char));
179
180
181
183
186 template <class C>
187 static
188 const std::type_info & guessArrayType(const C & container);
189
190
195 template <typename T, typename S>
196 static inline
197 bool isWithinRange(const S & x){
198 // Call limit(), eg. check if 352 stays within [0,256[
199 return (Type::call<typeLimiter<S> >(typeid(T))(x) == x);
200 }
201
206 template <typename S>
207 static inline
208 bool isWithinRange(const S & x, const std::type_info & type){
209 // Call limit(), eg. check if 352 stays within uchar range [0,256[
210 return (Type::call<typeLimiter<S> >(type)(x) == x);
211 }
212
213 // * \tparam S - source type, typically with a large value range, double for example
214
216
220 template <typename T=unsigned char>
221 static inline
222 const std::type_info & minimizeIntType(double value){
223 // a bit clumsy, as calls Type::call().
224 return minimizeIntType(value, typeid(T));
225 }
226
228
232 static
233 const std::type_info & minimizeIntType(double value, const std::type_info & type = typeid(unsigned char));
234
235
236 static
237 const drain::RegExp trueRegExp; // ignore case
238
239 static
240 const drain::RegExp falseRegExp; // ignore case
241
242 // TODO: better whitespace
243 static
244 const drain::RegExp numeralRegExp;
245
246};
247
248
249
254/*
255template <typename T=unsigned char>
256const std::type_info & TypeUtils::minimizeIntType(double d){ // , const std::type_info & type = typeid(unsigned char)){
257
258 // const bool IS_INTEGER = (d == ::trunc(d));
259
260 if (d != ::trunc(d)){
261 return typeid(double); // later, also float option?
262 }
263
264
265 if (isWithinRange<T>(d)){
266 return typeid(T);
267 }
268
269 static const std::type_info & type = typeid(T);
270
271 // Type::call<isSigned>(type)
272 // Later, directly "inline" through type specification ?
273 if (std::numeric_limits<T>::is_signed){
274 // throw std::runtime_error(StringBuilder<' '>(__FUNCTION__, " requested signed type: ", TypeName<T>::str()));
275 if (&type == &typeid(signed char)){
276 return minimizeIntType<signed short>(d);
277 }
278 else if (&type == &typeid(signed short)){
279 return minimizeIntType<signed int>(d);
280 }
281 else if (&type == &typeid(signed int)){
282 return minimizeIntType<signed long>(d);
283 }
284 }
285 else {
286 if (&type == &typeid(unsigned char)){
287 return minimizeIntType<unsigned short>(d);
288 }
289 else if (&type == &typeid(unsigned short)){
290 return minimizeIntType<unsigned int>(d);
291 }
292 else if (&type == &typeid(unsigned int)){
293 return minimizeIntType<unsigned long>(d);
294 }
295 }
296
297 return typeid(double);
298
299}
300*/
301
302
303
304/*
305 * \tparam C - contrainer, esp. std::list or std::vector
306 */
307template <class C>
308const std::type_info & TypeUtils::guessArrayType(const C & l){
309
310
311 typedef std::set<const std::type_info *> typeset;
312
313 typeset s;
314 for (typename C::const_iterator it = l.begin(); it != l.end(); ++it) {
315 s.insert(& guessType(*it));
316 }
317
319 if (s.find(& typeid(std::string)) != s.end())
320 return typeid(std::string);
321
323 if (s.find(& typeid(double)) != s.end())
324 return typeid(double);
325
326 if (s.find(& typeid(int)) != s.end())
327 return typeid(int);
328
330 if (s.find(& typeid(bool)) != s.end())
331 return typeid(bool);
332
333 // General fallback solution
334 return typeid(std::string);
335
336}
337
338
339
342public:
343
348 template <class S, class T>
349 static
350 void callback(T & target){
351 target.template setType<S>();
352 }
353};
354
355
357
366
367public:
368
369 typedef std::size_t value_t;
370
375 template <class S, class T>
376 static inline
378 return static_cast<T>(getSize<S>()); // static cast unneeded in these ?
379 }
380
381protected:
382
383 // Must be here to get template<void> implemented
384 template <class S>
385 static inline
386 size_t getSize(){
387 return sizeof(S);
388 }
389
390};
391
392template <>
393inline
394size_t sizeGetter::getSize<void>(){
395 return 0;
396}
397// todo:: std::string?
398
399
400
401
402
403
404
406
411
412public:
413
414 typedef const std::string & value_t;
415
420 template <class S, class T>
421 static
423
424 static std::string s;
425
426 if (s.empty()){
427 std::stringstream sstr;
428 size_t n = sizeGetter::callback<S, std::size_t>();
429 if (std::numeric_limits<S>::is_specialized){
430 if (std::numeric_limits<S>::is_integer){
431 if (!std::numeric_limits<S>::is_signed)
432 sstr << 'u';
433 // sstr << ' ';
434 if (n==1)
435 sstr << "char";
436 else
437 sstr << "int";
438 }
439 else
440 sstr << "float";
441 }
442 else {
443 if (typeid(S) == typeid(bool))
444 sstr << "bool";
445 else if (n==0)
446 sstr << "void";
447 else
448 sstr << "other"; // pointer, for example?
449 }
450 //sstr << (8 * n);
451 s = sstr.str();
452 }
453
454 return s;
455
456 }
457
458};
459
461
462public:
463
464 //typedef std::string value_t;
465 typedef const std::string & value_t;
466
471 template <class S, class T>
472 static
474 // typedef std::numeric_limits<S> nlim;
475 // return numInfo<nlim::is_specialized, nlim::is_integer, nlim::is_signed>::s;
476 static std::string s;
477
478 if (s.empty()){
479 std::stringstream sstr;
480 size_t n = sizeGetter::callback<S, std::size_t>();
481 if (std::numeric_limits<S>::is_specialized){
482 if (std::numeric_limits<S>::is_integer){
483 if (std::numeric_limits<S>::is_signed)
484 sstr << "signed";
485 else
486 sstr << "unsigned";
487 sstr << ' ';
488 if (n==1)
489 sstr << "char";
490 else
491 sstr << "integer";
492 }
493 else
494 sstr << "float";
495 }
496 else {
497 if (typeid(S) == typeid(bool))
498 sstr << "bool";
499 else if (n==0)
500 sstr << "void"; // "uninitialized";
501 else
502 sstr << "non-numeric";
503 }
504 sstr << " (" << (8 * n) << "b)";
505 s = sstr.str();
506 }
507
508 return s;
509
510 }
511
512
513
514};
515
516
517
518
521
522public:
523
524 typedef const char * value_t;
525
526 template <class S, class T>
527 static inline
528 T callback(){
529 return typeid(S).name();
530 }
531
532};
533
534/*
535 * Usage:
536 *
537 * Type::call<drain::typeIsFundamental>(t)
538 *
539 */
541
542public:
543
544 typedef bool value_t;
545
550 template <class S, class T>
551 static inline
552 T callback(){ return std::is_fundamental<S>::value; }
553
554};
555
559class isSigned { // F2
560
561public:
562
563 typedef bool value_t;
564
569 template <class S, class T>
570 static inline
572 return static_cast<T>( std::numeric_limits<S>::is_signed);
573 }
574
575};
576
577
579
586
587public:
588
589 typedef bool value_t;
590
595 template <class S, class T>
596 static inline
597 T callback(){ return std::numeric_limits<S>::is_specialized; }
598
599};
600
606class typeIsInteger { // F2
607
608public:
609
610 typedef bool value_t;
611
616 template <class S, class T>
617 static inline
618 T callback(){ return std::numeric_limits<S>::is_integer; }
619
620};
621
627class typeIsFloat { // F2
628
629public:
630
631 typedef bool value_t;
632
637 template <class S, class T>
638 static inline
640 return (typeid(S)==typeid(float)) || (typeid(S)==typeid(double));
641 //return std::numeric_limits<S>::is_float;
642 }
643 // { return (typeid(S)==typeid(float)) || (typeid(S)==typeid(double)); }
644
645};
646
647
649
656
657public:
658
659 typedef bool value_t;
660
665 template <class S, class D>
666 static inline
668 return (typeid(S) == typeid(char)) || (typeid(S) == typeid(unsigned char)) || (typeid(S) == typeid(short)) || (typeid(S) == typeid(unsigned short));
669 }
670};
671
672
673
674
676
682class typeMin {
683
684public:
685
686 //typedef T value_t;
687
692 template <class S, class D>
693 static inline
695 if (std::numeric_limits<S>::is_integer)
696 return static_cast<D>(+typeLimits<S>::getMinI());
697 else
698 return static_cast<D>(-typeLimits<S>::getMaxF());
699 }
700};
701
702
704
710class typeMax {
711
712public:
713
714 //typedef T value_t;
715
720 template <class S, class D>
721 static inline
723 if (std::numeric_limits<S>::is_integer)
724 return static_cast<D>(+typeLimits<S>::getMaxI());
725 else
726 return static_cast<D>(+typeLimits<S>::getMaxF());
727 }
728};
729
731
740
741public:
742
743 typedef double value_t;
744
749 template <class S, class D>
750 static inline
752 if (typeIsSmallInt::callback<S,bool>())
753 return static_cast<D>(typeLimits<S>::getMaxI());
754 else
755 return static_cast<D>(1.0);
756 }
757};
758
759
760
761// Experimental template exercise... (works ok)
798} // drain::
799
800#endif /* TypeUTILS_H_ */
Definition RegExp.h:58
static T call(const std::type_info &t)
Calls a static function that has no parameters.
Definition Type.h:223
Returns the basic type (integer, float, bool, string, void) as a string.
Definition TypeUtils.h:410
static T callback()
Definition TypeUtils.h:422
Definition TypeUtils.h:460
static T callback()
Definition TypeUtils.h:473
Definition TypeUtils.h:559
static T callback()
Definition TypeUtils.h:571
Returns the compiler specific ie. non-standard name of the type.
Definition TypeUtils.h:520
Returns the sizeof() of a type. Accepts void (and returns size 0), which is not supported by std::num...
Definition TypeUtils.h:365
static T callback()
Definition TypeUtils.h:377
Definition TypeUtils.h:627
static T callback()
Definition TypeUtils.h:639
Definition TypeUtils.h:540
static T callback()
Definition TypeUtils.h:552
Definition TypeUtils.h:606
static T callback()
Definition TypeUtils.h:618
Checks if type is numeric.
Definition TypeUtils.h:585
static T callback()
Definition TypeUtils.h:597
The maximum value of a type given as type_info, char or std::string.
Definition TypeUtils.h:655
static D callback()
Definition TypeUtils.h:667
Class for ensuring that variable of type D remains within limits of type S.
Definition TypeUtils.h:96
static T limitFloat(T x)
Definition TypeUtils.h:147
static T limitInteger(T x)
Definition TypeUtils.h:128
D(* value_t)(D)
Definition that simplifies.
Definition TypeUtils.h:101
static T callback()
Definition TypeUtils.h:109
The maximum value of a type given as type_info, char or std::string.
Definition TypeUtils.h:710
static D callback()
Definition TypeUtils.h:722
The minimum value of a type given as type_info, char or std::string.
Definition TypeUtils.h:682
static D callback()
Definition TypeUtils.h:694
The maximum value of a.
Definition TypeUtils.h:739
static D callback()
Definition TypeUtils.h:751
Utility for implementing setType(const std::type_info &t) in classes supporting setType<T>().
Definition TypeUtils.h:341
static void callback(T &target)
Definition TypeUtils.h:350
Definition DataSelector.cpp:1277
Definition TypeUtils.h:163
static const std::type_info & guessType(double d, const std::type_info &type=typeid(unsigned char))
Returns the (loosely) minimal type that could store the value without precision loss.
static const std::type_info & guessArrayType(const C &container)
Given a vector or list of strings, suggest a matching storage type (int, double, std::string).
Definition TypeUtils.h:308
static const std::type_info & guessType(const std::string &value)
Given a string, check if it could be stored as int or double instead of std::string .
Definition TypeUtils.cpp:52
static const std::type_info & minimizeIntType(double value)
Return a minimal numeric type, that can contain a numeric value.
Definition TypeUtils.h:222
static bool isWithinRange(const S &x, const std::type_info &type)
Definition TypeUtils.h:208
static bool isWithinRange(const S &x)
Definition TypeUtils.h:197
Definition TypeUtils.h:51