Skip to content

Instantly share code, notes, and snippets.

@pallas pallas/countable.hh

Last active Aug 29, 2015
What would you like to do?
C++ reference counter base, with hook
// All rights reserved,
// Derrick Pallas
// License: zlib
#include <cassert>
template <class T>
class countable {
countable() : r(0) { }
~countable() { assert(!r); }
unsigned refs() { return r; }
bool collect() { return true; }
static T* get(T * t) {
return t;
static bool put(T * t) {
if (--t->r)
return false;
if (t->collect())
delete t;
return true;
countable(const countable &);
countable & operator=(const countable &);
unsigned r;
namespace ref {
template <class T> T* get(T * t) { return countable<T>::get(t); }
template <class T> bool put(T * t) { return countable<T>::put(t); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.