[Bf-blender-cvs] [719357ffda9] functions: revisit autorefcount

Jacques Lucke noreply at git.blender.org
Mon Jul 15 18:12:40 CEST 2019


Commit: 719357ffda9058b2baba7d39528cc5b21495897d
Author: Jacques Lucke
Date:   Mon Jul 15 16:07:24 2019 +0200
Branches: functions
https://developer.blender.org/rB719357ffda9058b2baba7d39528cc5b21495897d

revisit autorefcount

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

M	source/blender/blenlib/BLI_shared.hpp
M	source/blender/functions/types/lists.hpp
M	tests/gtests/blenlib/BLI_shared_test.cc

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

diff --git a/source/blender/blenlib/BLI_shared.hpp b/source/blender/blenlib/BLI_shared.hpp
index 2fd0ea6884a..a12e1059586 100644
--- a/source/blender/blenlib/BLI_shared.hpp
+++ b/source/blender/blenlib/BLI_shared.hpp
@@ -58,33 +58,32 @@ template<typename T> class AutoRefCount {
  private:
   T *m_object;
 
-  AutoRefCount(T *object) : m_object(object)
-  {
-  }
-
   inline void incref()
   {
-    m_object->incref();
+    if (m_object) {
+      m_object->incref();
+    }
   }
 
   inline void decref()
   {
-    m_object->decref();
+    if (m_object) {
+      m_object->decref();
+    }
   }
 
  public:
-  AutoRefCount() : AutoRefCount(new T())
+  AutoRefCount() : m_object(nullptr)
   {
   }
 
-  template<typename... Args> static AutoRefCount<T> New(Args &&... args)
+  AutoRefCount(T *object) : m_object(object)
   {
-    T *object = new T(std::forward<Args>(args)...);
-    return AutoRefCount<T>(object);
   }
 
-  static AutoRefCount<T> FromPointer(T *object)
+  template<typename... Args> static AutoRefCount<T> New(Args &&... args)
   {
+    T *object = new T(std::forward<Args>(args)...);
     return AutoRefCount<T>(object);
   }
 
@@ -148,6 +147,7 @@ template<typename T> class AutoRefCount {
 
   T &ref() const
   {
+    BLI_assert(m_object);
     return *m_object;
   }
 
@@ -165,6 +165,8 @@ template<typename T> class AutoRefCount {
 
   friend bool operator==(const AutoRefCount &a, const AutoRefCount &b)
   {
+    BLI_assert(a.ptr());
+    BLI_assert(b.ptr());
     return *a.ptr() == *b.ptr();
   }
 
diff --git a/source/blender/functions/types/lists.hpp b/source/blender/functions/types/lists.hpp
index 8f60b3aff7d..93fa8d86fb7 100644
--- a/source/blender/functions/types/lists.hpp
+++ b/source/blender/functions/types/lists.hpp
@@ -81,12 +81,12 @@ template<typename T> class List : public BLI::SharedImmutable {
   SharedList<T> get_mutable()
   {
     if (this->is_mutable()) {
-      return SharedList<T>::FromPointer(this);
+      return SharedList<T>(this);
     }
     else {
       List *new_list = this->copy();
       BLI_assert(new_list->is_mutable());
-      return SharedList<T>::FromPointer(new_list);
+      return SharedList<T>(new_list);
     }
   }
 
diff --git a/tests/gtests/blenlib/BLI_shared_test.cc b/tests/gtests/blenlib/BLI_shared_test.cc
index b4d3fc170fa..ff20987430f 100644
--- a/tests/gtests/blenlib/BLI_shared_test.cc
+++ b/tests/gtests/blenlib/BLI_shared_test.cc
@@ -127,3 +127,9 @@ TEST(shared, DecRefToZero)
   ptr->decref();
   ASSERT_FALSE(alive);
 }
+
+TEST(shared, Empty)
+{
+  SharedClass obj;
+  ASSERT_EQ(obj.ptr(), nullptr);
+}



More information about the Bf-blender-cvs mailing list