[Bf-blender-cvs] [9697e81dab3] functions: use intrusive refcounter because it simplifies things quite a bit
Jacques Lucke
noreply at git.blender.org
Fri Mar 8 13:57:34 CET 2019
Commit: 9697e81dab3fb7721f2425f318a0dda6708e4ea2
Author: Jacques Lucke
Date: Fri Mar 8 13:42:55 2019 +0100
Branches: functions
https://developer.blender.org/rB9697e81dab3fb7721f2425f318a0dda6708e4ea2
use intrusive refcounter because it simplifies things quite a bit
===================================================================
M source/blender/blenlib/BLI_shared.hpp
M source/blender/depsgraph/intern/builder/deg_builder_relations.cc
M source/blender/functions/backends/tuple_call/tuple.hpp
M source/blender/functions/c_wrapper.cpp
M source/blender/functions/core/data_flow_graph.hpp
M source/blender/functions/core/function.hpp
M source/blender/functions/core/type.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 ba86319f41a..f9e9aa64f53 100644
--- a/source/blender/blenlib/BLI_shared.hpp
+++ b/source/blender/blenlib/BLI_shared.hpp
@@ -37,87 +37,66 @@ namespace BLI {
}
};
- template<typename T>
- class RefCountedPtr : public RefCountedBase {
- private:
- T *m_ptr;
-
- ~RefCountedPtr()
- {
- delete m_ptr;
- }
-
- public:
- RefCountedPtr(T *object)
- : RefCountedBase(), m_ptr(object) {}
-
- T *ptr() const
- {
- return m_ptr;
- }
- };
-
template<typename T>
class Shared {
private:
- RefCountedPtr<T> *m_refcounter;
+ T *m_object;
Shared() = delete;
- Shared(RefCountedPtr<T> *object)
- : m_refcounter(object) {}
+ Shared(T *object)
+ : m_object(object) {}
inline void incref()
{
- m_refcounter->incref();
+ m_object->incref();
}
inline void decref()
{
- m_refcounter->decref();
+ m_object->decref();
}
public:
template<typename ...Args>
static Shared<T> New(Args&&... args)
{
- T *actual_value = new T(std::forward<Args>(args)...);
- return Shared<T>::FromPointer(actual_value);
+ T *object = new T(std::forward<Args>(args)...);
+ return Shared<T>(object);
}
- static Shared<T> FromPointer(T *ptr)
+ static Shared<T> FromPointer(T *object)
{
- RefCountedPtr<T> *refcounter = new RefCountedPtr<T>(ptr);
- return Shared<T>(refcounter);
+ return Shared<T>(object);
}
Shared(const Shared &other)
{
- m_refcounter = other.m_refcounter;
+ m_object = other.m_object;
this->incref();
}
Shared(Shared &&other)
{
- m_refcounter = other.m_refcounter;
- other.m_refcounter = nullptr;
+ m_object = other.m_object;
+ other.m_object = nullptr;
}
~Shared()
{
/* Can be nullptr when previously moved. */
- if (m_refcounter != nullptr) {
+ if (m_object != nullptr) {
this->decref();
}
}
Shared &operator=(const Shared &other)
{
- if (m_refcounter == other.m_refcounter) {
+ if (m_object == other.m_object) {
return *this;
}
this->decref();
- m_refcounter = other.m_refcounter;
+ m_object = other.m_object;
this->incref();
return *this;
}
@@ -125,24 +104,24 @@ namespace BLI {
Shared &operator=(Shared &&other)
{
this->decref();
- m_refcounter = other.m_refcounter;
- other.m_refcounter = nullptr;
+ m_object = other.m_object;
+ other.m_object = nullptr;
return *this;
}
- T *operator->() const
+ T *ptr() const
{
- return m_refcounter->ptr();
+ return m_object;
}
- RefCountedPtr<T> *refcounter() const
+ T *operator->() const
{
- return m_refcounter;
+ return this->ptr();
}
friend bool operator==(const Shared &a, const Shared &b)
{
- return a.refcounter()->ptr() == b.refcounter()->ptr();
+ return a.m_object == b.m_object;
}
friend bool operator!=(const Shared &a, const Shared &b)
diff --git a/source/blender/depsgraph/intern/builder/deg_builder_relations.cc b/source/blender/depsgraph/intern/builder/deg_builder_relations.cc
index a393747f757..86dbf281597 100644
--- a/source/blender/depsgraph/intern/builder/deg_builder_relations.cc
+++ b/source/blender/depsgraph/intern/builder/deg_builder_relations.cc
@@ -1562,10 +1562,10 @@ void DepsgraphRelationBuilder::build_driver_variables(ID *id, FCurve *fcu)
build_id(dtar->id);
if (dvar->type == DVAR_TYPE_FUNCTION) {
- auto fn = (BLI::RefCountedPtr<FN::Function> *)get_driver_variable_function(dvar);
+ auto fn = (FN::Function *)get_driver_variable_function(dvar);
if (fn != NULL) {
FN::Dependencies dependencies;
- fn->ptr()->body<FN::DependenciesBody>()->dependencies(dependencies);
+ fn->body<FN::DependenciesBody>()->dependencies(dependencies);
dependencies.add_relations(
(struct DepsgraphRelationBuilderRef *)this,
(struct OperationKeyRef *)&driver_key);
diff --git a/source/blender/functions/backends/tuple_call/tuple.hpp b/source/blender/functions/backends/tuple_call/tuple.hpp
index 1fc026c7bba..98df36ffb43 100644
--- a/source/blender/functions/backends/tuple_call/tuple.hpp
+++ b/source/blender/functions/backends/tuple_call/tuple.hpp
@@ -4,7 +4,7 @@
namespace FN {
- class TupleMeta {
+ class TupleMeta : public RefCountedBase {
private:
SmallTypeVector m_types;
SmallVector<CPPTypeInfo *> m_type_info;
diff --git a/source/blender/functions/c_wrapper.cpp b/source/blender/functions/c_wrapper.cpp
index 057904d050d..a253ae1e2d0 100644
--- a/source/blender/functions/c_wrapper.cpp
+++ b/source/blender/functions/c_wrapper.cpp
@@ -16,8 +16,8 @@ using namespace FN::DataFlowNodes;
inline T2 wrap(T1 value) { return (T2)value; }
-WRAPPERS(RefCountedPtr<Function> *, FnFunction);
-WRAPPERS(RefCountedPtr<Type> *, FnType);
+WRAPPERS(Function *, FnFunction);
+WRAPPERS(Type *, FnType);
WRAPPERS(Tuple *, FnTuple);
WRAPPERS(const TupleCallBody *, FnTupleCallBody);
@@ -59,7 +59,7 @@ void FN_function_call(FnTupleCallBody fn_call, FnTuple fn_in, FnTuple fn_out)
FnTupleCallBody FN_function_get_callable(FnFunction fn)
{
- return wrap(unwrap(fn)->ptr()->body<TupleCallBody>());
+ return wrap(unwrap(fn)->body<TupleCallBody>());
}
void FN_function_free(FnFunction fn)
@@ -89,44 +89,44 @@ bool FN_function_has_signature(FnFunction fn, FnType *inputs, FnType *outputs)
uint FN_input_amount(FnFunction fn)
{
- return unwrap(fn)->ptr()->signature().inputs().size();
+ return unwrap(fn)->signature().inputs().size();
}
uint FN_output_amount(FnFunction fn)
{
- return unwrap(fn)->ptr()->signature().outputs().size();
+ return unwrap(fn)->signature().outputs().size();
}
bool FN_input_has_type(FnFunction fn, uint index, FnType type)
{
- Type *type1 = unwrap(fn)->ptr()->signature().inputs()[index].type().refcounter()->ptr();
- Type *type2 = unwrap(type)->ptr();
+ Type *type1 = unwrap(fn)->signature().inputs()[index].type().ptr();
+ Type *type2 = unwrap(type);
return type1 == type2;
}
bool FN_output_has_type(FnFunction fn, uint index, FnType type)
{
- Type *type1 = unwrap(fn)->ptr()->signature().outputs()[index].type().refcounter()->ptr();
- Type *type2 = unwrap(type)->ptr();
+ Type *type1 = unwrap(fn)->signature().outputs()[index].type().ptr();
+ Type *type2 = unwrap(type);
return type1 == type2;
}
void FN_function_print(FnFunction fn)
{
- Function *function = unwrap(fn)->ptr();
+ Function *function = unwrap(fn);
function->print();
}
FnTuple FN_tuple_for_input(FnFunction fn)
{
- auto tuple = new Tuple(unwrap(fn)->ptr()->signature().input_types());
+ auto tuple = new Tuple(unwrap(fn)->signature().input_types());
return wrap(tuple);
}
FnTuple FN_tuple_for_output(FnFunction fn)
{
- auto tuple = new Tuple(unwrap(fn)->ptr()->signature().output_types());
+ auto tuple = new Tuple(unwrap(fn)->signature().output_types());
return wrap(tuple);
}
@@ -169,7 +169,7 @@ void FN_tuple_get_float_vector_3(FnTuple tuple, uint index, float dst[3])
const char *FN_type_name(FnType type)
{
- return unwrap(type)->ptr()->name().c_str();
+ return unwrap(type)->name().c_str();
}
void FN_type_free(FnType type)
@@ -179,7 +179,7 @@ void FN_type_free(FnType type)
static FnType get_type_with_increased_refcount(const SharedType &type)
{
- RefCountedPtr<Type> *typeref = type.refcounter();
+ Type *typeref = type.ptr();
typeref->incref();
return wrap(typeref);
}
@@ -188,7 +188,7 @@ static FnType get_type_with_increased_refcount(const SharedType &type)
FnType FN_type_get_##name() \
{ return get_type_with_increased_refcount(Types::get_##name##_type()); } \
FnType FN_type_borrow_##name() \
- { return wrap(Types::get_##name##_type().refcounter()); }
+ { return wrap(Types::get_##name##_type().ptr()); }
SIMPLE_TYPE_GETTER(float);
SIMPLE_TYPE_GETTER(int32);
@@ -198,14 +198,14 @@ FnFunction FN_tree_to_function(bNodeTree *btree)
{
TIMEIT("Tree to function");
BLI_assert(btree);
- auto fn_ = DataFlowNodes::generate_function(btree);
- if (!fn_.has_value()) {
+ auto fn_opt = DataFlowNodes::generate_function(btree);
+ if (!fn_opt.has_value()) {
return nullptr;
}
- RefCountedPtr<Function> *fn_ref = fn_.value().refcounter();
- fn_ref->incref();
- return wrap(fn_ref);
+ Function *fn_ptr = fn_opt.value().ptr();
+ fn_ptr->incref();
+ return wrap(fn_ptr);
}
FnFunction FN_function_get_with_signature(
@@ -232,8 +232,8 @@ void FN_function_update_dependencies(
FnFunction fn,
struct DepsNodeHandle *deps_node)
{
- RefCountedPtr<Function> *fn_ref = unwrap(fn);
- const DependenciesBody *body = fn_ref->ptr()->body<DependenciesBody>();
+ Function *fn_ = unwrap(fn);
+ const DependenciesBody *body = fn_->body<DependenciesBody>();
if (body) {
Dependencies dependencies;
body->dependencies(dependencies);
diff --git a/source/blender/functions/core/data_flow_graph.hpp b/source/blender/functions/core/data_flow_graph.hpp
index 3aae24e3c62..8d7a0fb72f5 100644
--- a/source/blender/functions/core/data_flow_graph.hpp
+++ b/source/blender/functions/core/data_flow_graph.hpp
@@ -226,7 +226,7 @@ namespace FN {
SmallVector<Link> m_all_links;
};
- class DataFlowGraph {
+ class DataFlowGraph : public RefCountedBase {
public:
DataFlowGraph();
diff --git a/source/blender/functions/core/function.hpp b/source/blender/functions/core/function.hpp
index 3c57f4f6055..dda39182143 100644
--- a/source/blender/functions/core/function.hpp
+++ b/source/blender/functions/core/function.hpp
@@ -29,7 +29,7 @@ namespace FN {
}
};
- class Function final {
+ class Function final : public RefCountedBase {
public:
Function(const std::string &name, const Signature &signature)
: m_name(name), m_signature(signature) {}
diff --git a/source/blender/functions/core/type.hpp b/source/blender/functions/core/type.hpp
index 7f880ee49b8..ca9c04c72de 100644
--- a/source/blender/functions/core/type.hpp
+++ b/source/blender/functions/core/type.hpp
@@ -8,7 +8,7 @@ namespace FN {
using namespace BLI;
- class Type final {
+ class Type final : public RefCountedBase {
public:
Type() = delete;
Type(const std::string &name)
diff --git a/tests/gtests/blenlib/BLI_shared_test.cc b/tests/gtests/blenlib/BLI_shared_test.cc
index a59f9439760..282b5
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list