Skip to content

Commit

Permalink
refactor: handle, rhandle and loose_handle now use reset() not detach()
Browse files Browse the repository at this point in the history
using the syntax of current C++ smart pointers
  • Loading branch information
rodolforg committed Oct 11, 2023
1 parent 32cf979 commit 1aa1428
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 44 deletions.
30 changes: 13 additions & 17 deletions ETL/ETL/_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class handle
}

//! Handle is released on deletion
~handle() { detach(); }
~handle() { reset(); }

//! Assignment operator
handle<value_type> &
Expand All @@ -160,7 +160,7 @@ class handle
// add reference before detach
pointer xobj(x.get());
if(xobj) xobj->ref();
detach();
reset();
obj=xobj;
return *this;
}
Expand All @@ -178,7 +178,7 @@ class handle
//! Handle detach procedure
/*! unref()'s the object and sets the internal object pointer to \c nullptr */
void
detach()
reset()
{
pointer xobj(obj);
obj = nullptr;
Expand All @@ -191,8 +191,6 @@ class handle
#endif
}

void reset() { detach(); }

bool empty()const { return obj==0; }

//! Creates a new instance of a T object and puts it in the handle.
Expand Down Expand Up @@ -406,7 +404,7 @@ class rhandle : public handle<T>
}

//! Handle is released on deletion
~rhandle() { detach(); }
~rhandle() { reset(); }

//! Assignment operator
rhandle<value_type> &
Expand All @@ -415,7 +413,7 @@ class rhandle : public handle<T>
if(x.get()==obj)
return *this;

detach();
reset();

obj=x.get();
if(obj)
Expand All @@ -432,7 +430,7 @@ class rhandle : public handle<T>
if(x.get()==obj)
return *this;

detach();
reset();

obj=x.get();
if(obj)
Expand All @@ -449,7 +447,7 @@ class rhandle : public handle<T>
if(x==obj)
return *this;

detach();
reset();

obj=x;
if(obj)
Expand All @@ -463,15 +461,13 @@ class rhandle : public handle<T>
//! Handle release procedure
/*! unref()'s the object and sets the internal object pointer to \c nullptr */
void
detach()
reset()
{
if(obj)del_from_rlist();
handle<value_type>::detach();
handle<value_type>::reset();
obj = nullptr;
}

void reset() { detach(); }

//! Creates a new instance of a T object and puts it in the handle.
/*! Uses the default constructor */
void spawn() { operator=(handle<value_type>(new T())); }
Expand Down Expand Up @@ -608,10 +604,10 @@ class loose_handle
}

//! Handle release procedure
void detach() { obj=0; }


void reset() { detach(); }
void reset()
{
obj = nullptr;
}

bool empty()const { return obj==0; }

Expand Down
52 changes: 26 additions & 26 deletions synfig-core/test/handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,43 +300,43 @@ empty_handle_is_not_unique()


void
handle_detach_decreases_refcount()
handle_reset_decreases_refcount()
{
BasicSharedObject* real_obj = new BasicSharedObject();

BasicSharedObject::Handle obj1(real_obj);
BasicSharedObject::Handle obj2(real_obj);

obj2.detach();
obj2.reset();
ASSERT_EQUAL(1, real_obj->count());
}

void
handle_detach_makes_itself_empty()
handle_reset_makes_itself_empty()
{
BasicSharedObject* real_obj = new BasicSharedObject();

BasicSharedObject::Handle obj1(real_obj);
BasicSharedObject::Handle obj2(real_obj);

obj2.detach();
obj2.reset();
ASSERT(obj2.empty());
ASSERT_FALSE(obj1.empty());
obj1.detach();
obj1.reset();
ASSERT(obj1.empty());
}

void
handle_detach_an_already_empty_handle_does_nothing()
handle_reset_an_already_empty_handle_does_nothing()
{
BasicSharedObject::Handle obj1(new BasicSharedObject());
obj1.detach();
obj1.reset();
ASSERT(obj1.empty());
obj1.detach();
obj1.reset();
ASSERT(obj1.empty());

BasicSharedObject::Handle obj2;
obj2.detach();
obj2.reset();
ASSERT(obj2.empty());
}

Expand Down Expand Up @@ -682,48 +682,48 @@ empty_loose_handle_has_refcount_zero()
}

