// Copyright (c) 2023, QuantStack and Mamba Contributors // // Distributed under the terms of the BSD 3-Clause License. // // The full license is in the file LICENSE, distributed with this software. #ifndef MAMBA_UTIL_HEAP_OPTIONAL_HPP #define MAMBA_UTIL_HEAP_OPTIONAL_HPP #include #include #include namespace mamba::util { /** * An optional akin to ``std::optional`` but uses heap-allocated storage. * * This is useful for large unlikely data, akin to ``std::unique_ptr`` but also * provides copy semantics. */ template class heap_optional { public: using element_type = T; using const_element_type = const element_type; using reference_type = element_type&; using const_reference_type = const_element_type&; using right_reference_type = element_type&&; using pointer_type = element_type*; using const_pointer_type = element_type*; heap_optional() = default; heap_optional(std::nullopt_t); heap_optional(const heap_optional&); heap_optional(heap_optional&&) noexcept = default; explicit heap_optional(element_type&& obj); explicit heap_optional(const element_type& obj); auto operator=(const heap_optional&) -> heap_optional&; auto operator=(heap_optional&&) noexcept -> heap_optional& = default; [[nodiscard]] auto get() noexcept -> pointer_type; [[nodiscard]] auto get() const noexcept -> const_pointer_type; [[nodiscard]] auto operator*() const -> const_reference_type; [[nodiscard]] auto operator*() -> reference_type; [[nodiscard]] auto operator->() const noexcept -> const_pointer_type; [[nodiscard]] auto operator->() noexcept -> pointer_type; [[nodiscard]] auto has_value() const noexcept -> bool; [[nodiscard]] explicit operator bool() const noexcept; [[nodiscard]] auto value() & -> reference_type; [[nodiscard]] auto value() const& -> const_reference_type; [[nodiscard]] auto value() && -> right_reference_type; template [[nodiscard]] auto value_or(U&& other) const& -> element_type; template [[nodiscard]] auto value_or(U&& other) && -> element_type; template auto emplace(Args&&... args) -> reference_type; void reset(); [[nodiscard]] auto operator==(const heap_optional& other) const -> bool { if (has_value() && other.has_value()) { return *m_ptr == *other; } return !has_value() && !other.has_value(); } [[nodiscard]] auto operator!=(const heap_optional& other) const -> bool { return !(*this == other); } private: std::unique_ptr m_ptr = nullptr; }; template heap_optional(T&&) -> heap_optional; template heap_optional(const T&) -> heap_optional; /************************************* * Implementation of heap_optional * *************************************/ template heap_optional::heap_optional(std::nullopt_t) { } template heap_optional::heap_optional(element_type&& obj) : m_ptr(std::make_unique(std::move(obj))) { } template heap_optional::heap_optional(const element_type& obj) : m_ptr(std::make_unique(obj)) { } template heap_optional::heap_optional(const heap_optional& other) { if (other.has_value()) { m_ptr = std::make_unique(*other); } } template auto heap_optional::operator=(const heap_optional& other) -> heap_optional& { if (other.has_value()) { m_ptr = std::make_unique(*other); } else { m_ptr = nullptr; } return *this; } template auto heap_optional::get() noexcept -> pointer_type { return m_ptr.get(); } template auto heap_optional::get() const noexcept -> const_pointer_type { return m_ptr.get(); } template auto heap_optional::operator*() const -> const_reference_type { return *m_ptr; } template auto heap_optional::operator*() -> reference_type { return *m_ptr; } template auto heap_optional::operator->() const noexcept -> const_pointer_type { return m_ptr.get(); } template auto heap_optional::operator->() noexcept -> pointer_type { return m_ptr.get(); } template auto heap_optional::has_value() const noexcept -> bool { return m_ptr != nullptr; } template heap_optional::operator bool() const noexcept { return has_value(); } template auto heap_optional::value() & -> reference_type { if (has_value()) { return *m_ptr; } throw std::bad_optional_access(); } template auto heap_optional::value() const& -> const_reference_type { if (has_value()) { return *m_ptr; } throw std::bad_optional_access(); } template auto heap_optional::value() && -> right_reference_type { if (has_value()) { return std::move(*m_ptr); } throw std::bad_optional_access(); } template template auto heap_optional::value_or(U&& other) const& -> element_type { if (has_value()) { return *m_ptr; } return static_cast(std::forward(other)); } template template auto heap_optional::value_or(U&& other) && -> element_type { if (has_value()) { return std::move(*m_ptr); } return static_cast(std::forward(other)); } template template auto heap_optional::emplace(Args&&... args) -> reference_type { m_ptr = std::make_unique(std::forward(args)...); return *m_ptr; } template void heap_optional::reset() { m_ptr = nullptr; } } #endif