Advertisement
WeltEnSTurm

Untitled

May 26th, 2011
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1.  
  2. //  ##### main.cpp on 26 May 2011
  3. //  ##### Used for Hooks
  4. //  ##### Created by WeltEnSTurm
  5.  
  6. #include <vector>
  7. #include <iostream>
  8.  
  9. class hookbase {
  10.     public:
  11.         typedef void (hookbase::*func)();
  12.  
  13.     protected:
  14.         struct conv {
  15.             hookbase::func targ;
  16.             void* f;
  17.         };
  18.         static std::vector<conv> flist_pre;
  19.         static std::vector<conv> flist_post;
  20.  
  21.     public:
  22.  
  23.         template<typename... args>
  24.         static void AddPre(hookbase::func targ, void (*f)(args...)){
  25.             flist_pre.push_back((conv){targ,(void*)f});
  26.         }
  27.  
  28.         template<typename... args>
  29.         static void AddPost(hookbase::func targ, void (*f)(args...)){
  30.             flist_post.push_back((conv){targ,(void*)f});
  31.         }
  32.  
  33.         template<typename... args>
  34.         void Call(hookbase::func targ, args... a){
  35.  
  36.             typedef void (*ftyp)(args...);
  37.             typedef void (hookbase::*ctyp)(args...);
  38.  
  39.             for(unsigned int i = 0; i < flist_pre.size(); i++)
  40.                 if(targ == flist_pre[i].targ)
  41.                     ((ftyp)flist_pre[i].f)(a...);
  42.  
  43.             (this->*((ctyp)targ))(a...);
  44.  
  45.             for(unsigned int i = 0; i < flist_post.size(); i++)
  46.                 if(targ == flist_post[i].targ)
  47.                     ((ftyp)flist_post[i].f)(a...);
  48.         }
  49. };
  50. std::vector<hookbase::conv> hookbase::flist_pre;
  51. std::vector<hookbase::conv> hookbase::flist_post;
  52.  
  53. template<typename... args>
  54. class hook {
  55.     protected:
  56.         hookbase::func mTarg;
  57.         hookbase* mObj;
  58.         typedef void(*mFuncType)(args...);
  59.     public:
  60.         hook(hookbase& obj, hookbase::func targ){
  61.             mTarg = targ;
  62.             mObj = &obj;
  63.         }
  64.         void AddPre(mFuncType f){
  65.             hookbase::AddPre<args...>(mTarg,f);
  66.         }
  67.         void AddPost(mFuncType f){
  68.             hookbase::AddPost<args...>(mTarg,f);
  69.         }
  70.         void Call(args... a){
  71.             mObj->Call(mTarg, a...);
  72.         }
  73. };
  74.  
  75. class whatever : public hookbase {
  76.     public:
  77.         void Test(int x, int y){
  78.             std::cout << "In Class: " << x+y << std::endl;
  79.         }
  80. };
  81.  
  82. void TestHook(int x, int y){
  83.     std::cout << "In Hook: " << x+y << std::endl;
  84. }
  85.  
  86. int main(){
  87.     whatever test;
  88.     hook<int,int> testhook(test, (hookbase::func)&whatever::Test);
  89.     testhook.AddPre(TestHook);
  90.     testhook.AddPost(TestHook);
  91.     testhook.Call(123,51);
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement