Context.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #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. attributes_[derivedType] = *baseAttributes;
  96. const Vector<AttributeInfo>* baseNetworkAttributes = GetNetworkAttributes(baseType);
  97. if (baseNetworkAttributes)
  98. networkAttributes_[derivedType] = *baseNetworkAttributes;
  99. }
  100. void Context::AddEventReceiver(Object* receiver, StringHash eventType)
  101. {
  102. PODVector<Object*>& receivers = eventReceivers_[eventType];
  103. // Check if already registered
  104. if (receivers.Contains(receiver))
  105. return;
  106. receivers.Push(receiver);
  107. }
  108. void Context::AddEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  109. {
  110. PODVector<Object*>& receivers = specificEventReceivers_[MakePair(sender, eventType)];
  111. if (receivers.Contains(receiver))
  112. return;
  113. receivers.Push(receiver);
  114. }
  115. void Context::RemoveEventSender(Object* sender)
  116. {
  117. for (Map<Pair<Object*, StringHash>, PODVector<Object*> >::Iterator i = specificEventReceivers_.Begin();
  118. i != specificEventReceivers_.End();)
  119. {
  120. Map<Pair<Object*, StringHash>, PODVector<Object*> >::Iterator current = i++;
  121. if (current->first_.first_ == sender)
  122. {
  123. PODVector<Object*>& receivers = current->second_;
  124. for (PODVector<Object*>::Iterator j = receivers.Begin(); j != receivers.End(); ++j)
  125. {
  126. if (*j)
  127. (*j)->RemoveEventSender(sender);
  128. }
  129. specificEventReceivers_.Erase(current);
  130. }
  131. }
  132. }
  133. void Context::RemoveEventReceiver(Object* receiver, StringHash eventType)
  134. {
  135. PODVector<Object*>* group = GetReceivers(eventType);
  136. if (!group)
  137. return;
  138. PODVector<Object*>::Iterator i = group->Find(receiver);
  139. if (i != group->End())
  140. {
  141. // If no event handling going on, can erase the receiver. Otherwise reset the pointer and clean up later
  142. if (eventSenders_.Empty())
  143. group->Erase(i);
  144. else
  145. {
  146. *i = 0;
  147. dirtyReceivers_.Insert(eventType);
  148. }
  149. }
  150. }
  151. void Context::RemoveEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  152. {
  153. PODVector<Object*>* group = GetReceivers(sender, eventType);
  154. if (!group)
  155. return;
  156. PODVector<Object*>::Iterator i = group->Find(receiver);
  157. if (i != group->End())
  158. {
  159. if (eventSenders_.Empty())
  160. group->Erase(i);
  161. else
  162. {
  163. *i = 0;
  164. dirtySpecificReceivers_.Insert(MakePair(sender, eventType));
  165. }
  166. return;
  167. }
  168. }
  169. Object* Context::GetSubsystem(ShortStringHash type) const
  170. {
  171. Map<ShortStringHash, SharedPtr<Object> >::ConstIterator i = subsystems_.Find(type);
  172. if (i != subsystems_.End())
  173. return i->second_;
  174. else
  175. return 0;
  176. }
  177. Object* Context::GetEventSender() const
  178. {
  179. if (!eventSenders_.Empty())
  180. return eventSenders_.Back();
  181. else
  182. return 0;
  183. }
  184. const String& Context::GetTypeName(ShortStringHash type) const
  185. {
  186. // Search factories to find the hash-to-name mapping
  187. Map<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(type);
  188. return i != factories_.End() ? i->second_->GetTypeName() : noType;
  189. }