Skip to content

Commit f509bbf

Browse files
committed
еще чистка перечислений: без обращения к Instance
1 parent 90a4c87 commit f509bbf

File tree

10 files changed

+54
-129
lines changed

10 files changed

+54
-129
lines changed

src/OneScript.StandardLibrary/DriveInfo/DriveTypeEnum.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,22 @@ namespace OneScript.StandardLibrary.DriveInfo
2424
[SystemEnum("ТипДиска", "DriveType")]
2525
public class DriveTypeEnum : EnumerationContext
2626
{
27-
2827
private DriveTypeEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType)
2928
: base(typeRepresentation, valuesType)
3029
{
31-
30+
this.WrapClrValue("Неизвестный", "Unknown", System.IO.DriveType.Unknown);
31+
this.WrapClrValue("НеИмеетКорневойКаталог", "NoRootDirectory", System.IO.DriveType.NoRootDirectory);
32+
this.WrapClrValue("СъемноеЗапоминающееУстройство", "Removable", System.IO.DriveType.Removable);
33+
this.WrapClrValue("ЖесткийДиск", "Fixed", System.IO.DriveType.Fixed);
34+
this.WrapClrValue("СетевойДиск", "Network", System.IO.DriveType.Network);
35+
this.WrapClrValue("ОптическийДиск", "CDRom", System.IO.DriveType.CDRom);
36+
this.WrapClrValue("ДискОЗУ", "Ram", System.IO.DriveType.Ram);
3237
}
3338

3439
public static DriveTypeEnum CreateInstance(ITypeManager typeManager)
3540
{
36-
37-
var instance = EnumContextHelper.CreateClrEnumInstance<DriveTypeEnum, System.IO.DriveType>(
38-
typeManager,
39-
(t, v) => new DriveTypeEnum(t, v));
40-
41-
instance.WrapClrValue("Неизвестный", "Unknown", System.IO.DriveType.Unknown);
42-
instance.WrapClrValue("НеИмеетКорневойКаталог", "NoRootDirectory", System.IO.DriveType.NoRootDirectory);
43-
instance.WrapClrValue("СъемноеЗапоминающееУстройство", "Removable", System.IO.DriveType.Removable);
44-
instance.WrapClrValue("ЖесткийДиск", "Fixed", System.IO.DriveType.Fixed);
45-
instance.WrapClrValue("СетевойДиск", "Network", System.IO.DriveType.Network);
46-
instance.WrapClrValue("ОптическийДиск", "CDRom", System.IO.DriveType.CDRom);
47-
instance.WrapClrValue("ДискОЗУ", "Ram", System.IO.DriveType.Ram);
48-
49-
return instance;
41+
return EnumContextHelper.CreateClrEnumInstance<DriveTypeEnum, System.IO.DriveType>(
42+
typeManager, (t, v) => new DriveTypeEnum(t, v));
5043
}
51-
5244
}
53-
5445
}

src/OneScript.StandardLibrary/Text/ConsoleColorEnum.cs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,28 @@ public class ConsoleColorEnum : ClrEnumWrapper<ConsoleColor>
1818
private ConsoleColorEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType)
1919
: base(typeRepresentation, valuesType)
2020
{
21+
this.WrapClrValue("Черный", "Black", ConsoleColor.Black);
22+
this.WrapClrValue("ТемноСиний", "DarkBlue", ConsoleColor.DarkBlue);
23+
this.WrapClrValue("ТемноЗеленый", "DarkGreen", ConsoleColor.DarkGreen);
24+
this.WrapClrValue("ТемноБирюзовый", "DarkCyan", ConsoleColor.DarkCyan);
25+
this.WrapClrValue("ТемноКрасный", "DarkRed", ConsoleColor.DarkRed);
26+
this.WrapClrValue("ТемноМалиновый", "DarkMagenta", ConsoleColor.DarkMagenta);
27+
this.WrapClrValue("ТемноЖелтый", "DarkYellow", ConsoleColor.DarkYellow);
28+
this.WrapClrValue("Серый", "Gray", ConsoleColor.Gray);
29+
30+
this.WrapClrValue("ТемноСерый", "DarkGray", ConsoleColor.DarkGray);
31+
this.WrapClrValue("Синий", "Blue", ConsoleColor.Blue);
32+
this.WrapClrValue("Зеленый", "Green", ConsoleColor.Green);
33+
this.WrapClrValue("Бирюза", "Cyan", ConsoleColor.Cyan);
34+
this.WrapClrValue("Красный", "Red", ConsoleColor.Red);
35+
this.WrapClrValue("Малиновый", "Magenta", ConsoleColor.Magenta);
36+
this.WrapClrValue("Желтый", "Yellow", ConsoleColor.Yellow);
37+
this.WrapClrValue("Белый", "White", ConsoleColor.White);
2138
}
2239

