Context.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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(HashMap<ShortStringHash, Vector<AttributeInfo> >& attributes, ShortStringHash objectType, const String& name)
  28. {
  29. HashMap<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. subsystems_.Clear();
  52. factories_.Clear();
  53. }
  54. SharedPtr<Object> Context::CreateObject(ShortStringHash objectType)
  55. {
  56. HashMap<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(objectType);
  57. if (i != factories_.End())
  58. return i->second_->CreateObject();
  59. else
  60. return SharedPtr<Object>();
  61. }
  62. void Context::RegisterFactory(ObjectFactory* factory)
  63. {
  64. if (!factory)
  65. return;
  66. factories_[factory->GetType()] = factory;
  67. }
  68. void Context::RegisterSubsystem(Object* object)
  69. {
  70. if (!object)
  71. return;
  72. subsystems_[object->GetType()] = object;
  73. }
  74. void Context::RemoveSubsystem(ShortStringHash objectType)
  75. {
  76. HashMap<ShortStringHash, SharedPtr<Object> >::Iterator i = subsystems_.Find(objectType);
  77. if (i != subsystems_.End())
  78. subsystems_.Erase(i);
  79. }
  80. void Context::RegisterAttribute(ShortStringHash objectType, const AttributeInfo& attr)
  81. {
  82. // None or Pointer types can not be supported
  83. if (attr.type_ == VAR_NONE || attr.type_ == VAR_PTR)
  84. return;
  85. attributes_[objectType].Push(attr);
  86. if (attr.mode_ & AM_NET)
  87. networkAttributes_[objectType].Push(attr);
  88. }
  89. void Context::RemoveAttribute(ShortStringHash objectType, const String& name)
  90. {
  91. RemoveNamedAttribute(attributes_, objectType, name);
  92. RemoveNamedAttribute(networkAttributes_, objectType, name);
  93. }
  94. void Context::CopyBaseAttributes(ShortStringHash baseType, ShortStringHash derivedType)
  95. {
  96. const Vector<AttributeInfo>* baseAttributes = GetAttributes(baseType);
  97. if (baseAttributes)
  98. {
  99. for (unsigned i = 0; i < baseAttributes->Size(); ++i)
  100. {
  101. const AttributeInfo& attr = baseAttributes->At(i);
  102. attributes_[derivedType].Push(attr);
  103. if (attr.mode_ & AM_NET)
  104. networkAttributes_[derivedType].Push(attr);
  105. }
  106. }
  107. }
  108. Object* Context::GetSubsystem(ShortStringHash type) const
  109. {
  110. HashMap<ShortStringHash, SharedPtr<Object> >::ConstIterator i = subsystems_.Find(type);
  111. if (i != subsystems_.End())
  112. return i->second_;
  113. else
  114. return 0;
  115. }
  116. Object* Context::GetEventSender() const
  117. {
  118. if (!eventSenders_.Empty())
  119. return eventSenders_.Back();
  120. else
  121. return 0;
  122. }
  123. const String& Context::GetTypeName(ShortStringHash type) const
  124. {
  125. // Search factories to find the hash-to-name mapping
  126. HashMap<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(type);
  127. return i != factories_.End() ? i->second_->GetTypeName() : noType;
  128. }
  129. void Context::AddEventReceiver(Object* receiver, StringHash eventType)
  130. {
  131. eventReceivers_[eventType].Insert(receiver);
  132. }
  133. void Context::AddEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  134. {
  135. specificEventReceivers_[sender][eventType].Insert(receiver);
  136. }
  137. void Context::RemoveEventSender(Object* sender)
  138. {
  139. HashMap<Object*, HashMap<StringHash, HashSet<Object*> > >::Iterator i = specificEventReceivers_.Find(sender);
  140. if (i != specificEventReceivers_.End())
  141. {
  142. for (HashMap<StringHash, HashSet<Object*> >::Iterator j = i->second_.Begin(); j != i->second_.End(); ++j)
  143. {
  144. for (HashSet<Object*>::Iterator k = j->second_.Begin(); k != j->second_.End(); ++k)
  145. (*k)->RemoveEventSender(sender);
  146. }
  147. specificEventReceivers_.Erase(i);
  148. }
  149. }
  150. void Context::RemoveEventReceiver(Object* receiver, StringHash eventType)
  151. {
  152. HashSet<Object*>* group = GetEventReceivers(eventType);
  153. if (group)
  154. group->Erase(receiver);
  155. }
  156. void Context::RemoveEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  157. {
  158. HashSet<Object*>* group = GetEventReceivers(sender, eventType);
  159. if (group)
  160. group->Erase(receiver);
  161. }
  162. void Context::EndSendEvent()
  163. {
  164. eventSenders_.Pop();
  165. }