Object.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  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 "Map.h"
  25. #include "Ptr.h"
  26. #include "Variant.h"
  27. class Context;
  28. class EventHandler;
  29. /// Base class for objects with type identification, subsystem access and event sending/receiving capability.
  30. class Object : public RefCounted
  31. {
  32. friend class Context;
  33. public:
  34. /// Construct.
  35. Object(Context* context);
  36. /// Destruct. Clean up self from event sender & receiver structures.
  37. virtual ~Object();
  38. /// Return type hash.
  39. virtual ShortStringHash GetType() const = 0;
  40. /// Return type name.
  41. virtual const String& GetTypeName() const = 0;
  42. /// Handle event.
  43. virtual void OnEvent(Object* sender, bool broadcast, StringHash eventType, VariantMap& eventData);
  44. /// Subscribe to an event that can be sent by any sender.
  45. void SubscribeToEvent(StringHash eventType, EventHandler* handler);
  46. /// Subscribe to a specific sender's event.
  47. void SubscribeToEvent(Object* sender, StringHash eventType, EventHandler* handler);
  48. /// Unsubscribe from an event.
  49. void UnsubscribeFromEvent(StringHash eventType);
  50. /// Unsubscribe from a specific sender's event.
  51. void UnsubscribeFromEvent(Object* sender, StringHash eventType);
  52. /// Unsubscribe from a specific sender's events.
  53. void UnsubscribeFromEvents(Object* sender);
  54. /// Unsubscribe from all events.
  55. void UnsubscribeFromAllEvents();
  56. /// Unsubscribe from all events with userdata defined in the handler.
  57. void UnsubscribeFromAllEventsWithUserData();
  58. /// Send event to all subscribers.
  59. void SendEvent(StringHash eventType);
  60. /// Send event with parameters to all subscribers.
  61. void SendEvent(StringHash eventType, VariantMap& eventData);
  62. /// Send event to a specific receiver.
  63. void SendEvent(Object* receiver, StringHash eventType);
  64. /// Send event with parameters to a specific receiver.
  65. void SendEvent(Object* receiver, StringHash eventType, VariantMap& eventData);
  66. /// Return execution context.
  67. Context* GetContext() const { return context_; }
  68. /// Return subsystem by type.
  69. Object* GetSubsystem(ShortStringHash type) const;
  70. /// Return whether has subscribed to an event without specific sender.
  71. bool HasSubscribedToEvent(StringHash eventType) const;
  72. /// Return whether has subscribed to a specific sender's event.
  73. bool HasSubscribedToEvent(Object* sender, StringHash eventType) const;
  74. /// Template version of returning a subsystem.
  75. template <class T> T* GetSubsystem() const;
  76. protected:
  77. /// Execution context.
  78. Context* context_;
  79. private:
  80. /// Remove event handlers related to a specific sender.
  81. void RemoveEventSender(Object* sender);
  82. /// Event handlers. Sender is null for non-specific handlers.
  83. Map<Pair<Object*, StringHash>, SharedPtr<EventHandler> > eventHandlers_;
  84. };
  85. template <class T> T* Object::GetSubsystem() const { return static_cast<T*>(GetSubsystem(T::GetTypeStatic())); }
  86. /// Base class for object factories.
  87. class ObjectFactory : public RefCounted
  88. {
  89. public:
  90. /// Construct.
  91. ObjectFactory(Context* context) :
  92. context_(context)
  93. {
  94. assert(context_);
  95. }
  96. /// Create an object. Implemented in templated subclasses.
  97. virtual SharedPtr<Object> CreateObject() = 0;
  98. /// Return execution context.
  99. Context* GetContext() const { return context_; }
  100. /// Return type hash of objects created by this factory.
  101. ShortStringHash GetType() const { return type_; }
  102. /// Return type name of objects created by this factory.
  103. const String& GetTypeName() const { return typeName_; }
  104. protected:
  105. /// Execution context.
  106. Context* context_;
  107. /// Object type.
  108. ShortStringHash type_;
  109. /// Object type name.
  110. String typeName_;
  111. };
  112. /// Template implementation of the object factory.
  113. template <class T> class ObjectFactoryImpl : public ObjectFactory
  114. {
  115. public:
  116. /// Construct.
  117. ObjectFactoryImpl(Context* context) :
  118. ObjectFactory(context)
  119. {
  120. type_ = T::GetTypeStatic();
  121. typeName_ = T::GetTypeNameStatic();
  122. }
  123. /// Create an object of the specific type.
  124. virtual SharedPtr<Object>(CreateObject()) { return SharedPtr<Object>(new T(context_)); }
  125. };
  126. /// Internal helper class for invoking event handler functions.
  127. class EventHandler : public RefCounted
  128. {
  129. public:
  130. /// Construct with specified receiver.
  131. EventHandler(Object* receiver) :
  132. receiver_(receiver),
  133. userData_(0)
  134. {
  135. assert(receiver_);
  136. }
  137. /// Construct with specified receiver and userdata.
  138. EventHandler(Object* receiver, void* userData) :
  139. receiver_(receiver),
  140. userData_(userData)
  141. {
  142. assert(receiver_);
  143. }
  144. /// Destruct.
  145. virtual ~EventHandler() {}
  146. /// Invoke event handler function.
  147. virtual void Invoke(StringHash eventType, VariantMap& eventData) = 0;
  148. /// Return event receiver.
  149. Object* GetReceiver() const { return receiver_; }
  150. /// Return userdata.
  151. void* GetUserData() const { return userData_; }
  152. protected:
  153. /// Event receiver.
  154. Object* receiver_;
  155. /// Userdata.
  156. void* userData_;
  157. };
  158. /// Template implementation of the event handler invoke helper (stores a function pointer of specific class.)
  159. template <class T> class EventHandlerImpl : public EventHandler
  160. {
  161. public:
  162. typedef void (T::*HandlerFunctionPtr)(StringHash, VariantMap&);
  163. /// Construct with receiver and function pointers.
  164. EventHandlerImpl(T* receiver, HandlerFunctionPtr function) :
  165. EventHandler(receiver),
  166. function_(function)
  167. {
  168. assert(function_);
  169. }
  170. /// Construct with receiver and function pointers and userdata.
  171. EventHandlerImpl(T* receiver, HandlerFunctionPtr function, void* userData) :
  172. EventHandler(receiver, userData),
  173. function_(function)
  174. {
  175. assert(function_);
  176. }
  177. /// Invoke event handler function.
  178. virtual void Invoke(StringHash eventType, VariantMap& eventData)
  179. {
  180. T* receiver = static_cast<T*>(receiver_);
  181. (receiver->*function_)(eventType, eventData);
  182. }
  183. private:
  184. /// Class-specific pointer to handler function.
  185. HandlerFunctionPtr function_;
  186. };
  187. #define OBJECT(typeName) \
  188. private: \
  189. static const ShortStringHash typeStatic; \
  190. static const String typeNameStatic; \
  191. public: \
  192. virtual ShortStringHash GetType() const { return GetTypeStatic(); } \
  193. virtual const String& GetTypeName() const { return GetTypeNameStatic(); } \
  194. static ShortStringHash GetTypeStatic() { return typeStatic; } \
  195. static const String& GetTypeNameStatic() { return typeNameStatic; } \
  196. #define OBJECTTYPESTATIC(typeName) \
  197. const ShortStringHash typeName::typeStatic(#typeName); \
  198. const String typeName::typeNameStatic(#typeName); \
  199. #define EVENT(eventID, eventName) static const StringHash eventID(#eventName); namespace eventName
  200. #define PARAM(paramID, paramName) static const ShortStringHash paramID(#paramName)
  201. #define HANDLER(className, function) (new EventHandlerImpl<className>(this, &className::function))
  202. #define HANDLER_USERDATA(className, function, userData) (new EventHandlerImpl<className>(this, &className::function, userData))