Skip to content

Instantly share code, notes, and snippets.

@cleak
Created January 21, 2019 04:04
Show Gist options
  • Save cleak/289d0d9312ab08b24b0ee688c8215ddb to your computer and use it in GitHub Desktop.
Save cleak/289d0d9312ab08b24b0ee688c8215ddb to your computer and use it in GitHub Desktop.
typedef void (UpdateFn)(void*);
struct CompareUpdateFns {
bool operator()(const std::pair<UpdateFn*, void*>& a,
const std::pair<UpdateFn*, void*>& b) const {
if (a.first == b.first) {
return a.second < b.second;
}
return a.first < b.first;
}
};
class SceneObject {
public:
virtual ~SceneObject() {}
virtual void Update() {}
};
struct SceneObject_VTable {
void* dtor;
UpdateFn* update;
};
UpdateFn* GetUpdateFn(SceneObject* obj) {
SceneObject_VTable** vtable_ptr = (SceneObject_VTable**)obj;
return (*vtable_ptr)->update;
}
class SceneManager {
public:
void Add(SceneObject* obj) {
update_fns_.insert({ GetUpdateFn(obj), obj });
}
void UpdateAll() {
for (auto& update : update_fns_) {
update.first(update.second);
}
}
private:
std::set<std::pair<UpdateFn*, void*>, CompareUpdateFns> update_fns_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment