[Bf-blender-cvs] [bae72c4b8e4] functions: remove unused classes
Jacques Lucke
noreply at git.blender.org
Mon Feb 10 13:32:33 CET 2020
Commit: bae72c4b8e494aae69a8b97a6e4d2badf9e9a69b
Author: Jacques Lucke
Date: Mon Feb 10 13:27:30 2020 +0100
Branches: functions
https://developer.blender.org/rBbae72c4b8e494aae69a8b97a6e4d2badf9e9a69b
remove unused classes
===================================================================
D source/blender/blenlib/BLI_refcount.h
M source/blender/blenlib/CMakeLists.txt
D tests/gtests/blenlib/BLI_refcount_test.cc
M tests/gtests/blenlib/CMakeLists.txt
===================================================================
diff --git a/source/blender/blenlib/BLI_refcount.h b/source/blender/blenlib/BLI_refcount.h
deleted file mode 100644
index 3c5f53c8780..00000000000
--- a/source/blender/blenlib/BLI_refcount.h
+++ /dev/null
@@ -1,242 +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
- *
- * Objects with shared ownership require a reference counter, so that they can be freed when they
- * are not referenced anymore.
- *
- * Classes can subclass RefCounter to be extended with an intrusive reference count (the counter is
- * stored directly in the object). This is necessary, because the object might have to be used by
- * different languages (C, C++, Python).
- *
- * To avoid error-prone manual reference counting, there is an AutoRefCount class that works
- * similar to std::shared, but deals with objects of type RefCounter.
- */
-
-#pragma once
-
-#include <atomic>
-#include <utility>
-#include "BLI_utildefines.h"
-#include "BLI_hash_cxx.h"
-
-namespace BLI {
-
-class RefCounter {
- private:
- std::atomic<int> m_refcount;
-
- protected:
- virtual ~RefCounter(){};
-
- RefCounter() : m_refcount(1)
- {
- }
-
- public:
- /**
- * Increment the reference counter atomically.
- */
- void incref()
- {
- m_refcount.fetch_add(1);
- }
-
- /**
- * Decrement the reference counter atomically. Deletes the instance if the reference counter
- * becomes zero.
- */
- void decref()
- {
- int new_value = m_refcount.fetch_sub(1) - 1;
- BLI_assert(new_value >= 0);
- if (new_value == 0) {
- delete this;
- }
- }
-
- /**
- * Get the current reference count.
- */
- int refcount() const
- {
- return m_refcount;
- }
-};
-
-template<typename T> class AutoRefCount {
- private:
- T *m_object;
-
- void incref()
- {
- if (m_object) {
- m_object->incref();
- }
- }
-
- void decref()
- {
- if (m_object) {
- m_object->decref();
- }
- }
-
- public:
- AutoRefCount() : m_object(nullptr)
- {
- }
-
- AutoRefCount(T *object) : m_object(object)
- {
- }
-
- /**
- * Similar to std::make_shared.
- */
- template<typename... Args> static AutoRefCount<T> New(Args &&... args)
- {
- T *object = new T(std::forward<Args>(args)...);
- return AutoRefCount<T>(object);
- }
-
- AutoRefCount(const AutoRefCount &other)
- {
- m_object = other.m_object;
- this->incref();
- }
-
- AutoRefCount(AutoRefCount &&other)
- {
- m_object = other.m_object;
- other.m_object = nullptr;
- }
-
- ~AutoRefCount()
- {
- this->decref();
- }
-
- AutoRefCount &operator=(const AutoRefCount &other)
- {
- if (this == &other) {
- return *this;
- }
- else if (m_object == other.m_object) {
- return *this;
- }
- else {
- this->decref();
- m_object = other.m_object;
- this->incref();
- return *this;
- }
- }
-
- AutoRefCount &operator=(AutoRefCount &&other)
- {
- if (this == &other) {
- return *this;
- }
- else if (m_object == other.m_object) {
- other.m_object = nullptr;
- return *this;
- }
- else {
- this->decref();
- m_object = other.m_object;
- other.m_object = nullptr;
- return *this;
- }
- }
-
- /**
- * Get the pointer that is currently wrapped. This pointer can be null.
- */
- const T *ptr() const
- {
- return m_object;
- }
-
- T *ptr()
- {
- return m_object;
- }
-
- /**
- * Get a reference to the object that is currently wrapped.
- * Asserts when no object is wrapped.
- */
- const T &ref() const
- {
- BLI_assert(m_object);
- return *m_object;
- }
-
- T &ref()
- {
- BLI_assert(m_object);
- return *m_object;
- }
-
- /**
- * Get the pointer that is currently wrapped and remove it from this automatic reference counter,
- * effectively taking over the ownership. Note that this can return null.
- */
- T *extract_ptr()
- {
- T *value = m_object;
- m_object = nullptr;
- return value;
- }
-
- const T *operator->() const
- {
- return this->ptr();
- }
-
- T *operator->()
- {
- return this->ptr();
- }
-
- /**
- * They compare equal, when the wrapped objects compare equal.
- * Asserts when one of the two does not wrap an object currently.
- */
- friend bool operator==(const AutoRefCount &a, const AutoRefCount &b)
- {
- BLI_assert(a.ptr());
- BLI_assert(b.ptr());
- return *a.ptr() == *b.ptr();
- }
-
- friend bool operator!=(const AutoRefCount &a, const AutoRefCount &b)
- {
- return !(a == b);
- }
-};
-
-template<typename T> struct DefaultHash<AutoRefCount<T>> {
- uint32_t operator()(const AutoRefCount<T> &value) const
- {
- return DefaultHash<T>{}(value.ref());
- }
-};
-
-} /* namespace BLI */
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index 753b35c0048..f7e1bbf81b1 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -261,7 +261,6 @@ set(SRC
BLI_math_cxx.h
BLI_linear_allocator.h
BLI_optional.h
- BLI_refcount.h
BLI_multi_map.h
BLI_timeit.h
BLI_vector_adaptor.h
diff --git a/tests/gtests/blenlib/BLI_refcount_test.cc b/tests/gtests/blenlib/BLI_refcount_test.cc
deleted file mode 100644
index ba8805d3f02..00000000000
--- a/tests/gtests/blenlib/BLI_refcount_test.cc
+++ /dev/null
@@ -1,135 +0,0 @@
-#include "testing/testing.h"
-#include "BLI_refcount.h"
-
-#include <iostream>
-
-#define DEFAULT_VALUE 42
-
-class MyTestClass : public BLI::RefCounter {
- public:
- int m_value;
- bool *m_alive = nullptr;
-
- MyTestClass() : m_value(DEFAULT_VALUE)
- {
- }
-
- MyTestClass(int value) : m_value(value)
- {
- }
-
- MyTestClass(bool *alive) : m_alive(alive)
- {
- *alive = true;
- }
-
- ~MyTestClass()
- {
- if (m_alive)
- *m_alive = false;
- }
-};
-
-using namespace BLI;
-
-using SharedClass = AutoRefCount<MyTestClass>;
-
-TEST(ref_count, OneReferenceAfterConstruction)
-{
- SharedClass obj = SharedClass::New();
- ASSERT_EQ(obj->refcount(), 1);
-}
-
-TEST(ref_count, CopyConstructorIncreasesRefCount)
-{
- SharedClass obj1 = SharedClass::New();
- ASSERT_EQ(obj1->refcount(), 1);
- SharedClass obj2(obj1);
- ASSERT_EQ(obj1->refcount(), 2);
- ASSERT_EQ(obj2->refcount(), 2);
-}
-
-TEST(ref_count, MoveConstructorKeepsRefCount)
-{
- SharedClass obj(SharedClass::New());
- ASSERT_EQ(obj->refcount(), 1);
-}
-
-TEST(ref_count, DecreasedWhenScopeEnds)
-{
- SharedClass obj1 = SharedClass::New();
- ASSERT_EQ(obj1->refcount(), 1);
- {
- SharedClass obj2 = obj1;
- ASSERT_EQ(obj1->refcount(), 2);
- ASSERT_EQ(obj2->refcount(), 2);
- }
- ASSERT_EQ(obj1->refcount(), 1);
-}
-
-TEST(ref_count, DefaultConstructorCalled)
-{
- SharedClass obj = SharedClass::New();
- ASSERT_EQ(obj->m_value, DEFAULT_VALUE);
-}
-
-TEST(ref_count, OtherConstructorCalled)
-{
- SharedClass obj = SharedClass::New(123);
- ASSERT_EQ(obj->m_value, 123);
-}
-
-TEST(ref_count, DestructorCalled)
-{
- bool alive = false;
- {
- SharedClass obj = SharedClass::New(&alive);
- ASSERT_TRUE(alive);
- }
- ASSERT_FALSE(alive);
-}
-
-TEST(ref_count, CustomIncRef)
-{
- auto *ptr = new MyTestClass();
- ASSERT_EQ(ptr->refcount(), 1);
- ptr->incref();
- ASSERT_EQ(ptr->refcount(), 2);
- ptr->decref();
- ptr->decref();
-}
-
-TEST(ref_count, CustomDecRef)
-{
- auto *ptr = new MyTestClass();
- ptr->incref();
- ASSERT_EQ(ptr->refcount(), 2);
- ptr->decref();
- ASSERT_EQ(ptr->refcount(), 1);
- ptr->decref();
-}
-
-TEST(ref_count, ExtractRefCounted)
-{
- SharedClass obj = SharedClass::New();
- MyTestClass *ptr = obj.ptr();
- ASSERT_EQ(obj->refcount(), 1);
- ptr->incref();
- ASSERT_EQ(obj->refcount(), 2);
- ptr->decref();
-}
-
-TEST(ref_count, DecRefToZero)
-{
- bool alive = false;
- auto *ptr = new MyTestClass(&alive);
- ASSERT_TRUE(alive);
- ptr->decref();
- ASSERT_FALSE(alive);
-}
-
-TEST(ref_count, Empty)
-{
- SharedClass obj;
- ASSERT_EQ(obj.ptr(), nullptr);
-}
diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt
index 153613dc388..ff8dd7b0e9b 100644
--- a/tests/gtests/blenlib/CMakeLists.txt
+++ b/tests/gtests/blenlib/CMakeLists.txt
@@ -65,7 +65,6 @@ BLENDER_TEST(BLI_multi_map "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_refcount "bf_blenlib")
BLENDER_TEST(BLI_set "bf_blenlib")
BLENDER_TEST(BLI_stack "bf_blenlib")
BLENDER_TEST(BLI_stack_cxx "bf_blenlib")
More information about the Bf-blender-cvs
mailing list