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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
|
/*
* Copyright (c) 2017 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file parc_Object.h
* @ingroup memory
* @brief Reference Counted Object Memory
*
* An arbitrary C structure stored in allocated memory with a reference counter.
*
* When a PARC Object is created, via `parcObject_Create`, or `parcObject_Copy` it has reference count of 1.
*
* References to a PARC Object are acquired by calling `parcObject_Acquire`
* and once a reference is no longer needed it is released via parcObject_Release`.
* When the last reference is released, the memory storing the PARC Object is deallocated.
* Any further reference to that object or its memory is undefined.
*
* When creating a PARCObject, the caller may supply an instance of `PARCObjectDescriptor` containing
* configuration information used and pointers to functions that are invoked during the lifecycle of the PARC Object.
* Notable functions that are a finalization cleanup function for an object that will be deallocated,
* a clone function for an object that is being cloned,
* and a string generator for an object that is implementing the `ToString` function.
* Implementors of modules that use PARCObject supply a specification of callback
* functions that implement specific behaviours for interfaces using PARC Object.
*
*/
#ifndef libparc_parc_Object_h
#define libparc_parc_Object_h
#include <stdint.h>
#include <time.h>
#include <LongBow/runtime.h>
#include <LongBow/longBow_Compiler.h>
#include <parc/algol/parc_CMacro.h>
//#include <parc/algol/parc_JSON.h>
//#include <parc/algol/parc_HashCode.h>
#ifdef PARCLibrary_DISABLE_VALIDATION
# define parcObject_OptionalAssertValid(_instance_)
#else
# define parcObject_OptionalAssertValid(_instance_) parcObject_AssertValid(_instance_)
#endif
//Switch to strong type after subclasses are converted
//typedef struct {int unused;} PARCObject;
typedef void PARCObject;
#include <stdint.h>
#include <stdbool.h>
typedef struct PARCObjectDescriptor PARCObjectDescriptor;
/**
* @define parcObject_DescriptorName(_type)
*
* Compose the token for a subtype specific name for a subtype's `PARCObjectDescriptor`
* which is a parameter to `parcObject_Create`.
*
* For example
* @code
* parcObject_DescriptorName(MyType)
* @endcode
*
* generates the token `MyType_Descriptor`
*
* Implementations should use this macro, rather than hardcode the format in their code.
*/
#define parcObject_DescriptorName(_type) parcCMacro_Cat(_type, _Descriptor)
/**
* @define parcObjectDescriptor_Declaration(_type_)
*
* Create a declaration of a `PARCObjectDescriptor` implementation.
* To define the actual implementation, use `parcObject_Override`
*/
#define parcObjectDescriptor_Declaration(_type_) const PARCObjectDescriptor parcObject_DescriptorName(_type_)
/**
* @define parcObject_Declare(_type_)
*
* Create a declaration of a `PARCObject` implementation.
* This causes the corresponding `typedef` to be defined
* and the global PARCObjectDescriptor corresponding to the declared type.
*/
#define parcObject_Declare(_type_) \
typedef struct _type_ _type_; \
extern parcObjectDescriptor_Declaration(_type_)
#include <parc/algol/parc_HashCode.h>
#include <parc/algol/parc_JSON.h>
/**
* A Function that performs the final cleanup and resource deallocation when
* a PARC Object has no more references.
*
* This is deprecated and will be removed.
* Use `PARCObjectDestructor`
*/
typedef void (PARCObjectDestroy)(PARCObject **);
/**
* A Function that performs the final cleanup and resource deallocation when
* a PARC Object has no more references.
*
* If the function returns `true` the object is automatically deallocated and destroyed.
* If the function returns `false` the object is not automatically deallocated and destroyed,
* and the responsibility for the object's state and memory are the responsibility of
* the `PARCObjectDestructor` function.
*/
typedef bool (PARCObjectDestructor)(PARCObject **);
/**
* A Function that releases one reference to the given PARC Object.
* On the final release, where the number of references becomes 0,
* the PARCObjectDestroy function is invoked.
*/
typedef void (PARCObjectRelease)(PARCObject **objectPointer);
/**
* A function that produces a deep-copy of the given PARC Object instance.
*/
typedef PARCObject *(PARCObjectCopy)(const PARCObject *object);
/**
* A function that invokes the proper _Equals() function for two PARC Object instances.
*/
typedef bool (PARCObjectEquals)(const PARCObject *objectX, const PARCObject *objectY);
/**
* A function that invokes the proper _Compare() functions for two PARC Object instances.
*/
typedef int (PARCObjectCompare)(const PARCObject *, const PARCObject *);
/**
* A function that computes the 32-bit hashcode of the given PARC Object instance.
*/
typedef PARCHashCode (PARCObjectHashCode)(const PARCObject *object);
/**
* A function that produces a C style nul-terminated string representation of the given PARC Object instance.
*/
typedef char *(PARCObjectToString)(const PARCObject *object);
/**
* A function that displays a human readable representation of the given PARCObject.
*/
typedef void (PARCObjectDisplay)(const PARCObject *object, const int indentation);
/**
* A function that generates a JSON representation of the given PARC Object instance.
*/
typedef PARCJSON *(PARCObjectToJSON)(const PARCObject *);
/**
* Every PARCObject descriptor has a pointer to a `PARCObjectState`
* containing arbitrary data related to all instances sharing the descriptor.
*/
typedef void PARCObjectTypeState;
/**
* Every PARC Object instance contains a pointer to an instance of this structure defining
* the canonical meta-data for the object.
*/
struct PARCObjectDescriptor {
char name[64];
PARCObjectDestroy *destroy;
PARCObjectDestructor *destructor;
PARCObjectRelease *release;
PARCObjectCopy *copy;
PARCObjectToString *toString;
PARCObjectEquals *equals;
PARCObjectCompare *compare;
PARCObjectHashCode *hashCode;
PARCObjectToJSON *toJSON;
PARCObjectDisplay *display;
const struct PARCObjectDescriptor *super;
size_t objectSize;
unsigned objectAlignment;
bool isLockable;
PARCObjectTypeState *typeState;
};
/**
* Create an allocated instance of `PARCObjectDescriptor`.
*
* @param [in] name A nul-terminated, C string containing the name of the object descriptor.
* @param [in] objectSize The number of bytes necessary to contain the object.
* @param [in] objectAlignment The alignment boundary necessary for the object, a power of 2 greater than or equal to `sizeof(void *)`
* @param [in] isLockable True, if this object implementation supports locking.
* @param [in] destructor The callback function to call when the last `parcObject_Release()` is invoked (replaces @p destroy).
* @param [in] release The callback function to call when `parcObject_Release()` is invoked.
* @param [in] copy The callback function to call when parcObject_Copy() is invoked.
* @param [in] toString The callback function to call when `parcObject_ToString()` is invoked.
* @param [in] equals The callback function to call when `parcObject_Equals()` is invoked.
* @param [in] compare The callback function to call when `parcObject_Compare()` is invoked.
* @param [in] hashCode The callback function to call when `parcObject_HashCode()` is invoked.
* @param [in] toJSON The callback function to call when `parcObject_ToJSON()` is invoked.
* @param [in] display The callback function to call when `parcObject_Display()` is invoked.
* @param [in] super A pointer to a `PARCObjectDescriptor` for the supertype of created `PARCObjectDescriptor`
* @param [in] typeState A pointer to a `PARCObjectTypeState` for the per-type data for the created `PARCObjectDescriptor`
*
* @return NULL Memory could not be allocated to store the `PARCObjectDescriptor` instance.
* @return non-NULL Successfully created the implementation
*/
PARCObjectDescriptor *parcObjectDescriptor_Create(const char *name,
size_t objectSize,
unsigned int objectAlignment,
bool isLockable,
PARCObjectDestructor *destructor,
PARCObjectRelease *release,
PARCObjectCopy *copy,
PARCObjectToString *toString,
PARCObjectEquals *equals,
PARCObjectCompare *compare,
PARCObjectHashCode *hashCode,
PARCObjectToJSON *toJSON,
PARCObjectDisplay *display,
const PARCObjectDescriptor *superType,
PARCObjectTypeState *typeState);
PARCObjectDescriptor *parcObjectDescriptor_CreateExtension(const PARCObjectDescriptor *superType, const char *name);
PARCObjectTypeState *parcObjectDescriptor_GetTypeState(const PARCObjectDescriptor *descriptor);
const PARCObjectDescriptor *parcObjectDescriptor_GetSuperType(const PARCObjectDescriptor *descriptor);
bool parcObjectDescriptor_Destroy(PARCObjectDescriptor **descriptorPointer);
/**
* The globally available `PARCObject` descriptor.
*/
extern parcObjectDescriptor_Declaration(PARCObject);
typedef uint64_t PARCReferenceCount;
/**
* Assert that an instance of PARC Object is valid.
*
* If the instance is not valid, terminate via an assertion.
*
* Valid means the internal state of the type is consistent with its
* required current or future behaviour.
* This may include the validation of internal instances of types.
*
* @param [in] object A pointer to a PARC Object instance.
*/
void parcObject_AssertValid(const PARCObject *object);
/**
* Determine if an instance of PARCObject is valid.
*
* A valid PARCObject has non-NULL, it has a reference count > 0,
* it is non-zero in length, and has a valid alignment.
*
* @param [in] object A pointer to a PARC Object instance.
*
* @return true The PARCObject is valid.
* @return true The PARCObject is invalid.
*/
bool parcObject_IsValid(const PARCObject *object);
/**
* Create a new reference counted object that is a deep copy of the specified object,
* if possible, or, otherwise, a shallow copy of the object's total allocation memory.
*
* The reference count for the new object is 1.
*
* @param [in] object A pointer to the original object.
*
* @return NULL Memory could not be allocated.
* @return non-NULL A pointer to a new reference counted object.
*
* Example:
* @code
* {
* struct MyType *t = parcObject_Create(sizeof(struct MyType), &MyType_Descriptor);
*
* struct MyType *copy = parcObject_Copy(t);
* }
* @endcode
*
* @see parcObject_Release
*/
PARCObject *parcObject_Copy(const PARCObject *object);
/**
* Compare two object instances.
*
* The comparison function in the first `PARCObjectDescriptor` parameter is used for comparison.
* The objects are expected to be of the same type. Thus, if the comparison function
* associated with the first `PARCObjectDescriptor` function is NULL, it is assumed the
* same holds for the second parameter. In this case, the instance pointers are compared.
*
* @param [in] x An object.
* @param [in] y An object.
*
* @return int The result of the comparison.
*
* Example:
* @code
* {
* struct MyType *t = parcObject_Create(sizeof(struct MyType), &MyType_Descriptor);
*
* int compareResult = parcObject_Compare(t, t);
* printf("0? %d\n", compareResult);
*
* parcObject_Release(&t);
* }
* @endcode
*/
int parcObject_Compare(const PARCObject *x, const PARCObject *y);
/**
* Determine if two PARCObject instances are equal.
*
* Two PARCObject instances are equal if, and only if, the instance pointers are equal.
*
* The following equivalence relations on non-null `PARCObject` instances are maintained:
*
* * It is reflexive: for any non-null reference value x, `PARCObject_Equals(x, x)`
* must return true.
*
* * It is symmetric: for any non-null reference values x and y,
* `parcObject_Equals(x, y)` must return true if and only if
* `parcObject_Equals(y, x)` returns true.
*
* * It is transitive: for any non-null reference values x, y, and z, if
* `parcObject_Equals(x, y)` returns true and
* `parcObject_Equals(y, z)` returns true,
* then `parcObject_Equals(x, z)` must return true.
*
* * It is consistent: for any non-null reference values x and y, multiple
* invocations of `parcObject_Equals(x, y)` consistently return true or
* consistently return false.
*
* * For any non-null reference value x, `parcObject_Equals(x, NULL)` must
* return false.
*
* @param x A pointer to a `PARCObject` instance.
* @param y A pointer to a `PARCObject` instance.
* @return true if the two `PARCObject` instances are equal.
*
* Example:
* @code
* {
* struct MyType *a = parcObject_Create(sizeof(struct MyType), &MyType_Descriptor);
* struct MyType *b = parcObject_Create(sizeof(struct MyType), &MyType_Descriptor);
*
* if (parcObject_Equals(a, b)) {
* // true
* } else {
* // false
* }
* }
* @endcode
*/
bool parcObject_Equals(const PARCObject *x, const PARCObject *y);
/**
* Retrieve the hashcode of the given object.
*
* If no object implementation is provided, the hashcode is the 32-bit address
* of the base object pointer. Otherwise, the hashcode is computed by the
* provided hashcode function.
*
* @param [in] object An object.
*
* @return uint32_t The object hashcode
*
* Example:
* @code
* {
* struct MyType *t = parcObject_Create(sizeof(struct MyType), &MyType_Descriptor);
*
* PARCHashCode hashcode = parcObject_HashCode(t);
* printf("Hashcode = %" PRIXPARCHashCode "\n", hashcode);
*
* parcObject_Release(&t);
* }
* @endcode
*/
PARCHashCode parcObject_HashCode(const PARCObject *object);
/**
* Create a C string containing a human readable representation of the given object.
*
* @param [in] object The object from which a string representation will be generated.
*
* @return NULL Memory could not be allocated to contain the C string.
* @return non-NULL An allocated C string that must be deallocated via parcMemory_Deallocate().
*
* Example:
* @code
* {
* struct MyType *t = parcObject_Create(sizeof(struct MyType), &MyType_Descriptor);
*
* char *string = parcObject_ToString(t);
* printf("%s\n", string);
* parcMemory_Deallocate((void **) &string);
*
* parcObject_Release(&t);
* }
* @endcode
*/
char *parcObject_ToString(const PARCObject *object);
/**
* Create a `PARCJSON` instance (representation) of the given object.
*
* @param [in] object The object from which a JSON instance will be generated.
*
* @return NULL Memory could not be allocated to contain the `PARCJSON` instance.
* @return non-NULL An allocated C string that must be deallocated via parcMemory_Deallocate().
*
* Example:
* @code
* {
* struct MyType *t = parcObject_Create(sizeof(struct MyType), &MyType_Descriptor);
*
* PARCJSON *json = parcObject_ToJSON(t);
* printf("JSON representation: %s\n", parcJSON_ToString(json));
* parcJSON_Release(&json);
*
* parcObject_Release(&t);
* }
* @endcode
*/
PARCJSON *parcObject_ToJSON(const PARCObject *object);
/**
* Acquire a new reference to an object.
*
* The reference count to the object is incremented.
*
* @param [in] object The object to which to refer.
*
* @return The same value as the input parameter @p object
*
* Example:
* @code
* {
* struct MyType *t = parcObject_Create(sizeof(struct MyType), &MyType_Descriptor);
*
* struct MyType *new = parcObject_Acquire(t);
*
* parcObject_Release(&t);
* parcObject_Release(&new);
* }
* @endcode
*/
PARCObject *parcObject_Acquire(const PARCObject *object);
/**
* Release a previously acquired reference to the specified instance,
* decrementing the reference count for the instance.
*
* The pointer to the instance is set to NULL as a side-effect of this function.
*
* If an invocation of `parcObject_Release` causes the last reference to
* the instance to be released, it calls the instance's `destructor` function
* specified in the `PARCObjectDescriptor` structure supplied when the instance
* was created (see `parcObject_Create`.
*
* The contents of the deallocated memory used for the PARC object are undefined.
* Do not reference the object after the last release.
*
* @param [in] objectPointer A pointer to a pointer to the instance to release.
*
* @return The number of remaining references to the object.
*
* Example:
* @code
* {
* struct MyType *t = parcObject_Create(sizeof(struct MyType), &MyType_Descriptor);
*
* parcObject_Release(&t);
* }
* @endcode
*
* @see parcObject_Create
* @see parcObject_Acquire
*/
PARCReferenceCount parcObject_Release(PARCObject **objectPointer);
/**
* Get the current `PARCReferenceCount` for the specified object.
*
* The reference count must always be greater than zero.
*
* @param [in] object A pointer to a valid `PARCObject` instance.
*
* @return The current reference count for the specified object.
*
* Example:
* @code
* {
* struct MyType *t = parcObject_Create(sizeof(struct MyType), &MyType_Descriptor);
*
* PARCReferenceCount count = parcObject_GetReferenceCount(t);
*
* parcObject_Release(&t);
* }
* @endcode
*
* @see parcObject_Acquire
* @see parcObject_Release
*/
PARCReferenceCount parcObject_GetReferenceCount(const PARCObject *object);
/**
* Print a human readable representation of the given `PARC Object.
*
* @param [in] object A pointer to the instance to display.
* @param [in] indentation The level of indentation to use to pretty-print the output.
*
* Example:
* @code
* {
* struct MyType *t = parcObject_Create(sizeof(struct MyType), &MyType_Descriptor);
*
* parcObject_Display(t, 0);
*
* parcObject_Release(&t);
* }
* @endcode
*/
void parcObject_Display(const PARCObject *object, const int indentation);
/**
* Get the `PARCObjectDescriptor` of the given `PARCObject`.
*
* @param [in] object A pointer to a valid `PARCObject` instance
*
* @return A pointer to the given PARCObject's `PARCObjectDescriptor`.
*
* Example:
* @code
* {
* struct MyType *t = parcObject_Create(sizeof(struct MyType), &MyType_Descriptor);
*
* PARCObjectDescriptor *descriptor = parcObject_GetDescriptor(t);
*
* parcObject_Release(&t);
* }
* @endcode
*/
const PARCObjectDescriptor *parcObject_GetDescriptor(const PARCObject *object);
/**
* Set the `PARCObjectDescriptor` of the given `PARCObject`.
*
* @param [in] object A pointer to a valid `PARCObject` instance
* @param [in] objectType A pointer to a valid `PARCObjectDescriptor` instance
*
* @return The previous value of the given PARCObject's `PARCObjectDescriptor`.
*/
const PARCObjectDescriptor *parcObject_SetDescriptor(PARCObject *object, const PARCObjectDescriptor *objectType);
/**
* @def parcObject_MetaInitialize
* @deprecated Use parcObject_Override instead;
*
* Initialize a PARCObjectDescriptor structure. Every function pointer is set to NULL.
*
* @param [in] objectType A pointer to the PARCObjectDescriptor structure to initialize.
*
* @return The pointer to the initialized PARCObjectDescriptor.
*/
#define parcObject_MetaInitialize(objectType) \
(objectType)->destructor = NULL, \
(objectType)->destroy = NULL, \
(objectType)->release = NULL, \
(objectType)->copy = NULL, \
(objectType)->toString = NULL, \
(objectType)->equals = NULL, \
(objectType)->compare = NULL, \
(objectType)->hashCode = NULL, \
(objectType)->toJSON = NULL, \
(objectType)->display = NULL, \
(objectType)->super = NULL
/**
* Create new `PARCObjectDescriptor` based on an existing `PARCObjectDescriptor.`
* The new `PARCObjectDescriptor` uses the existing `PARCObjectDescriptor` as the super-type of the new descriptor.
*/
#define parcObject_Extends(_subtype, _superType, ...) \
LongBowCompiler_IgnoreInitializerOverrides \
parcObjectDescriptor_Declaration(_subtype) = { \
.super = &parcObject_DescriptorName(_superType), \
.name = #_subtype, \
.objectSize = 0, \
.objectAlignment = 0, \
.destroy = NULL, \
.destructor = NULL, \
.release = NULL, \
.copy = NULL, \
.toString = NULL, \
.equals = NULL, \
.compare = NULL, \
.hashCode = NULL, \
.toJSON = NULL, \
.display = NULL, \
.isLockable = true, \
.typeState = NULL, \
__VA_ARGS__ \
}; \
LongBowCompiler_WarnInitializerOverrides \
const PARCObjectDescriptor parcObject_DescriptorName(_subtype)
/**
* Define a new PARC Object implementation, by composing a new PARC Object Descriptor referencing an old one.
* The new PARC Object implementation must be accompanied by the corresponding `typedef` of the type containing the object's data.
*/
#define parcObject_Override(_subtype, _superType, ...) \
parcObject_Extends(_subtype, _superType, \
.objectSize = sizeof(_subtype), \
.objectAlignment = sizeof(void *), \
__VA_ARGS__)
/// Helper MACROS for PARCObject Normalization
/** \cond */
/**
* parcObject_DestroyWrapper builds the boiler plate wrapper for PARCObject type conversion in the
* destroy operation. Intended for internal use.
*/
#define parcObject_DestroyWrapper(_type, _fname) \
static void _autowrap_destroy_ ## _type(PARCObject **object) \
{ \
_fname((_type **) object); \
}
/**
* parcObject_DestructorWrapper builds the boiler plate wrapper for PARCObject type conversion in the
* destroy operation. Intended for internal use.
*/
#define parcObject_DestructorWrapper(_type, _fname) \
static bool _autowrap_destructor_ ## _type(PARCObject **object) \
{ \
return _fname((_type **) object); \
}
/**
* `parcObject_CopyWrapper` builds the boiler plate wrapper for PARCObject type conversion in the
* copy operation. Intended for internal use.
*/
#define parcObject_CopyWrapper(_type, _fname) \
static PARCObject *_autowrap_copy_ ## _type(const PARCObject *object) \
{ \
return (PARCObject *) _fname((const _type *) object); \
}
/**
* parcObject_ToStringWrapper builds the boiler plate wrapper for PARCObject type conversion in the
* ToString operation. Intended for internal use.
*/
#define parcObject_ToStringWrapper(_type, _fname) \
static char *_autowrap_toString_ ## _type(const PARCObject *object) \
{ \
return _fname((const _type *) object); \
}
/**
* `parcObject_EqualsWrapper` builds the boiler plate wrapper for PARCObject type conversion in the
* equals operation. Intended for internal use.
*/
#define parcObject_EqualsWrapper(_type, _fname) \
static bool _autowrap_equals_ ## _type(const PARCObject *a, const PARCObject *b) \
{ \
return _fname((const _type *) a, (const _type *) b); \
}
/**
* parcObject_CompareWrapper builds the boiler plate wrapper for PARCObject type conversion in the
* compare operation. Intended for internal use.
*/
#define parcObject_CompareWrapper(_type, _fname) \
static int _autowrap_compare_ ## _type(const PARCObject *a, const PARCObject *b) \
{ \
return _fname((const _type *) a, (const _type *) b); \
}
/**
* `parcObject_HashCodeWrapper` builds the boiler plate wrapper for PARCObject type conversion in the
* HashCode operation. Intended for internal use.
*/
#define parcObject_HashCodeWrapper(_type, _fname) \
static PARCHashCode _autowrap_hashCode_ ## _type(const PARCObject *object) \
{ \
return _fname((const _type *) object); \
}
/**
* `parcObject_CopyWrapper` builds the boiler plate wrapper for PARCObject type conversion in the
* ToJSON operation. Intended for internal use.
*/
#define parcObject_ToJSONWrapper(_type, _fname) \
static PARCJSON *_autowrap_toJSON_ ## _type(const PARCObject *object) \
{ \
return _fname((const _type *) object); \
}
/**
* `parcObject_DisplayWrapper` builds the boiler plate wrapper for PARCObject type conversion in the
* Display operation. Intended for internal use.
*/
#define parcObject_DisplayWrapper(_type, _fname) \
static void _autowrap_Display_ ## _type(const PARCObject *object, const int indentation) \
{ \
_fname((const _type *) object, indentation); \
}
/**
* _autowrap_NULL is a part of the c-macro trick for implement a macro If-Else switch.
* If included as a macro parameter it inserts a comma into the parameter list for that macro.
* This can be used by a switching macro that always resolves to the nth element and the
* presence of a comma generating macro changes which element is the nth. When NULL is used
* as a parameter in a call to "ExtendPARCObject", _autowrap_NULL will be the name generated which
* expands to a comma.
*/
#define _autowrap_NULL(...) ,
/** \endcond */
/**
* @define parcObject_ExtendPARCObject
* @deprecated Use parcObject_Override instead;
*
* @discussion parcObject_ExtendPARCObject is a helper macro for constructing a PARCObjectDescriptor Structure of
* function pointers pointing to a subtype's overriding functions. This struct serves the same
* purpose as a vTable in c++ and provides for simple polymorphism. The functions used as parameters
* should NOT call through to the parcObject function they override as this will result in an infinite loop.
* NULL should be used for functions where PARCObject's default implementation is desired.
*
* Note: It uses the parcCMacro_IfElse trickery to handle NULL parameters.
*
* @param [in] _destroy A pointer to the Destroy callback function.
* @param [in] _copy A pointer to the Copy callback function.
* @param [in] _toString A pointer to the ToString callback function.
* @param [in] _equals A pointer to the Equals callback function.
* @param [in] _compare A pointer to the Compare callback function.
* @param [in] _hashCode A pointer to the HashCode callback function.
* @param [in] _toJSON A pointer to the ToJSON callback function.
*/
#define parcObject_ExtendPARCObject(_type, _destroy, _copy, _toString, _equals, _compare, _hashCode, _toJSON) \
parcCMacro_IfElse(, parcObject_DestroyWrapper(_type, _destroy), _autowrap_ ## _destroy()) \
parcCMacro_IfElse(, parcObject_CopyWrapper(_type, _copy), _autowrap_ ## _copy()) \
parcCMacro_IfElse(, parcObject_ToStringWrapper(_type, _toString), _autowrap_ ## _toString()) \
parcCMacro_IfElse(, parcObject_EqualsWrapper(_type, _equals), _autowrap_ ## _equals()) \
parcCMacro_IfElse(, parcObject_CompareWrapper(_type, _compare), _autowrap_ ## _compare()) \
parcCMacro_IfElse(, parcObject_HashCodeWrapper(_type, _hashCode), _autowrap_ ## _hashCode()) \
parcCMacro_IfElse(, parcObject_ToJSONWrapper(_type, _toJSON), _autowrap_ ## _toJSON()) \
parcObject_Override(_type, PARCObject, \
.destroy = parcCMacro_IfElse(NULL, _autowrap_destroy_ ## _type, _autowrap_ ## _destroy()), \
.destructor = NULL, \
.release = NULL, \
.copy = parcCMacro_IfElse(NULL, _autowrap_copy_ ## _type, _autowrap_ ## _copy()), \
.toString = parcCMacro_IfElse(NULL, _autowrap_toString_ ## _type, _autowrap_ ## _toString()), \
.equals = parcCMacro_IfElse(NULL, _autowrap_equals_ ## _type, _autowrap_ ## _equals()), \
.compare = parcCMacro_IfElse(NULL, _autowrap_compare_ ## _type, _autowrap_ ## _compare()), \
.hashCode = parcCMacro_IfElse(NULL, _autowrap_hashCode_ ## _type, _autowrap_ ## _hashCode()), \
.toJSON = parcCMacro_IfElse(NULL, _autowrap_toJSON_ ## _type, _autowrap_ ## _toJSON()), \
.display = NULL)
/**
* @define parcObject_CreateInstance
*
* `parcObject_CreateInstance` is a helper C-macro that creates an instance of a PARCObject subtype
* using `parcObject_CreateInstanceImpl` that is based on the PARCObjectDescriptor.
*
* @param [in] _subtype A subtype's type string (e.g. PARCBuffer)
*/
#define parcObject_CreateInstance(_subtype) \
parcObject_CreateInstanceImpl(&parcObject_DescriptorName(_subtype))
PARCObject *parcObject_CreateInstanceImpl(const PARCObjectDescriptor *descriptor);
/**
* @define parcObject_CreateAndClearInstance
*
* parcObject_CreateAndClearInstance is a helper C-macro that creates an instance of a PARCObject subtype
* using parcObject_CreateAndClear that is based on the PARCObjectDescriptor struct created by the
* parcObject_ExtendPARCObject macro.
*
* @param [in] _subtype A subtype's type string (e.g. PARCBuffer)
*/
#define parcObject_CreateAndClearInstance(_subtype) \
(_subtype *) parcObject_CreateAndClearInstanceImpl(&parcObject_DescriptorName(_subtype))
/**
* Create a reference counted segment of memory of at least @p objectLength long.
*
* The implementation pointer, is either NULL or points to a valid `PARCObjectDescriptor` structure
* containing the callback functions that implement the object's life-cycle operations.
*
* The allocated memory is such that the memory's base address is aligned on a sizeof(void *) boundary,
* and filled with zero bytes.
*
* If memory cannot be allocated, `errno` is set to ENOMEM.
*
* @param [in] descriptor A pointer to a valid `PARCObjectDescriptor` structure.
*
* @return NULL The memory could not be allocated.
* @return non-NULL A pointer to reference counted memory of at least length bytes.
*
* Example:
* @code
* {
* struct timeval *t = parcObject_CreateAndClearInstanceImpl(sizeof(struct timeval), &PARCObject_Descriptor);
* }
* @endcode
*
* @see PARCObjectDescriptor
* @see parcObject_Create
*/
PARCObject *parcObject_CreateAndClearInstanceImpl(const PARCObjectDescriptor *descriptor);
/**
* Define a static PARCObject instance for the given type, alignment, per-object data.
*
* Once the instance has been defined, it must be initialised via `parcObject_InitInstance`
* or `parcObject_InitAndClearInstance` before it is used.
*
* @return A pointer to an invalid `PARCObject` instance that must be initialised .
*/
#define parcObject_Instance(_type_, _alignment_, _size_) \
(_type_ *) (& (char[parcObject_TotalSize(_alignment_, _size_)]) { 0 }[parcObject_PrefixLength(sizeof(void *))])
/**
* @define parcObject_InitInstance
*
* `parcObject_InitInstance` is a helper C-macro that initializes a portion of memory to contain a `PARCObject` subtype
* using `parcObject_InitInstanceImpl`.
*
* @param [in] _object_ A pointer to memory that will contain the object and its meta-data.
* @param [in] _subtype A subtype's type name.
*/
#define parcObject_InitInstance(_object_, _subtype) \
parcObject_InitInstanceImpl(_object_, &parcObject_DescriptorName(_subtype))
/**
* @define parcObject_InitInstanceImpl
*
* Initialize a PARCObject instance given the `PARCObjectDescriptor`.
* Any previous state of the given PARCObject is destroyed.
*
* @param [in] object A pointer to an existing valid or invalid `PARCObject` instance.
* @param [in] descriptor A pointer to a valid `PARCObjectDescriptor` structure.
*/
PARCObject *parcObject_InitInstanceImpl(PARCObject *object, const PARCObjectDescriptor *descriptor);
/**
* @define parcObject_InitAndClearInstance
*
* `parcObject_InitAndClearInstance` is a helper C-macro that initializes a portion of memory to contain a PARCObject subtype
* using `parcObject_InitAndClearInstanceImpl`.
*
* @param [in] _object_ A pointer to memory that will contain the object and its meta-data.
* @param [in] _subtype A subtype's type name.
*/
#define parcObject_InitAndClearInstance(_object_, _subtype) \
parcObject_InitAndClearInstanceImpl(_object_, &parcObject_DescriptorName(_subtype))
/**
* Create a reference counted segment of memory of at least @p objectLength long.
*
* The implementation pointer, is either NULL or points to a valid `PARCObjectDescriptor` structure
* containing the callback functions that implement the object's life-cycle operations.
*
* The allocated memory is such that the memory's base address is aligned on a sizeof(void *) boundary,
* and filled with zero bytes.
*
* If memory cannot be allocated, `errno` is set to `ENOMEM`.
*
* @param [in] object A pointer to an existing valid or invalid `PARCObject` instance.
* @param [in] descriptor A pointer to a valid `PARCObjectDescriptor` structure.
*
* @return NULL The memory could not be allocated.
* @return non-NULL A pointer to reference counted memory of at least length bytes.
*
* Example:
* @code
* {
*
* }
* @endcode
*
* @see PARCObjectDescriptor
* @see parcObject_Create
*/
PARCObject *parcObject_InitAndClearInstanceImpl(PARCObject *object, const PARCObjectDescriptor *descriptor);
/**
* Compute the number of bytes necessary for a PARC Object prefix.
*
* The @p _alignment_ parameter specifies the required memory alignment of the object.
*
* @param [in] _alignment_ An unsigned integer value greater than `sizeof(void *)`
*
* @return The number of bytes necessary for a PARC Object header.
*
* Example:
* @code
* {
* <#example#>
* }
* @endcode
*/
// The constant value here must be greater than or equal to the size of the internal _PARCObjectHeader structure.
#define parcObject_PrefixLength(_alignment_) ((152 + (_alignment_ - 1)) & - _alignment_)
/**
* Compute the number of bytes necessary for a PARC Object.
*
* The number of bytes consists of the number of bytes for the PARC Object header, padding, and the object's specific data.
*
* The @p _alignment_ parameter specifies the required memory alignment of the object.
* The @p _size_ parameter specifies the number of bytes necessary for the object specific data.
*/
#define parcObject_TotalSize(_alignment_, _size_) (parcObject_PrefixLength(_alignment_) + _size_)
/**
* Wrap a static, unallocated region of memory producing a valid pointer to a `PARCObject` instance.
*
* Note that the return value will not be equal to the value of @p origin.
*
* @param [in] memory A pointer to memory that will contain the object and its state.
* @param [in] descriptor The subtype name that will be used to compose the name of the `PARCObjectDescriptor` for the object.
*
* @return NULL An error occured.
*
* Example:
* @code
* {
* <#example#>
* }
* @endcode
*/
#define parcObject_Wrap(_memory_, _subtype) \
parcObject_WrapImpl(_memory_, &parcObject_DescriptorName(_subtype))
/**
* Wrap a static, unallocated region of memory producing a valid pointer to a `PARCObject` instance.
*
* Note that the return value will not be equal to the value of @p origin.
*
* @param [in] memory A pointer to memory that will contain the object and its state.
* @param [in] descriptor A pointer to a valid `PARCObjectDescriptor` for the object.
*
* @return NULL An error occured.
*
* Example:
* @code
* {
* <#example#>
* }
* @endcode
*/
PARCObject *parcObject_WrapImpl(void *memory, const PARCObjectDescriptor *descriptor);
/**
* @def parcObject_ImplementAcquire
*
* `parcObject_ImplementAcquire` is a helper C-macro that creates a canonical subtype specific
* Acquire function.
*
* @param [in] _namespace A subtype's namespace string (e.g. parcBuffer)
* @param [in] _type A subtype's type string (e.g. PARCBuffer)
*/
#define parcObject_ImplementAcquire(_namespace, _type) \
_type * _namespace ## _Acquire(const _type * pObject) { \
return (_type *) parcObject_Acquire((PARCObject *) pObject); \
} extern _type *_namespace ## _Acquire(const _type * pObject)
/**
* @def parcObject_ImplementRelease
*
* `parcObject_ImplementRelease` is a helper C-macro that creates a canonical subtype specific
* Release function.
*
* @param [in] _namespace A subtype's namespace string (e.g. parcBuffer)
* @param [in] _type A subtype's type string (e.g. PARCBuffer)
*/
#define parcObject_ImplementRelease(_namespace, _type) \
inline void _namespace ## _Release(_type **pObject) { \
parcObject_Release((PARCObject **) pObject); \
} extern void _namespace ## _Release(_type **pObject)
/**
* `parcObject_ImplementationCheck` is a helper macro that will generate compile time warnings for
* missing canonical functions or canonical functions with faulty signatures.
*
* @param _namespace A subtype's namespace string (e.g. parcBuffer)
* @param _type A subtype's type string (e.g. PARCBuffer)
*/
#define parcObject_ImplementationCheck(_namespace, _type) \
static void \
_impl_check() { \
_type *po; \
const _type co; \
const _type *pco; \
pco = _namespace ## _Copy(&co); \
pco = _namespace ## _Acquire(&co); \
pco = pco; \
_namespace ## _Release(&po); \
bool b = _namespace ## _Equals(&co, &co); \
b = b; \
int i = _namespace ## _Compare(&co, &co); \
i = i; \
char *pc = _namespace ## _ToString(&co); \
pc = pc; \
uint32_t ui = _namespace ## _HashCode(&co); \
ui = ui; \
PARCJSON *pj = _namespace ## _ToJSON(&co); \
pj = pj; \
} typedef void parcCMacro_Cat (_type, _IC_NOOP)
/**
* Obtain the lock on the given `PARCObject` instance.
*
* If the lock is already held by another thread, this function will block.
* If the lock is aleady held by the current thread, this function will return `false`.
*
* Implementors must avoid deadlock by attempting to lock the object a second time within the same calling thread.
*
* @param [in] object A pointer to a valid `PARCObject` instance.
*
* @return true The lock was obtained successfully.
* @return false The lock is already held by the current thread, the `PARCObject` is invalid, or does not support locking.
*
* Example:
* @code
* {
* if (parcObject_Lock(object)) {
*
* }
* }
* @endcode
*/
bool parcObject_Lock(const PARCObject *object);
/**
* @def parcObject_ImplementLock
*
* `parcObject_ImplementLock` is a helper C-macro that defines a static, inline facade for the `parcObject_Lock` function.
*
* @param [in] _namespace A subtype's namespace string (e.g. `parcBuffer`)
* @param [in] _type A subtype's type string (e.g. `PARCBuffer`)
*/
#define parcObject_ImplementLock(_namespace, _type) \
static inline bool _namespace ## _Lock(const _type * pObject) { \
return parcObject_Lock((PARCObject *) pObject); \
} typedef void parcCMacro_Cat (_type, _Lock_NOOP)
/**
* Try to obtain the advisory lock on the given `PARCObject` instance.
*
* Once the lock is obtained, the caller must release the lock via `parcObject_Unlock`.
*
* @param [in] object A pointer to a valid `PARCObject` instance.
*
* @return true The `PARCObject` is locked.
* @return false The `PARCObject` is unlocked, or does not support locking.
*
* Example:
* @code
* {
* while (parcObject_TryLock(object))
* ;
* }
* @endcode
*/
bool parcObject_TryLock(const PARCObject *object);
/**
* @def parcObject_ImplementTryLock
*
* `parcObject_ImplementTryLock` is a helper C-macro that defines a static, inline facade for the `parcObject_TryLock` function.
*
* @param [in] _namespace A subtype's namespace string (e.g. `parcBuffer`)
* @param [in] _type A subtype's type string (e.g. `PARCBuffer`)
*/
#define parcObject_ImplementTryLock(_namespace, _type) \
static inline bool _namespace ## _TryLock(const _type * pObject) { \
return parcObject_TryLock((PARCObject *) pObject); \
} typedef void parcCMacro_Cat (_type, _TryLock_NOOP)
/**
* Try to unlock the advisory lock on the given `PARCObject` instance.
*
* @param [in] object A pointer to a valid `PARCObject` instance.
*
* @return true The `PARCObject` was locked and now is unlocked.
* @return false The `PARCObject` was not locked and remains unlocked.
*
* Example:
* @code
* {
* if (parcObject_Lock(Object)) {
* parcObject_Unlock(object);
* }
* }
* @endcode
*/
bool parcObject_Unlock(const PARCObject *object);
/**
* @def parcObject_ImplementUnlock
*
* `parcObject_ImplementUnlock` is a helper C-macro that defines a static, inline facade for the `parcObject_Unlock` function.
*
* @param [in] _namespace A subtype's namespace string (e.g. `parcBuffer`)
* @param [in] _type A subtype's type string (e.g. `PARCBuffer`)
*/
#define parcObject_ImplementUnlock(_namespace, _type) \
static inline bool _namespace ## _Unlock(const _type * pObject) { \
return parcObject_Unlock((PARCObject *) pObject); \
} typedef void parcCMacro_Cat (_type, _Unlock_NOOP)
/**
* Determine if the advisory lock on the given PARCObject instance is locked.
*
* @param [in] object A pointer to a valid PARCObject instance.
*
* @return true The `PARCObject` is locked.
* @return false The `PARCObject` is unlocked.
* Example:
* @code
* {
* if (parcObject_Lock(object)) {
* ...
* if (parcObject_IsLocked(object) {
* ....
* }
* ...
* parcObject_Unlock(object);
* }
* }
* @endcode
*/
bool parcObject_IsLocked(const PARCObject *object);
/**
* @def parcObject_ImplementIsLocked
*
* parcObject_ImplementIsLocked is a helper C-macro that defines a static, inline facade for the `parcObject_IsLocked` function.
*
* @param [in] _namespace A subtype's namespace string (e.g. `parcBuffer`)
* @param [in] _type A subtype's type string (e.g. `PARCBuffer`)
*/
#define parcObject_ImplementIsLocked(_namespace, _type) \
static inline bool _namespace ## _IsLocked(const _type * pObject) { \
return parcObject_IsLocked((const PARCObject *) pObject); \
} typedef void parcCMacro_Cat (_type, _IsLocked_NOOP)
/**
* Causes the calling thread to wait until another thread invokes the `parcObject_Notify()` function on the same object.
*
* The calling thread must own this object's lock.
* The calling thread will release ownership of this lock and wait until another thread invokes `parcObject_Notify`
* on the same object. The original calling thread then re-obtains ownership of the lock and resumes execution.
*
* This function must only be called by a thread that is the owner of this object's lock.
*
* @param [in] object A pointer to a valid PARCObject instance.
*
* Example:
* @code
* {
* if (parcObject_Lock(object)) {
* ...
* if (parcObject_Wait(object) {
* ....
* }
* ...
* parcObject_Unlock(object);
* }
* }
* @endcode
*/
void parcObject_Wait(const PARCObject *object);
/**
* @def parcObject_ImplementWait
*
* parcObject_ImplementWait is a helper C-macro that defines a static, inline facade for the `parcObject_Wait` function.
*
* @param [in] _namespace A subtype's namespace string (e.g. `parcBuffer`)
* @param [in] _type A subtype's type string (e.g. `PARCBuffer`)
* @see parcObject_Wait
*/
#define parcObject_ImplementWait(_namespace, _type) \
static inline void _namespace ## _Wait(const _type * pObject) { \
parcObject_Wait((const PARCObject *) pObject); \
} typedef void parcCMacro_Cat (_type, _Wait_NOOP)
/**
* Causes the calling thread to wait until either another thread invokes the `parcObject_Notify()`
* function on the same object or the system time equals or exceeds the specified time.
*
* The calling thread must own the object's lock.
* The calling thread will release ownership of this lock and wait until another thread invokes
* `parcObject_Notify` or the computer's system time equals or exceeds that specified by @p time.
* on the same object.
* The original calling thread then re-obtains ownership of the lock and resumes execution.
*
* This function must only be called by a thread that is the owner of this object's lock.
*
* @param [in] object A pointer to a valid PARCObject instance.
*
* @returns false if the alloted time was exceeded.
* @returns true if another thread invoked the `parcObject_Notify()` function
*
* Example:
* @code
* {
* struct timeval tv;
* gettimeofday(&tv, NULL);
*
* struct timespec absoluteTime;
* absoluteTime.tv_sec = tv.tv_sec + 0;
* absoluteTime.tv_nsec = 0;
*
* parcObject_WaitUntil(object, &absoluteTime);
* }
* @endcode
*/
bool parcObject_WaitUntil(const PARCObject *object, const struct timespec *time);
/**
* @def parcObject_ImplementWaitUntil
*
* parcObject_ImplementWaitUntil is a helper C-macro that defines a static, inline facade for the
* `parcObject_WaitUntil` function.
*
* @param [in] _namespace A subtype's namespace string (e.g. `parcBuffer`)
* @param [in] _type A subtype's type string (e.g. `PARCBuffer`)
* @see parcObject_WaitUntil
*/
#define parcObject_ImplementWaitUntil(_namespace, _type) \
static inline bool _namespace ## _WaitUntil(const _type * pObject, const struct timespec *time) { \
return parcObject_WaitUntil((const PARCObject *) pObject, time); \
} typedef void parcCMacro_Cat (_type, _WaitUntil_NOOP)
/**
* Causes the calling thread to wait until either another thread invokes the `parcObject_Notify()`
* function on the same object or the given number of nanoseconds elapses.
*
* The calling thread must own the object's lock.
*
* The calling thread will release ownership of its lock and wait until another thread invokes
* `parcObject_Notify` on the same object,
* or the computer's system time equals or exceeds the time specified by the
* time of invocation plus nanoSeconds.
* The original calling thread then re-obtains ownership of the lock and resumes execution.
*
* This function must only be called by a thread that is the owner of this object's lock.
*
* @param [in] object A pointer to a valid PARCObject instance.
* @param [in] nanoSeconds The number of nanoseconds to wait.
*
* @returns false if the allotted time was exceeded.
* @returns true if another thread invoked the `parcObject_Notify()` function
*
* Example:
* @code
* {
* parcObject_WaitFor(object, 1000000000UL);
* }
* @endcode
*/
bool parcObject_WaitFor(const PARCObject *object, const uint64_t nanoSeconds);
/**
* @def parcObject_ImplementWaitUntil
*
* parcObject_ImplementWaitUntil is a helper C-macro that defines a static, inline facade for the
* `parcObject_WaitUntil` function.
*
* @param [in] _namespace A subtype's namespace string (e.g. `parcBuffer`)
* @param [in] _type A subtype's type string (e.g. `PARCBuffer`)
* @see parcObject_WaitUntil
*/
#define parcObject_ImplementWaitFor(_namespace, _type) \
static inline bool _namespace ## _WaitFor(const _type * pObject, const unsigned long nanoSeconds) { \
return parcObject_WaitFor((const PARCObject *) pObject, nanoSeconds); \
} typedef void parcCMacro_Cat (_type, _WaitFor_NOOP)
/**
* Wakes up a single thread that is waiting on this object (see `parcObject_Wait)`.
* If any threads are waiting on this object, one of them is chosen to be awakened.
* The choice is arbitrary and occurs at the discretion of the underlying implementation.
*
* The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object.
* The awakened thread will compete in the usual manner with any other threads that might be actively
* competing to synchronize on this object;
* for example,
* the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
*
* @param [in] object A pointer to a valid `PARCObject` instance.
*
* Example:
* @code
* {
* if (parcObject_Lock(object)) {
* parcObject_Notify(object);
* parcObject_Unlock(object);
* }
* }
* @endcode
*/
void parcObject_Notify(const PARCObject *object);
/**
* @def parcObject_ImplementNotify
*
* parcObject_ImplementNotify is a helper C-macro that defines a static, inline facade for the `parcObject_Notify` function.
*
* @param [in] _namespace A subtype's namespace string (e.g. `parcBuffer`)
* @param [in] _type A subtype's type string (e.g. `PARCBuffer`)
*/
#define parcObject_ImplementNotify(_namespace, _type) \
static inline void _namespace ## _Notify(const _type * pObject) { \
parcObject_Notify((const PARCObject *) pObject); \
} typedef void parcCMacro_Cat (_type, _Notify_NOOP)
/**
* Wakes up all threads that are waiting on the given object's lock.
*
* A thread waits on an object by calling one of the wait methods, `parcObject_Wait`, `parcObject_WaitFor`, `parcObject_WaitUntil`.
* The awakened threads will proceed after the current thread relinquishes the lock on the given object.
* The awakened threads will compete in the usual manner with any other threads that might be actively competing
* to synchronize on this object.
* Awakened threads have no priority between them in being the next thread to lock this object.
*
* This method can only be called by a thread that is the owner of this object's lock.
*
* @param [in] object A pointer to a valid `PARCObject` instance.
*
* Example:
* @code
* {
* if (parcObject_Lock(object)) {
* parcObject_NotifyAll(object);
* parcObject_Unlock(object);
* }
* }
* @endcode
*/
void parcObject_NotifyAll(const PARCObject *object);
/**
* @def parcObject_ImplementNotifyAll
*
* parcObject_ImplementNotifyAll is a helper C-macro that defines a static, inline facade for the `parcObject_NotifyAll` function.
*
* @param [in] _namespace A subtype's namespace string (e.g. `parcBuffer`)
* @param [in] _type A subtype's type (e.g. `PARCBuffer`)
*/
#define parcObject_ImplementNotifyAll(_namespace, _type) \
static inline void _namespace ## _NotifyAll(const _type * pObject) { \
parcObject_NotifyAll((const PARCObject *) pObject); \
} typedef void parcCMacro_Cat (_type, _NotifyAll_NOOP)
/**
* @def parcObject_Mutex
*
* This macro uses the functions `parcObject_Lock` and `parcObject_Unlock`
* to provide a simple syntax for implementing a mutual exclusion region of code.
*
* @param [in] _object_ A pointer to a valid PARCObject that implements locking.
*
* Example:
* @code
* {
* parcObject_Mutex(object) {
* ....
* }
* }
* @endcode
* @see parcObject_Synchronize
*/
#define parcObject_Mutex(_object_) for (bool once = true; once && parcObject_Lock(_object_); parcObject_Unlock(_object_), once = false)
/**
* Determine if a given `PARCObject` is and instance of the specified `PARCObjectDescriptor`.
*
* @param [in] object A pointer to a valid PARCObject instance.
* @param [in] descriptor A pointer to a valid PARCObjectDescriptor instance.
*
* @return true @p object is an instance of @p descriptor.
* @return false @p object is not an instance of @p descriptor.
*/
bool parcObject_IsInstanceOf(const PARCObject *object, const PARCObjectDescriptor *descriptor);
/**
* Atomically set an object's barrier.
*
* If the barrier is not set, the barrier will be set and this function returns `true`.
*
* If the barrier is already set, any subsequent attempt to set the barrier will block until the barrier is unset
* (see `parcObject_BarrierUnset`).
* If there are multiple competitors to set the barrier,
* only one will (indiscriminately) succeed and return and the remaining will continue to attempt to set the barrier.
*
* Barriers can be used in both threaded and non-threaded applications,
* but are not a substitute for thread locking and do not interoperate the wait and notify operations.
*
* Barriers should be used in pairs within the same level of program abstraction to avoid confusion.
* It is possible to set a barrier without ever unsetting the same barrier,
* and as a consequence any other attempt to set the same barrier will hang the program.
*
* @param [in] object A pointer to a valid PARCObject
*
* @return true
*
* Example:
* @code
* {
* parcObject_BarrierSet(object);
*
* ...
*
* parcObject_BarrierUnset(object);
* }
* @endcode
* @see parcObject_BarrierUnset
* @see parcObject_Synchronize
*/
bool parcObject_BarrierSet(const PARCObject *object);
/**
* Unset an objects' barrier.
*
* If a barrier is set (see `parcObject_BarrierSet`), the barrier is unset (see `parcObject_BarrierUnset`).
*
* If a barrier is not set, this function will block until the barrier is set,
* whereupon it will be immediately unset the barrier and return.
*
* If there are multiple competitors attempting to unset the barrier,
* only one will (indiscriminately) succeed and return and the remaining will continue to attempt to unset the barrier.
*
* Barriers are not a substitute for thread locking and do not interoperate the wait and notify operations.
*
*
* @param [in] object A pointer to a valid `PARCObject`
*
* @return false
*
* Example:
* @code
* {
* parcObject_BarrierSet(object);
*
* ...
*
* parcObject_BarrierUnset(object);
* }
* @endcode
*/
bool parcObject_BarrierUnset(const PARCObject *object);
/**
* Synchronize on a `PARCObject` instance to provide a simple mutual exclusion region of code.
*
* This macro uses the functions `parcObject_BarrierSet` and `parcObject_BarrierUnset`
* to provide a simple syntax for implementing a mutual exclusion region of code.
*
* This defines and uses the local variable `_parcObjectSynchronize` which will always appear to be true to the calling function
* and must never be defined by the calling function, or any module.
*
* @param [in] object A pointer to a valid `PARCObject`
*
* Example:
* @code
* {
* parcObject_Synchronize(_object_) {
* // Only one thread executes this code at a single time.
* }
* }
* @endcode
* @see parcObject_Mutex
*/
#define parcObject_Synchronize(_object_) for (bool _parcObjectSynchronize = parcObject_BarrierSet(_object_); _parcObjectSynchronize; _parcObjectSynchronize = parcObject_BarrierUnset(_object_))
#endif // libparc_parc_Object_h
|