This repository was archived by the owner on May 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableEntry.cpp
More file actions
107 lines (97 loc) · 3.15 KB
/
TableEntry.cpp
File metadata and controls
107 lines (97 loc) · 3.15 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "TableEntry.h"
#include <functional>
#include <cassert>
#include <iostream>
#include <utility>
#include <exception>
using std::string;
using std::greater;
using std::less;
using std::equal_to;
using std::not_equal_to;
using std::move;
using std::ostream;
using std::terminate;
// elt constructors
// const char* method, because otherwise it matches the bool one
TableEntry::TableEntry(const char* val) : tag{EntryType::String}, data_string{val} {}
TableEntry::TableEntry(const string& val) : tag{EntryType::String}, data_string{val} {}
TableEntry::TableEntry(string&& val) : tag{EntryType::String}, data_string{move(val)} {}
TableEntry::TableEntry(double val) : tag{EntryType::Double}, data_double{val} {}
TableEntry::TableEntry(int val) : tag{EntryType::Int}, data_int{val} {}
TableEntry::TableEntry(bool val) : tag{EntryType::Bool}, data_bool{val} {}
// copy/move ctors
TableEntry::TableEntry(const TableEntry& other) : tag{other.tag} {
construct_from(other);
}
TableEntry::TableEntry(TableEntry&& other) noexcept : tag{other.tag} {
construct_from(move(other));
}
// dtor
TableEntry::~TableEntry() noexcept {
if (tag == EntryType::String)
data_string.~string();
}
// comparisons
bool TableEntry::operator<(const TableEntry& other) const noexcept {
return compare<less>(other);
}
bool TableEntry::operator>(const TableEntry& other) const noexcept {
return compare<greater>(other);
}
bool TableEntry::operator==(const TableEntry& other) const noexcept {
return compare<equal_to>(other);
}
bool TableEntry::operator!=(const TableEntry& other) const noexcept {
return compare<not_equal_to>(other);
}
// specializations on the as<> template for conversion
template <> const string& TableEntry::as<string>() const noexcept {
assert(tag == EntryType::String &&
"tried to use TableEntry as a string when it didn't contain a string");
return data_string;
}
template <> double TableEntry::as<double>() const noexcept {
assert(tag == EntryType::Double &&
"tried to use TableEntry as a double when it didn't contain a double");
return data_double;
}
template <> int TableEntry::as<int>() const noexcept {
assert(tag == EntryType::Int &&
"tried to use TableEntry as an int when it didn't contain an int");
return data_int;
}
template <> bool TableEntry::as<bool>() const noexcept {
assert(tag == EntryType::Bool &&
"tried to use TableEntry as a bool when it didn't contain a bool");
return data_bool;
}
namespace std {
// overload to std::hash
size_t hash<TableEntry>::operator()(const TableEntry& tt) const noexcept {
switch (tt.tag) {
case EntryType::String:
return hash<string>{}(tt.data_string);
case EntryType::Double:
return hash<double>{}(tt.data_double);
case EntryType::Int:
return hash<int>{}(tt.data_int);
case EntryType::Bool:
return hash<bool>{}(tt.data_bool);
}
terminate();
}
}
ostream& operator<<(ostream& os, const TableEntry& tt) {
switch (tt.tag) {
case EntryType::String:
return os << tt.data_string;
case EntryType::Double:
return os << tt.data_double;
case EntryType::Int:
return os << tt.data_int;
case EntryType::Bool:
return os << tt.data_bool;
}
terminate();
}