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 <misc/gen/TVector.h>
4#include <misc/global.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
72 Value(const Value& v);
73
79 explicit Value(const Value* v);
80
85 explicit Value(EType type);
86
91 explicit Value(flt_type v);
92
97 explicit Value(const char* v);
98
103 explicit Value(const std::string& v);
104
105#if IS_QT
106
111 explicit Value(const QString& v);
112
113#endif
114
119 explicit Value(bool v);
120
125 explicit Value(int v);
126
127#if IS_WIN
134 explicit Value(long v);
135#endif
136
141 explicit Value(unsigned v);
142
147 explicit Value(int_type v);
148
154 Value(const void* v, size_t size);
155
162 Value(EType type, const void* content, size_t size = 0);
163
169
176 inline Value& set(const Value* v);
177
184 Value& set(const Value& v);
185
192 inline Value& set(bool v);
193
200 inline Value& set(int v);
201
202#if IS_WIN
211 Value& set(long v);
212#endif
213
220 inline Value& set(unsigned v);
221
228 inline Value& set(int_type v);
229
236 inline Value& set(flt_type v);
237
244 inline Value& set(const char* v);
245
252 inline Value& set(const std::string& v);
253
254#if IS_QT
255
262 inline Value& set(const QString& qs);
263
264#endif
265
274 Value& set(int type, const void* content, size_t size = 0);
275
283 inline Value& set(const void* v, size_t size);
284
291 Value& assign(const Value& v);
292
299 inline Value& assign(bool v);
300
307 inline Value& assign(int v);
308
315 inline Value& assign(unsigned v);
316
317#if IS_WIN
325 Value& assign(const long v);
326#endif
327
334 inline Value& assign(flt_type v);
335
342 inline Value& assign(const char* v);
343
350 inline Value& assign(const std::string& v);
351
352#if IS_QT
353
360 inline Value& assign(const QString& s);
361
362#endif
363
371 inline Value& assign(const void* v, size_t size);
372
377 [[nodiscard]] inline EType getType() const;
378
384 [[nodiscard]] static EType getType(const std::string_view& type);
385
391 [[nodiscard]] static std::string_view getType(EType type);
392
398 bool setType(EType type);
399
404 [[nodiscard]] bool isValid() const;
405
410 [[nodiscard]] bool isNumber() const;
411
417 flt_type getFloat(int* cnv_err) const;
418
422 [[nodiscard]] flt_type getFloat() const;
423
429 int_type getInteger(int* cnv_err) const;
430
434 [[nodiscard]] int_type getInteger() const;
435
439 [[nodiscard]] size_t getSize() const;
440
445 [[nodiscard]] const void* getBinary() const;
446
451 [[nodiscard]] const char* getData() const;
452
460 [[nodiscard]] std::string getString(int precision = std::numeric_limits<int>::max()) const;
461
462#if IS_QT
463
467 [[nodiscard]] QString getQString(int precision = std::numeric_limits<int>::max()) const;
468
469#endif
470
476 Value& round(const Value& v);
477
486 [[nodiscard]] bool isZero() const;
487
488#if IS_QT
489
494 Value& operator=(const QString& v);
495
499 explicit operator QString() const;
500
501#endif
502
506 explicit operator std::string() const;
507
511 explicit operator bool() const;
512
516 bool operator!() const;
517
521 int operator==(const Value& v) const;
522
526 int operator!=(const Value& v) const;
527
531 int operator>(const Value& v) const;
532
536 int operator>=(const Value& v) const;
537
541 int operator<(const Value& v) const;
542
546 int operator<=(const Value& v) const;
547
551 Value& operator*=(const Value& v);
552
556 Value& operator/=(const Value& v);
557
561 Value& operator%=(const Value& v);
562
566 Value& operator+=(const Value& v);
567
571 Value& operator-=(const Value& v);
572
576 Value& operator=(const Value& v);
577
581 Value& operator=(const std::string& v);
582
586 Value& operator=(const Value* v);
587
591 static Value calculateOffset(Value value, Value min, Value max, const Value& len, bool clip);
592
596 enum
597 {
599 maxString = 0xFFFF,
601 maxBinary = 0xFFFF,
603 maxCustom = 0xFFFF
604 };
605
609 [[nodiscard]] Value mul(const Value& v) const;
610
614 [[nodiscard]] Value div(const Value& v) const;
615
619 [[nodiscard]] Value add(const Value& v) const;
620
624 [[nodiscard]] Value sub(const Value& v) const;
625
629 [[nodiscard]] Value mod(const Value& v) const;
630
636 [[nodiscard]] int compare(const Value& v) const;
637
638 private:
642 void makeInvalid();
643
648 EType _type{vitUndefined};
649
653 size_t _size{0};
654
658 union
659 {
663 char* _ptr;
676 } _data{nullptr};
677
682 static const size_t _sizeExtra;
683
687 static const char* _typeNames[];
688
689 public:
693 static const char* _invalidStr;
698
699 friend Value operator*(const Value& v1, const Value& v2);
700 friend Value operator/(const Value& v1, const Value& v2);
701 friend Value operator%(const Value& v1, const Value& v2);
702 friend Value operator+(const Value& v1, const Value& v2);
703 friend Value operator-(const Value& v1, const Value& v2);
704 friend std::ostream& operator<<(std::ostream& os, const Value& v);
705 friend std::istream& operator>>(std::istream& is, Value& v);
706};
707
708inline Value& Value::set(bool v)
709{
710 return set(static_cast<int_type>(v));
711}
712
713inline Value& Value::set(int v)
714{
715 return set(static_cast<int_type>(v));
716}
717
718#if IS_WIN
719inline Value& Value::set(long v)
720{
721 return set(int_type(v));
722}
723#endif
724
725inline Value& Value::set(unsigned v)
726{
727 return set(static_cast<int_type>(v));
728}
729
730inline Value& Value::set(int64_t v)
731{
732 return set(vitInteger, &v);
733}
734
736{
737 return set(vitFloat, &v);
738}
739
740inline Value& Value::set(const char* v)
741{
742 return set(vitString, v);
743}
744
745inline Value& Value::set(const std::string& v)
746{
747 return set(vitString, v.data());
748}
749
750#if IS_QT
751
752inline Value& Value::set(const QString& qs)
753{
754 return set(qs.toStdString());
755}
756
757inline QString Value::getQString(int precision) const
758{
759 return QString::fromStdString(getString(precision));
760}
761
762#endif
763
764inline Value& Value::set(const void* v, size_t size)
765{
766 return set(vitBinary, v, size);
767}
768
769inline Value& Value::assign(const bool v)
770{
771 const int_type i = v;
772 return assign(&i, sizeof(int_type));
773}
774
775inline Value& Value::assign(const int v)
776{
777 const int_type i = v;
778 return assign(&i, sizeof(int_type));
779}
780
781inline Value& Value::assign(const unsigned v)
782{
783 const int_type i = v;
784 return assign(&i, sizeof(int_type));
785}
786
787#if IS_WIN
788inline Value& Value::assign(const long v)
789{
790 return assign(Value(v));
791}
792#endif
793
795{
796 return assign(Value(v));
797}
798
799inline Value& Value::assign(const char* v)
800{
801 return assign(Value(v));
802}
803
804inline Value& Value::assign(const std::string& v)
805{
806 return assign(Value(v));
807}
808
809#if IS_QT
810
811inline Value& Value::assign(const QString& s)
812{
813 return assign(Value(s));
814}
815
816#endif
817
818inline Value& Value::assign(const void* v, size_t size)
819{
820 return assign(Value(v, size));
821}
822
824{
825 return _type == vitReference ? _data._ref->getType() : _type;
826}
827
828inline bool Value::isValid() const
829{
830 return _type == vitReference ? _data._ref->isValid() : _type != vitInvalid;
831}
832
833inline bool Value::isNumber() const
834{
835 return _type == vitReference ? _data._ref->isNumber() : _type == vitInteger || _type == vitFloat;
836}
837
838inline size_t Value::getSize() const
839{
840 return _type == vitReference ? _data._ref->getSize() : _size;
841}
842
843inline Value::operator bool() const
844{
845 return !isZero();
846}
847
848inline bool Value::operator!() const
849{
850 return !isZero();
851}
852
853inline Value::operator std::string() const
854{
855 return getString();
856}
857
858inline int Value::operator==(const Value& v) const
859{
860 return !compare(v);
861}
862
863inline int Value::operator!=(const Value& v) const
864{
865 return compare(v) != 0;
866}
867
868inline int Value::operator>(const Value& v) const
869{
870 return compare(v) > 0;
871}
872
873inline int Value::operator>=(const Value& v) const
874{
875 return compare(v) >= 0;
876}
877
878inline int Value::operator<(const Value& v) const
879{
880 return compare(v) < 0;
881}
882
883inline int Value::operator<=(const Value& v) const
884{
885 return compare(v) <= 0;
886}
887
889{
890 return assign(mul(v));
891}
892
894{
895 return assign(div(v));
896}
897
899{
900 return assign(mod(v));
901}
902
904{
905 return assign(add(v));
906}
907
909{
910 return assign(sub(v));
911}
912
913inline Value& Value::operator=(const Value& v)
914{
915 assign(v);
916 return *this;
917}
918
919inline Value& Value::operator=(const std::string& v)
920{
921 assign(v);
922 return *this;
923}
924
925#if IS_QT
926
927inline Value& Value::operator=(const QString& v)
928{
929 assign(v);
930 return *this;
931}
932
933#endif
934
935inline Value& Value::operator=(const Value* v)
936{
937 set(v);
938 return *this;
939}
940
941inline Value& Value::set(const Value* v)
942{
943 return set(vitReference, v);
944}
945
947{
948 return getFloat(nullptr);
949}
950
952{
953 return getInteger(nullptr);
954}
955
956inline Value operator*(const Value& v1, const Value& v2)
957{
958 return v1.mul(v2);
959}
960
961inline Value operator/(const Value& v1, const Value& v2)
962{
963 return v1.div(v2);
964}
965
966inline Value operator%(const Value& v1, const Value& v2)
967{
968 return v1.mod(v2);
969}
970
971inline Value operator+(const Value& v1, const Value& v2)
972{
973 return v1.add(v2);
974}
975
976inline Value operator-(const Value& v1, const Value& v2)
977{
978 return v1.sub(v2);
979}
980
984_MISC_FUNC std::ostream& operator<<(std::ostream& os, const Value& v);
985
989_MISC_FUNC std::istream& operator>>(std::istream& is, Value& v);
990
991}// namespace sf
sf::TimeSpec operator+(const timespec &lhs, const timespec &rhs)
Addition operator for std::timespec types which also work for sf::TimeSpec.
Definition TimeSpec.h:370
sf::TimeSpec operator-(const timespec &lhs, const timespec &rhs)
Subtraction operator for std::timespec types which also work for sf::TimeSpec.
Definition TimeSpec.h:381
Counted vector having additional methods and operators for ease of usage.
Definition TVector.h:25
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.
static std::string_view getType(EType type)
Gets the type string of the passed type enumeration value.
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:873
int_type getInteger() const
Returns an integer value of the current value if possible.
Definition Value.h:951
Value & operator/=(const Value &v)
Divide by operator.
Definition Value.h:893
bool isNumber() const
Checks if this is a numeric type (vitFloat or vitInteger) of instance.
Definition Value.h:833
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 class output stream operator.
Value div(const Value &v) const
Divide this with the passed value.
Value & operator%=(const Value &v)
Modulus operator.
Definition Value.h:898
bool operator!() const
Boolean invert operator.
Definition Value.h:848
friend std::istream & operator>>(std::istream &is, Value &v)
Value class input stream operator.
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.
flt_type _flt
Definition Value.h:667
size_t getSize() const
Gets the size of the occupied space.
Definition Value.h:838
int operator>(const Value &v) const
Larger than operator.
Definition Value.h:868
bool isValid() const
Checks if the instance is valid. If the type is equals vitInvalid.
Definition Value.h:828
Value()
Default constructor.
static EType getType(const std::string_view &type)
Gets the type corresponding to the passed type string.
~Value()
Destructor. Frees the allocated memory if some was allocated.
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:693
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
Gets the pointer to the binary buffer if vitBinary.
Value sub(const Value &v) const
Subtract this with the passed value.
flt_type getFloat() const
Gets a floating point value of the current value if possible.
Definition Value.h:946
Value & set(const Value *v)
Set this instance to references to the instance passed here as pointer. Makes this instance an alias ...
Definition Value.h:941
int_type getInteger(int *cnv_err) const
Gets an integer value of the current value if possible. if the current type is a string the 'cnv_err'...
char * _ptr
Definition Value.h:663
Value * _ref
Definition Value.h:675
Value & operator-=(const Value &v)
Subtract operator.
Definition Value.h:908
int operator!=(const Value &v) const
Not equal operator.
Definition Value.h:863
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
Gets pointer to the data. Only when the type is vitString, vitBinary or vitCustom.
bool isZero() const
Returns if the instance is type is zero. For non-numeric values this is when the data length is zero....
int operator==(const Value &v) const
Equal operator.
Definition Value.h:858
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. An existing reference type is undone.
Value & operator=(Value &&) noexcept
Move assignment operator is default.
int operator<(const Value &v) const
Less than operator.
Definition Value.h:878
EType getType() const
Gets the current type for this instance.
Definition Value.h:823
flt_type getFloat(int *cnv_err) const
Returns a floating point value of the current value if possible. When the current type is a string th...
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. Called by constructors and all set functions.
int_type _int
Definition Value.h:671
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:883
Value mul(const Value &v) const
Multiply this with the passed value.
Value & operator+=(const Value &v)
Add operator.
Definition Value.h:903
TVector< Value > vector_type
Definition Value.h:697
Value & operator*=(const Value &v)
Multiply by operator.
Definition Value.h:888
#define _MISC_FUNC
Definition misc/global.h:39
#define _MISC_CLASS
Definition misc/global.h:40
Definition Application.h:10
_GII_FUNC std::istream & operator>>(std::istream &is, ResultData &)
Stream operator for setting up this instance with a setup std::string.
TMatrix44< T > operator*(const TMatrix44< T > &lm, const TMatrix44< T > &rm)
Multiplies to matrices into a single one.
TQuaternion< T > operator/(const TQuaternion< T > &lhs, const TQuaternion< T > &rhs)
Divides two quaternions.
Definition TQuaternion.h:416
TQuaternion< T > operator+(const TQuaternion< T > &lhs, const TQuaternion< T > &rhs)
Adds two quaternions.
Definition TQuaternion.h:377
Value operator%(const Value &v1, const Value &v2)
Definition Value.h:966
_GII_FUNC std::ostream & operator<<(std::ostream &os, const ResultData &)
Stream operator for the setup std::string.
_MISC_FUNC bool operator==(const Md5Hash::hash_type &h1, const Md5Hash::hash_type &h2)
Compare operator for storing an MD5 hash.
TQuaternion< T > operator-(const TQuaternion< T > &lhs, const TQuaternion< T > &rhs)
Subtracts two quaternions.
Definition TQuaternion.h:390
_MISC_FUNC int precision(double value)
Returns the precision of the passed floating point value. This is the amount of characters after the ...
bool isZero(T value, T epsilon=std::numeric_limits< T >::epsilon())
Check if the passed value is zero or near zero according the given epsilon.
Definition misc/gen/math.h:245
QRect operator+=(QRect &rc, const QPoint &pt)
Allows adjusting the QRect position using a QPoint.
Definition qt_utils.h:66
QRect operator-=(QRect &rc, const QPoint &pt)
Allows adjusting the QRect position using a QPoint.
Definition qt_utils.h:83