blob: d84705c1ac59cce0f011def318cbb9649b041c88 [file] [log] [blame]
James Ward22a4e152020-09-25 11:43:21 +01001#ifndef MAPBOX_UTIL_OPTIONAL_HPP
2#define MAPBOX_UTIL_OPTIONAL_HPP
3
4#pragma message("This implementation of optional is deprecated. See https://github.com/mapbox/variant/issues/64.")
5
6#include <type_traits>
7#include <utility>
8
9#include <mapbox/variant.hpp>
10
11namespace mapbox {
12namespace util {
13
14template <typename T>
15class optional
16{
17 static_assert(!std::is_reference<T>::value, "optional doesn't support references");
18
19 struct none_type
20 {
21 };
22
23 variant<none_type, T> variant_;
24
25public:
26 optional() = default;
27
28 optional(optional const& rhs)
29 {
30 if (this != &rhs)
31 { // protect against invalid self-assignment
32 variant_ = rhs.variant_;
33 }
34 }
35
36 optional(T const& v) { variant_ = v; }
37
38 explicit operator bool() const noexcept { return variant_.template is<T>(); }
39
40 T const& get() const { return variant_.template get<T>(); }
41 T& get() { return variant_.template get<T>(); }
42
43 T const& operator*() const { return this->get(); }
44 T operator*() { return this->get(); }
45
46 optional& operator=(T const& v)
47 {
48 variant_ = v;
49 return *this;
50 }
51
52 optional& operator=(optional const& rhs)
53 {
54 if (this != &rhs)
55 {
56 variant_ = rhs.variant_;
57 }
58 return *this;
59 }
60
61 template <typename... Args>
62 void emplace(Args&&... args)
63 {
64 variant_ = T{std::forward<Args>(args)...};
65 }
66
67 void reset() { variant_ = none_type{}; }
68
69}; // class optional
70
71} // namespace util
72} // namespace mapbox
73
74#endif // MAPBOX_UTIL_OPTIONAL_HPP