Context.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "DebugNew.h"
  26. static String noType;
  27. void RemoveNamedAttribute(Map<ShortStringHash, Vector<AttributeInfo> >& attributes, ShortStringHash objectType, const String& name)
  28. {
  29. Map<ShortStringHash, Vector<AttributeInfo> >::Iterator i = attributes.Find(objectType);
  30. if (i == attributes.End())
  31. return;
  32. Vector<AttributeInfo>& infos = i->second_;
  33. for (Vector<AttributeInfo>::Iterator j = infos.Begin(); j != infos.End(); ++j)
  34. {
  35. if (j->name_ == name)
  36. {
  37. infos.Erase(j);
  38. break;
  39. }
  40. }
  41. // If the vector became empty, erase the object type from the map
  42. if (infos.Empty())
  43. attributes.Erase(i);
  44. }
  45. Context::Context() :
  46. eventHandler_(0)
  47. {
  48. }
  49. Context::~Context()
  50. {
  51. // Release the subsystems before the event receiver maps are destroyed
  52. for (Map<ShortStringHash, SharedPtr<Object> >::Iterator i = subsystems_.Begin(); i != subsystems_.End(); ++i)
  53. i->second_.Reset();
  54. subsystems_.Clear();
  55. factories_.Clear();
  56. }
  57. SharedPtr<Object> Context::CreateObject(ShortStringHash objectType)
  58. {
  59. Map<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(objectType);
  60. if (i != factories_.End())
  61. return i->second_->CreateObject();
  62. else
  63. return SharedPtr<Object>();
  64. }
  65. void Context::RegisterFactory(ObjectFactory* factory)
  66. {
  67. if (!factory)
  68. return;
  69. factories_[factory->GetType()] = factory;
  70. }
  71. void Context::RegisterSubsystem(Object* object)
  72. {
  73. if (!object)
  74. return;
  75. subsystems_[object->GetType()] = object;
  76. }
  77. void Context::RegisterAttribute(ShortStringHash objectType, const AttributeInfo& attr)
  78. {
  79. // None or Pointer types can not be supported
  80. if (attr.type_ == VAR_NONE || attr.type_ == VAR_PTR)
  81. return;
  82. attributes_[objectType].Push(attr);
  83. if (attr.mode_ & AM_NET)
  84. networkAttributes_[objectType].Push(attr);
  85. }
  86. void Context::RemoveAttribute(ShortStringHash objectType, const String& name)
  87. {
  88. RemoveNamedAttribute(attributes_, objectType, name);
  89. RemoveNamedAttribute(networkAttributes_, objectType, name);
  90. }
  91. void Context::CopyBaseAttributes(ShortStringHash baseType, ShortStringHash derivedType)
  92. {
  93. const Vector<AttributeInfo>* baseAttributes = GetAttributes(baseType);
  94. if (baseAttributes)
  95. {
  96. for (unsigned i = 0; i < baseAttributes->Size(); ++i)
  97. {
  98. const AttributeInfo& attr = baseAttributes->At(i);
  99. attributes_[derivedType].Push(attr);
  100. if (attr.mode_ & AM_NET)
  101. networkAttributes_[derivedType].Push(attr);
  102. }
  103. }
  104. }
  105. Object* Context::GetSubsystem(ShortStringHash type) const
  106. {
  107. Map<ShortStringHash, SharedPtr<Object> >::ConstIterator i = subsystems_.Find(type);
  108. if (i != subsystems_.End())
  109. return i->second_;
  110. else
  111. return 0;
  112. }
  113. Object* Context::GetEventSender() const
  114. {
  115. if (!eventSenders_.Empty())
  116. return eventSenders_.Back();
  117. else
  118. return 0;
  119. }
  120. const String& Context::GetTypeName(ShortStringHash type) const
  121. {
  122. // Search factories to find the hash-to-name mapping
  123. Map<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(type);
  124. return i != factories_.End() ? i->second_->GetTypeName() : noType;
  125. }
  126. void Context::AddEventReceiver(Object* receiver, StringHash eventType)
  127. {
  128. PODVector<Object*>& receivers = eventReceivers_[eventType];
  129. // Check if already registered
  130. if (receivers.Contains(receiver))
  131. return;
  132. receivers.Push(receiver);
  133. }
  134. void Context::AddEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  135. {
  136. PODVector<Object*>& receivers = specificEventReceivers_[MakePair(sender, eventType)];
  137. if (receivers.Contains(receiver))
  138. return;
  139. receivers.Push(receiver);
  140. }
  141. void Context::RemoveEventSender(Object* sender)
  142. {
  143. for (Map<Pair<Object*, StringHash>, PODVector<Object*> >::Iterator i = specificEventReceivers_.Begin();
  144. i != specificEventReceivers_.End();)
  145. {
  146. Map<Pair<Object*, StringHash>, PODVector<Object*> >::Iterator current = i++;
  147. if (current->first_.first_ == sender)
  148. {
  149. PODVector<Object*>& receivers = current->second_;
  150. for (PODVector<Object*>::Iterator j = receivers.Begin(); j != receivers.End(); ++j)
  151. {
  152. if (*j)
  153. (*j)->RemoveEventSender(sender);
  154. }
  155. specificEventReceivers_.Erase(current);
  156. }
  157. }
  158. }
  159. void Context::RemoveEventReceiver(Object* receiver, StringHash eventType)
  160. {
  161. PODVector<Object*>* group = GetEventReceivers(eventType);
  162. if (!group)
  163. return;
  164. PODVector<Object*>::Iterator i = group->Find(receiver);
  165. if (i != group->End())
  166. {
  167. // If no event handling going on, can erase the receiver. Otherwise reset the pointer and clean up later
  168. if (eventSenders_.Empty())
  169. group->Erase(i);
  170. else
  171. {
  172. *i = 0;
  173. dirtyReceivers_.Insert(eventType);
  174. }
  175. }
  176. }
  177. void Context::RemoveEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  178. {
  179. PODVector<Object*>* group = GetEventReceivers(sender, eventType);
  180. if (!group)
  181. return;
  182. PODVector<Object*>::Iterator i = group->Find(receiver);
  183. if (i != group->End())
  184. {
  185. if (eventSenders_.Empty())
  186. group->Erase(i);
  187. else
  188. {
  189. *i = 0;
  190. dirtySpecificReceivers_.Insert(MakePair(sender, eventType));
  191. }
  192. return;
  193. }
  194. }
  195. void Context::EndSendEvent()
  196. {
  197. eventSenders_.Pop();
  198. // Clean up dirtied event receiver groups when event handling finishes
  199. if (eventSenders_.Empty())
  200. {
  201. if (!dirtySpecificReceivers_.Empty())
  202. {
  203. for (HashSet<Pair<Object*, StringHash> >::Iterator i = dirtySpecificReceivers_.Begin();
  204. i != dirtySpecificReceivers_.End(); ++i)
  205. {
  206. PODVector<Object*>& receivers = specificEventReceivers_[*i];
  207. for (PODVector<Object*>::Iterator j = receivers.Begin(); j != receivers.End();)
  208. {
  209. if (*j == 0)
  210. j = receivers.Erase(j);
  211. else
  212. ++j;
  213. }
  214. }
  215. dirtySpecificReceivers_.Clear();
  216. }
  217. if (!dirtyReceivers_.Empty())
  218. {
  219. for (HashSet<StringHash>::Iterator i = dirtyReceivers_.Begin(); i != dirtyReceivers_.End(); ++i)
  220. {
  221. PODVector<Object*>& receivers = eventReceivers_[*i];
  222. for (PODVector<Object*>::Iterator j = receivers.Begin(); j != receivers.End();)
  223. {
  224. if (*j == 0)
  225. j = receivers.Erase(j);
  226. else
  227. ++j;
  228. }
  229. }
  230. dirtyReceivers_.Clear();
  231. }
  232. }
  233. }