From 2d8f74890d6203c669db960c7108cad7f45c3a70 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Fri, 10 Jul 2009 19:13:16 +0200 Subject: python/generic.h: Introduce NoDelete field in CppPyObject. Setting NoDelete to true causes the deallocation functions to not delete the underlying C++ object. This is useful in situations where the object is already managed somewhere else, e.g. DepCache by CacheFile. It will also assist us in providing a flexible C++ API which only takes ownership if requested. --- python/generic.h | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'python') diff --git a/python/generic.h b/python/generic.h index 118917cb..408529a5 100644 --- a/python/generic.h +++ b/python/generic.h @@ -117,6 +117,11 @@ template struct CppPyObject : public PyObject // So basically having the c'tor here removes the need for T to have a // default c'tor, which is not always desireable. CppPyObject() { }; + + // Flag which causes the underlying object to not be deleted. + bool NoDelete; + + // The underlying C++ object. T Object; }; @@ -209,7 +214,8 @@ void CppDealloc(PyObject *Obj) #ifdef ALLOC_DEBUG std::cerr << "=== DEALLOCATING " << Obj->ob_type->tp_name << " ===\n"; #endif - GetCpp(Obj).~T(); + if (!((CppPyObject*)Obj)->NoDelete) + GetCpp(Obj).~T(); Obj->ob_type->tp_free(Obj); } @@ -220,7 +226,8 @@ void CppOwnedDealloc(PyObject *iObj) std::cerr << "=== DEALLOCATING " << iObj->ob_type->tp_name << "+ ===\n"; #endif CppOwnedPyObject *Obj = (CppOwnedPyObject *)iObj; - Obj->Object.~T(); + if (!((CppPyObject*)Obj)->NoDelete) + Obj->Object.~T(); CppOwnedClear(iObj); iObj->ob_type->tp_free(iObj); } @@ -233,7 +240,8 @@ void CppDeallocPtr(PyObject *Obj) #ifdef ALLOC_DEBUG std::cerr << "=== DEALLOCATING " << Obj->ob_type->tp_name << "* ===\n"; #endif - delete GetCpp(Obj); + if (!((CppPyObject*)Obj)->NoDelete) + delete GetCpp(Obj); Obj->ob_type->tp_free(Obj); } @@ -244,7 +252,8 @@ void CppOwnedDeallocPtr(PyObject *iObj) std::cerr << "=== DEALLOCATING " << iObj->ob_type->tp_name << "*+ ===\n"; #endif CppOwnedPyObject *Obj = (CppOwnedPyObject *)iObj; - delete Obj->Object; + if (!((CppPyObject*)Obj)->NoDelete) + delete Obj->Object; CppOwnedClear(iObj); iObj->ob_type->tp_free(iObj); } -- cgit v1.2.3