summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Andres Klode <jak@debian.org>2009-07-10 19:13:16 +0200
committerJulian Andres Klode <jak@debian.org>2009-07-10 19:13:16 +0200
commit2d8f74890d6203c669db960c7108cad7f45c3a70 (patch)
treeb9ad30e449a1e74c16363b3bc300c46b47b70163
parentc0818a4cdadb30ccb2ea9d63a6e81fa1a39394d6 (diff)
downloadpython-apt-2d8f74890d6203c669db960c7108cad7f45c3a70.tar.gz
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.
-rw-r--r--python/generic.h17
1 files changed, 13 insertions, 4 deletions
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 <class T> 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<T>(Obj).~T();
+ if (!((CppPyObject<T>*)Obj)->NoDelete)
+ GetCpp<T>(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<T> *Obj = (CppOwnedPyObject<T> *)iObj;
- Obj->Object.~T();
+ if (!((CppPyObject<T>*)Obj)->NoDelete)
+ Obj->Object.~T();
CppOwnedClear<T>(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<T>(Obj);
+ if (!((CppPyObject<T>*)Obj)->NoDelete)
+ delete GetCpp<T>(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<T> *Obj = (CppOwnedPyObject<T> *)iObj;
- delete Obj->Object;
+ if (!((CppPyObject<T>*)Obj)->NoDelete)
+ delete Obj->Object;
CppOwnedClear<T>(iObj);
iObj->ob_type->tp_free(iObj);
}