-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
any
182 lines (143 loc) · 5.91 KB
/
any
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_CONTAINER_ANY_HPP
#define GTL_CONTAINER_ANY_HPP
// Summary: Class that can hold any variable type.
#if defined(_MSC_VER)
# pragma warning(push, 0)
#endif
#include <memory>
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wweak-vtables"
#endif
namespace gtl {
/// @brief The any class is a variant type that can hold anything.
class any final {
private:
/// @brief This class is a pure virtual base class to hold any type.
class abstract_variable {
public:
/// @brief Virtual destructor.
virtual ~abstract_variable() = default;
protected:
/// @brief Defaulted constructor.
abstract_variable() = default;
public:
/// @brief Deleted copy constructor.
abstract_variable(const abstract_variable&) = delete;
/// @brief Deleted move constructor.
abstract_variable(abstract_variable&&) = delete;
/// @brief Deleted copy assignment operator.
abstract_variable& operator=(const abstract_variable&) = delete;
/// @brief Deleted move assignment operator.
abstract_variable& operator=(abstract_variable&&) = delete;
public:
/// @brief Pure virtual clone factory function to copy child class data.
/// @return A copy of the abstract_variable.
virtual std::unique_ptr<abstract_variable> clone() const = 0;
};
/// @brief This template class implements the abstract_variable class to hold a variable of template type.
template <typename type>
class variable final
: public abstract_variable {
public:
/// @brief The data of the type to be held in the variable.
type value;
public:
/// @brief Virtual destructor will call the descrutor for abstract_variable.
virtual ~variable() override = default;
/// @brief Explicit constructor that stores the provided value in this variable.
/// @param any_value The value to be stored in the variable.
explicit variable(type any_value)
: value(any_value) {
}
private:
/// @brief Deleted copy constructor.
variable(const variable&) = delete;
/// @brief Deleted move constructor.
variable(variable&&) = delete;
/// @brief Deleted copy assignment operator.
variable& operator=(const variable&) = delete;
/// @brief Deleted move assignment operator.
variable& operator=(variable&&) = delete;
public:
/// @brief Override of the base class clone function.
/// @return A copy of the variable.
virtual std::unique_ptr<abstract_variable> clone() const override {
return std::make_unique<variable>(this->value);
}
};
private:
/// @brief value stores an AbstrctVariable which will be either null or hold the variant data.
std::unique_ptr<abstract_variable> value;
public:
/// @brief Defaulted destructor.
~any() = default;
/// @brief Defaulted constructor.
any() = default;
/// @brief Copy constructor.
/// @param other The other any to be copied.
any(const any& other)
: value(other.value ? other.value->clone() : nullptr) {
}
/// @brief Defaulted move constructor.
any(any&&) = default;
/// @brief Copy assignment operator.
/// @param other The other any to be copied.
any& operator=(const any& other) {
// Check the we are not self assigning
if (this != &other) {
// Clone the other value if it is not a null pointer.
this->value = other.value ? other.value->clone() : nullptr;
}
// Return this
return *this;
}
/// @brief Defaulted move assignment operator.
any& operator=(any&&) = default;
public:
/// @brief Templated copy assignment operator.
/// @param any_value The value to store in the any.
template <typename type>
any& operator=(const type& any_value) {
// Allocate a variable to hold the value and store this in value
this->value = std::make_unique<variable<type > >(any_value);
// Return this
return *this;
}
/// @brief Templated constructor taking any type.
/// @param any_value The value to be stored in the any.
template <typename type>
any(const type& any_value)
: value(std::make_unique<variable<type> >(any_value)) {
}
/// @brief Templated conversion operator will cast any to anything.
/// @return The value of the any as the requested type.
template <typename type>
operator type() {
if (!this->value) {
return type();
}
return static_cast<variable<type>*>(this->value.get())->value;
}
};
}
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#endif // GTL_CONTAINER_ANY_HPP