hc
2023-02-14 0cc9b7c44253c93447ddf73e206fbdbb3d9f16b1
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
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2018 Intel Corporation
 
 
#ifndef OPENCV_GAPI_UTIL_OPTIONAL_HPP
#define OPENCV_GAPI_UTIL_OPTIONAL_HPP
 
#include <opencv2/gapi/util/variant.hpp>
 
// A poor man's `optional` implementation, incompletely modeled against C++17 spec.
namespace cv
{
namespace util
{
    class bad_optional_access: public std::exception
    {
    public:
        virtual const char *what() const noexcept override
        {
            return "Bad optional access";
        }
    };
 
    // TODO: nullopt_t
 
    // Interface ///////////////////////////////////////////////////////////////
    template<typename T> class optional
    {
    public:
        // Constructors
        // NB.: there were issues with Clang 3.8 when =default() was used
        // instead {}
        optional() {};
        optional(const optional&) = default;
        explicit optional(T &&value) noexcept;
        explicit optional(const T &value) noexcept;
        optional(optional &&) noexcept;
        // TODO: optional(nullopt_t) noexcept;
        // TODO: optional(const optional<U> &)
        // TODO: optional(optional<U> &&)
        // TODO: optional(Args&&...)
        // TODO: optional(initializer_list<U>)
        // TODO: optional(U&& value);
 
        // Assignment
        optional& operator=(const optional& rhs) = default;
        optional& operator=(optional&& rhs);
 
        // Observers
        T* operator-> ();
        const T* operator-> () const;
        T& operator* ();
        const T& operator* () const;
        // TODO: && versions
 
        operator bool() const noexcept;
        bool has_value() const noexcept;
 
        T& value();
        const T& value() const;
        // TODO: && versions
 
        template<class U>
        T value_or(U &&default_value) const;
 
        void swap(optional &other) noexcept;
        void reset() noexcept;
        // TODO: emplace
 
        // TODO: operator==, !=, <, <=, >, >=
 
    private:
        struct nothing {};
        util::variant<nothing, T> m_holder;
    };
 
    template<class T>
    optional<typename std::decay<T>::type> make_optional(T&& value);
 
    // TODO: Args... and initializer_list versions
 
    // Implementation //////////////////////////////////////////////////////////
    template<class T> optional<T>::optional(T &&v) noexcept
        : m_holder(v)
    {
    }
 
    template<class T> optional<T>::optional(const T &v) noexcept
        : m_holder(v)
    {
    }
 
    template<class T> optional<T>::optional(optional&& rhs) noexcept
        : m_holder(std::move(rhs.m_holder))
    {
        rhs.reset();
    }
 
    template<class T> optional<T>& optional<T>::operator=(optional&& rhs)
    {
        m_holder = std::move(rhs.m_holder);
        rhs.reset();
        return *this;
    }
 
    template<class T> T* optional<T>::operator-> ()
    {
        return & *(*this);
    }
 
    template<class T> const T* optional<T>::operator-> () const
    {
        return & *(*this);
    }
 
    template<class T> T& optional<T>::operator* ()
    {
        return this->value();
    }
 
    template<class T> const T& optional<T>::operator* () const
    {
        return this->value();
    }
 
    template<class T> optional<T>::operator bool() const noexcept
    {
        return this->has_value();
    }
 
    template<class T> bool optional<T>::has_value() const noexcept
    {
        return util::holds_alternative<T>(m_holder);
    }
 
    template<class T> T& optional<T>::value()
    {
        if (!this->has_value())
            throw_error(bad_optional_access());
        return util::get<T>(m_holder);
    }
 
    template<class T> const T& optional<T>::value() const
    {
        if (!this->has_value())
            throw_error(bad_optional_access());
        return util::get<T>(m_holder);
    }
 
    template<class T>
    template<class U> T optional<T>::value_or(U &&default_value) const
    {
        return (this->has_value() ? this->value() : T(default_value));
    }
 
    template<class T> void optional<T>::swap(optional<T> &other) noexcept
    {
        m_holder.swap(other.m_holder);
    }
 
    template<class T> void optional<T>::reset() noexcept
    {
        if (this->has_value())
            m_holder = nothing{};
    }
 
    template<class T>
    optional<typename std::decay<T>::type> make_optional(T&& value)
    {
        return optional<typename std::decay<T>::type>(std::forward<T>(value));
    }
} // namespace util
} // namespace cv
 
#endif // OPENCV_GAPI_UTIL_OPTIONAL_HPP