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
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
552
557
562
567
572
577
581 Value& operator=(const std::string& v);
582
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
711_MISC_FUNC std::ostream& operator<<(std::ostream& os, const Value& v);
712
716_MISC_FUNC std::istream& operator>>(std::istream& is, Value& v);
717
718}// namespace sf
719
720// Include all inlined functions and template implementations.
721#include <misc/gen/Value.hpp>
Counted vector having additional methods and operators for ease of usage. This template class extends...
Definition TVector.h:20
Value container class able to performing arithmetic functions.
Definition Value.h:19
friend Value operator-(const Value &v1, const Value &v2)
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
@ 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
@ vitString
Definition Value.h:46
int operator>=(const Value &v) const
Larger than or equal operator.
friend Value operator/(const Value &v1, const Value &v2)
friend Value operator*(const Value &v1, const Value &v2)
Value & assign(const std::string &v)
Assigns a string value but not changing the current type. Except for vitUndefined and vitInvalid type...
Value & operator/=(const Value &v)
Divide by operator.
bool isNumber() const
Checks if this is a numeric type (vitFloat or vitInteger) of instance.
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 Value operator+(const Value &v1, const Value &v2)
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 & set(flt_type v)
Sets the type and content. An existing type is undone.
Value & assign(flt_type v)
Assigns a floating point value but not changing the current type. Except for vitUndefined and vitInva...
Value & operator%=(const Value &v)
Modulus operator.
Value & operator=(const Value &v)
Assignment operator that only change the contents. Calls actually assign()
bool operator!() const
Boolean invert operator.
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.
friend Value operator%(const Value &v1, const Value &v2)
flt_type _flt
Definition Value.h:667
Value & set(int v)
Sets the type and content. An existing type is undone.
flt_type getFloat() const
Gets a floating point value of the current value if possible.
size_t getSize() const
Gets the size of the occupied space.
int operator>(const Value &v) const
Larger than operator.
bool isValid() const
Checks if the instance is valid. If the type is equals vitInvalid.
Value & assign(int v)
Assigns an integer value but not changing the current type. Except for vitUndefined and vitInvalid ty...
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 & operator=(const Value *v)
Assignment operator setting this instance to references the past instance pointer.
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.
Value & set(const char *v)
Sets the type and content. An existing type is undone.
Value & set(const void *v, size_t size)
Sets the type implicitly to vitBinary. An existing type is undone.
int64_t int_type
Type used internally for storing integers.
Definition Value.h:24
int_type getInteger() const
Returns an integer value of the current value if possible.
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.
Value & set(const Value *v)
Set this instance to references to the instance passed here as pointer. Makes this instance an alias ...
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
EType getType() const
Gets the current type for this instance.
Value * _ref
Definition Value.h:675
Value & operator-=(const Value &v)
Subtract operator.
Value & operator=(const std::string &v)
Assignment operator that only change the contents.
int operator!=(const Value &v) const
Not equal operator.
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.
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 & assign(unsigned v)
Assigns an unsigned integer value but not changing the current type. Except for vitUndefined and vitI...
Value & set(int_type v)
Sets the type and content. An existing type is undone.
Value & set(const std::string &v)
Sets the type and content. An existing type is undone.
Value & operator=(Value &&) noexcept
Move assignment operator is default.
int operator<(const Value &v) const
Less than operator.
Value & assign(bool v)
Assigns a boolean value but not changing the current type. Except for vitUndefined and vitInvalid typ...
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.
Value & assign(const char *v)
Assigns a string value but not changing the current type. Except for vitUndefined and vitInvalid type...
int operator<=(const Value &v) const
Less than or equal operator.
Value mul(const Value &v) const
Multiply this with the passed value.
Value & operator+=(const Value &v)
Add operator.
Value & set(unsigned v)
Sets the type and content. An existing type is undone.
TVector< Value > vector_type
Definition Value.h:697
Value & operator*=(const Value &v)
Multiply by operator.
Value & set(bool v)
Sets the type and content. An existing type is undone.
Value & assign(const void *v, size_t size)
Assigns raw binary data but not changing the current type. Except for vitUndefined and vitInvalid typ...
#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.
_GII_FUNC std::ostream & operator<<(std::ostream &os, const ResultData &)
Stream operator for the setup std::string.