Skip to content

Commit

Permalink
End of episode 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Eforen committed Jul 14, 2017
1 parent 5c92bba commit a39ee58
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 12 deletions.
85 changes: 76 additions & 9 deletions UberEntityComponentSystem/Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace UberEntityComponentSystem
{
public static class Factory
{
private static Dictionary<Type, Queue<object>> caches = new Dictionary<Type, Queue<object>>();

#region Helper Methods

public static void RegisterFactory<T, V>()
Expand Down Expand Up @@ -36,22 +38,58 @@ public static bool IsRegistered(Type type)
/// <returns></returns>
public static V Get<V>() where V : class, new()
{
throw new NotImplementedException();
if (caches.ContainsKey(typeof(V)) &&
caches[typeof(V)].Count > 0)
return caches[typeof(V)].Dequeue() as V;

return new V();
}

public static object Get(Type type)
{
throw new NotImplementedException();
if (caches.ContainsKey(type) &&
caches[type].Count > 0)
return caches[type].Dequeue();

try
{
return type.GetConstructor(Type.EmptyTypes).Invoke(new object[] { });
}
catch (Exception)
{
return null;
}
}

public static V Clean<V>(V target) where V : class, new()
{
throw new NotImplementedException();
return target;
}

public static void Cache<V>(params V[] targets) where V : class, new()
{
throw new NotImplementedException();
if (targets.Count() == 0) return;

if (caches.ContainsKey(typeof(V)) == false) //There is currently no cache for this type of obj
caches.Add(typeof(V), new Queue<object>()); //Create cache for this type of OBJ

foreach (V value in targets)
{
caches[typeof(V)].Enqueue(value);
}

//TODO: Profile with more types to see which is faster
/*
try
{
_counts[typeof(V)]++;
}
catch (KeyNotFoundException)
{
_counts.Add(typeof(V), 1);
}
*/

}
#endregion //Core Functionality

Expand All @@ -63,20 +101,38 @@ public static object Get(Type type)
/// <returns>Amount of objects cached</returns>
public static int Count<V>()
{
throw new NotImplementedException();
if (caches.ContainsKey(typeof(V)) == false)
return 0;
return caches[typeof(V)].Count;

//TODO: Profile with more types to see which is faster
/*
try
{
return _counts[typeof(V)];
}
catch (KeyNotFoundException)
{
return 0;
}
*/
}

public static int Count(params Type[] types)
{
throw new NotImplementedException();
int count = 0; //Setup Count Storage
foreach (Type type in types) // Loop through the types
if (caches.ContainsKey(type)) //Cache has the requested type setup
count += caches[type].Count; //The count of Cache for the object type
return count;
}

/// <summary>
/// Clears all Caches.
/// </summary>
public static void Reset()
{
throw new NotImplementedException();
caches.Clear();
}

/// <summary>
Expand All @@ -85,7 +141,18 @@ public static void Reset()
/// <typeparam name="V"></typeparam>
public static void Reset<V>()
{
throw new NotImplementedException();
if (caches.ContainsKey(typeof(V))) //Cache has the requested type setup
caches[typeof(V)].Clear();
}

/// <summary>
/// Clears the Cache for type.
/// </summary>
/// <typeparam name="V"></typeparam>
public static void Reset(Type type)
{
if (caches.ContainsKey(type)) //Cache has the requested type setup
caches[type].Clear();
}
#endregion // Debug
}
Expand Down
12 changes: 9 additions & 3 deletions UberEntityComponentSystemTests/FactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public void CacheAndGet()
}

[Test]
[Repeat(5000)]
public void CacheCount()
{
//Setup and test 1
Expand Down Expand Up @@ -438,15 +439,15 @@ public void MethodResetTargetedCache()
Assert.AreEqual(3, Factory.Count(typeof(FactoryTestObj2)), "Setup Failed");

//test
Factory.Reset<FactoryTestObj>();
Factory.Reset(typeof(FactoryTestObj));
Assert.AreEqual(0, Factory.Count(typeof(FactoryTestObj)), "FactoryTestObj Not Cleared");
Assert.AreEqual(3, Factory.Count(typeof(FactoryTestObj2)), "FactoryTestObj2 Not Cleared");

Factory.Reset<FactoryTestObj>();
Factory.Reset(typeof(FactoryTestObj));
Assert.AreEqual(0, Factory.Count(typeof(FactoryTestObj)), "FactoryTestObj Not Cleared");
Assert.AreEqual(3, Factory.Count(typeof(FactoryTestObj2)), "FactoryTestObj2 Not Cleared");

Factory.Reset<FactoryTestObj2>();
Factory.Reset(typeof(FactoryTestObj2));
Assert.AreEqual(0, Factory.Count(typeof(FactoryTestObj2)), "FactoryTestObj2 Not Cleared");
}

Expand Down Expand Up @@ -508,25 +509,30 @@ public void MethodCacheCount()
Factory.Cache(new FactoryTestObj());
Assert.AreEqual(1, Factory.Count(typeof(FactoryTestObj)));
Assert.AreEqual(0, Factory.Count(typeof(FactoryTestObj2)));
Assert.AreEqual(1, Factory.Count(typeof(FactoryTestObj), typeof(FactoryTestObj2)));

//Setup and test 2
Factory.Cache(new FactoryTestObj());
Factory.Cache(new FactoryTestObj2());
Assert.AreEqual(2, Factory.Count(typeof(FactoryTestObj)));
Assert.AreEqual(1, Factory.Count(typeof(FactoryTestObj2)));
Assert.AreEqual(3, Factory.Count(typeof(FactoryTestObj), typeof(FactoryTestObj2)));

//Setup and test 3
Factory.Cache(new FactoryTestObj());
Factory.Cache(new FactoryTestObj());
Assert.AreEqual(4, Factory.Count(typeof(FactoryTestObj)));
Assert.AreEqual(1, Factory.Count(typeof(FactoryTestObj2)));
Assert.AreEqual(5, Factory.Count(typeof(FactoryTestObj), typeof(FactoryTestObj2)));

//Setup and test 4
Factory.Cache(new FactoryTestObj2());
Factory.Cache(new FactoryTestObj2());
Factory.Cache(new FactoryTestObj2());
Assert.AreEqual(4, Factory.Count(typeof(FactoryTestObj)));
Assert.AreEqual(4, Factory.Count(typeof(FactoryTestObj2)));
Assert.AreEqual(8, Factory.Count(typeof(FactoryTestObj), typeof(FactoryTestObj2)));
Assert.AreEqual(8, Factory.Count(new Type[] { typeof(FactoryTestObj), typeof(FactoryTestObj2) }));
}

[Test]
Expand Down

0 comments on commit a39ee58

Please sign in to comment.