This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef void(VoidMemberFn)(void*); | |
VoidMemberFn* fn = (VoidMemberFn*)GetVTable<BaseType>(my_obj_ptr)[1]; | |
// ... sometime later | |
fn(my_obj_ptr); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename T> | |
void** GetVTable(T* obj) { | |
return *((void***)obj); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BouncyBall : public GameObject { | |
void Update() override { | |
// ... | |
} | |
void PhysicsUpdate() override { | |
// ... | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SceneManager { | |
public: | |
SceneManager() { | |
null_vtable_ = GetVtable(&null_object_); | |
} | |
void Add(SceneObject* obj) { | |
SceneObject_VTable* vtable = GetVtable(obj); | |
if (vtable->update != null_vtable_->update) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SceneObject { | |
public: | |
virtual ~SceneObject() {} | |
virtual void Update() {} | |
virtual void LateUpdate() {} | |
virtual void PhysicsUpdate() {} | |
virtual void DrawImmediate() {} | |
}; | |
struct SceneObject_VTable { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IncrementerBase { | |
public: | |
IncrementerBase() { | |
my_num = rand(); | |
} | |
virtual ~IncrementerBase() {} | |
virtual void Increment() {} | |
int my_num; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GenericGreeter_VTable* GetVTable(GenericGreeter* obj) { | |
GenericGreeter_VTable** vtable_ptr = (GenericGreeter_VTable**)obj; | |
return *(vtable_ptr); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef void (GreetFn)(void*, const char* name); | |
struct GenericGreeter_VTable { | |
GreetFn* greet; | |
GreetFn* dismiss; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GenericGreeter { | |
public: | |
virtual void Greet(const char* name) { | |
cout << "Hi " << name << "." << endl; | |
} | |
virtual void Dismiss(const char* name) { | |
cout << "Bye " << name << "." << endl; | |
} | |
}; |
NewerOlder