2 * Copyright (c) 2021 Arm Limited.
4 * SPDX-License-Identifier: MIT
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 optional(const optional &) = delete;
37 optional &operator=(const optional &) = delete;
40 * @brief Construct an empty optional object.
45 * @brief Construct an optional object with a value.
47 * @param val The value that will be placed in the optional.
49 optional(value_type &&val) noexcept
50 : m_has_value(true), m_value(std::move(val))
55 * @brief Construct a new optional object from another optional
57 * @param opt The optional object that will be moved
59 optional(optional &&opt)
60 : m_has_value(opt.m_has_value)
64 m_value = std::move(opt.m_value);
65 opt.m_has_value = false;
74 * @brief Check if optional has a value.
76 * @return true If the optional has a value.
77 * @return false If the optional does not have a value.
79 bool has_value() const noexcept
85 * @brief Return the value in the optional. It is only
86 * valid to call this function if optional has a value.
88 * @return value_type& The value that is in the optional
90 value_type &value() noexcept
97 * @brief Clears the value from the optional
100 void reset() noexcept
107 * @brief Reassign/assign the value in the optional.
109 * @return optional& This optional object with the value.
111 optional &set(T &&val) noexcept
113 m_value = std::move(val);
118 optional &set(const T &val) noexcept
126 * @brief Return the value in the optional, same as value()
128 * @return value_type& The value in the optional
130 value_type &operator*() noexcept
137 * @brief Return the value in the optional as pointer.
139 * @return value_type* The value in the optional
141 value_type *operator->() noexcept
148 * @brief Reassign/assign the value in the optional
150 * @param val The value to assign to this optional
151 * @return optional& This optional object with the value
153 optional &operator=(value_type &&val) noexcept
156 m_value = std::move(val);
162 * @brief Construct a new optional object from another optional
164 * @param opt The optional object that will be moved
165 * @return optional& This optional object with the value
167 optional &operator=(optional &&opt)
174 m_value = std::move(opt.m_value);
175 opt.m_has_value = false;
188 bool m_has_value{false};
192 template <typename T, typename... Args>
193 inline optional<T> make_optional(Args &&...args)
195 return optional<T>{T(std::forward<Args>(args)...)};
197 } /* namespace util */