Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ServiceRegistry.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 //
4 // Copyright (c) 2010-2013 SharpDX - Alexandre Mutel
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 using System;
24 using System.Collections.Generic;
25 using System.Reflection;
26 using SiliconStudio.Core.Reflection;
27 // THIS NAMESPACE MUST BE USED FOR 4.5 CORE PROFILE
28 
29 namespace SiliconStudio.Core
30 {
31  /// <summary>
32  /// Base implementation for <see cref="IServiceRegistry"/>
33  /// </summary>
35  {
36  public static PropertyKey<ServiceRegistry> ServiceRegistryKey = new PropertyKey<ServiceRegistry>("ServiceRegistryKey", typeof(ServiceRegistry));
37 
38  private readonly Dictionary<Type, object> registeredService = new Dictionary<Type, object>();
39 
40  #region IServiceRegistry Members
41 
42  /// <summary>
43  /// Gets the instance service providing a specified service.
44  /// </summary>
45  /// <param name="type">The type of service.</param>
46  /// <returns>The registered instance of this service.</returns>
47  /// <exception cref="System.ArgumentNullException">type</exception>
48  public object GetService(Type type)
49  {
50  if (type == null)
51  throw new ArgumentNullException("type");
52 
53  lock (registeredService)
54  {
55  if (registeredService.ContainsKey(type))
56  return registeredService[type];
57  }
58 
59  return null;
60  }
61 
62  public event EventHandler<ServiceEventArgs> ServiceAdded;
63 
64  public event EventHandler<ServiceEventArgs> ServiceRemoved;
65 
66  /// <summary>
67  /// Adds a service to this <see cref="ServiceRegistry"/>.
68  /// </summary>
69  /// <param name="type">The type of service to add.</param>
70  /// <param name="provider">The service provider to add.</param>
71  /// <exception cref="System.ArgumentNullException">type;Service type cannot be null</exception>
72  /// <exception cref="System.ArgumentException">Service is already registered;type</exception>
73  public void AddService(Type type, object provider)
74  {
75  if (type == null)
76  throw new ArgumentNullException("type");
77 
78  if (provider == null)
79  throw new ArgumentNullException("provider");
80 
81  if (!type.GetTypeInfo().IsAssignableFrom(provider.GetType().GetTypeInfo()))
82  throw new ArgumentException(String.Format("Service [{0}] must be assignable to [{1}]", provider.GetType().FullName, type.GetType().FullName));
83 
84  lock (registeredService)
85  {
86  if (registeredService.ContainsKey(type))
87  throw new ArgumentException("Service is already registered", "type");
88  registeredService.Add(type, provider);
89  }
90  OnServiceAdded(new ServiceEventArgs(type, provider));
91  }
92 
93  /// <summary>Removes the object providing a specified service.</summary>
94  /// <param name="type">The type of service.</param>
95  public void RemoveService(Type type)
96  {
97  if (type == null)
98  throw new ArgumentNullException("type");
99 
100  object oldService = null;
101  lock (registeredService)
102  {
103  if (registeredService.TryGetValue(type, out oldService))
104  registeredService.Remove(type);
105  }
106  if (oldService != null)
107  OnServiceRemoved(new ServiceEventArgs(type, oldService));
108  }
109 
110  #endregion
111 
112  private void OnServiceAdded(ServiceEventArgs e)
113  {
114  EventHandler<ServiceEventArgs> handler = ServiceAdded;
115  if (handler != null) handler(this, e);
116  }
117 
118  private void OnServiceRemoved(ServiceEventArgs e)
119  {
120  EventHandler<ServiceEventArgs> handler = ServiceRemoved;
121  if (handler != null) handler(this, e);
122  }
123  }
124 }
A service registry is a IServiceProvider that provides methods to register and unregister services...
void AddService(Type type, object provider)
Adds a service to this ServiceRegistry.
EventHandler< ServiceEventArgs > ServiceRemoved
void RemoveService(Type type)
Removes the object providing a specified service.
Base implementation for IServiceRegistry
EventHandler< ServiceEventArgs > ServiceAdded
A class that represents a tag propety.
Definition: PropertyKey.cs:17
object GetService(Type type)
Gets the instance service providing a specified service.