foobar2000 SDK  2015-01-14
main_thread_callback.h
Go to the documentation of this file.
1 class NOVTABLE main_thread_callback : public service_base {
3 public:
5  virtual void callback_run() = 0;
6 
7  void callback_enqueue(); // helper
8 
9  FB2K_MAKE_SERVICE_INTERFACE(main_thread_callback,service_base);
10 };
11 
16 class NOVTABLE main_thread_callback_manager : public service_base {
17 public:
19  virtual void add_callback(service_ptr_t<main_thread_callback> p_callback) = 0;
20 
21  FB2K_MAKE_SERVICE_INTERFACE_ENTRYPOINT(main_thread_callback_manager);
22 };
23 
24 
25 void main_thread_callback_add(main_thread_callback::ptr ptr);
26 
27 template<typename t_class> static void main_thread_callback_spawn() {
29 }
30 template<typename t_class, typename t_param1> static void main_thread_callback_spawn(const t_param1 & p1) {
32 }
33 template<typename t_class, typename t_param1, typename t_param2> static void main_thread_callback_spawn(const t_param1 & p1, const t_param2 & p2) {
35 }
36 
37 // Proxy class - friend this to allow callInMainThread to access your private methods
39 public:
40  template<typename host_t, typename param_t>
41  static void callThis(host_t * host, param_t & param) {
42  host->inMainThread(param);
43  }
44  template<typename host_t>
45  static void callThis( host_t * host ) {
46  host->inMainThread();
47  }
48 };
49 
50 // Internal class, do not use.
51 template<typename service_t, typename param_t>
53 public:
54  _callInMainThreadSvc_t(service_t * host, param_t const & param) : m_host(host), m_param(param) {}
55  void callback_run() {
57  }
58 private:
60  param_t m_param;
61 };
62 
63 
64 // Main thread callback helper. You can use this to easily invoke inMainThread(someparam) on your class without writing any wrapper code.
65 // Requires myservice_t to be a fb2k service class with reference counting.
66 template<typename myservice_t, typename param_t>
67 static void callInMainThreadSvc(myservice_t * host, param_t const & param) {
69  service_ptr_t<impl_t> obj = new service_impl_t<impl_t>(host, param);
71 }
72 
73 
74 
75 
83 public:
84 
86 
87  template<typename host_t, typename arg_t>
88  class entry : public main_thread_callback {
89  public:
90  entry( host_t * host, arg_t const & arg, killswitch_t ks ) : m_ks(ks), m_host(host), m_arg(arg) {}
91  void callback_run() {
93  }
94  private:
95  killswitch_t m_ks;
96  host_t * m_host;
97  arg_t m_arg;
98  };
99  template<typename host_t>
101  public:
102  entryVoid( host_t * host, killswitch_t ks ) : m_ks(ks), m_host(host) {}
103  void callback_run() {
105  }
106  private:
107  killswitch_t m_ks;
108  host_t * m_host;
109  };
110 
111  template<typename host_t, typename arg_t>
112  void add( host_t * host, arg_t const & arg) {
113  add_( new service_impl_t< entry<host_t, arg_t> >( host, arg, m_ks ) );
114  }
115  template<typename host_t>
116  void add( host_t * host ) {
117  add_( new service_impl_t< entryVoid<host_t> >( host, m_ks ) );
118  }
119  void add_( main_thread_callback::ptr cb ) {
121  }
122 
124  m_ks.new_t();
125  * m_ks = false;
126  }
127  void shutdown() {
128  PFC_ASSERT( core_api::is_main_thread() );
129  * m_ks = true;
130  }
132  shutdown();
133  }
134 
135 private:
136  killswitch_t m_ks;
137 
138 };
void add_(main_thread_callback::ptr cb)
Template implementing reference-counting features of service_base. Intended for dynamic instantiation...
Definition: service_impl.h:4
void main_thread_callback_add(main_thread_callback::ptr ptr)
void new_t()
Definition: rcptr.h:101
static void callThis(host_t *host, param_t &param)
T * get_ptr() const
Definition: service.h:117
void add(host_t *host, arg_t const &arg)
pfc::rcptr_t< bool > killswitch_t
entryVoid(host_t *host, killswitch_t ks)
void callback_run()
Gets called from main app thread. See main_thread_callback_manager description for more info...
_callInMainThreadSvc_t(service_t *host, param_t const &param)
entry(host_t *host, arg_t const &arg, killswitch_t ks)
Base class for all service classes. Provides interfaces for reference counter and querying for differ...
Definition: service.h:333
void callback_run()
Gets called from main app thread. See main_thread_callback_manager description for more info...
Autopointer class to be used with all services. Manages reference counter calls behind-the-scenes.
Definition: service.h:55
Helper class to call methods of your class (host class) in main thread with convenience. Deals with the otherwise ugly scenario of your class becoming invalid while a method is queued. Have this as a member of your class, then use m_mthelper.add( this, somearg ) ; to defer a call to this->inMainThread(somearg). If your class becomes invalid before inMainThread is executed, the pending callback is discarded. You can optionally call shutdown() to invalidate all pending callbacks early (in a destructor of your class - without waiting for callInMainThreadHelper destructor to do the job. In order to let callInMainThreadHelper access your private methods, declare friend class callInMainThread.
static void callInMainThreadSvc(myservice_t *host, param_t const &param)
bool is_main_thread()
Returns true if calling thread is main app thread, false otherwise.
service_ptr_t< service_t > m_host
void callback_run()
Gets called from main app thread. See main_thread_callback_manager description for more info...
Helper template used to easily access core services. Usage: static_api_ptr_t api; api->doso...
Definition: service.h:533
static void main_thread_callback_spawn()
static void callThis(host_t *host)
Callback object class for main_thread_callback_manager service.