From f54ba1d01a01634704504e8d1bccbc10059e71b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Mon, 24 Aug 2015 12:10:04 +0300 Subject: [PATCH] libcore|Garbage: C++ helper for putting objects in the trash --- .../sdk/libcore/include/de/core/garbage.h | 22 +++++++++++++++++-- doomsday/sdk/libcore/src/core/garbage.cpp | 7 ++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/doomsday/sdk/libcore/include/de/core/garbage.h b/doomsday/sdk/libcore/include/de/core/garbage.h index 5ad7094911..5d54abceca 100644 --- a/doomsday/sdk/libcore/include/de/core/garbage.h +++ b/doomsday/sdk/libcore/include/de/core/garbage.h @@ -18,7 +18,7 @@ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser * General Public License for more details. You should have received a copy of * the GNU Lesser General Public License along with this program; if not, see: - * http://www.gnu.org/licenses + * http://www.gnu.org/licenses */ #ifndef LIBDENG2_GARBAGE_COLLECTOR_H @@ -107,6 +107,24 @@ DENG2_PUBLIC void Garbage_ClearForThread(void); #ifdef __cplusplus } // extern "C" -#endif + +namespace de { + +/** + * Helper template for deleting arbitrary C++ objects via the garbage collector. + */ +template +void deleter(void *p) { + delete reinterpret_cast(p); +} + +template +void trash(Type *ptr) { + Garbage_TrashInstance(ptr, deleter); +} + +} // namespace de + +#endif // __cplusplus #endif // LIBDENG2_GARBAGE_COLLECTOR_H diff --git a/doomsday/sdk/libcore/src/core/garbage.cpp b/doomsday/sdk/libcore/src/core/garbage.cpp index aad2e45fbf..e1c0e8940d 100644 --- a/doomsday/sdk/libcore/src/core/garbage.cpp +++ b/doomsday/sdk/libcore/src/core/garbage.cpp @@ -155,8 +155,11 @@ void Garbage_TrashMalloc(void *ptr) void Garbage_TrashInstance(void *ptr, GarbageDestructor destructor) { - Garbage *g = garbageForThread(QThread::currentThread()); - g->allocs[ptr] = destructor; + if(ptr) + { + Garbage *g = garbageForThread(QThread::currentThread()); + g->allocs[ptr] = destructor; + } } int Garbage_IsTrashed(void const *ptr)