• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #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 
11 namespace mapbox {
12 namespace util {
13 
14 template <typename T>
15 class 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 
25 public:
26     optional() = default;
27 
optional(optional const & rhs)28     optional(optional const& rhs)
29     {
30         if (this != &rhs)
31         { // protect against invalid self-assignment
32             variant_ = rhs.variant_;
33         }
34     }
35 
optional(T const & v)36     optional(T const& v) { variant_ = v; }
37 
operator bool() const38     explicit operator bool() const noexcept { return variant_.template is<T>(); }
39 
get() const40     T const& get() const { return variant_.template get<T>(); }
get()41     T& get() { return variant_.template get<T>(); }
42 
operator *() const43     T const& operator*() const { return this->get(); }
operator *()44     T operator*() { return this->get(); }
45 
operator =(T const & v)46     optional& operator=(T const& v)
47     {
48         variant_ = v;
49         return *this;
50     }
51 
operator =(optional const & rhs)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>
emplace(Args &&...args)62     void emplace(Args&&... args)
63     {
64         variant_ = T{std::forward<Args>(args)...};
65     }
66 
reset()67     void reset() { variant_ = none_type{}; }
68 
69 }; // class optional
70 
71 } // namespace util
72 } // namespace mapbox
73 
74 #endif // MAPBOX_UTIL_OPTIONAL_HPP
75