Advertisement
bueddl

Untitled

Nov 3rd, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. template<class T>
  2. class proxy
  3. {
  4. public:
  5.     proxy(T &obj)
  6.         m_obj(obj)
  7.     {
  8.     }
  9.  
  10.     template<typename fn, typename rt, typename ...arg_types>
  11.     rt do_sync(arg_types ...args)
  12.     {
  13.         rt return_var;
  14.         obj->*fn(std::forward<arg_types>(args)..., [this, return_var]
  15.             (cb_arg)
  16.             {
  17.                 return_var = cb_arg;
  18.                 m_cv.notify();
  19.             });
  20.  
  21.         m_cv.wait();
  22.         return return_var;
  23.     }
  24.  
  25. private:
  26.     T &m_obj;
  27.     std::condition_variable m_cv;
  28. };
  29.  
  30. // Usage example
  31.  
  32. class gameserver
  33. {
  34. public:
  35.     game::map_list get_map_list(int offset, int count)
  36.     {
  37.         proxy<gameserver> gs_proxy(*this);
  38.         return gs_proxy.do_sync<gameserver::get_map_list_async>(offset, count);
  39.     }
  40.  
  41.     void get_map_list_async(int offset, int count, void(cb*)(const game::map_list &map_list))
  42.     {
  43.         // Impl
  44.     }
  45.  
  46. private:
  47. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement