OVR_Callbacks.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /************************************************************************************
  2. PublicHeader: Kernel
  3. Filename : OVR_Callbacks.h
  4. Content : Callback library
  5. Created : June 20, 2014
  6. Author : Chris Taylor
  7. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  8. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  9. you may not use the Oculus VR Rift SDK except in compliance with the License,
  10. which is provided at the time of installation or download, or which
  11. otherwise accompanies this software in either electronic or hard copy form.
  12. You may obtain a copy of the License at
  13. http://www.oculusvr.com/licenses/LICENSE-3.2
  14. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  15. distributed under the License is distributed on an "AS IS" BASIS,
  16. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. See the License for the specific language governing permissions and
  18. limitations under the License.
  19. ************************************************************************************/
  20. #ifndef OVR_Callbacks_h
  21. #define OVR_Callbacks_h
  22. #include "OVR_CallbacksInternal.h"
  23. #include "OVR_String.h" // For CallbackHash
  24. #include "OVR_Hash.h" // For CallbackHash
  25. namespace OVR {
  26. //-----------------------------------------------------------------------------
  27. // CallbackEmitter
  28. //
  29. // Emitter of callbacks.
  30. // Thread-safety: All public members may be safely called concurrently.
  31. template<class DelegateT>
  32. class CallbackEmitter : public NewOverrideBase
  33. {
  34. public:
  35. CallbackEmitter();
  36. ~CallbackEmitter();
  37. // Add a listener.
  38. bool AddListener(CallbackListener<DelegateT>* listener);
  39. // Get the current number of listeners. Note that this can change as other threads
  40. // add listeners to the emitter.
  41. int GetListenerCount() const;
  42. bool HasListeners() const
  43. {
  44. return Emitter->HasListeners();
  45. }
  46. void Call()
  47. {
  48. Emitter->Call();
  49. }
  50. template<class Param1>
  51. void Call(Param1* p1)
  52. {
  53. Emitter->Call(p1);
  54. }
  55. template<class Param1>
  56. void Call(Param1& p1)
  57. {
  58. Emitter->Call(p1);
  59. }
  60. template<class Param1, class Param2>
  61. void Call(Param1* p1, Param2* p2)
  62. {
  63. Emitter->Call(p1, p2);
  64. }
  65. template<class Param1, class Param2>
  66. void Call(Param1& p1, Param2& p2)
  67. {
  68. Emitter->Call(p1, p2);
  69. }
  70. template<class Param1, class Param2, class Param3>
  71. void Call(Param1* p1, Param2* p2, Param3* p3)
  72. {
  73. Emitter->Call(p1, p2, p3);
  74. }
  75. template<class Param1, class Param2, class Param3>
  76. void Call(Param1& p1, Param2& p2, Param3& p3)
  77. {
  78. Emitter->Call(p1, p2, p3);
  79. }
  80. // Remove all listeners and prevent further listeners from being added.
  81. void Shutdown();
  82. protected:
  83. Ptr< FloatingCallbackEmitter<DelegateT> > Emitter;
  84. };
  85. //-----------------------------------------------------------------------------
  86. // CallbackListener
  87. //
  88. // Listener for callbacks.
  89. // Thread-safety: Operations on a listener are not thread-safe.
  90. // The listener may only listen to *one emitter* at a time.
  91. template<class DelegateT>
  92. class CallbackListener : public NewOverrideBase
  93. {
  94. friend class CallbackEmitter<DelegateT>;
  95. public:
  96. CallbackListener();
  97. ~CallbackListener();
  98. // Stop listening to callbacks.
  99. // And set a new handler for callbacks.
  100. void SetHandler(DelegateT handler);
  101. // Is listening to an emitter at this instant?
  102. // If the Emitter has shutdown, then this may inaccurately return true.
  103. bool IsListening() const;
  104. // Stops listening to callbacks.
  105. void Cancel();
  106. protected:
  107. /// Internal data:
  108. // Reference to the associated listener.
  109. Ptr< FloatingCallbackListener<DelegateT> > FloatingListener;
  110. // Reference to the associated emitter.
  111. Ptr< FloatingCallbackEmitter<DelegateT> > FloatingEmitter;
  112. DelegateT Handler;
  113. };
  114. //-----------------------------------------------------------------------------
  115. // Template Implementation: CallbackEmitter
  116. template<class DelegateT>
  117. CallbackEmitter<DelegateT>::CallbackEmitter()
  118. {
  119. Emitter = *new FloatingCallbackEmitter<DelegateT>;
  120. }
  121. template<class DelegateT>
  122. CallbackEmitter<DelegateT>::~CallbackEmitter()
  123. {
  124. Emitter->Shutdown();
  125. // Emitter goes out of scope here.
  126. }
  127. template<class DelegateT>
  128. bool CallbackEmitter<DelegateT>::AddListener(CallbackListener<DelegateT>* listener)
  129. {
  130. // The listener object can only be attached to one emitter at a time.
  131. // The caller should explicitly Cancel() a listener before listening
  132. // to a new emitter, even if it is the same emitter.
  133. OVR_ASSERT(!listener->FloatingEmitter && !listener->FloatingListener);
  134. if (listener->FloatingEmitter || listener->FloatingListener)
  135. {
  136. // Cancel any previous listening
  137. listener->Cancel();
  138. }
  139. // Set the floating listener and emitter
  140. listener->FloatingListener = *new FloatingCallbackListener<DelegateT>(listener->Handler);
  141. listener->FloatingEmitter = Emitter.GetPtr();
  142. // The remaining input checks are performed inside.
  143. return Emitter->AddListener(listener->FloatingListener);
  144. }
  145. template<class DelegateT>
  146. int CallbackEmitter<DelegateT>::GetListenerCount() const
  147. {
  148. return Emitter->Listeners.GetSizeI();
  149. }
  150. template<class DelegateT>
  151. void CallbackEmitter<DelegateT>::Shutdown()
  152. {
  153. Emitter->Shutdown();
  154. }
  155. //-----------------------------------------------------------------------------
  156. // Template Implementation: CallbackListener
  157. template<class DelegateT>
  158. CallbackListener<DelegateT>::CallbackListener()
  159. {
  160. // Listener is null until a handler is set.
  161. }
  162. template<class DelegateT>
  163. CallbackListener<DelegateT>::~CallbackListener()
  164. {
  165. Cancel();
  166. }
  167. template<class DelegateT>
  168. void CallbackListener<DelegateT>::Cancel()
  169. {
  170. if (FloatingListener)
  171. {
  172. FloatingListener->EnterCancelState();
  173. }
  174. if (FloatingEmitter)
  175. {
  176. if (FloatingListener)
  177. {
  178. FloatingEmitter->OnListenerCancel(FloatingListener);
  179. }
  180. }
  181. // FloatingEmitter goes out of scope here.
  182. FloatingEmitter = nullptr;
  183. // FloatingListener goes out of scope here.
  184. FloatingListener = nullptr;
  185. }
  186. template<class DelegateT>
  187. void CallbackListener<DelegateT>::SetHandler(DelegateT handler)
  188. {
  189. Cancel();
  190. Handler = handler;
  191. }
  192. template<class DelegateT>
  193. bool CallbackListener<DelegateT>::IsListening() const
  194. {
  195. if (!FloatingListener.GetPtr())
  196. {
  197. return false;
  198. }
  199. return FloatingListener->IsValid();
  200. }
  201. //-----------------------------------------------------------------------------
  202. // CallbackHash
  203. //
  204. // A hash containing CallbackEmitters
  205. template<class DelegateT>
  206. class CallbackHash : public NewOverrideBase
  207. {
  208. typedef Hash<String, CallbackEmitter<DelegateT>*, String::HashFunctor> HashTable;
  209. public:
  210. ~CallbackHash()
  211. {
  212. Clear();
  213. }
  214. void Clear()
  215. {
  216. for (auto ii = Table.Begin(); ii != Table.End(); ++ii)
  217. {
  218. delete ii->Second;
  219. }
  220. Table.Clear();
  221. }
  222. CallbackEmitter<DelegateT>* GetKey(String key)
  223. {
  224. CallbackEmitter<DelegateT>** emitter = Table.Get(key);
  225. if (emitter)
  226. {
  227. return *emitter;
  228. }
  229. return nullptr;
  230. }
  231. void AddListener(String key, CallbackListener<DelegateT>* listener)
  232. {
  233. CallbackEmitter<DelegateT>** pEmitter = Table.Get(key);
  234. CallbackEmitter<DelegateT>* emitter = nullptr;
  235. if (!pEmitter)
  236. {
  237. emitter = new CallbackEmitter<DelegateT>;
  238. Table.Add(key, emitter);
  239. }
  240. else
  241. {
  242. emitter = *pEmitter;
  243. }
  244. emitter->AddListener(listener);
  245. }
  246. void RemoveKey(String key)
  247. {
  248. CallbackEmitter<DelegateT>** emitter = Table.Get(key);
  249. if (emitter)
  250. {
  251. delete *emitter;
  252. Table.Remove(key);
  253. }
  254. }
  255. protected:
  256. HashTable Table; // Hash table
  257. };
  258. } // namespace OVR
  259. #endif // OVR_Callbacks_h