1 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html 2 3 #include "smobject.hh" 4 #include <assert.h> 5 #include <glib.h> 6 7 using namespace SpectMorph; 8 Object()9Object::Object() 10 { 11 object_ref_count = 1; 12 } 13 14 void ref()15Object::ref() 16 { 17 std::lock_guard<std::mutex> lock (object_mutex); 18 19 assert (object_ref_count > 0); 20 object_ref_count++; 21 } 22 23 void unref()24Object::unref() 25 { 26 bool destroy; 27 // unlock before possible delete this 28 { 29 std::lock_guard<std::mutex> lock (object_mutex); 30 assert (object_ref_count > 0); 31 object_ref_count--; 32 destroy = (object_ref_count == 0); 33 } 34 if (destroy) 35 { 36 delete this; 37 } 38 } 39 ~Object()40Object::~Object() 41 { 42 // need virtual destructor to be able to delete derived classes properly 43 g_return_if_fail (object_ref_count == 0); 44 } 45