Mercurial > repos > IBBoard.WarFoundry.API
diff api/Factories/WarFoundryFactoryFactory.cs @ 0:520818033bb6
Initial commit of WarFoundry code
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 19 Dec 2008 15:57:51 +0000 |
parents | |
children | 306558904c2a |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/Factories/WarFoundryFactoryFactory.cs Fri Dec 19 15:57:51 2008 +0000 @@ -0,0 +1,117 @@ +// AbstractNativeWarFoundryFactory.cs +// +// Copyright (C) 2007 IBBoard +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License version 2.1 of the License as published by the Free +// Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// + +using System; +using System.Collections.Generic; +using System.Reflection; +using IBBoard; +using IBBoard.Logging; + +namespace IBBoard.WarFoundry.API.Factories +{ + public class WarFoundryFactoryFactory + { + private Dictionary<Type, IWarFoundryFactory> factories = new Dictionary<Type, IWarFoundryFactory>(); + private Type defaultType; + private static WarFoundryFactoryFactory factoryFactory; + + private WarFoundryFactoryFactory() + { + } + + public static WarFoundryFactoryFactory GetFactoryFactory() + { + if (factoryFactory == null) + { + factoryFactory = new WarFoundryFactoryFactory(); + } + + return factoryFactory; + } + + public IWarFoundryFactory GetFactory() + { + return GetFactory(DefaultType); + } + + public IWarFoundryFactory GetFactory(Type cls) + { + cls = CheckType(cls); + + IWarFoundryFactory factory = null; + factories.TryGetValue(cls, out factory); + + if (factory == null) + { + factory = null; + MethodInfo method = cls.GetMethod("CreateFactory"); + + if (method!=null) + { + LogNotifier.Debug(GetType(), "Found CreateFactory method on " + cls.Name); + object temp = method.Invoke(null, new object[]{}); + + if (temp is IWarFoundryFactory) + { + factory = (IWarFoundryFactory)temp; + factories.Add(cls, factory); + } + } + + if (factory == null) + { + throw new ArgumentException("Could not create factory for class "+cls.FullName); + } + } + + return factory; + } + + public Type DefaultType + { + get { return defaultType; } + set { + value = CheckType(value); + defaultType = value; + } + } + + private Type CheckType(Type cls) + { + if (cls == null) + { + if (DefaultType!=null) + { + return DefaultType; + } + else + { + throw new InvalidOperationException("Class cannot be null when no default class is set"); + } + } + else if (!typeof(IWarFoundryFactory).IsAssignableFrom(cls)) + { + throw new ArgumentException("Class "+cls.FullName+" was not a subtype of "+typeof(IWarFoundryFactory).FullName); + } + + return cls; + } + } +}