Skip to content

Instantly share code, notes, and snippets.

@Cleroth
Created April 13, 2017 23:10
Show Gist options
  • Save Cleroth/ad29da13536188dcc4103e7a69930a25 to your computer and use it in GitHub Desktop.
Save Cleroth/ad29da13536188dcc4103e7a69930a25 to your computer and use it in GitHub Desktop.
#pragma once
// -------------------------- USAGE INFO ----------------------------
/*
// T.h:
class T {
private:
OBJECT_TRACKER(T);
};
// T.cpp:
OBJECT_TRACKER_INIT(T);
// stdafx.cpp:
ObjectTracking::Tracker ObjectTracker;
*/
namespace ObjectTracking{
struct Tracker {
static const size_t MaxObjects = 256 * 128;
const char * Names[MaxObjects];
size_t Count[MaxObjects];
size_t NumObjects;
};
template <typename T>
struct Init {
Init(const char * name) {
Instance<T>::ID = ObjectTracker.NumObjects;
ObjectTracker.Names[ObjectTracker.NumObjects] = name;
ObjectTracker.NumObjects++;
assert(ObjectTracker.NumObjects < Tracker::MaxObjects);
}
};
template <typename T>
struct Instance {
static size_t ID;
Instance() {
++ObjectTracker.Count[ID];
}
Instance(const Instance&) {
++ObjectTracker.Count[ID];
}
Instance(Instance&&) {
++ObjectTracker.Count[ID];
}
Instance& operator=(const Instance& other) = delete;
Instance& operator=(Instance&& other) = delete;
~Instance() {
--ObjectTracker.Count[ID];
}
};
};
extern ObjectTracking::Tracker ObjectTracker;
#define OBJECT_TRACKER(T) ObjectTracking::Instance<T> __object_tracker;
#define OBJECT_TRACKER_INIT(T) \
size_t ObjectTracking::Instance<T>::ID; \
namespace ObjectTracking{ \
namespace { Init<T> __init(#T); }; \
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment