-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValue.hpp
More file actions
81 lines (69 loc) · 2.17 KB
/
Value.hpp
File metadata and controls
81 lines (69 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Created by Piotrek Rybiec on 29/04/2025.
//
#ifndef VALUE_H
#define VALUE_H
#include "data_types.hpp"
class Value {
private:
VariantType value;
public:
Value() : value(std::monostate{}){} // use null as default
explicit Value(int v) : value(v){}
explicit Value(double v) : value(v){}
explicit Value(bool v) : value(v){}
explicit Value(const std::string& v) : value(v){}
explicit Value(const char* v) : value(std::string(v)){} // for inline Value("adasd") definition
explicit Value(const Date& v) : value(v){}
explicit Value(const DateTime& v) : value(v){}
Value(const Value& other) = default; // copy constructor
Value& operator=(const Value& other) = default; // assignment operator
/*
Value v1;
Value v2 = Value::Null();
v1.isNull() == v2.isNull();
*/
static Value Null();
bool isNull() const;
DataType getType() const;
std::string toString() const;
// templates need to be in headers
template<typename T>
T get() const {
try {
return std::get<T>(value);
} catch (const std::bad_variant_access&) {
throw std::runtime_error("invalid type access in Value");
}
}
// operators
bool operator==(const Value& o) const {
if (isNull() && o.isNull()) return true;
if (isNull() || o.isNull()) return false;
return value == o.value;
}
bool operator!=(const Value& o) const {
return !(*this == o);
}
bool operator<(const Value& o) const {
if (isNull() || o.isNull()) return false;
return std::visit([](const auto& a, const auto& b) -> bool {
using A = std::decay_t<decltype(a)>;
using B = std::decay_t<decltype(b)>;
if constexpr (std::is_same_v<A, B>) {
return a < b;
}
throw std::runtime_error("cannot compare values of different types");
}, value, o.value);
}
bool operator>(const Value& o) const {
return o < *this;
}
bool operator<=(const Value& o) const {
return !(o < *this);
}
bool operator>=(const Value& o) const {
return !(*this < o);
}
};
#endif //VALUE_H