This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathfoundation-record.cpp
More file actions
418 lines (328 loc) · 12.4 KB
/
foundation-record.cpp
File metadata and controls
418 lines (328 loc) · 12.4 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <foundation.h>
#include "foundation-private.h"
#include "foundation-hash.h"
////////////////////////////////////////////////////////////////////////////////
static bool __check_conformance(MCTypeInfoRef p_typeinfo, const MCValueRef *p_values, uindex_t p_value_count, uindex_t& x_offset)
{
if (x_offset + p_typeinfo -> record . field_count > p_value_count)
return MCErrorThrowGeneric(MCSTR("record does not conform to target type: not enough fields"));
for(uindex_t i = 0; i < p_typeinfo -> record . field_count; i++)
if (MCTypeInfoConforms(MCValueGetTypeInfo(p_values[x_offset + i]), p_typeinfo -> record . fields[i] . type))
return MCErrorThrowGenericWithMessage(MCSTR("record field %{field} does not conform to target type %{type}"),
"field",
p_values[x_offset + i],
"type",
p_typeinfo->record.fields[i].type,
nullptr);
return true;
}
MC_DLLEXPORT_DEF
bool MCRecordCreate(MCTypeInfoRef p_typeinfo, const MCValueRef *p_values, uindex_t p_value_count, MCRecordRef& r_record)
{
MCAssert(p_values != nil || p_value_count == 0);
bool t_success;
t_success = true;
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo = __MCTypeInfoResolve(p_typeinfo);
MCAssert(MCTypeInfoIsRecord(t_resolved_typeinfo));
uindex_t t_offset = 0;
if (!__check_conformance(t_resolved_typeinfo, p_values, p_value_count, t_offset))
return false;
__MCRecord *self;
self = nil;
if (t_success)
t_success = __MCValueCreate(kMCValueTypeCodeRecord, self);
// Here 'offset' will be the total count of all fields.
if (t_success)
t_success = MCMemoryNewArray(t_offset, self -> fields);
if (t_success)
{
for(uindex_t i = 0; i < p_value_count; i++)
self -> fields[i] = MCValueRetain(p_values[i]);
self -> typeinfo = MCValueRetain(p_typeinfo);
r_record = self;
}
else
{
MCMemoryDeleteArray(self -> fields);
MCMemoryDelete(self);
}
return t_success;
}
MC_DLLEXPORT_DEF
bool MCRecordCreateMutable(MCTypeInfoRef p_typeinfo, MCRecordRef& r_record)
{
bool t_success;
t_success = true;
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo = __MCTypeInfoResolve(p_typeinfo);
MCAssert(MCTypeInfoIsRecord(p_typeinfo));
uindex_t t_field_count;
t_field_count = __MCRecordTypeInfoGetFieldCount(t_resolved_typeinfo);
__MCRecord *self;
self = nil;
if (t_success)
t_success = __MCValueCreate(kMCValueTypeCodeRecord, self);
if (t_success)
t_success = MCMemoryNewArray(t_field_count, self -> fields);
if (t_success)
{
for(uindex_t i = 0; i < t_field_count; i++)
self -> fields[i] = MCValueRetain(kMCNull);
self -> typeinfo = MCValueRetain (p_typeinfo);
self -> flags |= kMCRecordFlagIsMutable;
r_record = self;
}
else
{
MCMemoryDeleteArray(self -> fields);
MCMemoryDelete(self);
}
return t_success;
}
MC_DLLEXPORT_DEF
bool MCRecordCopy(MCRecordRef self, MCRecordRef& r_new_record)
{
if (!MCRecordIsMutable(self))
{
MCValueRetain(self);
r_new_record = self;
return true;
}
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo = __MCTypeInfoResolve(self -> typeinfo);
return MCRecordCreate(self -> typeinfo, self -> fields, __MCRecordTypeInfoGetFieldCount(t_resolved_typeinfo), r_new_record);
}
MC_DLLEXPORT_DEF
bool MCRecordCopyAndRelease(MCRecordRef self, MCRecordRef& r_new_record)
{
// If the MCRecord is immutable we just pass it through (as we are releasing it).
if (!MCRecordIsMutable(self))
{
r_new_record = self;
return true;
}
// If the reference is 1, convert it to an immutable MCData
if (self->references == 1)
{
self->flags &= ~kMCRecordFlagIsMutable;
r_new_record = self;
return true;
}
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo = __MCTypeInfoResolve(self -> typeinfo);
// Otherwise make a copy of the data and then release the original
bool t_success;
t_success = MCRecordCreate(self -> typeinfo, self -> fields, __MCRecordTypeInfoGetFieldCount(t_resolved_typeinfo), r_new_record);
MCValueRelease(self);
return t_success;
}
MC_DLLEXPORT_DEF
bool MCRecordMutableCopy(MCRecordRef self, MCRecordRef& r_new_record)
{
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo = __MCTypeInfoResolve(self -> typeinfo);
MCRecordRef t_new_self;
if (!MCRecordCreate(self -> typeinfo, self -> fields, __MCRecordTypeInfoGetFieldCount(t_resolved_typeinfo), t_new_self))
return false;
t_new_self -> flags |= kMCRecordFlagIsMutable;
r_new_record = t_new_self;
return true;
}
MC_DLLEXPORT_DEF
bool MCRecordMutableCopyAndRelease(MCRecordRef self, MCRecordRef& r_new_record)
{
if (MCRecordMutableCopy(self, r_new_record))
{
MCValueRelease(self);
return true;
}
return false;
}
MC_DLLEXPORT_DEF
bool MCRecordIsMutable(MCRecordRef self)
{
__MCAssertIsRecord(self);
return (self -> flags & kMCRecordFlagIsMutable) != 0;
}
static bool __fetch_value(MCTypeInfoRef p_typeinfo, MCRecordRef self, MCNameRef p_field, MCValueRef& r_value)
{
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo = __MCTypeInfoResolve(self -> typeinfo);
for(uindex_t i = 0; i < t_resolved_typeinfo -> record . field_count; i++)
if (MCNameIsEqualToCaseless(p_field, t_resolved_typeinfo -> record . fields[i] . name))
{
r_value = self -> fields[i];
return true;
}
return false;
}
MC_DLLEXPORT_DEF
bool MCRecordFetchValue(MCRecordRef self, MCNameRef p_field, MCValueRef& r_value)
{
__MCAssertIsRecord(self);
__MCAssertIsName(p_field);
return __fetch_value(self -> typeinfo, self, p_field, r_value);
}
static bool __store_value(MCTypeInfoRef p_typeinfo, MCRecordRef self, MCNameRef p_field, MCValueRef p_value)
{
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo = __MCTypeInfoResolve(self -> typeinfo);
for(uindex_t i = 0; i < t_resolved_typeinfo -> record . field_count; i++)
if (MCNameIsEqualToCaseless(p_field, t_resolved_typeinfo -> record . fields[i] . name))
{
if (!MCTypeInfoConforms(MCValueGetTypeInfo(p_value), t_resolved_typeinfo -> record . fields[i] . type))
return MCErrorThrowGeneric(nil);
self -> fields[i] = MCValueRetain(p_value);
return true;
}
return false;
}
MC_DLLEXPORT_DEF
bool MCRecordStoreValue(MCRecordRef self, MCNameRef p_field, MCValueRef p_value)
{
__MCAssertIsRecord(self);
__MCAssertIsName(p_field);
MCAssert(nil != p_value);
return __store_value(self -> typeinfo, self, p_field, p_value);
}
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF bool
MCRecordEncodeAsArray(MCRecordRef record,
MCArrayRef & r_array)
{
__MCAssertIsRecord(record);
MCTypeInfoRef t_typeinfo = MCValueGetTypeInfo (record);
uindex_t t_num_fields = MCRecordTypeInfoGetFieldCount (t_typeinfo);
/* Each field name in the record is used as a key in the array.
* N.b. records decode/encode to dictionaries, not maps, so field
* names are case insensitive. */
MCArrayRef t_new_array;
if (!MCArrayCreateMutable (t_new_array))
return false;
for (uindex_t i = 0; i < t_num_fields; ++i)
{
MCNameRef t_field_name = MCRecordTypeInfoGetFieldName (t_typeinfo, i);
MCValueRef t_field_value;
if (!(MCRecordFetchValue (record, t_field_name, t_field_value) &&
!MCArrayStoreValue (t_new_array, false,
t_field_name, t_field_value)))
{
MCValueRelease (t_new_array);
return false;
}
}
return MCArrayCopyAndRelease (t_new_array, r_array);
}
MC_DLLEXPORT_DEF bool
MCRecordDecodeFromArray(MCArrayRef array,
MCTypeInfoRef p_typeinfo,
MCRecordRef & r_record)
{
__MCAssertIsArray(array);
MCAssert(MCTypeInfoIsRecord(p_typeinfo));
/* Create the result record */
MCRecordRef t_new_record;
if (!MCRecordCreateMutable (p_typeinfo, t_new_record))
return false;
/* We require each field name in the record to be a key in the
* array, and for the types to match. N.b. records decode/encode
* to dictionaries, not maps, so field names are case
* insensitive. */
uindex_t t_num_fields = MCRecordTypeInfoGetFieldCount (p_typeinfo);
for (uindex_t i = 0; i < t_num_fields; ++i)
{
MCNameRef t_field_name = MCRecordTypeInfoGetFieldName (p_typeinfo, i);
MCValueRef t_field_value;
if (!(MCArrayFetchValue (array, false, t_field_name, t_field_value) &&
MCRecordStoreValue (t_new_record, t_field_name, t_field_value)))
{
MCValueRelease (t_new_record);
return false;
}
}
return MCRecordCopyAndRelease (t_new_record, r_record);
}
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF bool
MCRecordIterate(MCRecordRef record,
uintptr_t& x_iterator,
MCNameRef& r_field_name,
MCValueRef& r_field_value)
{
MCTypeInfoRef t_typeinfo = MCValueGetTypeInfo (record);
uindex_t t_num_fields = MCRecordTypeInfoGetFieldCount (t_typeinfo);
/* If the iterator parameter has reached the field count then we are
* done. */
if (x_iterator >= (uintptr_t)t_num_fields)
return false;
r_field_name = MCRecordTypeInfoGetFieldName (t_typeinfo, x_iterator);
r_field_value = record -> fields[x_iterator];
x_iterator += 1;
return true;
}
////////////////////////////////////////////////////////////////////////////////
void __MCRecordDestroy(__MCRecord *self)
{
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo = __MCTypeInfoResolve(self -> typeinfo);
for(uindex_t i = 0; i < __MCRecordTypeInfoGetFieldCount(t_resolved_typeinfo); i++)
MCValueRelease(self -> fields[i]);
MCValueRelease(self -> typeinfo);
MCMemoryDelete(self -> fields);
}
hash_t __MCRecordHash(__MCRecord *self)
{
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo = __MCTypeInfoResolve(self -> typeinfo);
hash_t t_hash;
t_hash = 0;
t_hash = MCHashObjectStream(t_hash, MCHashPointer(self->typeinfo));
for(uindex_t i = 0; i < __MCRecordTypeInfoGetFieldCount(t_resolved_typeinfo); i++)
{
t_hash = MCHashObjectStream(t_hash, MCValueHash(self -> fields[i]));
}
return t_hash;
}
bool __MCRecordIsEqualTo(__MCRecord *self, __MCRecord *other_self)
{
// Records must have the same typeinfo to be equal.
if (self -> typeinfo != other_self -> typeinfo)
return false;
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo = __MCTypeInfoResolve(self -> typeinfo);
// Each field within the record must be equal to be equal.
for(uindex_t i = 0; i < __MCRecordTypeInfoGetFieldCount(t_resolved_typeinfo); i++)
if (!MCValueIsEqualTo(self -> fields[i], other_self -> fields[i]))
return false;
return true;
}
bool __MCRecordCopyDescription(__MCRecord *self, MCStringRef &r_description)
{
return false;
}
bool __MCRecordImmutableCopy(__MCRecord *self, bool p_release, __MCRecord *&r_immutable_value)
{
if (!p_release)
return MCRecordCopy(self, r_immutable_value);
return MCRecordCopyAndRelease(self, r_immutable_value);
}
bool __MCRecordInitialize(void)
{
return true;
}
void __MCRecordFinalize(void)
{
}
////////////////////////////////////////////////////////////////////////////////