| 1 | /* Thing.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2012,2014-2026 Paul Boersma |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <stdarg.h> |
| 20 | #include <time.h> |
| 21 | #include "Thing.h"cxx . . $ `<file>/sys/Thing.h`/ |
| 22 | |
| 23 | stdcxx . . $ std/::atomiccxx . . $ std/atomic# <integercxx . . $ integer#> theTotalNumberOfThingscxx . . $ theTotalNumberOfThings.; |
| 24 | |
| 25 | void structThingcxx . . $ structThing# :: v1_infocxx . . $ structThing#v1_info(49f6e7a06ebc5aa8). () { |
| 26 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Object type: ", Thing_classNamecxx . . $ Thing_className(fbb3dc34e3ef688d). (this)); |
| 27 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Object name: ", this -> namecxx . . $ structThing#name. ? this -> namecxx . . $ structThing#name..getcxx . . $ _autostring#get(5a2a78a048fb49a8).() : U"<no name>"); |
| 28 | time_tcxx . . $ time_t# today = time (nullptr); |
| 29 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Date: ", Melder_peek8to32_u (ctime (& today))); // includes a newline |
| 30 | } |
| 31 | |
| 32 | /* |
| 33 | Instead of the Thing_implement macro. |
| 34 | */ |
| 35 | struct structClassInfocxx . . $ structClassInfo# theClassInfo_Thingcxx . . $ theClassInfo_Thing. = { |
| 36 | U"Thing", |
| 37 | nullptr, // no parent class |
| 38 | sizeof (class structThingcxx . . $ structThing#), |
| 39 | nullptr, // no _new function (not needed, because Thing is never instantiated) |
| 40 | 0, // version |
| 41 | 0, // sequentialUniqueIdOfReadableClass |
| 42 | nullptr // dummyObject |
| 43 | }; |
| 44 | ClassInfocxx . . $ ClassInfo# classThingcxx . . $ classThing. = & theClassInfo_Thingcxx . . $ theClassInfo_Thing.; |
| 45 | |
| 46 | conststring32cxx . . $ conststring32# Thing_classNamecxx . . $ Thing_className(fbb3dc34e3ef688d). (Thingcxx . . $ Thing# me) { |
| 47 | return mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. -> classNamecxx . . $ structClassInfo#className.; |
| 48 | } |
| 49 | |
| 50 | autoThingcxx . . $ autoThing# Thing_newFromClasscxx . . $ Thing_newFromClass(f67a3a815c0fee1d). (ClassInfocxx . . $ ClassInfo# classInfo) { |
| 51 | autoThingcxx . . $ autoThing# mecxx . . $ autoSomeThing#autoSomeThing(b2b8400651464df9). { classInfo }; |
| 52 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"created ", classInfo -> className); |
| 53 | theTotalNumberOfThingscxx . . $ theTotalNumberOfThings. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1; |
| 54 | mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. = classInfo; |
| 55 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (! mycxx . . $ `melder/melder.h:44:9`! name); // confirm that _new called calloc, so that we see null pointers |
| 56 | if (Melder_debugcxx . . $ Melder_debug. == 40) |
| 57 | Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"created ", classInfo -> classNamecxx . . $ structClassInfo#className., U" (", Melder_pointercxx . . $ Melder_pointer(a9959086b14cc90d). (classInfo), U", ", me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), U")"); |
| 58 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 59 | } |
| 60 | |
| 61 | #pragma mark - READABLE CLASSES |
| 62 | |
| 63 | stdcxx . . $ std/::unordered_mapcxx . . $ std/unordered_map# <stdcxx . . $ std/::u32string_viewcxx . . $ std/u32string_view#, ClassInfocxx . . $ ClassInfo#> theReadableClassescxx . . $ theReadableClasses.; |
| 64 | void Thing_recognizeClassesByNamecxx . . $ Thing_recognizeClassesByName(b2b8400651464df9). (ClassInfocxx . . $ ClassInfo# readableClass) { // base case (there is a template for multiple arguments) |
| 65 | Melder_precxx . . $ `melder/melder_assert.h:40:10`! (readableClass); |
| 66 | theReadableClassescxx . . $ theReadableClasses.. emplacecxx . . $ std/unordered_map#emplace(8e800962ae7274b6). (readableClass -> classNamecxx . . $ structClassInfo#className., readableClass); |
| 67 | readableClass -> sequentialUniqueIdOfReadableClasscxx . . $ structClassInfo#sequentialUniqueIdOfReadableClass. = uinteger_to_integer_acxx . . $ uinteger_to_integer_a(f7e21917e59627ec). (theReadableClassescxx . . $ theReadableClasses..sizecxx . . $ std/unordered_map#size(fe713de6d184c8a).()); |
| 68 | } |
| 69 | |
| 70 | void Thing_listReadableClassescxx . . $ Thing_listReadableClasses(49f6e7a06ebc5aa8). () { |
| 71 | MelderInfo_opencxx . . $ MelderInfo_open(49f6e7a06ebc5aa8). (); |
| 72 | for (auto element : theReadableClassescxx . . $ theReadableClasses.) { |
| 73 | ClassInfocxx . . $ ClassInfo# klas = element. secondcxx . . $ std/pair#second.; |
| 74 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (klas -> sequentialUniqueIdOfReadableClasscxx . . $ structClassInfo#sequentialUniqueIdOfReadableClass., U"\t", klas -> classNamecxx . . $ structClassInfo#className.); |
| 75 | } |
| 76 | MelderInfo_closecxx . . $ MelderInfo_close(49f6e7a06ebc5aa8). (); |
| 77 | } |
| 78 | |
| 79 | #pragma mark - ALTERNATIVE CLASS NAMES |
| 80 | |
| 81 | stdcxx . . $ std/::unordered_mapcxx . . $ std/unordered_map# <stdcxx . . $ std/::u32string_viewcxx . . $ std/u32string_view#, ClassInfocxx . . $ ClassInfo#> theAliasescxx . . $ theAliases.; |
| 82 | void Thing_recognizeClassByOtherNamecxx . . $ Thing_recognizeClassByOtherName(cb82456e35792de1). (ClassInfocxx . . $ ClassInfo# readableClass, conststring32cxx . . $ conststring32# alternativeClassName) { |
| 83 | Melder_precxx . . $ `melder/melder_assert.h:40:10`! (readableClass); |
| 84 | Melder_precxx . . $ `melder/melder_assert.h:40:10`! (alternativeClassName); |
| 85 | theAliasescxx . . $ theAliases.. emplacecxx . . $ std/unordered_map#emplace(8e800962ae7274b6). (alternativeClassName, readableClass); |
| 86 | } |
| 87 | |
| 88 | ClassInfocxx . . $ ClassInfo# Thing_classFromClassNamecxx . . $ Thing_classFromClassName(8643cf72d5cb42e3). (conststring32cxx . . $ conststring32# className, int *out_formatVersion) { |
| 89 | static char32cxx . . $ char32# buffer [1+100]; |
| 90 | str32ncpycxx . . $ str32ncpy(a0fb29e7f5bbd1dc). (buffer, className ? className : U"", 100); |
| 91 | buffer [100] = U'\0'; |
| 92 | char32cxx . . $ char32# *space = str32chrcxx . . $ str32chr(328a9ede0fba853a). (buffer, U' '); |
| 93 | if (space) { |
| 94 | *space = U'\0'; // strip version number |
| 95 | if (out_formatVersion) |
| 96 | *out_formatVersion = (int) Melder_atoicxx . . $ Melder_atoi(c94c67ff4219bf83). (space + 1); |
| 97 | } else { |
| 98 | if (out_formatVersion) |
| 99 | *out_formatVersion = 0; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | First try the class names that were registered with Thing_recognizeClassesByName. |
| 104 | */ |
| 105 | if (auto search = theReadableClassescxx . . $ theReadableClasses.. findcxx . . $ std/unordered_map#find(f711f2f81556fe93). (buffercxx . . $ std/basic_string_view#basic_string_view(837a1ee5a10cb792).); search !=cxx . . $ std/__detail/`operator!=`(6ab342297d2d0253). theReadableClassescxx . . $ theReadableClasses..endcxx . . $ std/unordered_map#end(58ae48d364eea91c).()) |
| 106 | return search ->cxx . . $ std/__detail/_Node_iterator#`operator->`(35610babf8706b50). secondcxx . . $ std/pair#second.; |
| 107 | |
| 108 | /* |
| 109 | Then try the aliases that were registered with Thing_recognizeClassByOtherName. |
| 110 | */ |
| 111 | if (auto search = theAliasescxx . . $ theAliases.. findcxx . . $ std/unordered_map#find(f711f2f81556fe93). (buffercxx . . $ std/basic_string_view#basic_string_view(837a1ee5a10cb792).); search !=cxx . . $ std/__detail/`operator!=`(6ab342297d2d0253). theAliasescxx . . $ theAliases..endcxx . . $ std/unordered_map#end(58ae48d364eea91c).()) |
| 112 | return search ->cxx . . $ std/__detail/_Node_iterator#`operator->`(35610babf8706b50). secondcxx . . $ std/pair#second.; |
| 113 | |
| 114 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Class “", buffer, U"” not recognized."); |
| 115 | } |
| 116 | |
| 117 | autoThingcxx . . $ autoThing# Thing_newFromClassNamecxx . . $ Thing_newFromClassName(87ac77d99e238079). (conststring32cxx . . $ conststring32# className, int *out_formatVersion) { |
| 118 | try { |
| 119 | ClassInfocxx . . $ ClassInfo# classInfo = Thing_classFromClassNamecxx . . $ Thing_classFromClassName(8643cf72d5cb42e3). (className, out_formatVersion); |
| 120 | return Thing_newFromClasscxx . . $ Thing_newFromClass(f67a3a815c0fee1d). (classInfo); |
| 121 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 122 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (className, U" not created."); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | Thingcxx . . $ Thing# _Thing_dummyObjectcxx . . $ _Thing_dummyObject(5a47185acae0d935). (ClassInfocxx . . $ ClassInfo# classInfo) { |
| 127 | if (! classInfo -> dummyObjectcxx . . $ structClassInfo#dummyObject.) |
| 128 | classInfo -> dummyObjectcxx . . $ structClassInfo#dummyObject. = classInfo -> _newcxx . . $ structClassInfo#_new. (); |
| 129 | Melder_postcxx . . $ `melder/melder_assert.h:41:10`! (classInfo -> dummyObject); |
| 130 | return classInfo -> dummyObjectcxx . . $ structClassInfo#dummyObject.; |
| 131 | } |
| 132 | |
| 133 | void _Thing_forget_nozerocxx . . $ _Thing_forget_nozero(97cddd01752945f6). (Thingcxx . . $ Thing# me) { |
| 134 | if (! me) |
| 135 | return; |
| 136 | if (Melder_debugcxx . . $ Melder_debug. == 40) |
| 137 | Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"destroying ", mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. -> classNamecxx . . $ structClassInfo#className.); |
| 138 | //Melder_casual (U"_Thing_forget_nozero before"); |
| 139 | mycxx . . $ `melder/melder.h:44:9`! v9_destroycxx . . $ structThing#v9_destroy(ced63f7c635d850d). (); |
| 140 | if (mycxx . . $ `melder/melder.h:44:9`! idOfExistingThingcxx . . $ structThing#idOfExistingThing.) |
| 141 | theSharedThingscxx . . $ theSharedThings.. erasecxx . . $ std/unordered_map#erase(ff4e454bb663bf95). (mycxx . . $ `melder/melder.h:44:9`! idOfExistingThingcxx . . $ structThing#idOfExistingThing.); |
| 142 | //Melder_casual (U"_Thing_forget_nozero after"); |
| 143 | theTotalNumberOfThingscxx . . $ theTotalNumberOfThings. -=cxx . . $ std/__atomic_base#`operator-=`(73de6b30e1938209). 1; |
| 144 | } |
| 145 | |
| 146 | void _Thing_forgetcxx . . $ _Thing_forget(97cddd01752945f6). (Thingcxx . . $ Thing# me) { |
| 147 | if (! me) |
| 148 | return; |
| 149 | if (Melder_debugcxx . . $ Melder_debug. == 40) |
| 150 | Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"destroying ", mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. -> classNamecxx . . $ structClassInfo#className.); |
| 151 | mycxx . . $ `melder/melder.h:44:9`! v9_destroycxx . . $ structThing#v9_destroy(ced63f7c635d850d). (); |
| 152 | if (mycxx . . $ `melder/melder.h:44:9`! idOfExistingThingcxx . . $ structThing#idOfExistingThing.) |
| 153 | theSharedThingscxx . . $ theSharedThings.. erasecxx . . $ std/unordered_map#erase(ff4e454bb663bf95). (mycxx . . $ `melder/melder.h:44:9`! idOfExistingThingcxx . . $ structThing#idOfExistingThing.); |
| 154 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"destroyed ", mycxx . . $ `melder/melder.h:44:9`! classInfo -> className, U" ", Melder_pointer (me)); |
| 155 | //Melder_free (me); |
| 156 | delete me; |
| 157 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"deleted"); |
| 158 | theTotalNumberOfThingscxx . . $ theTotalNumberOfThings. -=cxx . . $ std/__atomic_base#`operator-=`(73de6b30e1938209). 1; |
| 159 | } |
| 160 | |
| 161 | bool Thing_isSubclasscxx . . $ Thing_isSubclass(d3903fe7cfc5bd39). (ClassInfocxx . . $ ClassInfo# klas, ClassInfocxx . . $ ClassInfo# ancestor) { |
| 162 | while (klas != ancestor && klas) |
| 163 | klas = klas -> semanticParentcxx . . $ structClassInfo#semanticParent.; |
| 164 | return !! klas; |
| 165 | } |
| 166 | |
| 167 | bool Thing_isacxx . . $ Thing_isa(8cbb12e93d03b2a1). (Thingcxx . . $ Thing# me, ClassInfocxx . . $ ClassInfo# klas) { |
| 168 | if (! me) |
| 169 | Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"(Thing_isa:) Found null object."); |
| 170 | return Thing_isSubclasscxx . . $ Thing_isSubclass(d3903fe7cfc5bd39). (mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo., klas); |
| 171 | } |
| 172 | |
| 173 | void Thing_infoWithIdAndFilecxx . . $ Thing_infoWithIdAndFile(deb45308b1f72d29). (Thingcxx . . $ Thing# me, integercxx . . $ integer# id, MelderFilecxx . . $ MelderFile# file) { |
| 174 | Melder_precxx . . $ `melder/melder_assert.h:40:10`! (me); |
| 175 | Melder_clearInfocxx . . $ Melder_clearInfo(49f6e7a06ebc5aa8). (); |
| 176 | MelderInfo_opencxx . . $ MelderInfo_open(49f6e7a06ebc5aa8). (); |
| 177 | if (id != 0) |
| 178 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Object id: ", id); |
| 179 | if (! MelderFile_isNullcxx . . $ MelderFile_isNull(8f92cdf95b27d908). (file)) |
| 180 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Associated file: ", MelderFile_peekPathcxx . . $ MelderFile_peekPath(d9f043369fa9b55b). (file)); |
| 181 | mycxx . . $ `melder/melder.h:44:9`! v1_infocxx . . $ structThing#v1_info(49f6e7a06ebc5aa8). (); |
| 182 | MelderInfo_closecxx . . $ MelderInfo_close(49f6e7a06ebc5aa8). (); |
| 183 | } |
| 184 | |
| 185 | void Thing_infocxx . . $ Thing_info(97cddd01752945f6). (Thingcxx . . $ Thing# me) { |
| 186 | Thing_infoWithIdAndFilecxx . . $ Thing_infoWithIdAndFile(deb45308b1f72d29). (me, 0, nullptr); |
| 187 | } |
| 188 | |
| 189 | conststring32cxx . . $ conststring32# Thing_getNamecxx . . $ Thing_getName(fbb3dc34e3ef688d). (Thingcxx . . $ Thing# me) { |
| 190 | return mycxx . . $ `melder/melder.h:44:9`! namecxx . . $ structThing#name..getcxx . . $ _autostring#get(5a2a78a048fb49a8).(); |
| 191 | } |
| 192 | |
| 193 | conststring32cxx . . $ conststring32# Thing_messageNamecxx . . $ Thing_messageName(3625746f4613ba50). (constThingcxx . . $ constThing# me) { |
| 194 | static MelderStringcxx . . $ MelderString# buffers [19]; |
| 195 | static int ibuffer = 0; |
| 196 | if (++ ibuffer == 19) |
| 197 | ibuffer = 0; |
| 198 | if (mycxx . . $ `melder/melder.h:44:9`! namecxx . . $ structThing#name.) |
| 199 | MelderString_copycxx . . $ MelderString_copy(65be51a793e0ff96). (& buffers [ibuffer], mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. -> classNamecxx . . $ structClassInfo#className., U" “", mycxx . . $ `melder/melder.h:44:9`! namecxx . . $ structThing#name..getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), U"”"); |
| 200 | else |
| 201 | MelderString_copycxx . . $ MelderString_copy(65be51a793e0ff96). (& buffers [ibuffer], mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. -> classNamecxx . . $ structClassInfo#className.); |
| 202 | return buffers [ibuffer].stringcxx . . $ MelderString#string.; |
| 203 | } |
| 204 | |
| 205 | conststring32cxx . . $ conststring32# Thing_messageNameAndAddresscxx . . $ Thing_messageNameAndAddress(fbb3dc34e3ef688d). (Thingcxx . . $ Thing# me) { |
| 206 | static MelderStringcxx . . $ MelderString# buffers [19]; |
| 207 | static int ibuffer = 0; |
| 208 | if (++ ibuffer == 19) |
| 209 | ibuffer = 0; |
| 210 | if (mycxx . . $ `melder/melder.h:44:9`! namecxx . . $ structThing#name.) |
| 211 | MelderString_copycxx . . $ MelderString_copy(65be51a793e0ff96). (& buffers [ibuffer], mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. -> classNamecxx . . $ structClassInfo#className., U"-", |
| 212 | Melder_pointercxx . . $ Melder_pointer(a9959086b14cc90d). (me), U"-“", mycxx . . $ `melder/melder.h:44:9`! namecxx . . $ structThing#name..getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), U"”"); |
| 213 | else |
| 214 | MelderString_copycxx . . $ MelderString_copy(65be51a793e0ff96). (& buffers [ibuffer], mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. -> classNamecxx . . $ structClassInfo#className., U"-", |
| 215 | Melder_pointercxx . . $ Melder_pointer(a9959086b14cc90d). (me)); |
| 216 | return buffers [ibuffer].stringcxx . . $ MelderString#string.; |
| 217 | } |
| 218 | |
| 219 | void Thing_setNamecxx . . $ Thing_setName(b5a42e5e01c267b5). (Thingcxx . . $ Thing# me, conststring32cxx . . $ conststring32# name /* cattable */) { |
| 220 | mycxx . . $ `melder/melder.h:44:9`! namecxx . . $ structThing#name. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (name); |
| 221 | mycxx . . $ `melder/melder.h:44:9`! v_nameChangedcxx . . $ structThing#v_nameChanged(49f6e7a06ebc5aa8). (); |
| 222 | } |
| 223 | |
| 224 | void Thing_swapcxx . . $ Thing_swap(3b1e0b93a75a6e5d). (Thingcxx . . $ Thing# me, Thingcxx . . $ Thing# thee) { |
| 225 | Melder_precxx . . $ `melder/melder_assert.h:40:10`! (mycxx . . $ `melder/melder.h:44:9`! classInfo == thycxx . . $ `melder/melder.h:45:9`! classInfo); |
| 226 | const integercxx . . $ integer# n = mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo. -> sizecxx . . $ structClassInfo#size.; |
| 227 | char *p, *q; |
| 228 | integercxx . . $ integer# i; |
| 229 | for (p = (char *) me, q = (char *) thee, i = n; i > 0; i --, p ++, q ++) { |
| 230 | char tmp = *p; |
| 231 | *p = *q; |
| 232 | *q = tmp; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | stdcxx . . $ std/::unordered_mapcxx . . $ std/unordered_map# <integercxx . . $ integer#, SharedThingcxx . . $ SharedThing#> theSharedThingscxx . . $ theSharedThings.; |
| 237 | |
| 238 | void Thing_sharecxx . . $ Thing_share(97cddd01752945f6). (Thingcxx . . $ Thing# me) { |
| 239 | static integercxx . . $ integer# s_idOfExistingThing = 20'000'000; // to make them a bit recognizable (in the alphabet, T = 20) |
| 240 | mycxx . . $ `melder/melder.h:44:9`! idOfExistingThingcxx . . $ structThing#idOfExistingThing. = ++ s_idOfExistingThing; |
| 241 | theSharedThingscxx . . $ theSharedThings.. emplacecxx . . $ std/unordered_map#emplace(8e800962ae7274b6). (s_idOfExistingThing, SharedThingcxx . . $ SharedThing# { me }); |
| 242 | } |
| 243 | |
| 244 | /* End of file Thing.cpp */ |