From 18bff53c997f5bfac0435b8a03ee967eed8c9e20 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Mon, 29 Jun 2020 14:29:05 +0200 Subject: BLI: remove blender::Optional in favor of std::optional `std::optional` can be used now, because we switched to C++17. --- source/blender/blenlib/BLI_dot_export.hh | 6 +- source/blender/blenlib/BLI_map.hh | 11 +- source/blender/blenlib/BLI_optional.hh | 189 ------------------------------- source/blender/blenlib/CMakeLists.txt | 1 - 4 files changed, 9 insertions(+), 198 deletions(-) delete mode 100644 source/blender/blenlib/BLI_optional.hh (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/BLI_dot_export.hh b/source/blender/blenlib/BLI_dot_export.hh index 201fbf12c4a..b496c00a75d 100644 --- a/source/blender/blenlib/BLI_dot_export.hh +++ b/source/blender/blenlib/BLI_dot_export.hh @@ -25,13 +25,13 @@ */ #include "BLI_map.hh" -#include "BLI_optional.hh" #include "BLI_set.hh" #include "BLI_utility_mixins.hh" #include "BLI_vector.hh" #include "BLI_dot_export_attribute_enums.hh" +#include #include namespace blender { @@ -197,10 +197,10 @@ class DirectedGraph final : public Graph { class NodePort { private: Node *m_node; - Optional m_port_name; + std::optional m_port_name; public: - NodePort(Node &node, Optional port_name = {}) + NodePort(Node &node, std::optional port_name = {}) : m_node(&node), m_port_name(std::move(port_name)) { } diff --git a/source/blender/blenlib/BLI_map.hh b/source/blender/blenlib/BLI_map.hh index 7d5c8b19a78..688f334001f 100644 --- a/source/blender/blenlib/BLI_map.hh +++ b/source/blender/blenlib/BLI_map.hh @@ -67,13 +67,13 @@ * interface as blender::Map. This is useful for benchmarking. */ +#include #include #include "BLI_array.hh" #include "BLI_hash.hh" #include "BLI_hash_tables.hh" #include "BLI_map_slots.hh" -#include "BLI_optional.hh" #include "BLI_probing_strategies.hh" namespace blender { @@ -392,7 +392,7 @@ class Map { * Get the value that is stored for the given key and remove it from the map. If the key is not * in the map, a value-less optional is returned. */ - Optional pop_try(const Key &key) + std::optional pop_try(const Key &key) { return this->pop_try_as(key); } @@ -400,7 +400,7 @@ class Map { /** * Same as `pop_try`, but accepts other key types that are supported by the hash function. */ - template Optional pop_try_as(const ForwardKey &key) + template std::optional pop_try_as(const ForwardKey &key) { return this->pop_try__impl(key, m_hash(key)); } @@ -1074,11 +1074,12 @@ class Map { MAP_SLOT_PROBING_END(); } - template Optional pop_try__impl(const ForwardKey &key, uint32_t hash) + template + std::optional pop_try__impl(const ForwardKey &key, uint32_t hash) { MAP_SLOT_PROBING_BEGIN (hash, slot) { if (slot.contains(key, m_is_equal, hash)) { - Optional value = std::move(*slot.value()); + std::optional value = std::move(*slot.value()); slot.remove(); m_removed_slots++; return value; diff --git a/source/blender/blenlib/BLI_optional.hh b/source/blender/blenlib/BLI_optional.hh deleted file mode 100644 index 2e6b66d0eac..00000000000 --- a/source/blender/blenlib/BLI_optional.hh +++ /dev/null @@ -1,189 +0,0 @@ -/* - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -/** \file - * \ingroup bli - * - * Simple version of std::optional, which is only available since C++17. - */ - -#ifndef __BLI_OPTIONAL_HH__ -#define __BLI_OPTIONAL_HH__ - -#include "BLI_memory_utils.hh" -#include "BLI_utildefines.h" - -#include -#include - -namespace blender { - -template class Optional { - private: - AlignedBuffer m_storage; - bool m_set; - - public: - Optional() : m_set(false) - { - } - - ~Optional() - { - this->reset(); - } - - Optional(const T &value) : Optional() - { - this->set(value); - } - - Optional(T &&value) : Optional() - { - this->set(std::forward(value)); - } - - Optional(const Optional &other) : Optional() - { - if (other.has_value()) { - this->set(other.value()); - } - } - - Optional(Optional &&other) : Optional() - { - if (other.has_value()) { - this->set(std::move(other.value())); - } - } - - Optional &operator=(const Optional &other) - { - if (this == &other) { - return *this; - } - if (other.has_value()) { - this->set(other.value()); - } - else { - this->reset(); - } - return *this; - } - - Optional &operator=(Optional &&other) - { - if (this == &other) { - return *this; - } - if (other.has_value()) { - this->set(std::move(other.value())); - } - else { - this->reset(); - } - return *this; - } - - bool has_value() const - { - return m_set; - } - - const T &value() const - { - BLI_assert(m_set); - return *this->value_ptr(); - } - - T &value() - { - BLI_assert(m_set); - return *this->value_ptr(); - } - - void set(const T &value) - { - if (m_set) { - this->value() = value; - } - else { - new ((void *)this->value_ptr()) T(value); - m_set = true; - } - } - - void set(T &&value) - { - if (m_set) { - this->value() = std::move(value); - } - else { - new ((void *)this->value_ptr()) T(std::move(value)); - m_set = true; - } - } - - void set_new(const T &value) - { - BLI_assert(!m_set); - new ((void *)this->value_ptr()) T(value); - m_set = true; - } - - void set_new(T &&value) - { - BLI_assert(!m_set); - new ((void *)this->value_ptr()) T(std::move(value)); - m_set = true; - } - - void reset() - { - if (m_set) { - this->value_ptr()->~T(); - m_set = false; - } - } - - T extract() - { - BLI_assert(m_set); - T value = std::move(this->value()); - this->reset(); - return value; - } - - T *operator->() - { - return this->value_ptr(); - } - - T &operator*() - { - return *this->value_ptr(); - } - - private: - T *value_ptr() const - { - return (T *)m_storage.ptr(); - } -}; - -} /* namespace blender */ - -#endif /* __BLI_OPTIONAL_HH__ */ diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt index 69df0505dfe..2630eb6bbd0 100644 --- a/source/blender/blenlib/CMakeLists.txt +++ b/source/blender/blenlib/CMakeLists.txt @@ -225,7 +225,6 @@ set(SRC BLI_memory_utils.hh BLI_mempool.h BLI_noise.h - BLI_optional.hh BLI_path_util.h BLI_polyfill_2d.h BLI_polyfill_2d_beautify.h -- cgit v1.2.3