Created
October 24, 2014 22:25
-
-
Save rje/44dd09b7abea6a91a945 to your computer and use it in GitHub Desktop.
generic 'catalog' singleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ICatalogEntry | |
{ | |
string GetName(); | |
} | |
public class Catalog<T> where T : ICatalogEntry | |
{ | |
static Catalog<T> _instance = new Catalog<T>(); | |
Dictionary<string, T> _entries = new Dictionary<string, T>(); | |
public static T GetEntryByName(string name) | |
{ | |
return _instance.GetEntry(name); | |
} | |
public static void Add(T toAdd) | |
{ | |
Debug.Log("Entry " + toAdd.GetName() + " added to Catalog<" + typeof(T).Name + ">"); | |
_instance.AddEntry(toAdd); | |
} | |
public static void Clear() | |
{ | |
_instance._entries.Clear(); | |
} | |
void AddEntry(T toAdd) | |
{ | |
_entries.Add(toAdd.GetName(), toAdd); | |
} | |
T GetEntry(string name) | |
{ | |
if (_entries.ContainsKey(name)) | |
{ | |
return _entries[name]; | |
} | |
else | |
{ | |
Debug.LogError("Error: Trying to find invalid " + typeof(T).Name +": '" + name + "'"); | |
return default(T); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment