2
0

Context.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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::RegisterAttribute(ShortStringHash objectType, const AttributeInfo& attr)
  75. {
  76. // None or Pointer types can not be supported
  77. if (attr.type_ == VAR_NONE || attr.type_ == VAR_PTR)
  78. return;
  79. attributes_[objectType].Push(attr);
  80. if (attr.mode_ & AM_NET)
  81. networkAttributes_[objectType].Push(attr);
  82. }
  83. void Context::RemoveAttribute(ShortStringHash objectType, const String& name)
  84. {
  85. RemoveNamedAttribute(attributes_, objectType, name);
  86. RemoveNamedAttribute(networkAttributes_, objectType, name);
  87. }
  88. void Context::CopyBaseAttributes(ShortStringHash baseType, ShortStringHash derivedType)
  89. {
  90. const Vector<AttributeInfo>* baseAttributes = GetAttributes(baseType);
  91. if (baseAttributes)
  92. {
  93. for (unsigned i = 0; i < baseAttributes->Size(); ++i)
  94. {
  95. const AttributeInfo& attr = baseAttributes->At(i);
  96. attributes_[derivedType].Push(attr);
  97. if (attr.mode_ & AM_NET)
  98. networkAttributes_[derivedType].Push(attr);
  99. }
  100. }
  101. }
  102. Object* Context::GetSubsystem(ShortStringHash type) const
  103. {
  104. HashMap<ShortStringHash, SharedPtr<Object> >::ConstIterator i = subsystems_.Find(type);
  105. if (i != subsystems_.End())
  106. return i->second_;
  107. else
  108. return 0;
  109. }
  110. Object* Context::GetEventSender() const
  111. {
  112. if (!eventSenders_.Empty())
  113. return eventSenders_.Back();
  114. else
  115. return 0;
  116. }
  117. const String& Context::GetTypeName(ShortStringHash type) const
  118. {
  119. // Search factories to find the hash-to-name mapping
  120. HashMap<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories_.Find(type);
  121. return i != factories_.End() ? i->second_->GetTypeName() : noType;
  122. }
  123. void Context::AddEventReceiver(Object* receiver, StringHash eventType)
  124. {
  125. eventReceivers_[eventType].Insert(receiver);
  126. }
  127. void Context::AddEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  128. {
  129. specificEventReceivers_[sender][eventType].Insert(receiver);
  130. }
  131. void Context::RemoveEventSender(Object* sender)
  132. {
  133. specificEventReceivers_.Erase(sender);
  134. }
  135. void Context::RemoveEventReceiver(Object* receiver, StringHash eventType)
  136. {
  137. HashSet<Object*>* group = GetEventReceivers(eventType);
  138. if (group)
  139. group->Erase(receiver);
  140. }
  141. void Context::RemoveEventReceiver(Object* receiver, Object* sender, StringHash eventType)
  142. {
  143. HashSet<Object*>* group = GetEventReceivers(sender, eventType);
  144. if (group)
  145. group->Erase(receiver);
  146. }
  147. void Context::EndSendEvent()
  148. {
  149. eventSenders_.Pop();
  150. }