Skip to content

Commit

Permalink
еще чистка перечислений: без обращения к Instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Rm committed Nov 18, 2024
1 parent 90a4c87 commit c9aad0d
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 126 deletions.
27 changes: 9 additions & 18 deletions src/OneScript.StandardLibrary/DriveInfo/DriveTypeEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,22 @@ namespace OneScript.StandardLibrary.DriveInfo
[SystemEnum("ТипДиска", "DriveType")]
public class DriveTypeEnum : EnumerationContext
{

private DriveTypeEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType)
: base(typeRepresentation, valuesType)
{

this.WrapClrValue("Неизвестный", "Unknown", System.IO.DriveType.Unknown);
this.WrapClrValue("НеИмеетКорневойКаталог", "NoRootDirectory", System.IO.DriveType.NoRootDirectory);
this.WrapClrValue("СъемноеЗапоминающееУстройство", "Removable", System.IO.DriveType.Removable);
this.WrapClrValue("ЖесткийДиск", "Fixed", System.IO.DriveType.Fixed);
this.WrapClrValue("СетевойДиск", "Network", System.IO.DriveType.Network);
this.WrapClrValue("ОптическийДиск", "CDRom", System.IO.DriveType.CDRom);
this.WrapClrValue("ДискОЗУ", "Ram", System.IO.DriveType.Ram);
}

public static DriveTypeEnum CreateInstance(ITypeManager typeManager)
{

var instance = EnumContextHelper.CreateClrEnumInstance<DriveTypeEnum, System.IO.DriveType>(
typeManager,
(t, v) => new DriveTypeEnum(t, v));

instance.WrapClrValue("Неизвестный", "Unknown", System.IO.DriveType.Unknown);
instance.WrapClrValue("НеИмеетКорневойКаталог", "NoRootDirectory", System.IO.DriveType.NoRootDirectory);
instance.WrapClrValue("СъемноеЗапоминающееУстройство", "Removable", System.IO.DriveType.Removable);
instance.WrapClrValue("ЖесткийДиск", "Fixed", System.IO.DriveType.Fixed);
instance.WrapClrValue("СетевойДиск", "Network", System.IO.DriveType.Network);
instance.WrapClrValue("ОптическийДиск", "CDRom", System.IO.DriveType.CDRom);
instance.WrapClrValue("ДискОЗУ", "Ram", System.IO.DriveType.Ram);

return instance;
return EnumContextHelper.CreateClrEnumInstance<DriveTypeEnum, System.IO.DriveType>(
typeManager, (t, v) => new DriveTypeEnum(t, v));
}

}

}
44 changes: 19 additions & 25 deletions src/OneScript.StandardLibrary/Text/ConsoleColorEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This Source Code Form is subject to the terms of the

using System;
using OneScript.Contexts.Enums;
using OneScript.StandardLibrary.XMLSchema.Enumerations;
using OneScript.Types;
using ScriptEngine.Machine.Contexts;

Expand All @@ -18,35 +19,28 @@ public class ConsoleColorEnum : ClrEnumWrapper<ConsoleColor>
private ConsoleColorEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType)
: base(typeRepresentation, valuesType)
{
this.WrapClrValue("Черный", "Black", ConsoleColor.Black);
this.WrapClrValue("ТемноСиний", "DarkBlue", ConsoleColor.DarkBlue);
this.WrapClrValue("ТемноЗеленый", "DarkGreen", ConsoleColor.DarkGreen);
this.WrapClrValue("ТемноБирюзовый", "DarkCyan", ConsoleColor.DarkCyan);
this.WrapClrValue("ТемноКрасный", "DarkRed", ConsoleColor.DarkRed);
this.WrapClrValue("ТемноМалиновый", "DarkMagenta", ConsoleColor.DarkMagenta);
this.WrapClrValue("ТемноЖелтый", "DarkYellow", ConsoleColor.DarkYellow);
this.WrapClrValue("Серый", "Gray", ConsoleColor.Gray);

this.WrapClrValue("ТемноСерый", "DarkGray", ConsoleColor.DarkGray);
this.WrapClrValue("Синий", "Blue", ConsoleColor.Blue);
this.WrapClrValue("Зеленый", "Green", ConsoleColor.Green);
this.WrapClrValue("Бирюза", "Cyan", ConsoleColor.Cyan);
this.WrapClrValue("Красный", "Red", ConsoleColor.Red);
this.WrapClrValue("Малиновый", "Magenta", ConsoleColor.Magenta);
this.WrapClrValue("Желтый", "Yellow", ConsoleColor.Yellow);
this.WrapClrValue("Белый", "White", ConsoleColor.White);
}

