56 lines
1.9 KiB
C#
56 lines
1.9 KiB
C#
using System;
|
|
using ln.types.btree;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
namespace ln.types.reflection
|
|
{
|
|
public class ObjectPool
|
|
{
|
|
public Type ObjectType { get; }
|
|
public Type IdentityType { get; }
|
|
|
|
public TypeDescriptor TypeDescriptor => TypeDescriptor.GetDescriptor(ObjectType);
|
|
|
|
Func<object, object> getIdentity;
|
|
public object GetIdentity(object o) => getIdentity(o);
|
|
|
|
BTree<object, object> objects = new BTree<object, object>();
|
|
|
|
List<ObjectPool> derivedTypes = new List<ObjectPool>();
|
|
|
|
public ObjectPool(Type objectType, Type identityType, Func<object, object> getIdentityDelegate)
|
|
{
|
|
ObjectType = objectType;
|
|
IdentityType = identityType;
|
|
getIdentity = getIdentityDelegate;
|
|
}
|
|
|
|
public IEnumerable<object> Identities => objects.Keys.Concat(derivedTypes.SelectMany((op)=>op.Identities));
|
|
public IEnumerable<object> Instances => objects.Values.Concat(derivedTypes.SelectMany((op) => op.Instances));
|
|
public IEnumerable<object> OwnInstances => objects.Values;
|
|
|
|
public object this[object identity]
|
|
{
|
|
get
|
|
{
|
|
if (objects.ContainsKey(identity))
|
|
return objects[identity];
|
|
foreach (ObjectPool derivedPool in derivedTypes)
|
|
if (derivedPool.objects.ContainsKey(identity))
|
|
return derivedPool.objects[identity];
|
|
throw new KeyNotFoundException();
|
|
}
|
|
}
|
|
|
|
public void Add(object o) => objects.Add(GetIdentity(o), o);
|
|
public void Remove(object identity) => objects.Remove(identity);
|
|
|
|
|
|
public ObjectPoolValue GetValue(string attributePath) => throw new NotImplementedException();
|
|
|
|
public void AddDerivedTypePool(ObjectPool objectPool) => derivedTypes.Add(objectPool);
|
|
|
|
|
|
}
|
|
}
|