refactor out class implementation registry into separate class

This commit is contained in:
Michael Becker 2025-10-21 14:10:21 -04:00
parent 9eebbef630
commit d15f7066e5
2 changed files with 88 additions and 27 deletions

View File

@ -0,0 +1,71 @@
// Copyright (C) 2025 Michael Becker <alcexhim@gmail.com>
//
// This file is part of Mocha.NET.
//
// Mocha.NET is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Mocha.NET 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
using Mocha.Core.Oop;
namespace Mocha.Core;
public class ClassImplementationRegistry
{
private Dictionary<Guid, ClassImplementation> implementations = new Dictionary<Guid, ClassImplementation>();
public void Register<T>(Guid methodClassId, T implementation) where T : ClassImplementation
{
implementations[methodClassId] = implementation;
}
public bool Contains(Guid classGuid)
{
return implementations.ContainsKey(classGuid);
}
public bool Contains<T>(Guid classGuid) where T : ClassImplementation
{
if (implementations.ContainsKey(classGuid))
{
if (implementations[classGuid] is T)
{
return true;
}
}
return false;
}
public T Get<T>(Guid classGuid) where T : ClassImplementation
{
if (implementations.ContainsKey(classGuid))
{
if (implementations[classGuid] is T obj)
{
return obj;
}
}
throw new NotImplementedException(String.Format("implementation for class {0} not found or incorrect type", classGuid));
}
public bool TryGet<T>(Guid classGuid, out T? implementation) where T : ClassImplementation
{
if (implementations.ContainsKey(classGuid))
{
if (implementations[classGuid] is T obj)
{
implementation = obj;
return true;
}
}
implementation = null;
return false;
}
}

View File

@ -55,13 +55,13 @@ public abstract class Oms
MethodImplementation[] methodImplementations = MBS.Core.Reflection.TypeLoader.GetAvailableTypes<MethodImplementation>(new System.Reflection.Assembly[] { Assembly.GetExecutingAssembly() });
foreach (MethodImplementation impl in methodImplementations)
{
RegisterMethodImplementation(impl.ClassGuid, impl);
ClassImplementations.Register(impl.ClassGuid, impl);
}
AttributeImplementation[] attributeImplementations = MBS.Core.Reflection.TypeLoader.GetAvailableTypes<AttributeImplementation>(new System.Reflection.Assembly[] { Assembly.GetExecutingAssembly() });
foreach (AttributeImplementation impl in attributeImplementations)
{
RegisterAttributeImplementation(impl.ClassGuid, impl);
ClassImplementations.Register(impl.ClassGuid, impl);
}
}
@ -1219,17 +1219,7 @@ public abstract class Oms
return value;
}
private Dictionary<Guid, MethodImplementation> methodImplementations = new Dictionary<Guid, MethodImplementation>();
private void RegisterMethodImplementation(Guid methodClassId, MethodImplementation implementation)
{
methodImplementations[methodClassId] = implementation;
}
private Dictionary<Guid, AttributeImplementation> attributeImplementations = new Dictionary<Guid, AttributeImplementation>();
private void RegisterAttributeImplementation(Guid attributeClassId, AttributeImplementation implementation)
{
attributeImplementations[attributeClassId] = implementation;
}
public ClassImplementationRegistry ClassImplementations { get; } = new ClassImplementationRegistry();
public InstanceHandle Execute(OmsContext context, MethodBinding methodBinding, IInstanceReference? targetInstance = null)
{
@ -1380,7 +1370,7 @@ public abstract class Oms
}
else
{
if (methodImplementations.ContainsKey(parentClassId))
if (ClassImplementations.TryGet<MethodImplementation>(parentClassId, out MethodImplementation? impl))
{
if (targetInstance == null)
{
@ -1435,15 +1425,14 @@ public abstract class Oms
InstanceHandle pclassInstance = GetParentClass(hh);
context.SetWorkData(pclassInstance, hh);
}
retval = methodImplementations[parentClassId].Execute(this, context, methodOrMethodBinding);
}
}
if (retval == null)
{
// return InstanceHandle.Empty;
throw new NotImplementedException(String.Format("method implementation not found for method class {0}", GetGlobalIdentifier(parentClass)));
retval = impl.Execute(this, context, methodOrMethodBinding);
}
else
{
throw new NotImplementedException(String.Format("method implementation not found for method class {0}", GetGlobalIdentifier(parentClass)));
}
}
if (retval != null)
{
// context.StackTrace.Pop();
IDictionary<InstanceHandle, object?> dict = context.GetAllWorkData();
@ -1455,6 +1444,11 @@ public abstract class Oms
}
return retval.Value;
}
else
{
// return InstanceHandle.Empty;
throw new InvalidOperationException(String.Format("method did not return work data for method class {0}", GetGlobalIdentifier(parentClass)));
}
}
private InstanceHandle ExecuteMethodBinding(OmsContext context, InstanceHandle methodBinding, IInstanceReference? targetInstance = null)
@ -2787,10 +2781,6 @@ public abstract class Oms
Guid classGuid = GetGlobalIdentifier(parentClass);
// find AttributeImplementation whose ClassGuid matches
if (attributeImplementations.ContainsKey(classGuid))
{
return attributeImplementations[classGuid];
}
throw new NotImplementedException(String.Format("implementation for attribute class {0} not found", classGuid));
return ClassImplementations.Get<AttributeImplementation>(classGuid);
}
}