/
Obj.cs
246 lines (220 loc) · 10.4 KB
/
Obj.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using System.Text;
using System.Collections.Generic;
namespace HRAOS_take2
{
public class Obj : IHaximaSerializeable
{
public int ID { get; set; }
public string autotag { get; set; }
public string tag { get; set; } // TODO: Implicit tag composed from sigil+prefix+ID, as well as zero or more manually-set custom tags
public Archetype archetype { get; private set; }
private Dictionary<string, IObjField> fields;
public Obj(Archetype _arch_, string _tag_, int _ID_)
{
// Null archetype is uncommon, but valid -- verify this is still true
// Null tag is common, and valid (tag.get returns implicit composed tag...)
register(_ID_);
archetype = _arch_;
tag = _tag_;
autotag = String.Format("{0}-{1}", "OBJ", ID);
Object_Registrar.All.register_tag(autotag, ID);
if (tag != null)
{
Object_Registrar.All.register_tag(tag, ID);
}
fields = new Dictionary<string, IObjField>(); // Starts with zero fields
} // Obj(archetype, tag)
// TODO: Need to define a destructor/finalizer/cleanup method to un-register a defunct Obj
public void register(int new_ID)
{
this.ID = Object_Registrar.All.register_object(this, new_ID);
}
public void unregister()
{
Object_Registrar.All.unregister_object(this);
}
public IObjField this[string field_name]
{
// TODO: 2010/04/08
// Determined that get and set BOTH need to ALWAYS auto-vivify the field.
// This is because someobj["some_field"].iv = 42 calls OBJ indexer property GET, and then FIELD property SET on the field that returns.
//
// In future, maybe some means of avoiding "needless" auto-vivification on get can be found,
// but it may be a non-critical optimization anyways -- that needs study to justify optimization effort
get
{
if (field_name == null) { Error.BadArg("Got null field_name"); }
if (!Token.is_bare_multi_word(field_name)) { Error.BadArg("Got invalid field_name '{0}'", field_name); }
if (!fields.ContainsKey(field_name))
{
// If the field is not present, auto-vivify the field from the Archetype's corresponding field
IArchetypeField arch_field = archetype[field_name];
if (arch_field == null)
{
// TODO: Policy control over behavior upon .get of a field not present in the Archetype.
// Default will likely be to auto-vivify and return default value?
//Form1.stdout.print("ObjField.get: found null archetype field for field_name '{0}', auto-vivifying.\n", field_name);
fields[field_name] = new FieldTempNull(this, field_name);
// Let's see if the FieldTempNull approach works...wow, surprisingly well at near-first test!
// Hmmm...if we don't have an Archetype field to get an IObjField from, how do we know what type to instantiate?
// Seems it will have to be fatal? I'd rather be able to auto-vivify, but this gets called _before_ the .set ...argh...
//Error.Throw("Somehow got null archetype arch_field for field_name '{0}'", field_name);
}
else
{
fields[field_name] = arch_field.default_value; // TODO: Verify that this is a "deep" copy, or deep enough at any rate...
}
}
return fields[field_name];
}
set
{
if (field_name == null) { Error.BadArg("Got null field_name"); }
if (!Token.is_bare_multi_word(field_name)) { Error.BadArg("Got invalid field_name '{0}'", field_name); }
if (archetype[field_name] != null)
{
SemanticTypes type_of_new_field = value.type.semantic_type;
SemanticTypes type_of_arch_field = archetype[field_name].type.semantic_type;
if (type_of_new_field != type_of_arch_field)
{
Error.BadArg("ObjField.set: Called for Obj '{0}' with Archetype '{1}' on field_name '{2}', " +
"with strange IObjField having semantic type '{3}' " +
"which differs from the archetype field semantic type '{4}'",
autotag, archetype.tag, field_name, type_of_new_field, type_of_arch_field);
}
}
//else
//{
// Form1.stdout.print("Obj.this[].set called for field '{0}', archetype does not contain that field\n", field_name);
//}
if (fields.ContainsKey(field_name) && fields[field_name].type != FieldType.EMPTY)
{
// Call to set for an already-present field.
// This is OK if the already-present field is a FieldTempNull... ???
// TODO: Should be controlled by a policy variable.
// A likely default would be to warn and replace the existing attribute.
Error.BadArg("Got already-present Obj parse_field_name '{0}'", field_name);
}
fields[field_name] = value;
}
} // this[field_name]
public void add_field(string field_name, FieldType type)
{
IObjField field = null; // or perhaps: new FieldTempNull(this, field_name);
switch (type.semantic_type)
{
case SemanticTypes.INT:
field = new FieldInt();
break;
case SemanticTypes.STRING:
field = new FieldString();
break;
case SemanticTypes.DECIMAL:
field = new FieldDecimal();
break;
case SemanticTypes.ID:
field = new FieldID();
break;
case SemanticTypes.LIST_INT:
field = new FieldListInt();
break;
case SemanticTypes.LIST_STRING:
field = new FieldListString();
break;
case SemanticTypes.LIST_DECIMAL:
field = new FieldListDecimal();
break;
case SemanticTypes.LIST_ID:
field = new FieldListID();
break;
default:
Error.BadArg("Got unknown field type '{0}'", type);
break;
}
fields[field_name] = field;
} // add_field()
public void remove_field(string field_name)
{
if (field_name == null) { Error.BadArg("Got null field_name"); }
if (!Token.is_bare_multi_word(field_name)) { Error.BadArg("Got invalid field_name '{0}'", field_name); }
if (!fields.ContainsKey(field_name))
{
// TODO: Should be controlled by a policy variable.
// A likely default would be to warn and continue.
Error.BadArg("Called for not-present parse_field_name '{0}'", field_name);
}
fields[field_name] = null;
} // remove_field()
// ...What other methods are useful?
// - support an iterator interface to loop through all fields?
// - serialize() method...
public string serialize()
{
StringBuilder sb = new StringBuilder();
if (tag != null)
{
sb.AppendFormat("OBJ (TAG => {0}, ID => {1}, ARCHETYPE => {2})", tag, ID, archetype.tag);
}
else
{
// Omit tag if not set
sb.AppendFormat("OBJ (ID => {0}, ARCHETYPE => {1})", ID, archetype.tag);
}
// TODO: Might be nice to have an option to print empty {} together...
sb.AppendLine(" {");
foreach (string kk in fields.Keys)
{
// May want to control the order these are sorted in...
int ww_ff = width_widest_field();
int ww_tt = width_widest_type();
// TODO:
if (archetype[kk] != null)
{
string field_format_string = " {0,-" + ww_ff + "} => {1}\n";
sb.AppendFormat(field_format_string, kk, fields[kk].ToString());
}
else
{
// This is a field not present in the Archetype, so specify a type field:
int ww_NIA_types = width_widest_type_not_in_archetype();
string field_format_string = " {0,-" + ww_ff + "} => {1,-" + ww_NIA_types + "}, {2}\n";
sb.AppendFormat(field_format_string, kk, fields[kk].type, fields[kk].ToString());
}
}
sb.AppendLine("}");
return sb.ToString();
}
public int width_widest_field()
{
int ww = 0;
foreach (string kk in fields.Keys)
{
if (kk.Length > ww) { ww = kk.Length; }
}
//Form1.stdout.print(">>> archetype {0} widest={1}\n", tag, ww);
return ww;
}
public int width_widest_type()
{
int ww = 0;
foreach (string kk in fields.Keys)
{
int length = fields[kk].type.keyword.Length;
if (length > ww) { ww = length; }
}
return ww;
}
public int width_widest_type_not_in_archetype()
{
int ww = 0;
foreach (string kk in fields.Keys)
{
if (archetype[kk] != null) { continue; }
int length = fields[kk].type.keyword.Length;
if (length > ww) { ww = length; }
}
return ww;
}
} // class Obj
} // namespace