Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
Value.h
Go to the documentation of this file.
1#pragma once
2
3#include "../global.h"
4#include "TVector.h"
5#if IS_QT
6 #include <QString>
7#endif
8#include <limits>
9
10namespace sf
11{
12
19{
20 public:
24 typedef int64_t int_type;
28 typedef double flt_type;
29
33 enum EType
34 {
36 vitReference = -1,
38 vitInvalid = 0,
51 };
52
57
61 Value& operator=(Value&&) noexcept;
62
66 Value(Value&&) noexcept;
67
73 Value(const Value& v);
74
81 explicit Value(const Value* v);
82
88 explicit Value(EType type);
89
95 explicit Value(flt_type v);
96
102 explicit Value(const char* v);
103
108 explicit Value(const std::string& v);
109
110#if IS_QT
111
116 explicit Value(const QString& v);
117
118#endif
119
125 explicit Value(bool v);
126
132 explicit Value(int v);
133
134#if IS_WIN
141 explicit Value(long v);
142#endif
143
149 explicit Value(unsigned v);
150
156 explicit Value(int_type v);
157
164 Value(const void* v, size_t size);
165
173 Value(EType type, const void* content, size_t size = 0);
174
181
189 inline Value& set(Value* v);
190
198 Value& set(const Value& v);
199
207 inline Value& set(bool v);
208
216 inline Value& set(int v);
217
218#if IS_WIN
227 inline Value& set(long v);
228#endif
229
237 inline Value& set(unsigned v);
238
246 inline Value& set(int_type v);
247
255 inline Value& set(flt_type v);
256
264 inline Value& set(const char* v);
265
273 inline Value& set(const std::string& v);
274
275#if IS_QT
276
284 inline Value& set(const QString& as);
285
286#endif
287
297 Value& set(int type, const void* content, size_t size = 0);
298
307 inline Value& set(const void* v, size_t size);
308
315 Value& assign(const Value& v);
316
324 inline Value& assign(bool v);
325
333 inline Value& assign(int v);
334
342 inline Value& assign(unsigned v);
343
344#if IS_WIN
352 inline Value& assign(const long v);
353#endif
354
362 inline Value& assign(flt_type v);
363
371 inline Value& assign(const char* v);
372
380 inline Value& assign(const std::string& v);
381
382#if IS_QT
383
391 inline Value& assign(const QString& s);
392
393#endif
394
403 inline Value& assign(const void* v, size_t size);
404
410 [[nodiscard]] inline EType getType() const;
411
418 [[nodiscard]] static EType getType(const char* type);
419
426 [[nodiscard]] static const char* getType(EType type);
427
434 bool setType(EType type);
435
441 [[nodiscard]] inline bool isValid() const;
442
448 [[nodiscard]] inline bool isNumber() const;
449
456 flt_type getFloat(int* cnv_err) const;
457
461 [[nodiscard]] inline flt_type getFloat() const;
462
468 int_type getInteger(int* cnv_err) const;
469
473 [[nodiscard]] inline int_type getInteger() const;
474
478 [[nodiscard]] inline size_t getSize() const;
479
485 [[nodiscard]] const void* getBinary() const;
486
492 [[nodiscard]] const char* getData() const;
493
501 [[nodiscard]] std::string getString(int precision = std::numeric_limits<int>::max()) const;
502
503#if IS_QT
504
508 [[nodiscard]] inline QString getQString(int precision = std::numeric_limits<int>::max()) const;
509
510#endif
511
517 Value& round(const Value& v);
518
528 [[nodiscard]] bool isZero() const;
529
530#if IS_QT
531
536 inline Value& operator=(const QString& v);
537
541 explicit operator QString() const;
542
543#endif
544
548 explicit inline operator std::string() const;
549
553 explicit inline operator bool() const;
554
558 inline bool operator!() const;
559
563 inline int operator==(const Value& v) const;
564
568 inline int operator!=(const Value& v) const;
569
573 inline int operator>(const Value& v) const;
574
578 inline int operator>=(const Value& v) const;
579
583 inline int operator<(const Value& v) const;
584
588 inline int operator<=(const Value& v) const;
589
593 inline Value& operator*=(const Value& v);
594
598 inline Value& operator/=(const Value& v);
599
603 inline Value& operator%=(const Value& v);
604
608 inline Value& operator+=(const Value& v);
609
613 inline Value& operator-=(const Value& v);
614
618 inline Value& operator=(const Value& v);
619
623 inline Value& operator=(const std::string& v);
624
628 inline Value& operator=(Value* v);
629
633 static Value calculateOffset(Value value, Value min, Value max, const Value& len, bool clip);
634
638 enum
639 {
641 maxString = 0xFFFF,
643 maxBinary = 0xFFFF,
645 maxCustom = 0xFFFF
646 };
647
651 [[nodiscard]] Value mul(const Value& v) const;
652
656 [[nodiscard]] Value div(const Value& v) const;
657
661 [[nodiscard]] Value add(const Value& v) const;
662
666 [[nodiscard]] Value sub(const Value& v) const;
667
671 [[nodiscard]] Value mod(const Value& v) const;
672
678 [[nodiscard]] int compare(const Value& v) const;
679
680 private:
684 void makeInvalid();
685
690 EType _type{vitUndefined};
691
695 size_t _size{0};
696
700 union
701 {
705 char* _ptr;
718 } _data{nullptr};
719
724 static const size_t _sizeExtra;
725
729 static const char* _typeNames[];
730
731 public:
735 static const char* _invalidStr;
740
741 friend Value operator*(const Value& v1, const Value& v2);
742
743 friend Value operator/(const Value& v1, const Value& v2);
744
745 friend Value operator%(const Value& v1, const Value& v2);
746
747 friend Value operator+(const Value& v1, const Value& v2);
748
749 friend Value operator-(const Value& v1, const Value& v2);
750
751 friend std::ostream& operator<<(std::ostream& os, const Value& v);
752
753 friend std::istream& operator>>(std::istream& is, Value& v);
754};
755
756inline Value& Value::set(bool v)
757{
758 return set(int_type(v));
759}
760
761inline Value& Value::set(int v)
762{
763 return set(int_type(v));
764}
765
766#if IS_WIN
767inline Value& Value::set(long v)
768{
769 return set(int_type(v));
770}
771#endif
772
773inline Value& Value::set(unsigned v)
774{
775 return set(int_type(v));
776}
777
779{
780 return set(vitInteger, &v);
781}
782
784{
785 return set(vitFloat, &v);
786}
787
788inline Value& Value::set(const char* v)
789{
790 return set(vitString, static_cast<const void*>(v));
791}
792
793inline Value& Value::set(const std::string& v)
794{
795 return set(vitString, static_cast<const void*>(v.data()));
796}
797
798#if IS_QT
799
800inline Value& Value::set(const QString& as)
801{
802 return set(as.toStdString());
803}
804
805inline QString Value::getQString(int precision) const
806{
807 return QString::fromStdString(getString(precision));
808}
809
810#endif
811
812inline Value& Value::set(const void* v, size_t size)
813{
814 return set(vitBinary, v, size);
815}
816
817inline Value& Value::assign(const bool v)
818{
819 int_type i = v;
820 return assign(&i, sizeof(int_type));
821}
822
823inline Value& Value::assign(const int v)
824{
825 int_type i = v;
826 return assign(&i, sizeof(int_type));
827}
828
829inline Value& Value::assign(const unsigned v)
830{
831 int_type i = v;
832 return assign(&i, sizeof(int_type));
833}
834
835#if IS_WIN
836inline Value& Value::assign(const long v)
837{
838 return assign(Value(v));
839}
840#endif
841
843{
844 return assign(Value(v));
845}
846
847inline Value& Value::assign(const char* v)
848{
849 return assign(Value(v));
850}
851
852inline Value& Value::assign(const std::string& v)
853{
854 return assign(Value(v));
855}
856
857#if IS_QT
858
859inline Value& Value::assign(const QString& s)
860{
861 return assign(Value(s));
862}
863
864#endif
865
866inline Value& Value::assign(const void* v, size_t size)
867{
868 return assign(Value(v, size));
869}
870
871inline Value::EType Value::getType() const// NOLINT(misc-no-recursion)
872{
873 return (_type == vitReference) ? _data._ref->getType() : _type;
874}
875
876inline bool Value::isValid() const// NOLINT(misc-no-recursion)
877{
878 return (_type == vitReference) ? _data._ref->isValid() : _type != vitInvalid;
879}
880
881inline bool Value::isNumber() const// NOLINT(misc-no-recursion)
882{
883 return (_type == vitReference) ? _data._ref->isNumber() : _type == vitInteger || _type == vitFloat;
884}
885
886inline size_t Value::getSize() const// NOLINT(misc-no-recursion)
887{
888 return (_type == vitReference) ? _data._ref->getSize() : _size;
889}
890
891inline Value::operator bool() const
892{
893 return !isZero();
894}
895
896inline bool Value::operator!() const
897{
898 return !isZero();
899}
900
901inline Value::operator std::string() const
902{
903 return getString();
904}
905
906inline int Value::operator==(const Value& v) const
907{
908 return !compare(v);
909}
910
911inline int Value::operator!=(const Value& v) const
912{
913 return compare(v) != 0;
914}
915
916inline int Value::operator>(const Value& v) const
917{
918 return compare(v) > 0;
919}
920
921inline int Value::operator>=(const Value& v) const
922{
923 return compare(v) >= 0;
924}
925
926inline int Value::operator<(const Value& v) const
927{
928 return compare(v) < 0;
929}
930
931inline int Value::operator<=(const Value& v) const
932{
933 return compare(v) <= 0;
934}
935
937{
938 return assign(mul(v));
939}
940
942{
943 return assign(div(v));
944}
945
947{
948 return assign(mod(v));
949}
950
952{
953 return assign(add(v));
954}
955
957{
958 return assign(sub(v));
959}
960
961inline Value& Value::operator=(const Value& v)
962{
963 assign(v);
964 return *this;
965}
966
967inline Value& Value::operator=(const std::string& v)
968{
969 assign(v);
970 return *this;
971}
972
973#if IS_QT
974
975inline Value& Value::operator=(const QString& v)
976{
977 assign(v);
978 return *this;
979}
980
981#endif
982
984{
985 set(v);
986 return *this;
987}
988
990{
991 return set(vitReference, static_cast<const void*>(v));
992}
993
995{
996 return getFloat(nullptr);
997}
998
1000{
1001 return getInteger(nullptr);
1002}
1003
1004inline Value operator*(const Value& v1, const Value& v2)
1005{
1006 return v1.mul(v2);
1007}
1008
1009inline Value operator/(const Value& v1, const Value& v2)
1010{
1011 return v1.div(v2);
1012}
1013
1014inline Value operator%(const Value& v1, const Value& v2)
1015{
1016 return v1.mod(v2);
1017}
1018
1019inline Value operator+(const Value& v1, const Value& v2)
1020{
1021 return v1.add(v2);
1022}
1023
1024inline Value operator-(const Value& v1, const Value& v2)
1025{
1026 return v1.sub(v2);
1027}
1028
1032_MISC_FUNC std::ostream& operator<<(std::ostream& os, const Value& v);
1033
1037_MISC_FUNC std::istream& operator>>(std::istream& is, Value& v);
1038
1039}// namespace sf
Counted vector having additional methods and operators for ease of usage.
Definition TVector.h:28
Value container class able to performing arithmetic functions.
Definition Value.h:19
int compare(const Value &v) const
Compare this with the passed value.
Value(const void *v, size_t size)
Binary type constructor for implicit vitBinary.
bool setType(EType type)
Convert the instance to the passed type.
EType
Enumerate for available types.
Definition Value.h:34
@ vitReference
Definition Value.h:36
@ vitFloat
Definition Value.h:44
@ vitCustom
Definition Value.h:50
@ vitBinary
Definition Value.h:48
@ vitUndefined
Definition Value.h:40
@ vitInteger
Definition Value.h:42
@ vitInvalid
Definition Value.h:38
@ vitString
Definition Value.h:46
int operator>=(const Value &v) const
Larger than or equal operator.
Definition Value.h:921
int_type getInteger() const
Returns an integer value of the current value if possible.
Definition Value.h:999
Value & operator/=(const Value &v)
Divide by operator.
Definition Value.h:941
bool isNumber() const
Checks if this is a numeric type (vitFloat or vitInteger) of instance.
Definition Value.h:881
Value(EType type, const void *content, size_t size=0)
Specific type constructor.
static Value calculateOffset(Value value, Value min, Value max, const Value &len, bool clip)
Calculates the offset for a given range and set point.
friend std::ostream & operator<<(std::ostream &os, const Value &v)
Value div(const Value &v) const
Divide this with the passed value.
Value & operator%=(const Value &v)
Modulus operator.
Definition Value.h:946
bool operator!() const
Boolean invert operator.
Definition Value.h:896
static EType getType(const char *type)
the type corresponding to the passed type string.
friend std::istream & operator>>(std::istream &is, Value &v)
Value & round(const Value &v)
Rounds the current instance to a multiple of the passed value.
Value(int_type v)
# 64bit int_type type constructor for implicit vitInteger.
static const char * getType(EType type)
the type string of the passed type enumeration value.
flt_type _flt
Definition Value.h:709
size_t getSize() const
Returns size of the occupied space.
Definition Value.h:886
int operator>(const Value &v) const
Larger than operator.
Definition Value.h:916
bool isValid() const
Checks if the instance is valid. If the type is equals vitInvalid.
Definition Value.h:876
Value()
Default constructor.
~Value()
Destructor.
Value mod(const Value &v) const
Modulus this with the passed value.
double flt_type
Type used internally for storing floating point value.
Definition Value.h:28
static const char * _invalidStr
Holds the invalid string when needed.
Definition Value.h:735
Value(bool v)
Boolean type constructor for implicit vitInteger.
int64_t int_type
Type used internally for storing integers.
Definition Value.h:24
const void * getBinary() const
Returns the pointer to the binary buffer if vitBinary.
Value sub(const Value &v) const
Subtract this with the passed value.
flt_type getFloat() const
Returns a floating point value of the current value if possible.
Definition Value.h:994
int_type getInteger(int *cnv_err) const
Returns an integer value of the current value if possible. if the current type is a string the 'cnv_e...
char * _ptr
Definition Value.h:705
Value * _ref
Definition Value.h:717
Value & operator-=(const Value &v)
Subtract operator.
Definition Value.h:956
int operator!=(const Value &v) const
Not equal operator.
Definition Value.h:911
std::string getString(int precision=std::numeric_limits< int >::max()) const
Gets the string of the value with the specified precision.
const char * getData() const
Returns pointer to the data.
bool isZero() const
Returns if the instance is type is zero.
int operator==(const Value &v) const
Equal operator.
Definition Value.h:906
Value add(const Value &v) const
Add this with the passed value.
Value & set(const Value &v)
Copies the content and type of the passed value.
Value & operator=(Value &&) noexcept
Move assignment operator is default.
int operator<(const Value &v) const
Less than operator.
Definition Value.h:926
EType getType() const
the current type for this instance.
Definition Value.h:871
flt_type getFloat(int *cnv_err) const
Returns a floating point value of the current value if possible.
Value(int v)
32bit integer type constructor for implicit vitInteger.
Value & set(int type, const void *content, size_t size=0)
Sets the instance type and content.
int_type _int
Definition Value.h:713
Value & assign(const Value &v)
Assigns a value of an instance but not changing the current type. Except for vitUndefined and vitInva...
Value(unsigned v)
32-bit unsigned integer type constructor for implicit vitInteger.
int operator<=(const Value &v) const
Less than or equal operator.
Definition Value.h:931
Value mul(const Value &v) const
Multiply this with the passed value.
Value & operator+=(const Value &v)
Add operator.
Definition Value.h:951
TVector< Value > vector_type
Definition Value.h:739
Value & operator*=(const Value &v)
Multiply by operator.
Definition Value.h:936
Value & set(Value *v)
Set this instance to references to the instance passed here as pointer.
Definition Value.h:989
#define _MISC_FUNC
Definition misc/global.h:34
#define _MISC_CLASS
Definition misc/global.h:35
Definition CodeEditor.h:8
const TQuaternion< T > operator-(const TQuaternion< T > &lhs, const TQuaternion< T > &rhs)
Definition TQuaternion.h:283
TARGET_IMPORT std::ostream & operator<<(std::ostream &os, const ResultData &)
Stream operator for the setup std::string.
TMatrix44< T > operator*(const TMatrix44< T > &lm, const TMatrix44< T > &rm)
Multiplies to matrices into a single one.
Definition TMatrix44.h:352
Value operator%(const Value &v1, const Value &v2)
Definition Value.h:1014
int precision(double value)
Returns the precision of the passed floating point value. This is the amount of characters after the ...
const TQuaternion< T > operator/(const TQuaternion< T > &lhs, const TQuaternion< T > &rhs)
Definition TQuaternion.h:295
TARGET_IMPORT std::istream & operator>>(std::istream &is, ResultData &)
Stream operator for setting up this instance with a setup std::string.
const TQuaternion< T > operator+(const TQuaternion< T > &lhs, const TQuaternion< T > &rhs)
Definition TQuaternion.h:277
bool operator==(const Md5Hash::hash_type &h1, const Md5Hash::hash_type &h2)
Compare operator for storing an MD5 hash.
QRect operator+=(QRect &rc, const QPoint &pt)
Allows adjusting the QRect position using a QPoint.
Definition qt_utils.h:80
QPoint operator+(const QPoint &pt, const QSize &sz)
Allows moving a QPoint using a QSize.
Definition qt_utils.h:44
QPoint operator-(const QPoint &pt, const QSize &sz)
Allows moving a QPoint using a QSize.
Definition qt_utils.h:35
QRect operator-=(QRect &rc, const QPoint &pt)
Allows adjusting the QRect position using a QPoint.
Definition qt_utils.h:99