Skip to content

Instantly share code, notes, and snippets.

@rje
Created October 24, 2014 22:25
Show Gist options
  • Save rje/44dd09b7abea6a91a945 to your computer and use it in GitHub Desktop.
Save rje/44dd09b7abea6a91a945 to your computer and use it in GitHub Desktop.
generic 'catalog' singleton
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