PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/OpenSim/Framework/RegistryCore.cs

http://github.com/aurora-sim/Aurora-Sim
C# | 162 lines | 99 code | 18 blank | 45 comment | 9 complexity | 5ece8896dd7ddf4c7aba7ab5fd05dd87 MD5 | raw file
Possible License(s): MIT, LGPL-3.0
  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.org/, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the Aurora-Sim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Text;
  30. namespace OpenSim.Framework
  31. {
  32. public class RegistryCore : IRegistryCore
  33. {
  34. /// <value>
  35. /// The module interfaces available from this scene.
  36. /// </value>
  37. protected Dictionary<Type, List<object>> ModuleInterfaces = new Dictionary<Type, List<object>>();
  38. /// <summary>
  39. /// Register an interface to a region module. This allows module methods to be called directly as
  40. /// well as via events. If there is already a module registered for this interface, it is not replaced
  41. /// (is this the best behaviour?)
  42. /// </summary>
  43. /// <param name="mod"></param>
  44. public void RegisterModuleInterface<T>(T mod)
  45. {
  46. // m_log.DebugFormat("[SCENE BASE]: Registering interface {0}", typeof(M));
  47. List<Object> l = null;
  48. if (!ModuleInterfaces.TryGetValue(typeof(T), out l))
  49. {
  50. l = new List<Object>();
  51. ModuleInterfaces.Add(typeof(T), l);
  52. }
  53. if (l.Count > 0)
  54. l.Clear();
  55. l.Add(mod);
  56. }
  57. public void AddModuleInterfaces(Dictionary<Type, List<object>> dictionary)
  58. {
  59. foreach (KeyValuePair<Type, List<object>> kvp in dictionary)
  60. {
  61. if (!ModuleInterfaces.ContainsKey(kvp.Key))
  62. ModuleInterfaces.Add(kvp.Key, kvp.Value);
  63. else
  64. ModuleInterfaces[kvp.Key].AddRange(kvp.Value);
  65. }
  66. }
  67. public void UnregisterModuleInterface<T>(T mod)
  68. {
  69. List<Object> l;
  70. if (ModuleInterfaces.TryGetValue(typeof(T), out l))
  71. {
  72. l.Remove(mod);
  73. }
  74. }
  75. public void StackModuleInterface<T>(T mod)
  76. {
  77. List<Object> l;
  78. if (ModuleInterfaces.ContainsKey(typeof(T)))
  79. l = ModuleInterfaces[typeof(T)];
  80. else
  81. l = new List<Object>();
  82. if (l.Contains(mod))
  83. return;
  84. l.Add(mod);
  85. ModuleInterfaces[typeof(T)] = l;
  86. }
  87. /// <summary>
  88. /// For the given interface, retrieve the region module which implements it.
  89. /// </summary>
  90. /// <returns>null if there is no registered module implementing that interface</returns>
  91. public T RequestModuleInterface<T>()
  92. {
  93. if (ModuleInterfaces.ContainsKey(typeof(T)) &&
  94. (ModuleInterfaces[typeof(T)].Count > 0))
  95. return (T)ModuleInterfaces[typeof(T)][0];
  96. else
  97. return default(T);
  98. }
  99. public bool TryRequestModuleInterface<T>(out T iface)
  100. {
  101. iface = default(T);
  102. if (ModuleInterfaces.ContainsKey(typeof(T)) &&
  103. (ModuleInterfaces[typeof(T)].Count > 0))
  104. {
  105. iface = (T)ModuleInterfaces[typeof(T)][0];
  106. return true;
  107. }
  108. else
  109. return false;
  110. }
  111. /// <summary>
  112. /// For the given interface, retrieve an array of region modules that implement it.
  113. /// </summary>
  114. /// <returns>an empty array if there are no registered modules implementing that interface</returns>
  115. public T[] RequestModuleInterfaces<T>()
  116. {
  117. if (ModuleInterfaces.ContainsKey(typeof(T)))
  118. {
  119. List<T> ret = new List<T>();
  120. foreach (Object o in ModuleInterfaces[typeof(T)])
  121. ret.Add((T)o);
  122. return ret.ToArray();
  123. }
  124. else
  125. {
  126. return new T[] { default(T) };
  127. }
  128. }
  129. public Dictionary<Type, List<object>> GetInterfaces()
  130. {
  131. //Flatten the array
  132. Dictionary<Type, List<object>> interfaces = new Dictionary<Type, List<object>>();
  133. foreach (KeyValuePair<Type, List<object>> kvp in ModuleInterfaces)
  134. {
  135. interfaces.Add(kvp.Key, kvp.Value);
  136. }
  137. return interfaces;
  138. }
  139. public void RemoveAllInterfaces ()
  140. {
  141. ModuleInterfaces.Clear ();
  142. }
  143. }
  144. }