2340
public static ConsoleColorEnum CreateInstance(ITypeManager typeManager)
2441
{
25-
var instance = EnumContextHelper.CreateClrEnumInstance<ConsoleColorEnum, ConsoleColor>(
26-
typeManager,
27-
(t,v) => new ConsoleColorEnum(t,v));
28-
29-
instance.WrapClrValue("Черный", "Black", ConsoleColor.Black);
30-
instance.WrapClrValue("ТемноСиний", "DarkBlue", ConsoleColor.DarkBlue);
31-
instance.WrapClrValue("ТемноЗеленый", "DarkGreen", ConsoleColor.DarkGreen);
32-
instance.WrapClrValue("ТемноБирюзовый", "DarkCyan", ConsoleColor.DarkCyan);
33-
instance.WrapClrValue("ТемноКрасный", "DarkRed", ConsoleColor.DarkRed);
34-
instance.WrapClrValue("ТемноМалиновый", "DarkMagenta", ConsoleColor.DarkMagenta);
35-
instance.WrapClrValue("ТемноЖелтый", "DarkYellow", ConsoleColor.DarkYellow);
36-
instance.WrapClrValue("Серый", "Gray", ConsoleColor.Gray);
37-
38-
instance.WrapClrValue("ТемноСерый", "DarkGray", ConsoleColor.DarkGray);
39-
instance.WrapClrValue("Синий", "Blue", ConsoleColor.Blue);
40-
instance.WrapClrValue("Зеленый", "Green", ConsoleColor.Green);
41-
instance.WrapClrValue("Бирюза", "Cyan", ConsoleColor.Cyan);
42-
instance.WrapClrValue("Красный", "Red", ConsoleColor.Red);
43-
instance.WrapClrValue("Малиновый", "Magenta", ConsoleColor.Magenta);
44-
instance.WrapClrValue("Желтый", "Yellow", ConsoleColor.Yellow);
45-
instance.WrapClrValue("Белый", "White", ConsoleColor.White);
46-
47-
OnInstanceCreation(instance);
48-
49-
return instance;
42+
return CreateInstance(typeManager, (t, v) => new ConsoleColorEnum(t, v));
5043
}
5144
}
5245
}