public static ConsoleColorEnum CreateInstance(ITypeManager typeManager)
{
var instance = EnumContextHelper.CreateClrEnumInstance<ConsoleColorEnum, ConsoleColor>(
typeManager,
(t,v) => new ConsoleColorEnum(t,v));

instance.WrapClrValue("Черный", "Black", ConsoleColor.Black);
instance.WrapClrValue("ТемноСиний", "DarkBlue", ConsoleColor.DarkBlue);
instance.WrapClrValue("ТемноЗеленый", "DarkGreen", ConsoleColor.DarkGreen);
instance.WrapClrValue("ТемноБирюзовый", "DarkCyan", ConsoleColor.DarkCyan);
instance.WrapClrValue("ТемноКрасный", "DarkRed", ConsoleColor.DarkRed);
instance.WrapClrValue("ТемноМалиновый", "DarkMagenta", ConsoleColor.DarkMagenta);
instance.WrapClrValue("ТемноЖелтый", "DarkYellow", ConsoleColor.DarkYellow);
instance.WrapClrValue("Серый", "Gray", ConsoleColor.Gray);

instance.WrapClrValue("ТемноСерый", "DarkGray", ConsoleColor.DarkGray);
instance.WrapClrValue("Синий", "Blue", ConsoleColor.Blue);
instance.WrapClrValue("Зеленый", "Green", ConsoleColor.Green);
instance.WrapClrValue("Бирюза", "Cyan", ConsoleColor.Cyan);
instance.WrapClrValue("Красный", "Red", ConsoleColor.Red);
instance.WrapClrValue("Малиновый", "Magenta", ConsoleColor.Magenta);
instance.WrapClrValue("Желтый", "Yellow", ConsoleColor.Yellow);
instance.WrapClrValue("Белый", "White", ConsoleColor.White);

OnInstanceCreation(instance);

return instance;
return CreateInstance(typeManager, (t, v) => new ConsoleColorEnum(t, v));
}
}
}
7 changes: 2 additions & 5 deletions src/OneScript.StandardLibrary/XDTO/XDTOSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ namespace OneScript.StandardLibrary.XDTO
public sealed class XDTOSerializer : AutoContext<XDTOSerializer>
{
private readonly XmlGlobalFunctions _xmlGlobalFunctions;
private readonly XmlNodeTypeEnum _xmlNodeEnum;


private XDTOSerializer(IGlobalsManager globalsManager)
{
_xmlGlobalFunctions = globalsManager.GetInstance<XmlGlobalFunctions>();
_xmlNodeEnum = globalsManager.GetInstance<XmlNodeTypeEnum>();
}

private void WriteXMLSimpleData(XmlWriterImpl xmlWriter,
Expand Down Expand Up @@ -190,7 +187,7 @@ public IValue ReadXML(XmlReaderImpl xmlReader, IValue valueType = null)
if (valueType is BslTypeValue typeTypeValue)
typeValue = typeTypeValue;

else if (xmlReader.NodeType.Equals(_xmlNodeEnum.FromNativeValue(XmlNodeType.Element)))
else if (xmlReader.NodeType.Equals(XmlNodeTypeEnum.FromNativeValue(XmlNodeType.Element)))
{
IValue xsiType = xmlReader.GetAttribute(ValueFactory.Create("type"), XmlSchema.InstanceNamespace);
IValue xsiNil = xmlReader.GetAttribute(ValueFactory.Create("nil"), XmlSchema.InstanceNamespace);
Expand Down Expand Up @@ -238,7 +235,7 @@ public IValue ReadXML(XmlReaderImpl xmlReader, IValue valueType = null)
else if (typeof(BslPrimitiveValue).IsAssignableFrom(implType))
{
xmlReader.Read();
if (xmlReader.NodeType.Equals(_xmlNodeEnum.FromNativeValue(XmlNodeType.Text)))
if (xmlReader.NodeType.Equals(XmlNodeTypeEnum.FromNativeValue(XmlNodeType.Text)))
{
result = XMLValue(typeValue, xmlReader.Value);
xmlReader.Read();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public sealed class XSSchemaFinalUnion : AutoContext<XSSchemaFinalUnion>

private bool Contains(XmlSchemaDerivationMethod value)
{
var enumValue = EnumerationXSSchemaFinal.Instance.FromNativeValue(value);
var enumValue = EnumerationXSSchemaFinal.FromNativeValue(value);
var idx = _values.Find(enumValue);
return (idx.SystemType != BasicTypes.Undefined);
}
Expand Down
27 changes: 3 additions & 24 deletions src/OneScript.StandardLibrary/Xml/XmlNodeTypeEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ This Source Code Form is subject to the terms of the
using System.Collections.Generic;
using System.Xml;
using OneScript.Contexts.Enums;
using OneScript.StandardLibrary.XMLSchema.Enumerations;
using OneScript.Types;
using ScriptEngine.Machine.Contexts;

namespace OneScript.StandardLibrary.Xml
{
[SystemEnum("ТипУзлаXML", "XMLNodeType")]
public class XmlNodeTypeEnum : ClrEnumWrapper<XmlNodeType>
public class XmlNodeTypeEnum : ClrEnumWrapperCached<XmlNodeType>
{
readonly Dictionary<XmlNodeType, ClrEnumValueWrapper<XmlNodeType>> _valuesCache = new Dictionary<XmlNodeType,ClrEnumValueWrapper<XmlNodeType>>();

private XmlNodeTypeEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType)
: base(typeRepresentation, valuesType)
{
Expand All @@ -38,29 +37,9 @@ private XmlNodeTypeEnum(TypeDescriptor typeRepresentation, TypeDescriptor values
MakeValue("Текст", "Text", XmlNodeType.Text);
}

private void MakeValue(string name, string alias, XmlNodeType enumValue)
{
var wrappedValue = this.WrapClrValue(name, alias, enumValue);
_valuesCache[enumValue] = wrappedValue;
}

public override ClrEnumValueWrapper<XmlNodeType> FromNativeValue(XmlNodeType native)
{
if (native == XmlNodeType.SignificantWhitespace)
native = XmlNodeType.Whitespace;

return _valuesCache[native];
}

public static XmlNodeTypeEnum CreateInstance(ITypeManager typeManager)
{
var instance = EnumContextHelper.CreateClrEnumInstance<XmlNodeTypeEnum, XmlNodeType>(
typeManager,
(t,v) => new XmlNodeTypeEnum(t, v));

OnInstanceCreation(instance);

return instance;
return CreateInstance(typeManager, (t, v) => new XmlNodeTypeEnum(t, v));
}
}
}
7 changes: 3 additions & 4 deletions src/OneScript.StandardLibrary/Xml/XmlReaderImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ public ClrEnumValueWrapper<XmlNodeType> NodeType
{
get
{
var enumInstance = XmlNodeTypeEnum.Instance;
XmlNodeType nodeType;
if (_reader == null)
{
Expand All @@ -212,7 +211,7 @@ public ClrEnumValueWrapper<XmlNodeType> NodeType
nodeType = _reader.NodeType;
}

return enumInstance.FromNativeValue(nodeType);
return XmlNodeTypeEnum.FromNativeValue(nodeType);
}
}

Expand Down Expand Up @@ -431,7 +430,7 @@ private void CheckEmptyElementEntering()

private bool IsEndElement()
{
var isEnd = (NodeType == XmlNodeTypeEnum.Instance.FromNativeValue(XmlNodeType.EndElement));
var isEnd = (NodeType == XmlNodeTypeEnum.FromNativeValue(XmlNodeType.EndElement));
return isEnd;
}

Expand Down Expand Up @@ -484,7 +483,7 @@ public IValue MoveToContent()
{
var nodeType = _reader.MoveToContent();
CheckEmptyElementEntering();
return GlobalsHelper.GetEnum<XmlNodeTypeEnum>().FromNativeValue(nodeType);
return XmlNodeTypeEnum.FromNativeValue(nodeType);
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions src/OneScript.StandardLibrary/Xml/XmlReaderSettingsImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public XmlReaderSettingsImpl(string version, XmlParserContext context, XmlReader
public string Language => _context.XmlLang;

[ContextProperty("ПробельныеСимволы", "Space")]
public IValue Space => XmlSpaceEnum.Instance.FromNativeValue(_context.XmlSpace);
public IValue Space => XmlSpaceEnum.FromNativeValue(_context.XmlSpace);

[ContextProperty("ТипПроверкиПравильности","ValidationType")]
public IValue ValidationTypeImpl => XmlValidationTypeEnum.Instance.FromNativeValue(_settings.ValidationType);
public IValue ValidationTypeImpl => XmlValidationTypeEnum.FromNativeValue(_settings.ValidationType);

[ContextProperty("ИгнорироватьОбъявлениеXML", "IgnoreXMLDeclaration")]
public bool IgnoreXMLDeclaration { get; }
Expand Down
11 changes: 3 additions & 8 deletions src/OneScript.StandardLibrary/Xml/XmlSpaceEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ This Source Code Form is subject to the terms of the

using System.Xml;
using OneScript.Contexts.Enums;
using OneScript.StandardLibrary.XMLSchema.Enumerations;
using OneScript.Types;
using ScriptEngine.Machine.Contexts;

namespace OneScript.StandardLibrary.Xml
{
[SystemEnum("ПробельныеСимволыXML", "XMLSpace")]
public class XmlSpaceEnum : ClrEnumWrapper<XmlSpace>
public class XmlSpaceEnum : ClrEnumWrapperCached<XmlSpace>
{
private XmlSpaceEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType)
: base(typeRepresentation, valuesType)
Expand All @@ -24,13 +25,7 @@ private XmlSpaceEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesTyp

public static XmlSpaceEnum CreateInstance(ITypeManager typeManager)
{
var instance = EnumContextHelper.CreateClrEnumInstance<XmlSpaceEnum, XmlSpace>(
typeManager,
(t,v) => new XmlSpaceEnum(t, v));

OnInstanceCreation(instance);

return instance;
return CreateInstance(typeManager, (t, v) => new XmlSpaceEnum(t, v));
}
}
}
32 changes: 3 additions & 29 deletions src/OneScript.StandardLibrary/Xml/XmlValidationTypeEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ This Source Code Form is subject to the terms of the
using System.Collections.Generic;
using System.Xml;
using OneScript.Contexts.Enums;
using OneScript.StandardLibrary.XMLSchema.Enumerations;
using OneScript.Types;
using ScriptEngine.Machine.Contexts;

namespace OneScript.StandardLibrary.Xml
{
[SystemEnum("ТипПроверкиXML", "XMLValidationType")]
public class XmlValidationTypeEnum : ClrEnumWrapper<ValidationType>
public class XmlValidationTypeEnum : ClrEnumWrapperCached<ValidationType>
{
readonly Dictionary<ValidationType, ClrEnumValueWrapper<ValidationType>> _valuesCache = new Dictionary<ValidationType, ClrEnumValueWrapper<ValidationType>>();

private XmlValidationTypeEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType)
: base(typeRepresentation, valuesType)
{
Expand All @@ -26,34 +25,9 @@ private XmlValidationTypeEnum(TypeDescriptor typeRepresentation, TypeDescriptor
MakeValue("СхемаXML", "XMLSchema", ValidationType.Schema);
}

private void MakeValue(string name, string alias, ValidationType enumValue)
{
var wrappedValue = this.WrapClrValue(name, alias, enumValue);
_valuesCache[enumValue] = wrappedValue;
}

public override ClrEnumValueWrapper<ValidationType> FromNativeValue(ValidationType native)
{
if (_valuesCache.TryGetValue(native, out var val))
{
return val;
}

val = base.FromNativeValue(native);
_valuesCache.Add(native, val);

return val;
}

public static XmlValidationTypeEnum CreateInstance(ITypeManager typeManager)
{
var instance = EnumContextHelper.CreateClrEnumInstance<XmlValidationTypeEnum, ValidationType>(
typeManager,
(t,v) => new XmlValidationTypeEnum(t, v));

OnInstanceCreation(instance);

return instance;
return CreateInstance(typeManager, (t, v) => new XmlValidationTypeEnum(t, v));
}
}
}
20 changes: 10 additions & 10 deletions src/ScriptEngine/Machine/Contexts/ClrEnumWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ protected static void OnInstanceCreation(ClrEnumWrapper<T> instance)
{
Instance = instance;
}

protected static TE CreateInstance<TE>(ITypeManager typeManager,EnumCreationDelegate<TE> creator)
where TE: ClrEnumWrapper<T>
{
var instance = EnumContextHelper.CreateClrEnumInstance<TE, T>(typeManager, creator);

OnInstanceCreation(instance);
return instance;
}
}

public abstract class ClrEnumWrapperCached<T> : ClrEnumWrapper<T> where T : struct
Expand All @@ -111,16 +120,7 @@ protected void MakeValue(string name, string alias, T enumValue)
public static new ClrEnumValueWrapper<T> FromNativeValue(T native)
{
_valuesCache.TryGetValue(native, out ClrEnumValueWrapper<T> value);
return value;
}

public static TE CreateInstance<TE>(ITypeManager typeManager,EnumCreationDelegate<TE> creator)
where TE: ClrEnumWrapperCached<T>
{
var instance = EnumContextHelper.CreateClrEnumInstance<TE, T>(typeManager, creator);

OnInstanceCreation(instance);
return instance;
return value; // TODO: исключение или null?
}
}
}

0 comments on commit c9aad0d

Please sign in to comment.