-
Notifications
You must be signed in to change notification settings - Fork 8
/
SpObject.cs
151 lines (128 loc) · 3.08 KB
/
SpObject.cs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System.Collections.Generic;
using System;
public class SpObject {
public enum ArgType {
Array,
Table,
Boolean,
String,
Int,
Long,
Null,
}
private object mValue;
private ArgType mType;
public SpObject () {
mValue = null;
mType = ArgType.Null;
}
public SpObject (object arg) {
mValue = arg;
mType = ArgType.Null;
if (mValue != null) {
Type t = mValue.GetType ();
if (t == typeof (long)) {
mType = ArgType.Long;
}
else if (t == typeof (int)) {
mType = ArgType.Int;
}
else if (t == typeof (string)) {
mType = ArgType.String;
}
else if (t == typeof (bool)) {
mType = ArgType.Boolean;
}
}
}
public SpObject (ArgType type, params object[] args) {
mType = ArgType.Null;
switch (type) {
case ArgType.Array:
for (int i = 0; i < args.Length; i++) {
Insert (i, args[i]);
}
break;
case ArgType.Table:
for (int i = 0; i < args.Length; i += 2) {
Insert (args[i], args[i + 1]);
}
break;
}
}
public bool IsTable () {
return (mType == ArgType.Table);
}
public Dictionary<object, SpObject> AsTable () {
return mValue as Dictionary<object, SpObject>;
}
public void Insert (object key, SpObject obj) {
if (IsTable () == false) {
mType = ArgType.Table;
mValue = new Dictionary<object, SpObject> ();
}
AsTable ()[key] = obj;
}
public void Insert (object key, object value) {
if (value.GetType () == typeof (SpObject))
Insert (key, (SpObject)value);
else
Insert (key, new SpObject (value));
}
public void Append (SpObject obj) {
if (IsTable () == false) {
mType = ArgType.Table;
mValue = new Dictionary<object, SpObject> ();
}
Insert (AsTable ().Count, obj);
}
public void Append (object value) {
if (value.GetType () == typeof (SpObject))
Append ((SpObject)value);
else
Append (new SpObject (value));
}
public bool IsLong () {
return (mType == ArgType.Long);
}
public long AsLong () {
if (IsLong ())
return (long)mValue;
return Convert.ToInt64 (mValue);
}
public bool IsInt () {
return (mType == ArgType.Int);
}
public int AsInt () {
return (int)mValue;
}
public bool IsBoolean () {
return (mType == ArgType.Boolean);
}
public bool AsBoolean () {
return (bool)mValue;
}
public bool IsString () {
return (mType == ArgType.String);
}
public string AsString () {
return mValue as string;
}
public object Value { get { return mValue; } }
public SpObject this[object key] {
get {
if (IsTable () == false)
return null;
Dictionary<object, SpObject> t = AsTable ();
if (t.ContainsKey (key) == false)
return null;
return t[key];
}
set {
if (IsTable () == false)
return;
Dictionary<object, SpObject> t = AsTable ();
t[key] = value;
}
}
}