OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
Variable.h
Go to the documentation of this file.
1/*****************************************************************/
8#pragma once
9#include <variant>
10#include <stdint.h>
12#include "OTCore/TypeNames.h"
13#include <limits>
14#include <algorithm>
15#include <math.h>
16#include "ComplexNumbers.h"
17
18#pragma warning(disable:4251)
19namespace ot
20{
21
23 {
24 public:
25 StringWrapper(const std::string& val)
26 {
27 _ptr = new char[val.size() + 1] {};
28 val.copy(_ptr, val.size());
29 }
30 StringWrapper(std::string&& val)
31 {
32 _ptr = new char[val.size() + 1] {};
33 memmove_s(_ptr, val.size(), val.c_str(), val.size());
34 }
35
37 {
38 size_t length = strlen(other._ptr) + 1;
39 _ptr = new char[length] {};
40 memcpy(_ptr, other._ptr, length);
41 }
42 StringWrapper(StringWrapper&& other) noexcept
43 {
44 size_t length = strlen(other._ptr) + 1;
45 _ptr = new char[length];
46 memmove_s(_ptr, length, other._ptr, length);
47 other._ptr = nullptr;
48 }
50 {
51 if (_ptr != nullptr)
52 {
53 delete[] _ptr;
54 _ptr = nullptr;
55 }
56 size_t length = strlen(other._ptr) + 1;
57 _ptr = new char[length] {};
58 memcpy(_ptr, other._ptr, length);
59 return *this;
60 }
62 {
63 if (_ptr != nullptr)
64 {
65 delete[] _ptr;
66 _ptr = nullptr;
67 }
68 size_t length = strlen(other._ptr) + 1;
69 _ptr = new char[length] {};
70 memmove_s(_ptr, length, other._ptr, length);
71 other._ptr = nullptr;
72 return *this;
73 }
74
75 bool operator==(const StringWrapper& other)
76 {
77 const int t = strcmp(_ptr, other._ptr);
78 return t == 0;
79 }
80 bool operator<(const StringWrapper& other)
81 {
82 const int t = strcmp(_ptr, other._ptr);
83 return t < 0;
84 }
85 bool operator>(const StringWrapper& other)
86 {
87 const int t = strcmp(_ptr, other._ptr);
88 return t > 0;
89 }
90
91 operator const char* () const { return _ptr; }
92
94 {
95 if (_ptr != nullptr)
96 {
97 delete[] _ptr;
98 _ptr = nullptr;
99 }
100 }
101 private:
102 char* _ptr = nullptr;
103 };
104
105
107 {
108 public:
110 Variable(float value);
111 Variable(double value);
112 Variable(int32_t value);
113 Variable(int64_t value);
114 Variable(bool value);
115 Variable(const char* value);
116 Variable(const std::string& value);
117 Variable(std::string&& value) noexcept;
118 Variable(const complex& value);
119 Variable(complex&& value) noexcept;
120 Variable(const Variable& other) = default;
121 Variable(Variable&& other) = default;
122 Variable& operator=(const Variable& other);
123 Variable& operator=(Variable&& other) noexcept;
124
125 void setValue(float value);
126 void setValue(double value);
127 void setValue(int32_t value);
128 void setValue(int64_t value);
129 void setValue(bool value);
130 void setValue(const char* value);
131 void setValue(const std::string& value);
132 void setValue(std::string&& value);
133 void setValue(const complex& _value);
134 void setValue(complex&& _value);
135
136 bool isFloat() const;
137 bool isDouble() const;
138 bool isInt32() const;
139 bool isInt64() const;
140 bool isBool() const;
141 bool isConstCharPtr() const;
142 bool isComplex() const;
143
144 float getFloat() const;
145 double getDouble() const;
146 int32_t getInt32() const;
147 int64_t getInt64() const;
148 bool getBool() const;
149 const char* getConstCharPtr() const;
150 const complex getComplex() const;
151
152 bool operator==(const Variable& other)const;
153 bool operator>(const Variable& other)const;
154 bool operator<(const Variable& other)const;
155
156 std::string getTypeName()const;
157
158 private:
159 using variable_t = std::variant<int32_t, int64_t, bool, float, double ,StringWrapper, complex>;
160 inline bool DoubleCompare(const double& a, const double& b) const
161 {
162 constexpr const double epsilon = 1.0e-12; //std::numeric_limits<double>::epsilon()
163 //Adaptive eps. Source: https://embeddeduse.com/2019/08/26/qt-compare-two-floats/
164 if (abs(a - b) <= epsilon)
165 {
166 return true;
167 }
168 return abs(a - b) <= epsilon * (std::max)(abs(a), abs(b));
169 }
170
171 inline const bool FloatCompare(const float& a, const float& b) const
172 {
173 constexpr const float epsilon = 1.0e-6f; //std::numeric_limits<float>::epsilon()
174 //Adaptive eps. Source: https://embeddeduse.com/2019/08/26/qt-compare-two-floats/
175 if (abs(a - b) <= epsilon)
176 {
177 return true;
178 }
179 return abs(a - b) <= epsilon * (std::max)(abs(a), abs(b));
180 }
181 variable_t _value;
182 };
183
184}
#define OT_CORE_API_EXPORT
Dll import.
Definition CoreAPIExport.h:8
bsoncxx::types::value value
Definition DocumentManager.h:16
bool operator==(const FaceSelection &left, const FaceSelection &right)
Definition Model.cpp:55
Some type names are not system independent, hence the need to create a standardised way for this proj...
Definition Variable.h:23
StringWrapper(std::string &&val)
Definition Variable.h:30
StringWrapper(const StringWrapper &other)
Definition Variable.h:36
StringWrapper & operator=(const StringWrapper &other)
Definition Variable.h:49
StringWrapper(StringWrapper &&other) noexcept
Definition Variable.h:42
StringWrapper & operator=(StringWrapper &&other) noexcept
Definition Variable.h:61
bool operator<(const StringWrapper &other)
Definition Variable.h:80
StringWrapper(const std::string &val)
Definition Variable.h:25
bool operator>(const StringWrapper &other)
Definition Variable.h:85
bool operator==(const StringWrapper &other)
Definition Variable.h:75
~StringWrapper()
Definition Variable.h:93
Definition Variable.h:107
Variable()
Definition Variable.h:109
Variable(const Variable &other)=default
Variable(Variable &&other)=default
Definition Connector.h:8
std::complex< double > complex
Definition ComplexNumbers.h:6