|
1
|
1 #pragma once
|
|
|
2 #include <SDK/main_thread_callback.h>
|
|
|
3
|
|
|
4 // ======================================================================================================
|
|
|
5 // Obsolete helpers - they still work, but it's easier to just use fb2k::inMainThread().
|
|
|
6 // ======================================================================================================
|
|
|
7
|
|
|
8
|
|
|
9 // Proxy class - friend this to allow callInMainThread to access your private methods
|
|
|
10 class callInMainThread {
|
|
|
11 public:
|
|
|
12 template<typename host_t, typename param_t>
|
|
|
13 static void callThis(host_t * host, param_t & param) {
|
|
|
14 host->inMainThread(param);
|
|
|
15 }
|
|
|
16 template<typename host_t>
|
|
|
17 static void callThis( host_t * host ) {
|
|
|
18 host->inMainThread();
|
|
|
19 }
|
|
|
20 };
|
|
|
21
|
|
|
22 // Internal class, do not use.
|
|
|
23 template<typename service_t, typename param_t>
|
|
|
24 class _callInMainThreadSvc_t : public main_thread_callback {
|
|
|
25 public:
|
|
|
26 _callInMainThreadSvc_t(service_t * host, param_t const & param) : m_host(host), m_param(param) {}
|
|
|
27 void callback_run() {
|
|
|
28 callInMainThread::callThis(m_host.get_ptr(), m_param);
|
|
|
29 }
|
|
|
30 private:
|
|
|
31 service_ptr_t<service_t> m_host;
|
|
|
32 param_t m_param;
|
|
|
33 };
|
|
|
34
|
|
|
35
|
|
|
36 // Main thread callback helper. You can use this to easily invoke inMainThread(someparam) on your class without writing any wrapper code.
|
|
|
37 // Requires myservice_t to be a fb2k service class with reference counting.
|
|
|
38 template<typename myservice_t, typename param_t>
|
|
|
39 static void callInMainThreadSvc(myservice_t * host, param_t const & param) {
|
|
|
40 typedef _callInMainThreadSvc_t<myservice_t, param_t> impl_t;
|
|
|
41 service_ptr_t<impl_t> obj = new service_impl_t<impl_t>(host, param);
|
|
|
42 main_thread_callback_manager::get()->add_callback( obj );
|
|
|
43 }
|
|
|
44
|
|
|
45 //! Helper class to call methods of your class (host class) in main thread with convenience. \n
|
|
|
46 //! Deals with the otherwise ugly scenario of your class becoming invalid while a method is queued. \n
|
|
|
47 //! Have this as a member of your class, then use m_mthelper.add( this, somearg ) ; to defer a call to this->inMainThread(somearg). \n
|
|
|
48 //! If your class becomes invalid before inMainThread is executed, the pending callback is discarded. \n
|
|
|
49 //! 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. \n
|
|
|
50 //! In order to let callInMainThreadHelper access your private methods, declare friend class callInMainThread. \n
|
|
|
51 //! Note that callInMainThreadHelper is expected to be created and destroyed in main thread.
|
|
|
52 class callInMainThreadHelper {
|
|
|
53 public:
|
|
|
54
|
|
|
55 typedef std::function< void () > func_t;
|
|
|
56
|
|
|
57 typedef pfc::rcptr_t< bool > killswitch_t;
|
|
|
58
|
|
|
59 class entryFunc : public main_thread_callback {
|
|
|
60 public:
|
|
|
61 entryFunc( func_t const & func, killswitch_t ks ) : m_ks(ks), m_func(func) {}
|
|
|
62
|
|
|
63 void callback_run() {
|
|
|
64 if (!*m_ks) m_func();
|
|
|
65 }
|
|
|
66
|
|
|
67 private:
|
|
|
68 killswitch_t m_ks;
|
|
|
69 func_t m_func;
|
|
|
70 };
|
|
|
71
|
|
|
72 template<typename host_t, typename arg_t>
|
|
|
73 class entry : public main_thread_callback {
|
|
|
74 public:
|
|
|
75 entry( host_t * host, arg_t const & arg, killswitch_t ks ) : m_ks(ks), m_host(host), m_arg(arg) {}
|
|
|
76 void callback_run() {
|
|
|
77 if (!*m_ks) callInMainThread::callThis( m_host, m_arg );
|
|
|
78 }
|
|
|
79 private:
|
|
|
80 killswitch_t m_ks;
|
|
|
81 host_t * m_host;
|
|
|
82 arg_t m_arg;
|
|
|
83 };
|
|
|
84 template<typename host_t>
|
|
|
85 class entryVoid : public main_thread_callback {
|
|
|
86 public:
|
|
|
87 entryVoid( host_t * host, killswitch_t ks ) : m_ks(ks), m_host(host) {}
|
|
|
88 void callback_run() {
|
|
|
89 if (!*m_ks) callInMainThread::callThis( m_host );
|
|
|
90 }
|
|
|
91 private:
|
|
|
92 killswitch_t m_ks;
|
|
|
93 host_t * m_host;
|
|
|
94 };
|
|
|
95
|
|
|
96 void add(func_t f) {
|
|
|
97 add_( new service_impl_t< entryFunc > ( f, m_ks ) );
|
|
|
98 }
|
|
|
99
|
|
|
100 template<typename host_t, typename arg_t>
|
|
|
101 void add( host_t * host, arg_t const & arg) {
|
|
|
102 add_( new service_impl_t< entry<host_t, arg_t> >( host, arg, m_ks ) );
|
|
|
103 }
|
|
|
104 template<typename host_t>
|
|
|
105 void add( host_t * host ) {
|
|
|
106 add_( new service_impl_t< entryVoid<host_t> >( host, m_ks ) );
|
|
|
107 }
|
|
|
108 void add_( main_thread_callback::ptr cb ) {
|
|
|
109 main_thread_callback_add( cb );
|
|
|
110 }
|
|
|
111
|
|
|
112 callInMainThreadHelper() {
|
|
|
113 m_ks.new_t();
|
|
|
114 * m_ks = false;
|
|
|
115 }
|
|
|
116 void shutdown() {
|
|
|
117 PFC_ASSERT( core_api::is_main_thread() );
|
|
|
118 * m_ks = true;
|
|
|
119 }
|
|
|
120 ~callInMainThreadHelper() {
|
|
|
121 shutdown();
|
|
|
122 }
|
|
|
123
|
|
|
124 private:
|
|
|
125 killswitch_t m_ks;
|
|
|
126
|
|
|
127 };
|