void
loose_handle_detach_does_not_decrease_refcount()
loose_handle_reset_does_not_decrease_refcount()
{
BasicSharedObject* real_obj = new BasicSharedObject();
DeleteGuard<BasicSharedObject> guard(real_obj);

BasicSharedObject::LooseHandle obj1(real_obj);
BasicSharedObject::LooseHandle obj2(real_obj);

obj2.detach();
obj2.reset();
ASSERT_EQUAL(0, real_obj->count());
}

void
loose_handle_detach_makes_itself_empty()
loose_handle_reset_makes_itself_empty()
{
BasicSharedObject* real_obj = new BasicSharedObject();
DeleteGuard<BasicSharedObject> guard(real_obj);

BasicSharedObject::LooseHandle obj1(real_obj);
BasicSharedObject::LooseHandle obj2(real_obj);

obj2.detach();
obj2.reset();
ASSERT(obj2.empty());
ASSERT_FALSE(obj1.empty());
obj1.detach();
obj1.reset();
ASSERT(obj1.empty());
}

void
loose_handle_detach_an_already_empty_loose_handle_does_nothing()
loose_handle_reset_an_already_empty_loose_handle_does_nothing()
{
BasicSharedObject* real_obj = new BasicSharedObject();
DeleteGuard<BasicSharedObject> guard(real_obj);

BasicSharedObject::LooseHandle obj1(real_obj);
obj1.detach();
obj1.reset();
ASSERT(obj1.empty());
obj1.detach();
obj1.reset();
ASSERT(obj1.empty());

BasicSharedObject::LooseHandle obj2;
obj2.detach();
obj2.reset();
ASSERT(obj2.empty());
}

Expand Down Expand Up @@ -1161,8 +1161,8 @@ rhandle_general_use_test()


my_list.clear();
obj.detach();
new_obj.detach();
obj.reset();
new_obj.reset();

ASSERT_EQUAL(0, MyTestObj::instance_count);

Expand Down Expand Up @@ -1319,9 +1319,9 @@ int main()
TEST_FUNCTION(handle_destructor_deletes_the_object_if_no_more_references);
TEST_FUNCTION(empty_handle_has_refcount_zero);
TEST_FUNCTION(empty_handle_is_not_unique);
TEST_FUNCTION(handle_detach_decreases_refcount);
TEST_FUNCTION(handle_detach_makes_itself_empty);
TEST_FUNCTION(handle_detach_an_already_empty_handle_does_nothing);
TEST_FUNCTION(handle_reset_decreases_refcount);
TEST_FUNCTION(handle_reset_makes_itself_empty);
TEST_FUNCTION(handle_reset_an_already_empty_handle_does_nothing);
TEST_FUNCTION(handle_self_assignment_does_not_increase_refcount);
TEST_FUNCTION(handle_assignment_increases_refcount);
TEST_FUNCTION(handle_assignment_stores_the_same_object);
Expand All @@ -1348,9 +1348,9 @@ int main()
TEST_FUNCTION(loose_handle_constructor_stores_the_same_object);
TEST_FUNCTION(loose_handle_destructor_does_not_delete_the_object_if_no_more_references);
TEST_FUNCTION(empty_loose_handle_has_refcount_zero);
TEST_FUNCTION(loose_handle_detach_does_not_decrease_refcount);
TEST_FUNCTION(loose_handle_detach_makes_itself_empty);
TEST_FUNCTION(loose_handle_detach_an_already_empty_loose_handle_does_nothing);
TEST_FUNCTION(loose_handle_reset_does_not_decrease_refcount);
TEST_FUNCTION(loose_handle_reset_makes_itself_empty);
TEST_FUNCTION(loose_handle_reset_an_already_empty_loose_handle_does_nothing);
TEST_FUNCTION(loose_handle_self_assignment_does_not_increase_refcount);
TEST_FUNCTION(loose_handle_assignment_does_not_increase_refcount);
TEST_FUNCTION(loose_handle_assignment_stores_the_same_object);
Expand Down
2 changes: 1 addition & 1 deletion synfig-studio/src/gui/preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ void studio::Widget_Preview::stoprender()
// don't crash if the render has already been stopped
if (!preview->renderer)
return;
preview->renderer.detach();
preview->renderer.reset();
App::dock_info_->set_render_progress(0.0);
}
}
Expand Down

0 comments on commit 1aa1428

Please sign in to comment.