progress
This commit is contained in:
parent
16e76d6b31
commit
484dbfc9d9
529 changed files with 113694 additions and 0 deletions
78
AspClassic.Scripting/Runtime/ContextId.cs
Normal file
78
AspClassic.Scripting/Runtime/ContextId.cs
Normal file
|
@ -0,0 +1,78 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace AspClassic.Scripting.Runtime;
|
||||
|
||||
[Serializable]
|
||||
public struct ContextId : IEquatable<ContextId>
|
||||
{
|
||||
private int _id;
|
||||
|
||||
private static Dictionary<object, ContextId> _contexts = new Dictionary<object, ContextId>();
|
||||
|
||||
private static int _maxId = 1;
|
||||
|
||||
public static readonly ContextId Empty = default(ContextId);
|
||||
|
||||
public int Id => _id;
|
||||
|
||||
internal ContextId(int id)
|
||||
{
|
||||
_id = id;
|
||||
}
|
||||
|
||||
public static ContextId RegisterContext(object identifier)
|
||||
{
|
||||
lock (_contexts)
|
||||
{
|
||||
if (_contexts.TryGetValue(identifier, out var _))
|
||||
{
|
||||
throw Error.LanguageRegistered();
|
||||
}
|
||||
ContextId result = default(ContextId);
|
||||
result._id = _maxId++;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public static ContextId LookupContext(object identifier)
|
||||
{
|
||||
lock (_contexts)
|
||||
{
|
||||
if (_contexts.TryGetValue(identifier, out var value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return Empty;
|
||||
}
|
||||
|
||||
public bool Equals(ContextId other)
|
||||
{
|
||||
return _id == other._id;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _id;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (!(obj is ContextId contextId))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return contextId._id == _id;
|
||||
}
|
||||
|
||||
public static bool operator ==(ContextId self, ContextId other)
|
||||
{
|
||||
return self.Equals(other);
|
||||
}
|
||||
|
||||
public static bool operator !=(ContextId self, ContextId other)
|
||||
{
|
||||
return !self.Equals(other);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue