Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate api/Factories/WarFoundryFactoryFactory.cs @ 34:b28be912adab
Re #32 - Migrate to schema
* Remove use of Categories (now just has a single category)
* Fix infinite loop of trying to load files by adding a "is loading" flag
* Fix invalid setting of MinSize to MaxNumber when we're testing if MinSize > MaxSize
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 15 Mar 2009 16:07:52 +0000 |
parents | a99d3b8466ba |
children |
rev | line source |
---|---|
15 | 1 // This file (WarFoundryFactoryFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. |
0 | 2 // |
15 | 3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. |
0 | 4 |
5 using System; | |
6 using System.Collections.Generic; | |
7 using System.Reflection; | |
8 using IBBoard; | |
9 using IBBoard.Logging; | |
10 | |
11 namespace IBBoard.WarFoundry.API.Factories | |
12 { | |
13 public class WarFoundryFactoryFactory | |
14 { | |
15 private Dictionary<Type, IWarFoundryFactory> factories = new Dictionary<Type, IWarFoundryFactory>(); | |
16 private Type defaultType; | |
17 private static WarFoundryFactoryFactory factoryFactory; | |
18 | |
19 private WarFoundryFactoryFactory() | |
20 { | |
21 } | |
22 | |
23 public static WarFoundryFactoryFactory GetFactoryFactory() | |
24 { | |
25 if (factoryFactory == null) | |
26 { | |
27 factoryFactory = new WarFoundryFactoryFactory(); | |
28 } | |
29 | |
30 return factoryFactory; | |
31 } | |
32 | |
33 public IWarFoundryFactory GetFactory() | |
34 { | |
35 return GetFactory(DefaultType); | |
36 } | |
37 | |
38 public IWarFoundryFactory GetFactory(Type cls) | |
39 { | |
40 cls = CheckType(cls); | |
41 | |
42 IWarFoundryFactory factory = null; | |
43 factories.TryGetValue(cls, out factory); | |
44 | |
45 if (factory == null) | |
46 { | |
47 factory = null; | |
17
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
48 MethodInfo method = cls.GetMethod("GetFactory"); |
0 | 49 |
50 if (method!=null) | |
51 { | |
17
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
52 LogNotifier.Debug(GetType(), "Found GetFactory method on " + cls.Name); |
0 | 53 object temp = method.Invoke(null, new object[]{}); |
54 | |
55 if (temp is IWarFoundryFactory) | |
56 { | |
57 factory = (IWarFoundryFactory)temp; | |
58 factories.Add(cls, factory); | |
59 } | |
60 } | |
61 | |
62 if (factory == null) | |
63 { | |
64 throw new ArgumentException("Could not create factory for class "+cls.FullName); | |
65 } | |
66 } | |
67 | |
68 return factory; | |
69 } | |
70 | |
71 public Type DefaultType | |
72 { | |
73 get { return defaultType; } | |
74 set { | |
75 value = CheckType(value); | |
76 defaultType = value; | |
77 } | |
78 } | |
79 | |
80 private Type CheckType(Type cls) | |
81 { | |
82 if (cls == null) | |
83 { | |
84 if (DefaultType!=null) | |
85 { | |
86 return DefaultType; | |
87 } | |
88 else | |
89 { | |
90 throw new InvalidOperationException("Class cannot be null when no default class is set"); | |
91 } | |
92 } | |
93 else if (!typeof(IWarFoundryFactory).IsAssignableFrom(cls)) | |
94 { | |
95 throw new ArgumentException("Class "+cls.FullName+" was not a subtype of "+typeof(IWarFoundryFactory).FullName); | |
96 } | |
97 | |
98 return cls; | |
99 } | |
100 } | |
101 } |