| 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" |
| 22 | |
| 23 | std::atomic <integer> theTotalNumberOfThings; |
| 24 | |
| 25 | void structThing :: v1_info () { |
| 26 | MelderInfo_writeLine (U"Object type: ", Thing_className (this)); |
| 27 | MelderInfo_writeLine (U"Object name: ", this -> name ? this -> name.get() : U"<no name>"); |
| 28 | time_t today = time (nullptr); |
| 29 | MelderInfo_writeLine (U"Date: ", Melder_peek8to32_u (ctime (& today))); // includes a newline |
| 30 | } |
| 31 | |
| 32 | /* |
| 33 | Instead of the Thing_implement macro. |
| 34 | */ |
| 35 | struct structClassInfo theClassInfo_Thing = { |
| 36 | U"Thing", |
| 37 | nullptr, // no parent class |
| 38 | sizeof (class structThing), |
| 39 | nullptr, // no _new function (not needed, because Thing is never instantiated) |
| 40 | 0, // version |
| 41 | 0, // sequentialUniqueIdOfReadableClass |
| 42 | nullptr // dummyObject |
| 43 | }; |
| 44 | ClassInfo classThing = & theClassInfo_Thing; |
| 45 | |
| 46 | conststring32 Thing_className (Thing me) { |
| 47 | return my classInfo -> className; |
| 48 | } |
| 49 | |
| 50 | autoThing Thing_newFromClass (ClassInfo classInfo) { |
| 51 | autoThing me { classInfo }; |
| 52 | trace (U"created ", classInfo -> className); |
| 53 | theTotalNumberOfThings += 1; |
| 54 | my classInfo = classInfo; |
| 55 | Melder_assert (! my name); // confirm that _new called calloc, so that we see null pointers |
| 56 | if (Melder_debug == 40) |
| 57 | Melder_casual (U"created ", classInfo -> className, U" (", Melder_pointer (classInfo), U", ", me.get(), U")"); |
| 58 | return me; |
| 59 | } |
| 60 | |
| 61 | #pragma mark - READABLE CLASSES |
| 62 | |
| 63 | std::unordered_map <std::u32string_view, ClassInfo> theReadableClasses; |
| 64 | void Thing_recognizeClassesByName (ClassInfo readableClass) { // base case (there is a template for multiple arguments) |
| 65 | Melder_pre (readableClass); |
| 66 | theReadableClasses. emplace (readableClass -> className, readableClass); |
| 67 | readableClass -> sequentialUniqueIdOfReadableClass = uinteger_to_integer_a (theReadableClasses.size()); |
| 68 | } |
| 69 | |
| 70 | void Thing_listReadableClasses () { |
| 71 | MelderInfo_open (); |
| 72 | for (auto element : theReadableClasses) { |
| 73 | ClassInfo klas = element. second; |
| 74 | MelderInfo_writeLine (klas -> sequentialUniqueIdOfReadableClass, U"\t", klas -> className); |
| 75 | } |
| 76 | MelderInfo_close (); |
| 77 | } |
| 78 | |
| 79 | #pragma mark - ALTERNATIVE CLASS NAMES |
| 80 | |
| 81 | std::unordered_map <std::u32string_view, ClassInfo> theAliases; |
| 82 | void Thing_recognizeClassByOtherName (ClassInfo readableClass, conststring32 alternativeClassName) { |
| 83 | Melder_pre (readableClass); |
| 84 | Melder_pre (alternativeClassName); |
| 85 | theAliases. emplace (alternativeClassName, readableClass); |
| 86 | } |
| 87 | |
| 88 | ClassInfo Thing_classFromClassName (conststring32 className, int *out_formatVersion) { |
| 89 | static char32 buffer [1+100]; |
| 90 | str32ncpy (buffer, className ? className : U"", 100); |
| 91 | buffer [100] = U'\0'; |
| 92 | char32 *space = str32chr (buffer, U' '); |
| 93 | if (space) { |
| 94 | *space = U'\0'; // strip version number |
| 95 | if (out_formatVersion) |
| 96 | *out_formatVersion = (int) Melder_atoi (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 = theReadableClasses. find (buffer); search != theReadableClasses.end()) |
| 106 | return search -> second; |
| 107 | |
| 108 | /* |
| 109 | Then try the aliases that were registered with Thing_recognizeClassByOtherName. |
| 110 | */ |
| 111 | if (auto search = theAliases. find (buffer); search != theAliases.end()) |
| 112 | return search -> second; |
| 113 | |
| 114 | Melder_throw (U"Class “", buffer, U"” not recognized."); |
| 115 | } |
| 116 | |
| 117 | autoThing Thing_newFromClassName (conststring32 className, int *out_formatVersion) { |
| 118 | try { |
| 119 | ClassInfo classInfo = Thing_classFromClassName (className, out_formatVersion); |
| 120 | return Thing_newFromClass (classInfo); |
| 121 | } catch (MelderError) { |
| 122 | Melder_throw (className, U" not created."); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | Thing _Thing_dummyObject (ClassInfo classInfo) { |
| 127 | if (! classInfo -> dummyObject) |
| 128 | classInfo -> dummyObject = classInfo -> _new (); |
| 129 | Melder_post (classInfo -> dummyObject); |
| 130 | return classInfo -> dummyObject; |
| 131 | } |
| 132 | |
| 133 | void _Thing_forget_nozero (Thing me) { |
| 134 | if (! me) |
| 135 | return; |
| 136 | if (Melder_debug == 40) |
| 137 | Melder_casual (U"destroying ", my classInfo -> className); |
| 138 | //Melder_casual (U"_Thing_forget_nozero before"); |
| 139 | my v9_destroy (); |
| 140 | if (my idOfExistingThing) |
| 141 | theSharedThings. erase (my idOfExistingThing); |
| 142 | //Melder_casual (U"_Thing_forget_nozero after"); |
| 143 | theTotalNumberOfThings -= 1; |
| 144 | } |
| 145 | |
| 146 | void _Thing_forget (Thing me) { |
| 147 | if (! me) |
| 148 | return; |
| 149 | if (Melder_debug == 40) |
| 150 | Melder_casual (U"destroying ", my classInfo -> className); |
| 151 | my v9_destroy (); |
| 152 | if (my idOfExistingThing) |
| 153 | theSharedThings. erase (my idOfExistingThing); |
| 154 | trace (U"destroyed ", my classInfo -> className, U" ", Melder_pointer (me)); |
| 155 | //Melder_free (me); |
| 156 | delete me; |
| 157 | trace (U"deleted"); |
| 158 | theTotalNumberOfThings -= 1; |
| 159 | } |
| 160 | |
| 161 | bool Thing_isSubclass (ClassInfo klas, ClassInfo ancestor) { |
| 162 | while (klas != ancestor && klas) |
| 163 | klas = klas -> semanticParent; |
| 164 | return !! klas; |
| 165 | } |
| 166 | |
| 167 | bool Thing_isa (Thing me, ClassInfo klas) { |
| 168 | if (! me) |
| 169 | Melder_crash (U"(Thing_isa:) Found null object."); |
| 170 | return Thing_isSubclass (my classInfo, klas); |
| 171 | } |
| 172 | |
| 173 | void Thing_infoWithIdAndFile (Thing me, integer id, MelderFile file) { |
| 174 | Melder_pre (me); |
| 175 | Melder_clearInfo (); |
| 176 | MelderInfo_open (); |
| 177 | if (id != 0) |
| 178 | MelderInfo_writeLine (U"Object id: ", id); |
| 179 | if (! MelderFile_isNull (file)) |
| 180 | MelderInfo_writeLine (U"Associated file: ", MelderFile_peekPath (file)); |
| 181 | my v1_info (); |
| 182 | MelderInfo_close (); |
| 183 | } |
| 184 | |
| 185 | void Thing_info (Thing me) { |
| 186 | Thing_infoWithIdAndFile (me, 0, nullptr); |
| 187 | } |
| 188 | |
| 189 | conststring32 Thing_getName (Thing me) { |
| 190 | return my name.get(); |
| 191 | } |
| 192 | |
| 193 | conststring32 Thing_messageName (constThing me) { |
| 194 | static MelderString buffers [19]; |
| 195 | static int ibuffer = 0; |
| 196 | if (++ ibuffer == 19) |
| 197 | ibuffer = 0; |
| 198 | if (my name) |
| 199 | MelderString_copy (& buffers [ibuffer], my classInfo -> className, U" “", my name.get(), U"”"); |
| 200 | else |
| 201 | MelderString_copy (& buffers [ibuffer], my classInfo -> className); |
| 202 | return buffers [ibuffer].string; |
| 203 | } |
| 204 | |
| 205 | conststring32 Thing_messageNameAndAddress (Thing me) { |
| 206 | static MelderString buffers [19]; |
| 207 | static int ibuffer = 0; |
| 208 | if (++ ibuffer == 19) |
| 209 | ibuffer = 0; |
| 210 | if (my name) |
| 211 | MelderString_copy (& buffers [ibuffer], my classInfo -> className, U"-", |
| 212 | Melder_pointer (me), U"-“", my name.get(), U"”"); |
| 213 | else |
| 214 | MelderString_copy (& buffers [ibuffer], my classInfo -> className, U"-", |
| 215 | Melder_pointer (me)); |
| 216 | return buffers [ibuffer].string; |
| 217 | } |
| 218 | |
| 219 | void Thing_setName (Thing me, conststring32 name /* cattable */) { |
| 220 | my name = Melder_dup (name); |
| 221 | my v_nameChanged (); |
| 222 | } |
| 223 | |
| 224 | void Thing_swap (Thing me, Thing thee) { |
| 225 | Melder_pre (my classInfo == thy classInfo); |
| 226 | const integer n = my classInfo -> size; |
| 227 | char *p, *q; |
| 228 | 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 | std::unordered_map <integer, SharedThing> theSharedThings; |
| 237 | |
| 238 | void Thing_share (Thing me) { |
| 239 | static integer s_idOfExistingThing = 20'000'000; // to make them a bit recognizable (in the alphabet, T = 20) |
| 240 | my idOfExistingThing = ++ s_idOfExistingThing; |
| 241 | theSharedThings. emplace (s_idOfExistingThing, SharedThing { me }); |
| 242 | } |
| 243 | |
| 244 | /* End of file Thing.cpp */ |