src/OneScript.StandardLibrary/XDTO/XDTOSerializer.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@ namespace OneScript.StandardLibrary.XDTO
2323
public sealed class XDTOSerializer : AutoContext<XDTOSerializer>
2424
{
2525
private readonly XmlGlobalFunctions _xmlGlobalFunctions;
26-
private readonly XmlNodeTypeEnum _xmlNodeEnum;
27-
2826

2927
private XDTOSerializer(IGlobalsManager globalsManager)
3028
{
3129
_xmlGlobalFunctions = globalsManager.GetInstance<XmlGlobalFunctions>();
32-
_xmlNodeEnum = globalsManager.GetInstance<XmlNodeTypeEnum>();
3330
}
3431

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

193-
else if (xmlReader.NodeType.Equals(_xmlNodeEnum.FromNativeValue(XmlNodeType.Element)))
190+
else if (xmlReader.NodeType.Equals(XmlNodeTypeEnum.FromNativeValue(XmlNodeType.Element)))
194191
{
195192
IValue xsiType = xmlReader.GetAttribute(ValueFactory.Create("type"), XmlSchema.InstanceNamespace);
196193
IValue xsiNil = xmlReader.GetAttribute(ValueFactory.Create("nil"), XmlSchema.InstanceNamespace);
@@ -238,7 +235,7 @@ public IValue ReadXML(XmlReaderImpl xmlReader, IValue valueType = null)
238235
else if (typeof(BslPrimitiveValue).IsAssignableFrom(implType))
239236
{
240237
xmlReader.Read();
241-
if (xmlReader.NodeType.Equals(_xmlNodeEnum.FromNativeValue(XmlNodeType.Text)))
238+
if (xmlReader.NodeType.Equals(XmlNodeTypeEnum.FromNativeValue(XmlNodeType.Text)))
242239
{
243240
result = XMLValue(typeValue, xmlReader.Value);
244241
xmlReader.Read();

src/OneScript.StandardLibrary/XMLSchema/Collections/XSSchemaFinalUnion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public sealed class XSSchemaFinalUnion : AutoContext<XSSchemaFinalUnion>
2121

2222
private bool Contains(XmlSchemaDerivationMethod value)
2323
{
24-
var enumValue = EnumerationXSSchemaFinal.Instance.FromNativeValue(value);
24+
var enumValue = EnumerationXSSchemaFinal.FromNativeValue(value);
2525
var idx = _values.Find(enumValue);
2626
return (idx.SystemType != BasicTypes.Undefined);
2727
}

src/OneScript.StandardLibrary/Xml/XmlNodeTypeEnum.cs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ This Source Code Form is subject to the terms of the
55
at http://mozilla.org/MPL/2.0/.
66
----------------------------------------------------------*/
77

8-
using System.Collections.Generic;
98
using System.Xml;
109
using OneScript.Contexts.Enums;
1110
using OneScript.Types;
@@ -14,10 +13,8 @@ This Source Code Form is subject to the terms of the
1413
namespace OneScript.StandardLibrary.Xml
1514
{
1615
[SystemEnum("ТипУзлаXML", "XMLNodeType")]
17-
public class XmlNodeTypeEnum : ClrEnumWrapper<XmlNodeType>
16+
public class XmlNodeTypeEnum : ClrEnumWrapperCached<XmlNodeType>
1817
{
19-
readonly Dictionary<XmlNodeType, ClrEnumValueWrapper<XmlNodeType>> _valuesCache = new Dictionary<XmlNodeType,ClrEnumValueWrapper<XmlNodeType>>();
20-
2118
private XmlNodeTypeEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType)
2219
: base(typeRepresentation, valuesType)
2320
{
@@ -38,29 +35,9 @@ private XmlNodeTypeEnum(TypeDescriptor typeRepresentation, TypeDescriptor values
3835
MakeValue("Текст", "Text", XmlNodeType.Text);
3936
}
4037

41-
private void MakeValue(string name, string alias, XmlNodeType enumValue)
42-
{
43-
var wrappedValue = this.WrapClrValue(name, alias, enumValue);
44-
_valuesCache[enumValue] = wrappedValue;
45-
}
46-
47-
public override ClrEnumValueWrapper<XmlNodeType> FromNativeValue(XmlNodeType native)
48-
{
49-
if (native == XmlNodeType.SignificantWhitespace)
50-
native = XmlNodeType.Whitespace;
51-
52-
return _valuesCache[native];
53-
}
54-
5538
public static XmlNodeTypeEnum CreateInstance(ITypeManager typeManager)
5639
{
57-
var instance = EnumContextHelper.CreateClrEnumInstance<XmlNodeTypeEnum, XmlNodeType>(
58-
typeManager,
59-
(t,v) => new XmlNodeTypeEnum(t, v));
60-
61-
OnInstanceCreation(instance);
62-
63-
return instance;
40+
return CreateInstance(typeManager, (t, v) => new XmlNodeTypeEnum(t, v));
6441
}
6542
}
6643
}

src/OneScript.StandardLibrary/Xml/XmlReaderImpl.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ public ClrEnumValueWrapper<XmlNodeType> NodeType
189189
{
190190
get
191191
{
192-
var enumInstance = XmlNodeTypeEnum.Instance;
193192
XmlNodeType nodeType;
194193
if (_reader == null)
195194
{
@@ -212,7 +211,7 @@ public ClrEnumValueWrapper<XmlNodeType> NodeType
212211
nodeType = _reader.NodeType;
213212
}
214213

215-
return enumInstance.FromNativeValue(nodeType);
214+
return XmlNodeTypeEnum.FromNativeValue(nodeType);
216215
}
217216
}
218217

@@ -431,7 +430,7 @@ private void CheckEmptyElementEntering()
431430

432431
private bool IsEndElement()
433432
{
434-
var isEnd = (NodeType == XmlNodeTypeEnum.Instance.FromNativeValue(XmlNodeType.EndElement));
433+
var isEnd = (NodeType == XmlNodeTypeEnum.FromNativeValue(XmlNodeType.EndElement));
435434
return isEnd;
436435
}
437436

@@ -484,7 +483,7 @@ public IValue MoveToContent()
484483
{
485484
var nodeType = _reader.MoveToContent();
486485
CheckEmptyElementEntering();
487-
return GlobalsHelper.GetEnum<XmlNodeTypeEnum>().FromNativeValue(nodeType);
486+
return XmlNodeTypeEnum.FromNativeValue(nodeType);
488487
}
489488

490489
#endregion

src/OneScript.StandardLibrary/Xml/XmlReaderSettingsImpl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ public XmlReaderSettingsImpl(string version, XmlParserContext context, XmlReader
5252
public string Language => _context.XmlLang;
5353

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

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

6060
[ContextProperty("ИгнорироватьОбъявлениеXML", "IgnoreXMLDeclaration")]
6161
public bool IgnoreXMLDeclaration { get; }

src/OneScript.StandardLibrary/Xml/XmlSpaceEnum.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,18 @@ This Source Code Form is subject to the terms of the
1313
namespace OneScript.StandardLibrary.Xml
1414
{
1515
[SystemEnum("ПробельныеСимволыXML", "XMLSpace")]
16-
public class XmlSpaceEnum : ClrEnumWrapper<XmlSpace>
16+
public class XmlSpaceEnum : ClrEnumWrapperCached<XmlSpace>
1717
{
1818
private XmlSpaceEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType)
1919
: base(typeRepresentation, valuesType)
2020
{
21-
this.WrapClrValue("ПоУмолчанию", "Default", XmlSpace.Default);
22-
this.WrapClrValue("Сохранять", "Preserve", XmlSpace.Preserve);
21+
MakeValue("ПоУмолчанию", "Default", XmlSpace.Default);
22+
MakeValue("Сохранять", "Preserve", XmlSpace.Preserve);
2323
}
2424

2525
public static XmlSpaceEnum CreateInstance(ITypeManager typeManager)
2626
{
27-
var instance = EnumContextHelper.CreateClrEnumInstance<XmlSpaceEnum, XmlSpace>(
28-
typeManager,
29-
(t,v) => new XmlSpaceEnum(t, v));
30-
31-
OnInstanceCreation(instance);
32-
33-
return instance;
27+
return CreateInstance(typeManager, (t, v) => new XmlSpaceEnum(t, v));
3428
}
3529
}
3630
}

src/OneScript.StandardLibrary/Xml/XmlValidationTypeEnum.cs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ This Source Code Form is subject to the terms of the
88
using System.Collections.Generic;
99
using System.Xml;
1010
using OneScript.Contexts.Enums;
11+
using OneScript.StandardLibrary.XMLSchema.Enumerations;
1112
using OneScript.Types;
1213
using ScriptEngine.Machine.Contexts;
1314

1415
namespace OneScript.StandardLibrary.Xml
1516
{
1617
[SystemEnum("ТипПроверкиXML", "XMLValidationType")]
17-
public class XmlValidationTypeEnum : ClrEnumWrapper<ValidationType>
18+
public class XmlValidationTypeEnum : ClrEnumWrapperCached<ValidationType>
1819
{
19-
readonly Dictionary<ValidationType, ClrEnumValueWrapper<ValidationType>> _valuesCache = new Dictionary<ValidationType, ClrEnumValueWrapper<ValidationType>>();
20-
2120
private XmlValidationTypeEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType)
2221
: base(typeRepresentation, valuesType)
2322
{
@@ -26,34 +25,9 @@ private XmlValidationTypeEnum(TypeDescriptor typeRepresentation, TypeDescriptor
2625
MakeValue("СхемаXML", "XMLSchema", ValidationType.Schema);
2726
}
2827

29-
private void MakeValue(string name, string alias, ValidationType enumValue)
30-
{
31-
var wrappedValue = this.WrapClrValue(name, alias, enumValue);
32-
_valuesCache[enumValue] = wrappedValue;
33-
}
34-
35-
public override ClrEnumValueWrapper<ValidationType> FromNativeValue(ValidationType native)
36-
{
37-
if (_valuesCache.TryGetValue(native, out var val))
38-
{
39-
return val;
40-
}
41-
42-
val = base.FromNativeValue(native);
43-
_valuesCache.Add(native, val);
44-
45-
return val;
46-
}
47-
4828
public static XmlValidationTypeEnum CreateInstance(ITypeManager typeManager)
4929
{
50-
var instance = EnumContextHelper.CreateClrEnumInstance<XmlValidationTypeEnum, ValidationType>(
51-
typeManager,
52-
(t,v) => new XmlValidationTypeEnum(t, v));
53-
54-
OnInstanceCreation(instance);
55-
56-
return instance;
30+
return CreateInstance(typeManager, (t, v) => new XmlValidationTypeEnum(t, v));
5731
}
5832
}
5933
}

src/ScriptEngine/Machine/Contexts/ClrEnumWrapper.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ protected static void OnInstanceCreation(ClrEnumWrapper<T> instance)
9090
{
9191
Instance = instance;
9292
}
93+
94+
protected static TE CreateInstance<TE>(ITypeManager typeManager,EnumCreationDelegate<TE> creator)
95+
where TE: ClrEnumWrapper<T>
96+
{
97+
var instance = EnumContextHelper.CreateClrEnumInstance<TE, T>(typeManager, creator);
98+
99+
OnInstanceCreation(instance);
100+
return instance;
101+
}
93102
}
94103

95104
public abstract class ClrEnumWrapperCached<T> : ClrEnumWrapper<T> where T : struct
@@ -111,16 +120,7 @@ protected void MakeValue(string name, string alias, T enumValue)
111120
public static new ClrEnumValueWrapper<T> FromNativeValue(T native)
112121
{
113122
_valuesCache.TryGetValue(native, out ClrEnumValueWrapper<T> value);
114-
return value;
115-
}
116-
117-
public static TE CreateInstance<TE>(ITypeManager typeManager,EnumCreationDelegate<TE> creator)
118-
where TE: ClrEnumWrapperCached<T>
119-
{
120-
var instance = EnumContextHelper.CreateClrEnumInstance<TE, T>(typeManager, creator);
121-
122-
OnInstanceCreation(instance);
123-
return instance;
123+
return value; // TODO: исключение или null?
124124
}
125125
}
126126
}

0 commit comments

Comments
 (0)