[Bf-blender-cvs] [18bff53c997] master: BLI: remove blender::Optional in favor of std::optional

Jacques Lucke noreply at git.blender.org
Mon Jun 29 14:31:38 CEST 2020


Commit: 18bff53c997f5bfac0435b8a03ee967eed8c9e20
Author: Jacques Lucke
Date:   Mon Jun 29 14:29:05 2020 +0200
Branches: master
https://developer.blender.org/rB18bff53c997f5bfac0435b8a03ee967eed8c9e20

BLI: remove blender::Optional in favor of std::optional

`std::optional` can be used now, because we switched to C++17.

===================================================================

M	source/blender/blenlib/BLI_dot_export.hh
M	source/blender/blenlib/BLI_map.hh
D	source/blender/blenlib/BLI_optional.hh
M	source/blender/blenlib/CMakeLists.txt
M	source/blender/depsgraph/intern/depsgraph_type.h
M	source/blender/depsgraph/intern/eval/deg_eval_runtime_backup_object.cc
M	source/blender/functions/FN_attributes_ref.hh
M	tests/gtests/blenlib/BLI_map_test.cc
D	tests/gtests/blenlib/BLI_optional_test.cc
M	tests/gtests/blenlib/CMakeLists.txt

===================================================================

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 <optional>
 #include <sstream>
 
 namespace blender {
@@ -197,10 +197,10 @@ class DirectedGraph final : public Graph {
 class NodePort {
  private:
   Node *m_node;
-  Optional<std::string> m_port_name;
+  std::optional<std::string> m_port_name;
 
  public:
-  NodePort(Node &node, Optional<std::string> port_name = {})
+  NodePort(Node &node, std::optional<std::string> 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 <optional>
 #include <unordered_map>
 
 #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<Value> pop_try(const Key &key)
+  std::optional<Value> 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<typename ForwardKey> Optional<Value> pop_try_as(const ForwardKey &key)
+  template<typename ForwardKey> std::optional<Value> 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<typename ForwardKey> Optional<Value> pop_try__impl(const ForwardKey &key, uint32_t hash)
+  template<typename ForwardKey>
+  std::optional<Value> 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> value = std::move(*slot.value());
+        std::optional<Value> 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 <algorithm>
-#include <memory>
-
-namespace blender {
-
-template<typename T> class Optional {
- private:
-  AlignedBuffer<sizeof(T), alignof(T)> 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<T>(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<bPoseChannel_Runtime> runtime = pose_channel_runtime_data.pop_try(
+        optional<bPoseChannel_Runtime> 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 <optional>
+
 #include "FN_spans.hh"
 
 #include "BLI_linear_allocator.hh"
@@ -204,7 +206,7 @@ class MutableAttributesRef {
     return this->get<T>(m_info->index_of(name));
   }
 
-  Optional<GMutableSpan> try_get(StringRef name, const CPPType &type) const
+  std::optional<GMutableSpan> 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<typename T> Optional<MutableSpan<T>> try_get(StringRef name) const
+  template<typename T> std::optional<MutableSpan<T>> 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<int> value = map.pop_try(4);
+  std::optional<int> 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

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list