forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoundation-record.cpp
More file actions
515 lines (411 loc) · 15.5 KB
/
Copy pathfoundation-record.cpp
File metadata and controls
515 lines (411 loc) · 15.5 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/* Copyright (C) 2003-2013 Runtime Revolution 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"
////////////////////////////////////////////////////////////////////////////////
static bool __check_conformance(MCTypeInfoRef p_typeinfo, const MCValueRef *p_values, uindex_t p_value_count, uindex_t& x_offset)
{
if (p_typeinfo -> record . base != kMCNullTypeInfo)
{
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo = __MCTypeInfoResolve(p_typeinfo);
if (!__check_conformance(t_resolved_typeinfo, p_values, p_value_count, x_offset))
return false;
}
if (x_offset + p_typeinfo -> record . field_count > p_value_count)
return MCErrorThrowGeneric(nil);
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 MCErrorThrowGeneric(nil);
return true;
}
MC_DLLEXPORT_DEF
bool MCRecordCreate(MCTypeInfoRef p_typeinfo, const MCValueRef *p_values, uindex_t p_value_count, MCRecordRef& r_record)
{
bool t_success;
t_success = true;
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo = __MCTypeInfoResolve(p_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);
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)
{
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 (MCNameIsEqualTo(p_field, t_resolved_typeinfo -> record . fields[i] . name))
{
r_value = self -> fields[i];
return true;
}
if (p_typeinfo -> record . base != kMCNullTypeInfo)
return __fetch_value(p_typeinfo -> record . base, self, p_field, r_value);
return false;
}
MC_DLLEXPORT_DEF
bool MCRecordFetchValue(MCRecordRef self, MCNameRef p_field, MCValueRef& r_value)
{
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 (MCNameIsEqualTo(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;
}
if (p_typeinfo -> record . base != kMCNullTypeInfo)
return __store_value(p_typeinfo -> record . base, self, p_field, p_value);
return false;
}
MC_DLLEXPORT_DEF
bool MCRecordStoreValue(MCRecordRef self, MCNameRef p_field, MCValueRef p_value)
{
return __store_value(self -> typeinfo, self, p_field, p_value);
}
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF bool
MCRecordCopyAsBaseType(MCRecordRef self,
MCTypeInfoRef p_base_typeinfo,
MCRecordRef & r_new_record)
{
bool t_success;
MCValueRetain(self);
t_success = MCRecordCopyAsBaseTypeAndRelease(self,
p_base_typeinfo,
r_new_record);
if (!t_success) MCValueRelease(self);
return t_success;
}
MC_DLLEXPORT_DEF bool
MCRecordCopyAsBaseTypeAndRelease(MCRecordRef self,
MCTypeInfoRef p_base_typeinfo,
MCRecordRef & r_new_record)
{
MCAssert(MCRecordTypeInfoIsDerivedFrom(self -> typeinfo, p_base_typeinfo));
/* If there's only one reference, just swap the typeinfo out and
* make it immutable */
if (self -> references == 1)
{
MCValueRelease(self -> typeinfo);
self -> typeinfo = MCValueRetain(p_base_typeinfo);
self -> flags &= ~kMCRecordFlagIsMutable;
r_new_record = self;
return true;
}
if (!MCRecordCreate(p_base_typeinfo, self -> fields,
MCRecordTypeInfoGetFieldCount (p_base_typeinfo),
r_new_record))
return false;
MCValueRelease(self);
return true;
}
MC_DLLEXPORT_DEF bool
MCRecordCopyAsDerivedType(MCRecordRef self,
MCTypeInfoRef p_derived_typeinfo,
MCRecordRef & r_new_record)
{
bool t_success;
MCValueRetain(self);
t_success = MCRecordCopyAsDerivedTypeAndRelease(self,
p_derived_typeinfo,
r_new_record);
if (!t_success) MCValueRelease(self);
return t_success;
}
MC_DLLEXPORT_DEF bool
MCRecordCopyAsDerivedTypeAndRelease(MCRecordRef self,
MCTypeInfoRef p_derived_typeinfo,
MCRecordRef & r_new_record)
{
MCAssert(MCRecordTypeInfoIsDerivedFrom(p_derived_typeinfo, self -> typeinfo));
uindex_t t_field_count, t_new_field_count;
t_field_count = MCRecordTypeInfoGetFieldCount(self -> typeinfo);
t_new_field_count = MCRecordTypeInfoGetFieldCount(p_derived_typeinfo);
/* If there's only one reference, then we need to: 1) swap the
* typeinfo, 2) resize the field array and fill the new fields
* with null values, and 3) make the record immutable. */
if (self -> references == 1)
{
/* Resize the array */
if (!MCMemoryResizeArray(t_new_field_count, self -> fields, t_field_count))
return false;
/* Clear new values */
for (uindex_t i = t_field_count; i < t_new_field_count; ++i)
self -> fields[i] = MCValueRetain(kMCNull);
/* Set the typeinfo and make immutable */
MCValueRelease(self -> typeinfo);
self -> typeinfo = MCValueRetain(p_derived_typeinfo);
self -> flags &= ~kMCRecordFlagIsMutable;
r_new_record = self;
return true;
}
/* Otherwise, create and manually populate the new array */
MCRecordRef t_result;
if (!MCRecordCreateMutable(p_derived_typeinfo, t_result))
return false;
for (uindex_t i = 0; i < t_field_count; ++i)
t_result -> fields[i] = MCValueRetain(self -> fields[i]);
return MCRecordCopyAndRelease(t_result, r_new_record);
}
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF bool
MCRecordEncodeAsArray(MCRecordRef record,
MCArrayRef & r_array)
{
MCAssert (record != nil);
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)
{
/* 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;
hash_t t_typeinfo_hash;
t_typeinfo_hash = MCHashPointer(self -> typeinfo);
t_hash = MCHashBytesStream(t_hash, &t_typeinfo_hash, sizeof(hash_t));
for(uindex_t i = 0; i < __MCRecordTypeInfoGetFieldCount(t_resolved_typeinfo); i++)
{
hash_t t_element_hash;
t_element_hash = MCValueHash(self -> fields[i]);
t_hash = MCHashBytesStream(t_hash, &t_element_hash, sizeof(hash_t));
}
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)
{
}
////////////////////////////////////////////////////////////////////////////////