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 diff --git a/source/blender/depsgraph/intern/depsgraph_type.h b/source/blender/depsgraph/intern/depsgraph_type.h index 12150320391..5439a9dd31c 100644 --- a/source/blender/depsgraph/intern/depsgraph_type.h +++ b/source/blender/depsgraph/intern/depsgraph_type.h @@ -54,7 +54,6 @@ namespace DEG { /* Commonly used types. */ using blender::Map; -using blender::Optional; using blender::Set; using blender::Span; using blender::StringRef; @@ -62,6 +61,7 @@ using blender::StringRefNull; using blender::Vector; using blender::VectorSet; using std::deque; +using std::optional; using std::pair; using std::string; using std::unique_ptr; diff --git a/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup_object.cc b/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup_object.cc index 975887ae7bf..71301ed45e5 100644 --- a/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup_object.cc +++ b/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup_object.cc @@ -172,10 +172,10 @@ void ObjectRuntimeBackup::restore_pose_channel_runtime_data(Object *object) LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) { /* This is nullptr in Edit mode. */ if (pchan->orig_pchan != nullptr) { - Optional runtime = pose_channel_runtime_data.pop_try( + optional runtime = pose_channel_runtime_data.pop_try( pchan->orig_pchan); if (runtime.has_value()) { - pchan->runtime = runtime.extract(); + pchan->runtime = *runtime; } } } diff --git a/source/blender/functions/FN_attributes_ref.hh b/source/blender/functions/FN_attributes_ref.hh index 1a4c1d2aadc..383b26330bf 100644 --- a/source/blender/functions/FN_attributes_ref.hh +++ b/source/blender/functions/FN_attributes_ref.hh @@ -24,6 +24,8 @@ * and index. */ +#include + #include "FN_spans.hh" #include "BLI_linear_allocator.hh" @@ -204,7 +206,7 @@ class MutableAttributesRef { return this->get(m_info->index_of(name)); } - Optional try_get(StringRef name, const CPPType &type) const + std::optional try_get(StringRef name, const CPPType &type) const { int index = m_info->try_index_of(name, type); if (index == -1) { @@ -215,7 +217,7 @@ class MutableAttributesRef { } } - template Optional> try_get(StringRef name) const + template std::optional> try_get(StringRef name) const { int index = m_info->try_index_of(name); if (index == -1) { diff --git a/tests/gtests/blenlib/BLI_map_test.cc b/tests/gtests/blenlib/BLI_map_test.cc index 79a8786def9..7a67255d8d3 100644 --- a/tests/gtests/blenlib/BLI_map_test.cc +++ b/tests/gtests/blenlib/BLI_map_test.cc @@ -88,7 +88,7 @@ TEST(map, PopTry) map.add(1, 5); map.add(2, 7); EXPECT_EQ(map.size(), 2u); - Optional value = map.pop_try(4); + std::optional value = map.pop_try(4); EXPECT_EQ(map.size(), 2u); EXPECT_FALSE(value.has_value()); value = map.pop_try(2); diff --git a/tests/gtests/blenlib/BLI_optional_test.cc b/tests/gtests/blenlib/BLI_optional_test.cc deleted file mode 100644 index 2fc2188563e..00000000000 --- a/tests/gtests/blenlib/BLI_optional_test.cc +++ /dev/null @@ -1,61 +0,0 @@ -#include "BLI_optional.hh" -#include "BLI_strict_flags.h" -#include "testing/testing.h" -#include - -using namespace blender; - -TEST(optional, DefaultConstructor) -{ - Optional a; - EXPECT_FALSE(a.has_value()); -} - -TEST(optional, ValueConstructor) -{ - Optional a(5); - EXPECT_TRUE(a.has_value()); - EXPECT_EQ(a.value(), 5); -} - -TEST(optional, CopyConstructor) -{ - Optional a("Hello"); - Optional b = a; - EXPECT_TRUE(a.has_value()); - EXPECT_TRUE(b.has_value()); - b.value()[0] = 'T'; - EXPECT_EQ(a.value(), "Hello"); - EXPECT_EQ(b.value(), "Tello"); -} - -TEST(optional, Reset) -{ - Optional a(4); - EXPECT_TRUE(a.has_value()); - a.reset(); - EXPECT_FALSE(a.has_value()); -} - -TEST(optional, Extract) -{ - Optional a(32); - EXPECT_TRUE(a.has_value()); - EXPECT_EQ(a.extract(), 32); - EXPECT_FALSE(a.has_value()); -} - -TEST(optional, ArrowOperator) -{ - Optional value = std::string("Hello"); - EXPECT_TRUE(value.has_value()); - EXPECT_EQ(value->size(), 5); -} - -TEST(optional, StarOperator) -{ - Optional value = std::string("Hello"); - EXPECT_TRUE(value.has_value()); - std::string &s = *value; - EXPECT_EQ(s.size(), 5); -} diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt index 31c8e983292..ba493b22b42 100644 --- a/tests/gtests/blenlib/CMakeLists.txt +++ b/tests/gtests/blenlib/CMakeLists.txt @@ -63,7 +63,6 @@ BLENDER_TEST(BLI_math_geom "bf_blenlib") BLENDER_TEST(BLI_math_matrix "bf_blenlib") BLENDER_TEST(BLI_math_vector "bf_blenlib") BLENDER_TEST(BLI_memiter "bf_blenlib") -BLENDER_TEST(BLI_optional "bf_blenlib") BLENDER_TEST(BLI_path_util "${BLI_path_util_extra_libs}") BLENDER_TEST(BLI_polyfill_2d "bf_blenlib") BLENDER_TEST(BLI_set "bf_blenlib")