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 char* ptr) {
26 size_t length = strlen(ptr) + 1;
27 _ptr = new char[length];
28 strcpy_s(_ptr, length, ptr);
29 }
30 StringWrapper(const std::string& val)
31 {
32 _ptr = new char[val.size() + 1] {};
33 val.copy(_ptr, val.size());
34 }
35 StringWrapper(std::string&& val)
36 {
37 _ptr = new char[val.size() + 1] {};
38 memmove_s(_ptr, val.size(), val.c_str(), val.size());
39 }
40
42 {
43 size_t length = strlen(other._ptr) + 1;
44 _ptr = new char[length] {};
45 memcpy(_ptr, other._ptr, length);
46 }
47 StringWrapper(StringWrapper&& other) noexcept
48 {
49 size_t length = strlen(other._ptr) + 1;
50 _ptr = new char[length];
51 memmove_s(_ptr, length, other._ptr, length);
52 other._ptr = nullptr;
53 }
54 StringWrapper& operator=(const char* ptr) {
55 if (_ptr != nullptr) {
56 delete[] _ptr;
57 _ptr = nullptr;
58 }
59
60 size_t length = strlen(ptr) + 1;
61 _ptr = new char[length];
62 strcpy_s(_ptr, length, ptr);
63 return *this;
64 }
66 {
67 if (_ptr != nullptr)
68 {
69 delete[] _ptr;
70 _ptr = nullptr;
71 }
72 size_t length = strlen(other._ptr) + 1;
73 _ptr = new char[length] {};
74 memcpy(_ptr, other._ptr, length);
75 return *this;
76 }
78 {
79 if (_ptr != nullptr)
80 {
81 delete[] _ptr;
82 _ptr = nullptr;
83 }
84 size_t length = strlen(other._ptr) + 1;
85 _ptr = new char[length] {};
86 memmove_s(_ptr, length, other._ptr, length);
87 other._ptr = nullptr;
88 return *this;
89 }
90
91 bool operator==(const StringWrapper& other)
92 {
93 const int t = strcmp(_ptr, other._ptr);
94 return t == 0;
95 }
96 bool operator<(const StringWrapper& other)
97 {
98 const int t = strcmp(_ptr, other._ptr);
99 return t < 0;
100 }
101 bool operator>(const StringWrapper& other)
102 {
103 const int t = strcmp(_ptr, other._ptr);
104 return t > 0;
105 }
106
107 operator const char* () const { return _ptr; }
108
110 {
111 if (_ptr != nullptr)
112 {
113 delete[] _ptr;
114 _ptr = nullptr;
115 }
116 }
117 private:
118 char* _ptr = nullptr;
119 };
120
121
123 {
124 public:
126 Variable(float value);
127 Variable(double value);
128 Variable(int32_t value);
129 Variable(int64_t value);
130 Variable(bool value);
131 Variable(const char* value);
132 Variable(const std::string& value);
133 Variable(std::string&& value) noexcept;
134 Variable(const complex& value);
135 Variable(complex&& value) noexcept;
136 Variable(const Variable& other) = default;
137 Variable(Variable&& other) = default;
138 Variable& operator=(const Variable& other);
139 Variable& operator=(Variable&& other) noexcept;
140
141 void setValue(float value);
142 void setValue(double value);
143 void setValue(int32_t value);
144 void setValue(int64_t value);
145 void setValue(bool value);
146 void setValue(const char* value);
147 void setValue(const std::string& value);
148 void setValue(std::string&& value);
149 void setValue(const complex& _value);
150 void setValue(complex&& _value);
151
152 bool isFloat() const;
153 bool isDouble() const;
154 bool isInt32() const;
155 bool isInt64() const;
156 bool isBool() const;
157 bool isConstCharPtr() const;
158 bool isComplex() const;
159
160 float getFloat() const;
161 double getDouble() const;
162 int32_t getInt32() const;
163 int64_t getInt64() const;
164 bool getBool() const;
165 const char* getConstCharPtr() const;
166 const complex getComplex() const;
167
168 bool operator==(const Variable& other)const;
169 bool operator>(const Variable& other)const;
170 bool operator<(const Variable& other)const;
171
172 std::string getTypeName()const;
173
174 private:
175 using variable_t = std::variant<int32_t, int64_t, bool, float, double ,StringWrapper, complex>;
176 inline bool DoubleCompare(const double& a, const double& b) const
177 {
178 constexpr const double epsilon = 1.0e-12; //std::numeric_limits<double>::epsilon()
179 //Adaptive eps. Source: https://embeddeduse.com/2019/08/26/qt-compare-two-floats/
180 if (abs(a - b) <= epsilon)
181 {
182 return true;
183 }
184 return abs(a - b) <= epsilon * (std::max)(abs(a), abs(b));
185 }
186
187 inline const bool FloatCompare(const float& a, const float& b) const
188 {
189 constexpr const float epsilon = 1.0e-6f; //std::numeric_limits<float>::epsilon()
190 //Adaptive eps. Source: https://embeddeduse.com/2019/08/26/qt-compare-two-floats/
191 if (abs(a - b) <= epsilon)
192 {
193 return true;
194 }
195 return abs(a - b) <= epsilon * (std::max)(abs(a), abs(b));
196 }
197 variable_t _value;
198 };
199
200}
#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:45
strcpy_s(retVal, response.length()+1, response.c_str())
Some type names are not system independent, hence the need to create a standardised way for this proj...
Definition Variable.h:23
StringWrapper(const char *ptr)
Definition Variable.h:25
StringWrapper(std::string &&val)
Definition Variable.h:35
StringWrapper(const StringWrapper &other)
Definition Variable.h:41
StringWrapper & operator=(const StringWrapper &other)
Definition Variable.h:65
StringWrapper & operator=(const char *ptr)
Definition Variable.h:54
StringWrapper(StringWrapper &&other) noexcept
Definition Variable.h:47
StringWrapper & operator=(StringWrapper &&other) noexcept
Definition Variable.h:77
bool operator<(const StringWrapper &other)
Definition Variable.h:96
StringWrapper(const std::string &val)
Definition Variable.h:30
bool operator>(const StringWrapper &other)
Definition Variable.h:101
bool operator==(const StringWrapper &other)
Definition Variable.h:91
~StringWrapper()
Definition Variable.h:109
Definition Variable.h:123
Variable()
Definition Variable.h:125
Variable(const Variable &other)=default
Variable(Variable &&other)=default
Definition Connector.h:8
std::complex< double > complex
Definition ComplexNumbers.h:6