Navigation Menu

Skip to content

Commit

Permalink
libcore|Garbage: C++ helper for putting objects in the trash
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Aug 24, 2015
1 parent 52813dd commit f54ba1d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
22 changes: 20 additions & 2 deletions doomsday/sdk/libcore/include/de/core/garbage.h
Expand Up @@ -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</small>
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBDENG2_GARBAGE_COLLECTOR_H
Expand Down Expand Up @@ -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 <typename Type>
void deleter(void *p) {
delete reinterpret_cast<Type *>(p);
}

template <typename Type>
void trash(Type *ptr) {
Garbage_TrashInstance(ptr, deleter<Type>);
}

} // namespace de

#endif // __cplusplus

#endif // LIBDENG2_GARBAGE_COLLECTOR_H
7 changes: 5 additions & 2 deletions doomsday/sdk/libcore/src/core/garbage.cpp
Expand Up @@ -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)
Expand Down

0 comments on commit f54ba1d

Please sign in to comment.