hch
2025-09-04 62188b271cce5e3aec5ca40d58c30f08643e2f60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jace.Util;
 
namespace Jace.Execution
{
    public class ConstantRegistry : IConstantRegistry
    {
        private readonly bool caseSensitive;
        private readonly Dictionary<string, ConstantInfo> constants;
 
        public ConstantRegistry(bool caseSensitive)
        {
            this.caseSensitive = caseSensitive;
            this.constants = new Dictionary<string, ConstantInfo>();
        }
 
        public IEnumerator<ConstantInfo> GetEnumerator()
        {
            return constants.Values.GetEnumerator();
        }
 
        IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
 
        public ConstantInfo GetConstantInfo(string constantName)
        {
            if (string.IsNullOrEmpty(constantName))
                throw new ArgumentNullException("constantName");
 
            ConstantInfo constantInfo = null;
            return constants.TryGetValue(ConvertConstantName(constantName), out constantInfo) ? constantInfo : null;
        }
 
        public bool IsConstantName(string constantName)
        {
            if (string.IsNullOrEmpty(constantName))
                throw new ArgumentNullException("constantName");
 
            return constants.ContainsKey(ConvertConstantName(constantName));
        }
 
        public void RegisterConstant(string constantName, double value)
        {
            RegisterConstant(constantName, value, true);
        }
 
        public void RegisterConstant(string constantName, double value, bool isOverWritable)
        {
            if(string.IsNullOrEmpty(constantName))
                throw new ArgumentNullException("constantName");
 
            constantName = ConvertConstantName(constantName);
 
            if (constants.ContainsKey(constantName) && !constants[constantName].IsOverWritable)
            {
                string message = string.Format("The constant \"{0}\" cannot be overwriten.", constantName);
                throw new Exception(message);
            }
 
            ConstantInfo constantInfo = new ConstantInfo(constantName, value, isOverWritable);
 
            if (constants.ContainsKey(constantName))
                constants[constantName] = constantInfo;
            else
                constants.Add(constantName, constantInfo);
        }
 
        private string ConvertConstantName(string constantName)
        {
            return caseSensitive ? constantName : constantName.ToLowerFast();
        }
    }
}