1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
5 // Copyright (C) 2018-2019 Intel Corporation
8 #ifndef OPENCV_GAPI_UTIL_OPTIONAL_HPP
9 #define OPENCV_GAPI_UTIL_OPTIONAL_HPP
11 #include "opencv2/gapi/util/variant.hpp"
13 // A poor man's `optional` implementation, incompletely modeled against C++17 spec.
18 class bad_optional_access: public std::exception
21 virtual const char *what() const noexcept override
23 return "Bad optional access";
29 // Interface ///////////////////////////////////////////////////////////////
30 template<typename T> class optional
34 // NB.: there were issues with Clang 3.8 when =default() was used
37 optional(const optional&) = default;
38 explicit optional(T &&value) noexcept;
39 explicit optional(const T &value) noexcept;
40 optional(optional &&) noexcept;
41 // TODO: optional(nullopt_t) noexcept;
42 // TODO: optional(const optional<U> &)
43 // TODO: optional(optional<U> &&)
44 // TODO: optional(Args&&...)
45 // TODO: optional(initializer_list<U>)
46 // TODO: optional(U&& value);
49 optional& operator=(const optional& rhs) = default;
50 optional& operator=(optional&& rhs);
54 const T* operator-> () const;
56 const T& operator* () const;
59 operator bool() const noexcept;
60 bool has_value() const noexcept;
63 const T& value() const;
67 T value_or(U &&default_value) const;
69 void swap(optional &other) noexcept;
70 void reset() noexcept;
73 // TODO: operator==, !=, <, <=, >, >=
77 util::variant<nothing, T> m_holder;
81 optional<typename std::decay<T>::type> make_optional(T&& value);
83 // TODO: Args... and initializer_list versions
85 // Implementation //////////////////////////////////////////////////////////
86 template<class T> optional<T>::optional(T &&v) noexcept
91 template<class T> optional<T>::optional(const T &v) noexcept
96 template<class T> optional<T>::optional(optional&& rhs) noexcept
97 : m_holder(std::move(rhs.m_holder))
102 template<class T> optional<T>& optional<T>::operator=(optional&& rhs)
104 m_holder = std::move(rhs.m_holder);
109 template<class T> T* optional<T>::operator-> ()
114 template<class T> const T* optional<T>::operator-> () const
119 template<class T> T& optional<T>::operator* ()
121 return this->value();
124 template<class T> const T& optional<T>::operator* () const
126 return this->value();
129 template<class T> optional<T>::operator bool() const noexcept
131 return this->has_value();
134 template<class T> bool optional<T>::has_value() const noexcept
136 return util::holds_alternative<T>(m_holder);
139 template<class T> T& optional<T>::value()
141 if (!this->has_value())
142 throw_error(bad_optional_access());
143 return util::get<T>(m_holder);
146 template<class T> const T& optional<T>::value() const
148 if (!this->has_value())
149 throw_error(bad_optional_access());
150 return util::get<T>(m_holder);
154 template<class U> T optional<T>::value_or(U &&default_value) const
156 return (this->has_value() ? this->value() : T(default_value));
159 template<class T> void optional<T>::swap(optional<T> &other) noexcept
161 m_holder.swap(other.m_holder);
164 template<class T> void optional<T>::reset() noexcept
166 if (this->has_value())
167 m_holder = nothing{};
171 optional<typename std::decay<T>::type> make_optional(T&& value)
173 return optional<typename std::decay<T>::type>(std::forward<T>(value));
178 #endif // OPENCV_GAPI_UTIL_OPTIONAL_HPP