Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ##### main.cpp on 26 May 2011
- // ##### Used for Hooks
- // ##### Created by WeltEnSTurm
- #include <vector>
- #include <iostream>
- class hookbase {
- public:
- typedef void (hookbase::*func)();
- protected:
- struct conv {
- hookbase::func targ;
- void* f;
- };
- static std::vector<conv> flist_pre;
- static std::vector<conv> flist_post;
- public:
- template<typename... args>
- static void AddPre(hookbase::func targ, void (*f)(args...)){
- flist_pre.push_back((conv){targ,(void*)f});
- }
- template<typename... args>
- static void AddPost(hookbase::func targ, void (*f)(args...)){
- flist_post.push_back((conv){targ,(void*)f});
- }
- template<typename... args>
- void Call(hookbase::func targ, args... a){
- typedef void (*ftyp)(args...);
- typedef void (hookbase::*ctyp)(args...);
- for(unsigned int i = 0; i < flist_pre.size(); i++)
- if(targ == flist_pre[i].targ)
- ((ftyp)flist_pre[i].f)(a...);
- (this->*((ctyp)targ))(a...);
- for(unsigned int i = 0; i < flist_post.size(); i++)
- if(targ == flist_post[i].targ)
- ((ftyp)flist_post[i].f)(a...);
- }
- };
- std::vector<hookbase::conv> hookbase::flist_pre;
- std::vector<hookbase::conv> hookbase::flist_post;
- template<typename... args>
- class hook {
- protected:
- hookbase::func mTarg;
- hookbase* mObj;
- typedef void(*mFuncType)(args...);
- public:
- hook(hookbase& obj, hookbase::func targ){
- mTarg = targ;
- mObj = &obj;
- }
- void AddPre(mFuncType f){
- hookbase::AddPre<args...>(mTarg,f);
- }
- void AddPost(mFuncType f){
- hookbase::AddPost<args...>(mTarg,f);
- }
- void Call(args... a){
- mObj->Call(mTarg, a...);
- }
- };
- class whatever : public hookbase {
- public:
- void Test(int x, int y){
- std::cout << "In Class: " << x+y << std::endl;
- }
- };
- void TestHook(int x, int y){
- std::cout << "In Hook: " << x+y << std::endl;
- }
- int main(){
- whatever test;
- hook<int,int> testhook(test, (hookbase::func)&whatever::Test);
- testhook.AddPre(TestHook);
- testhook.AddPost(TestHook);
- testhook.Call(123,51);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement