-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathService.cs
68 lines (63 loc) · 2.15 KB
/
Service.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/***************************************************************************
* Services.cs
* Copyright (c) 2015 UltimaXNA Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
***************************************************************************/
#region usings
using System;
using System.Collections.Generic;
using UltimaXNA.Core.Diagnostics.Tracing;
#endregion
namespace UltimaXNA
{
public static class Service
{
static readonly Dictionary<Type, object> m_Services = new Dictionary<Type, object>();
public static T Add<T>(T service)
{
Type type = typeof(T);
if (m_Services.ContainsKey(type))
{
Tracer.Critical(string.Format("Attempted to register service of type {0} twice.", type));
m_Services.Remove(type);
}
m_Services.Add(type, service);
return service;
}
public static void Remove<T>()
{
Type type = typeof(T);
if (m_Services.ContainsKey(type))
{
m_Services.Remove(type);
}
else
{
Tracer.Critical(string.Format("Attempted to unregister service of type {0}, but no service of this type (or type and equality) is registered.", type));
}
}
public static bool Has<T>()
{
Type type = typeof(T);
return m_Services.ContainsKey(type);
}
public static T Get<T>(bool failIfNotRegistered = true)
{
Type type = typeof(T);
if (m_Services.ContainsKey(type))
{
return (T)m_Services[type];
}
if (failIfNotRegistered)
{
Tracer.Critical(string.Format("Attempted to get service service of type {0}, but no service of this type is registered.", type));
}
return default(T);
}
}
}