Object.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "LinkedList.h"
  25. #include "Ptr.h"
  26. #include "Variant.h"
  27. namespace Urho3D
  28. {
  29. class Context;
  30. class EventHandler;
  31. /// Base class for objects with type identification, subsystem access and event sending/receiving capability.
  32. class Object : public RefCounted
  33. {
  34. friend class Context;
  35. public:
  36. /// Construct.
  37. Object(Context* context);
  38. /// Destruct. Clean up self from event sender & receiver structures.
  39. virtual ~Object();
  40. /// Return type hash.
  41. virtual ShortStringHash GetType() const = 0;
  42. /// Return type name.
  43. virtual const String& GetTypeName() const = 0;
  44. /// Handle event.
  45. virtual void OnEvent(Object* sender, StringHash eventType, VariantMap& eventData);
  46. /// Subscribe to an event that can be sent by any sender.
  47. void SubscribeToEvent(StringHash eventType, EventHandler* handler);
  48. /// Subscribe to a specific sender's event.
  49. void SubscribeToEvent(Object* sender, StringHash eventType, EventHandler* handler);
  50. /// Unsubscribe from an event.
  51. void UnsubscribeFromEvent(StringHash eventType);
  52. /// Unsubscribe from a specific sender's event.
  53. void UnsubscribeFromEvent(Object* sender, StringHash eventType);
  54. /// Unsubscribe from a specific sender's events.
  55. void UnsubscribeFromEvents(Object* sender);
  56. /// Unsubscribe from all events.
  57. void UnsubscribeFromAllEvents();
  58. /// Unsubscribe from all events with userdata defined in the handler.
  59. void UnsubscribeFromAllEventsWithUserData();
  60. /// Send event to all subscribers.
  61. void SendEvent(StringHash eventType);
  62. /// Send event with parameters to all subscribers.
  63. void SendEvent(StringHash eventType, VariantMap& eventData);
  64. /// Return execution context.
  65. Context* GetContext() const { return context_; }
  66. /// Return subsystem by type.
  67. Object* GetSubsystem(ShortStringHash type) const;
  68. /// Return active event sender. Null outside event handling.
  69. Object* GetEventSender() const;
  70. /// Return active event handler. Null outside event handling.
  71. EventHandler* GetEventHandler() const;
  72. /// Return whether has subscribed to an event without specific sender.
  73. bool HasSubscribedToEvent(StringHash eventType) const;
  74. /// Return whether has subscribed to a specific sender's event.
  75. bool HasSubscribedToEvent(Object* sender, StringHash eventType) const;
  76. /// Template version of returning a subsystem.
  77. template <class T> T* GetSubsystem() const;
  78. protected:
  79. /// Execution context.
  80. Context* context_;
  81. private:
  82. /// Find the first event handler with no specific sender.
  83. EventHandler* FindEventHandler(StringHash eventType, EventHandler** previous = 0) const;
  84. /// Find the first event handler with specific sender.
  85. EventHandler* FindSpecificEventHandler(Object* sender, EventHandler** previous = 0) const;
  86. /// Find the first event handler with specific sender and event type.
  87. EventHandler* FindSpecificEventHandler(Object* sender, StringHash eventType, EventHandler** previous = 0) const;
  88. /// Remove event handlers related to a specific sender.
  89. void RemoveEventSender(Object* sender);
  90. /// Event handlers. Sender is null for non-specific handlers.
  91. LinkedList<EventHandler> eventHandlers_;
  92. };
  93. template <class T> T* Object::GetSubsystem() const { return static_cast<T*>(GetSubsystem(T::GetTypeStatic())); }
  94. /// Base class for object factories.
  95. class ObjectFactory : public RefCounted
  96. {
  97. public:
  98. /// Construct.
  99. ObjectFactory(Context* context) :
  100. context_(context)
  101. {
  102. assert(context_);
  103. }
  104. /// Create an object. Implemented in templated subclasses.
  105. virtual SharedPtr<Object> CreateObject() = 0;
  106. /// Return execution context.
  107. Context* GetContext() const { return context_; }
  108. /// Return type hash of objects created by this factory.
  109. ShortStringHash GetType() const { return type_; }
  110. /// Return type name of objects created by this factory.
  111. const String& GetTypeName() const { return typeName_; }
  112. protected:
  113. /// Execution context.
  114. Context* context_;
  115. /// Object type.
  116. ShortStringHash type_;
  117. /// Object type name.
  118. String typeName_;
  119. };
  120. /// Template implementation of the object factory.
  121. template <class T> class ObjectFactoryImpl : public ObjectFactory
  122. {
  123. public:
  124. /// Construct.
  125. ObjectFactoryImpl(Context* context) :
  126. ObjectFactory(context)
  127. {
  128. type_ = T::GetTypeStatic();
  129. typeName_ = T::GetTypeNameStatic();
  130. }
  131. /// Create an object of the specific type.
  132. virtual SharedPtr<Object>(CreateObject()) { return SharedPtr<Object>(new T(context_)); }
  133. };
  134. /// Internal helper class for invoking event handler functions.
  135. class EventHandler : public LinkedListNode
  136. {
  137. public:
  138. /// Construct with specified receiver.
  139. EventHandler(Object* receiver) :
  140. receiver_(receiver),
  141. sender_(0),
  142. userData_(0)
  143. {
  144. assert(receiver_);
  145. }
  146. /// Construct with specified receiver and userdata.
  147. EventHandler(Object* receiver, void* userData) :
  148. receiver_(receiver),
  149. sender_(0),
  150. userData_(userData)
  151. {
  152. assert(receiver_);
  153. }
  154. /// Destruct.
  155. virtual ~EventHandler() {}
  156. /// Set sender and event type.
  157. void SetSenderAndEventType(Object* sender, StringHash eventType)
  158. {
  159. sender_ = sender;
  160. eventType_ = eventType;
  161. }
  162. /// Invoke event handler function.
  163. virtual void Invoke(VariantMap& eventData) = 0;
  164. /// Return event receiver.
  165. Object* GetReceiver() const { return receiver_; }
  166. /// Return event sender. Null if the handler is non-specific.
  167. Object* GetSender() const { return sender_; }
  168. /// Return event type.
  169. const StringHash& GetEventType() const { return eventType_; }
  170. /// Return userdata.
  171. void* GetUserData() const { return userData_; }
  172. protected:
  173. /// Event receiver.
  174. Object* receiver_;
  175. /// Event sender.
  176. Object* sender_;
  177. /// Event type.
  178. StringHash eventType_;
  179. /// Userdata.
  180. void* userData_;
  181. };
  182. /// Template implementation of the event handler invoke helper (stores a function pointer of specific class.)
  183. template <class T> class EventHandlerImpl : public EventHandler
  184. {
  185. public:
  186. typedef void (T::*HandlerFunctionPtr)(StringHash, VariantMap&);
  187. /// Construct with receiver and function pointers.
  188. EventHandlerImpl(T* receiver, HandlerFunctionPtr function) :
  189. EventHandler(receiver),
  190. function_(function)
  191. {
  192. assert(function_);
  193. }
  194. /// Construct with receiver and function pointers and userdata.
  195. EventHandlerImpl(T* receiver, HandlerFunctionPtr function, void* userData) :
  196. EventHandler(receiver, userData),
  197. function_(function)
  198. {
  199. assert(function_);
  200. }
  201. /// Invoke event handler function.
  202. virtual void Invoke(VariantMap& eventData)
  203. {
  204. T* receiver = static_cast<T*>(receiver_);
  205. (receiver->*function_)(eventType_, eventData);
  206. }
  207. private:
  208. /// Class-specific pointer to handler function.
  209. HandlerFunctionPtr function_;
  210. };
  211. #define OBJECT(typeName) \
  212. private: \
  213. static const ShortStringHash typeStatic; \
  214. static const String typeNameStatic; \
  215. public: \
  216. virtual ShortStringHash GetType() const { return GetTypeStatic(); } \
  217. virtual const String& GetTypeName() const { return GetTypeNameStatic(); } \
  218. static ShortStringHash GetTypeStatic() { return typeStatic; } \
  219. static const String& GetTypeNameStatic() { return typeNameStatic; } \
  220. #define OBJECTTYPESTATIC(typeName) \
  221. const ShortStringHash typeName::typeStatic(#typeName); \
  222. const String typeName::typeNameStatic(#typeName); \
  223. #define EVENT(eventID, eventName) static const StringHash eventID(#eventName); namespace eventName
  224. #define PARAM(paramID, paramName) static const ShortStringHash paramID(#paramName)
  225. #define HANDLER(className, function) (new EventHandlerImpl<className>(this, &className::function))
  226. #define HANDLER_USERDATA(className, function, userData) (new EventHandlerImpl<className>(this, &className::function, userData))
  227. }