diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2014-10-22 16:29:10 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2014-10-22 16:29:10 +0200 |
commit | 7d6feffd56cde7d6e5d9dd53b358f5afa8e4eb47 (patch) | |
tree | bf152444305b89c25d73028cac813875c9e312bc /include/mbgl/util/optional.hpp | |
parent | 2422e938ac87f5790c4977d93e667c95e695f76c (diff) | |
download | qtlocation-mapboxgl-7d6feffd56cde7d6e5d9dd53b358f5afa8e4eb47.tar.gz |
replace boost optional with mapbox optional
Diffstat (limited to 'include/mbgl/util/optional.hpp')
-rw-r--r-- | include/mbgl/util/optional.hpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/include/mbgl/util/optional.hpp b/include/mbgl/util/optional.hpp new file mode 100644 index 0000000000..133e2c8f97 --- /dev/null +++ b/include/mbgl/util/optional.hpp @@ -0,0 +1,69 @@ +#ifndef MAPBOX_UTIL_OPTIONAL_HPP +#define MAPBOX_UTIL_OPTIONAL_HPP + +#include <type_traits> + +#include "variant.hpp" + +namespace mapbox +{ +namespace util +{ + +template <typename T> class optional +{ + static_assert(!std::is_reference<T>::value, "optional doesn't support references"); + + struct none_type + { + }; + + variant<none_type, T> variant_; + + public: + optional() = default; + + optional(optional const &rhs) + { + if (this != &rhs) + { // protect against invalid self-assignment + variant_ = rhs.variant_; + } + } + + optional(T const &v) { variant_ = v; } + + explicit operator bool() const noexcept { return variant_.template is<T>(); } + + T const &get() const { return variant_.template get<T>(); } + T &get() { return variant_.template get<T>(); } + + T const &operator*() const { return this->get(); } + T operator*() { return this->get(); } + + optional &operator=(T const &v) + { + variant_ = v; + return *this; + } + + optional &operator=(optional const &rhs) + { + if (this != &rhs) + { + variant_ = rhs.variant_; + } + return *this; + } + + template <typename... Args> void emplace(Args &&... args) + { + variant_ = T{std::forward<Args>(args)...}; + } + + void reset() { variant_ = none_type{}; } +}; +} +} + +#endif |