Debian Codegraph @

sys/Thing.h

raw symbols SCIP
1 #cxx . . $ `<file>/sys/Thing.h`/ifndef _Thing_h_
2 #define _Thing_h_cxx . . $ `sys/Thing.h:2:9`!
3 /* Thing.h
4 *
5 * Copyright (C) 1992-2009,2011-2020,2022,2024-2026 Paul Boersma
6 *
7 * This code is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or (at
10 * your option) any later version.
11 *
12 * This code is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this work. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /* The root class of all objects. */
22
23 /* Anyone who uses Thing can also use: */
24 #include "melder.h"cxx . . $ `<file>/melder/melder.h`/
25 /* The macros for struct and class definitions: */
26 #include "oo.h"cxx . . $ `<file>/sys/oo.h`/
27
28 #define _Thing_auto_DEBUGcxx . . $ `sys/Thing.h:28:9`! 0
29
30 typedef struct structClassInfocxx . . $ structClassInfo# *ClassInfocxx . . $ ClassInfo#;
31 struct structClassInfocxx . . $ structClassInfo# {
32 /*
33 The following five fields are statically initialized by the Thing_implement() macro.
34 */
35 conststring32cxx . . $ conststring32# classNamecxx . . $ structClassInfo#className.;
36 ClassInfocxx . . $ ClassInfo# semanticParentcxx . . $ structClassInfo#semanticParent.;
37 integercxx . . $ integer# sizecxx . . $ structClassInfo#size.;
38 Thingcxx . . $ Thing# (* _newcxx . . $ structClassInfo#_new.) (); // objects have to be constructed via this function, because it calls C++ "new", which initializes the C++ class pointer
39 integercxx . . $ integer# versioncxx . . $ structClassInfo#version.;
40 /*
41 The following field is initialized by Thing_recognizeClassesByName,
42 only for classes that have to be read (usually from disk).
43 */
44 integercxx . . $ integer# sequentialUniqueIdOfReadableClasscxx . . $ structClassInfo#sequentialUniqueIdOfReadableClass.;
45 /*
46 The following field is initialized by Thing_dummyObject(), which is used only rarely.
47 */
48 Thingcxx . . $ Thing# dummyObjectcxx . . $ structClassInfo#dummyObject.;
49 };
50
51 #define Thing_declarecxx . . $ `sys/Thing.h:51:9`!(klas) \
52 typedef struct struct##klas *klas; \
53 typedef struct struct##klas *mutable##klas; \
54 typedef const struct struct##klas *const##klas; \
55 typedef autoSomeThing <struct##klas> auto##klas; \
56 typedef SomeThingHandle <struct##klas> klas##Handle; \
57 extern struct structClassInfo theClassInfo_##klas; \
58 extern ClassInfo class##klas
59
60 #define Thing_definecxx . . $ `sys/Thing.h:60:9`!(klas,syntacticParentKlas) \
61 Thing_declarecxx . . $ `sys/Thing.h:51:9`! (klas); \
62 typedef struct##syntacticParentKlas klas##_Parent; \
63 struct struct##klas : public struct##syntacticParentKlas
64
65 #define Thing_implementcxx . . $ `sys/Thing.h:65:9`!(klas,semanticParentKlas,version) \
66 static Thing _##klas##_new () { return new struct##klas; } \
67 struct structClassInfo theClassInfo_##klas = { U"" #klas, & theClassInfo_##semanticParentKlas, sizeof (class struct##klas), _##klas##_new, version, 0, nullptr}; \
68 ClassInfo class##klas = & theClassInfo_##klas
69
70 #define Thing_implement_pureVirtualcxx . . $ `sys/Thing.h:70:9`!(klas,semanticParentKlas,version) \
71 struct structClassInfo theClassInfo_##klas = { U"" #klas, & theClassInfo_##semanticParentKlas, sizeof (class struct##klas), nullptr, version, 0, nullptr}; \
72 ClassInfo class##klas = & theClassInfo_##klas
73
74 /*
75 Thing has no parent class, so instead of using the Thing_define macro
76 we write out the stuff that does exist.
77 */
78 typedef struct structThingcxx . . $ structThing# *Thingcxx . . $ Thing#; // we need an explicit "struct" here, because this is a forward declaration
79 typedef struct structThingcxx . . $ structThing# *mutableThingcxx . . $ mutableThing#;
80 typedef const struct structThingcxx . . $ structThing# *constThingcxx . . $ constThing#;
81 extern ClassInfocxx . . $ ClassInfo# classThingcxx . . $ classThing.;
82 extern struct structClassInfocxx . . $ structClassInfo# theClassInfo_Thingcxx . . $ theClassInfo_Thing.;
83 struct structThingcxx . . $ structThing# {
84 ClassInfocxx . . $ ClassInfo# classInfocxx . . $ structThing#classInfo.; // the Praat class pointer (every object also has a C++ class pointer initialized by C++ "new")
85 autostring32cxx . . $ autostring32# namecxx . . $ structThing#name.;
86 integercxx . . $ integer# idOfExistingThingcxx . . $ structThing#idOfExistingThing.; // 0 for most Things
87 void * operatorcxx . . $ structThing#`operator new`(bfcfa4d6b7f7ef64). new (size_t size) { return Melder_calloccxx . . $ int64# (char, (int64) size); }
88 void operatorcxx . . $ structThing#`operator delete`(e819ef7a57397d90). delete (void *ptr, size_t /* size */) { Melder_freecxx . . $ `melder/melder_alloc.h:38:9`! (ptr); }
89
90 /*
91 If a Thing has members of type autoThing and/or autovector and/or automatrix and/or autostring,
92 then we want the destructors of those objects to be called automatically whenever a Thing is `delete`d.
93 For this to happen, it is necessary that every Thing itself has a destructor.
94 We therefore define a destructor here,
95 and we make it virtual to ensure that every subclass has its own automatic version.
96 */
97 virtual ~cxx . . $ structThing#`~structThing`(ced63f7c635d850d).structThingcxx . . $ structThing# () noexcept { }
98
99 virtual void v9_destroycxx . . $ structThing#v9_destroy(ced63f7c635d850d). () noexcept { }
100 /*
101 This method should destroy all members that are not destroyed automatically
102 (any autoThing or autovector or automatrix or autostring is destroyed automatically),
103 and to remove dangling links to self.
104 Destroying *all* members means that we have to destroy all members that the
105 derived class has added, as well as all members of the base class,
106 and so on recursively; v9_destroy therefore has to call the inherited v9_destroy.
107 The convention is to call the parent's v9_destroy at our end, i.e. to chain up;
108 this makes sense because the destruction of an added member might depend
109 on the presence of a member from the base class.
110 Thus, v9_destroy is called recursively up the inheritance hierarchy,
111 and this is implemented by calling the parent's v9_destroy at the end,
112 i.e. at position "9" (hence the name).
113 */
114 virtual void v1_infocxx . . $ structThing#v1_info(49f6e7a06ebc5aa8). ();
115 /*
116 Implement as follows: call a set of MelderInfo_writeXXX describing your data.
117
118 Thing::v1_info writes object id, object name, and date;
119 derived::v1_info often calls base::v1_info at start
120 and then writes information on the new data (i.e., it chains down),
121 but a few ancestors can be skipped if their data have new meanings.
122 */
123 virtual void v1_assertInvariantscxx . . $ structThing#v1_assertInvariants(49f6e7a06ebc5aa8). () { }
124 /*
125 derived::v1_assertInvariants typically calls base::v1_assertInvariants at start
126 */
127 friend void Thing_assertInvariantscxx . . $ Thing_assertInvariants(97cddd01752945f6). (Thingcxx . . $ Thing# me) {
128 mycxx . . $ `melder/melder.h:44:9`! v1_assertInvariantscxx . . $ structThing#v1_assertInvariants(49f6e7a06ebc5aa8). ();
129 }
130 virtual void v_checkConstraintscxx . . $ structThing#v_checkConstraints(49f6e7a06ebc5aa8). () { }
131 /*
132 derived::v_checkConstraints typically calls base::v_checkConstraints at start
133 */
134 virtual void v_nameChangedcxx . . $ structThing#v_nameChanged(49f6e7a06ebc5aa8). () { }
135 /*
136 derived::v_nameChanged may call base::v_nameChanged at start, middle or end
137 */
138 protected:
139 /*
140 The following two functions look like they belong in DataGui instead of in Thing,
141 but their content is automated via a macro that includes "override",
142 and some of that overridden content already appears in DataGui,
143 so we introduce these two functions here.
144 */
145 virtual void v1_copyPreferencesToInstancecxx . . $ structThing#v1_copyPreferencesToInstance(49f6e7a06ebc5aa8). () { }
146 /*
147 derived::v1_copyPreferencesToInstance calls base::v1_copyPreferencesToInstance at *start*,
148 because specifications at derived level have to override those of the base level
149 */
150 virtual void v9_repairPreferencescxx . . $ structThing#v9_repairPreferences(49f6e7a06ebc5aa8). () { }
151 /*
152 derived::v9_repairPreferences may call base::v9_repairPreferences at *end*,
153 because restrictions at base level may be laxer than restrictions at derived level
154 (preferences will be overridden only if their current value violates a restriction)
155 */
156 };
157
158 #define forgetcxx . . $ `sys/Thing.h:158:9`!(thing) do { _Thing_forget (thing); thing = nullptr; } while (false)
159 /*
160 Function:
161 free all memory associated with 'thing'.
162 Postcondition:
163 thing == nullptr;
164 */
165 #define forget_nozerocxx . . $ `sys/Thing.h:165:9`!(thing) do { _Thing_forget_nozero (thing); delete thing; } while (false)
166 /*
167 Function:
168 free all memory associated with 'thing'.
169 */
170
171 /* All functions with 'Thing me' as the first argument assume that it is not null. */
172
173 conststring32cxx . . $ conststring32# Thing_classNamecxx . . $ Thing_className(fbb3dc34e3ef688d). (Thingcxx . . $ Thing# me);
174 /* Return your class name. */
175
176 bool Thing_isacxx . . $ Thing_isa(8cbb12e93d03b2a1). (Thingcxx . . $ Thing# me, ClassInfocxx . . $ ClassInfo# klas);
177 /*
178 return true if you are a 'klas',
179 i.e., if you are an object of the class 'klas' or of one of the classes derived from 'klas'.
180 E.g., Thing_isa (object, classThing) will always return true.
181 */
182
183 bool Thing_isSubclasscxx . . $ Thing_isSubclass(d3903fe7cfc5bd39). (ClassInfocxx . . $ ClassInfo# klas, ClassInfocxx . . $ ClassInfo# ancestor);
184 /*
185 return true if <klas> is a subclass of <ancestor>,
186 i.e., if <klas> equals <ancestor>, or if the parent class of <klas> is a subclass of <ancestor>.
187 E.g., Thing_isSubclass (classX, classThing) will always return true.
188 */
189
190 void Thing_infocxx . . $ Thing_info(97cddd01752945f6). (Thingcxx . . $ Thing# me);
191 void Thing_infoWithIdAndFilecxx . . $ Thing_infoWithIdAndFile(deb45308b1f72d29). (Thingcxx . . $ Thing# me, integercxx . . $ integer# id, MelderFilecxx . . $ MelderFile# file);
192
193 /*
194 Function:
195 to make Thing_classFromClassName () and Thing_newFromClassName ()
196 recognize a class from its name (a string).
197 Arguments:
198 as many classes as you want.
199 It is not an error if a class occurs more than once in the list.
200 Behaviour:
201 calling this function more than once, each time for different classes,
202 has the same result as calling it once for all these classes together.
203 Usage:
204 you should call this function for all classes that you want to read by name,
205 e.g., with Data_readFromTextFile () or Data_readFromBinaryFile (),
206 or with Data_readText () or Data_readBinary () if the object is a Collection.
207 Calls to this function should preferably be put in the beginning of main ().
208 */
209 void Thing_recognizeClassesByNamecxx . . $ Thing_recognizeClassesByName(b2b8400651464df9). (ClassInfocxx . . $ ClassInfo# readableClass);
210 template <typename... ClassInfoType>
211 void Thing_recognizeClassesByNamecxx . . $ Thing_recognizeClassesByName(f327490b5edb0dcb). (ClassInfoType... classInfos) {
212 ( Thing_recognizeClassesByNamecxx . . $ Thing_recognizeClassesByName(f327490b5edb0dcb). (classInfos), ... ); // folded over comma
213 }
214 void Thing_recognizeClassByOtherNamecxx . . $ Thing_recognizeClassByOtherName(cb82456e35792de1). (ClassInfocxx . . $ ClassInfo# readableClass, conststring32cxx . . $ conststring32# otherName);
215
216 void Thing_listReadableClassescxx . . $ Thing_listReadableClasses(49f6e7a06ebc5aa8). ();
217
218 ClassInfocxx . . $ ClassInfo# Thing_classFromClassNamecxx . . $ Thing_classFromClassName(8643cf72d5cb42e3). (conststring32cxx . . $ conststring32# className, int *formatVersion);
219 /*
220 Function:
221 Return the class info table of class 'className', or null if it is not recognized.
222 E.g. the value returned from Thing_classFromClassName (U"PietjePuk")
223 will be equal to classPietjePuk.
224 Side effect:
225 If 'className' equals U"PietjePuk 300", the value returned will be classPietjePuk,
226 and formatVersion (if not null) will be set to 300.
227 */
228
229 #define Thing_dummyObjectcxx . . $ `sys/Thing.h:229:9`!(klas) \
230 ((klas) _Thing_dummyObject (class##klas))
231 Thingcxx . . $ Thing# _Thing_dummyObjectcxx . . $ _Thing_dummyObject(5a47185acae0d935). (ClassInfocxx . . $ ClassInfo# classInfo);
232
233 conststring32cxx . . $ conststring32# Thing_getNamecxx . . $ Thing_getName(fbb3dc34e3ef688d). (Thingcxx . . $ Thing# me);
234 /* Return a pointer to your internal name (which can be null). */
235 conststring32cxx . . $ conststring32# Thing_messageNamecxx . . $ Thing_messageName(3625746f4613ba50). (constThingcxx . . $ constThing# me);
236 conststring32cxx . . $ conststring32# Thing_messageNameAndAddresscxx . . $ Thing_messageNameAndAddress(fbb3dc34e3ef688d). (Thingcxx . . $ Thing# me); // for debugging
237
238 void Thing_setNamecxx . . $ Thing_setName(b5a42e5e01c267b5). (Thingcxx . . $ Thing# me, conststring32cxx . . $ conststring32# name /* cattable */);
239 /*
240 Function:
241 remember that you are called 'name'.
242 Postconditions:
243 my name *and* my name are copies of 'name'.
244 */
245
246 #define Thing_castcxx . . $ `sys/Thing.h:246:9`!(Klas,var,expr) \
247 Klas var = static_cast <Klas> (expr); /* The compiler checks this. */ \
248 Melder_assert (! var || Thing_isa (var, class##Klas));
249
250 #define Thing_cast_0cxx . . $ `sys/Thing.h:250:9`!(Klas,var,expr) \
251 Klas var = static_cast <Klas> (expr); /* The compiler checks this. */ \
252 Thing_isa (var, class##Klas)
253
254 void Thing_swapcxx . . $ Thing_swap(3b1e0b93a75a6e5d). (Thingcxx . . $ Thing# me, Thingcxx . . $ Thing# thee);
255 /*
256 Function:
257 Swap my and thy contents.
258 Precondition:
259 my classInfo == thy classInfo;
260 Postconditions:
261 my xxx == thy old xxx;
262 thy xxx == my old xxx;
263 Usage:
264 Swap two objects without changing any references to them.
265 */
266
267 /* For the macros. */
268
269 void _Thing_forgetcxx . . $ _Thing_forget(97cddd01752945f6). (Thingcxx . . $ Thing# me);
270 void _Thing_forget_nozerocxx . . $ _Thing_forget_nozero(97cddd01752945f6). (Thingcxx . . $ Thing# me);
271
272 /* For debugging. */
273
274 extern stdcxx . . $ std/::atomiccxx . . $ std/atomic# <integercxx . . $ integer#> theTotalNumberOfThingscxx . . $ theTotalNumberOfThings.;
275 /* This number is 0 initially, increments at every successful `new', and decrements at every `forget'. */
276
277 template <class T>
278 class autoSomeThingcxx . . $ autoSomeThing# {
279 T *ptrcxx . . $ autoSomeThing#ptr.;
280 public:
281 /*
282 A default constructor, as in
283 autoPitch pitch;
284 should initialize the pointer to null.
285 */
286 autoSomeThingcxx . . $ autoSomeThing#`autoSomeThing<T>`(49f6e7a06ebc5aa8). () : ptrcxx . . $ autoSomeThing#ptr. (nullptr) {
287 #if _Thing_auto_DEBUGcxx . . $ `sys/Thing.h:28:9`!
288 fprintf (Melder_stderr, "default constructor\n");
289 #endif
290 }
291 autoSomeThingcxx . . $ autoSomeThing#`autoSomeThing<T>`(b2b8400651464df9). (ClassInfocxx . . $ ClassInfo# classInfo) {
292 ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr. = classInfo -> _newcxx . . $ structClassInfo#_new. ();
293 }
294 /*
295 After
296 autoPitch pitch1 = Pitch_create ();
297 pitch1 should be destroyed when going out of scope,
298 either at the end of the try block or whenever a throw occurs.
299 */
300 ~cxx . . $ autoSomeThing#`~autoSomeThing<T>`(ced63f7c635d850d).autoSomeThing () noexcept {
301 #if _Thing_auto_DEBUGcxx . . $ `sys/Thing.h:28:9`!
302 fprintf (Melder_stderr, "destructor %p %s\n",
303 our ptr, our ptr ? Melder_peek32to8 (our ptr -> classInfo -> className) : "(class unknown)");
304 #endif
305 if (ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr.) {
306 _Thing_forgetcxx . . $ _Thing_forget(97cddd01752945f6). (ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr.);
307 ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr. = nullptr;
308 }
309 }
310 T* getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab). () const noexcept {
311 return ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr.;
312 }
313 #if 0
314 /*
315 The following would allow implicit conversions of autoPitch to Pitch,
316 as in
317 Pitch_draw (my pitch);
318 instead of the explicit
319 Pitch_draw (my pitch.get());
320 This is unwanted, because we are not copying, so we #if this out.
321 */
322 operator T* () const noexcept {
323 return our ptr;
324 }
325 #endif
326 /*
327 The expression
328 pitch. ptr -> xmin
329 should be abbreviatable by
330 pitch -> xmin
331 */
332 T* operatorcxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).-> () const noexcept { // as r-value
333 return ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr.;
334 }
335 /*
336 T& operator* () const noexcept { // as l-value
337 return *our ptr;
338 }*/
339 /*
340 After construction, there are two ways to access the pointer: with and without transfer of ownership.
341
342 Without transfer:
343 Pitch_draw (pitch.get());
344
345 With transfer:
346 return thee;
347 and
348 *out_pitch = pitch.move();
349 *out_pulses = pulses.move();
350 and
351 my addItem_move (pitch.move());
352 and
353 praat_new (pitch.move(), my name);
354 */
355 void releaseToUsercxx . . $ autoSomeThing#releaseToUser(ced63f7c635d850d). () noexcept {
356 ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr. = nullptr; // make the pointer non-automatic again
357 }
358
359 /*
360 Sometimes ownership is determined by a flag such as _ownItems or _ownData or _ownSound.
361 In that case, the autoThing has be adopted from a raw Thing pointer from the ambiguous owner,
362 or released as a raw Thing pointer making the ambiguous owner responsible for destroying the object.
363 In Praat, this happens with Collection items and with some editors.
364 */
365 void adoptFromAmbiguousOwnercxx . . $ autoSomeThing#adoptFromAmbiguousOwner(6f97683fca3ece44). (T* newPtr) noexcept {
366 ourcxx . . $ `melder/melder.h:42:9`! resetcxx . . $ autoSomeThing#reset(ced63f7c635d850d).();
367 ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr. = newPtr;
368 }
369 T* releaseToAmbiguousOwnercxx . . $ autoSomeThing#releaseToAmbiguousOwner(258ee1c9e0a18a5c). () noexcept {
370 T* temp = ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr.;
371 ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr. = nullptr; // make the pointer non-automatic again
372 return temp;
373 }
374 void resetcxx . . $ autoSomeThing#reset(ced63f7c635d850d). () noexcept {
375 _Thing_forgetcxx . . $ _Thing_forget(97cddd01752945f6). (ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr.);
376 ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr. = nullptr;
377 }
378 void _zerocxx . . $ autoSomeThing#_zero(ced63f7c635d850d). () noexcept {
379 ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr. = nullptr;
380 }
381 explicit operatorcxx . . $ autoSomeThing#`operator bool`(3482b152b9333168). bool () const noexcept {
382 return !! ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr.;
383 }
384 bool operatorcxx . . $ autoSomeThing#`operator==`(3e632b4bec2c9911).== (autoSomeThingcxx . . $ autoSomeThing#<T> other) const noexcept {
385 return other. ptrcxx . . $ autoSomeThing#ptr. == ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr.;
386 }
387 bool operatorcxx . . $ autoSomeThing#`operator!=`(3e632b4bec2c9911).!= (autoSomeThingcxx . . $ autoSomeThing#<T> other) const noexcept {
388 return other. ptrcxx . . $ autoSomeThing#ptr. != ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr.;
389 }
390 /*
391 The compiler should prevent initializations from autoSomeThing l-values, as in
392 autoPitch pitch2 = pitch;
393 as well as assignments from autoSomeThing l-values, as in
394 pitch2 = pitch;
395 This is because the syntax of these statements is *copy* syntax,
396 but the semantics of these statements has to be, confusingly, *move* semantics
397 (i.e., pitch.ptr should be set to null),
398 because if the semantics were copy semantics instead,
399 a destructor would be called at some point for both pitch and pitch2,
400 twice deleting the same object, which is a run-time error.
401 */
402 autoSomeThingcxx . . $ autoSomeThing#`autoSomeThing<T>`(769e03d765fbeca0).<T> (const autoSomeThingcxx . . $ autoSomeThing#<T>&) = delete; // disable copy constructor from an l-value of class T*
403 template <class Y> autoSomeThingcxx . . $ autoSomeThing#`autoSomeThing<T>`(9197bd583a33e895).<T> (const autoSomeThingcxx . . $ autoSomeThing#<Y>&) = delete; // disable copy constructor from an l-value of a descendant class of T*
404 autoSomeThingcxx . . $ autoSomeThing#<T>& operatorcxx . . $ autoSomeThing#`operator=`(e19efb258c745e9a).= (const autoSomeThingcxx . . $ autoSomeThing#<T>&) = delete; // disable copy assignment from an l-value of class T*
405 template <class Y> autoSomeThingcxx . . $ autoSomeThing#<T>& operatorcxx . . $ autoSomeThing#`operator=`(6dacbba5a06595f6).= (const autoSomeThingcxx . . $ autoSomeThing#<Y>&) = delete; // disable copy assignment from an l-value of a descendant class of T*
406 /*
407 The compiler should treat initializations from autoSomeThing r-values, as in
408 extern autoPitch Pitch_create (...);
409 autoPitch pitch = Pitch_create (...);
410 as move constructors.
411 */
412 autoSomeThingcxx . . $ autoSomeThing#`autoSomeThing<T>`(b79141a71f7bc86f).<T> (autoSomeThingcxx . . $ autoSomeThing#<T>&& other) noexcept : ptrcxx . . $ autoSomeThing#ptr. (other. ptrcxx . . $ autoSomeThing#ptr.) {
413 #if _Thing_auto_DEBUGcxx . . $ `sys/Thing.h:28:9`!
414 if (our ptr)
415 fprintf (Melder_stderr, "move constructor %p from same class %s\n",
416 our ptr, Melder_peek32to8 (our ptr -> classInfo -> className));
417 #endif
418 other. ptrcxx . . $ autoSomeThing#ptr. = nullptr;
419 }
420 template <class Y> autoSomeThingcxx . . $ autoSomeThing#`autoSomeThing<T>`(327dc2ad7b4df21).<T> (autoSomeThingcxx . . $ autoSomeThing#<Y>&& other) noexcept : ptrcxx . . $ autoSomeThing#ptr. (other.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()) {
421 #if _Thing_auto_DEBUGcxx . . $ `sys/Thing.h:28:9`!
422 if (our ptr)
423 fprintf (Melder_stderr, "move constructor %p from other class %s\n",
424 our ptr, Melder_peek32to8 (our ptr -> classInfo -> className));
425 #endif
426 other. _zerocxx . . $ autoSomeThing#_zero(ced63f7c635d850d).();
427 }
428 /*
429 The compiler should treat assignments from autoSomeThing r-values, as in
430 extern autoPitch Pitch_create (...);
431 autoPitch pitch;
432 pitch = Pitch_create (...);
433 as move assignments.
434 */
435 autoSomeThingcxx . . $ autoSomeThing#<T>& operatorcxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8).= (autoSomeThingcxx . . $ autoSomeThing#<T>&& other) noexcept {
436 if (other. ptrcxx . . $ autoSomeThing#ptr. != ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr.) {
437 #if _Thing_auto_DEBUGcxx . . $ `sys/Thing.h:28:9`!
438 fprintf (Melder_stderr, "move assignment before %p from same class %s\n",
439 our ptr, our ptr ? Melder_peek32to8 (our ptr -> classInfo -> className) : "(class unknown)");
440 #endif
441 if (ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr.)
442 _Thing_forgetcxx . . $ _Thing_forget(97cddd01752945f6). (ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr.);
443 ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr. = other. ptrcxx . . $ autoSomeThing#ptr.;
444 #if _Thing_auto_DEBUGcxx . . $ `sys/Thing.h:28:9`!
445 fprintf (Melder_stderr, "move assignment after %p from same class %s\n",
446 our ptr, our ptr ? Melder_peek32to8 (our ptr -> classInfo -> className) : "(class unknown)");
447 #endif
448 other. ptrcxx . . $ autoSomeThing#ptr. = nullptr;
449 }
450 return *this;
451 }
452 template <class Y> autoSomeThingcxx . . $ autoSomeThing#<T>& operatorcxx . . $ autoSomeThing#`operator=`(ed6a1a4da6aedae3).= (autoSomeThingcxx . . $ autoSomeThing#<Y>&& other) noexcept {
453 if (other.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).() != ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr.) {
454 #if _Thing_auto_DEBUGcxx . . $ `sys/Thing.h:28:9`!
455 fprintf (Melder_stderr, "move assignment before %p from other class %s\n",
456 our ptr, our ptr ? Melder_peek32to8 (our ptr -> classInfo -> className) : "(class unknown)");
457 #endif
458 if (ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr.)
459 _Thing_forgetcxx . . $ _Thing_forget(97cddd01752945f6). (ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr.);
460 ourcxx . . $ `melder/melder.h:42:9`! ptrcxx . . $ autoSomeThing#ptr. = other.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).();
461 #if _Thing_auto_DEBUGcxx . . $ `sys/Thing.h:28:9`!
462 fprintf (Melder_stderr, "move assignment after %p from other class %s\n",
463 our ptr, our ptr ? Melder_peek32to8 (our ptr -> classInfo -> className) : "(class unknown)");
464 #endif
465 other. _zerocxx . . $ autoSomeThing#_zero(ced63f7c635d850d).();
466 }
467 return *this;
468 }
469 /*
470 Move semantics from l-values can be achieved with move syntax:
471 autoPitch pitch2 = pitch1.move(); // calls the move constructor and therefore nullifies pitch1
472
473 pitch2 = pitch1.move(); // performs move assignment and therefore nullifies pitch1
474 */
475 autoSomeThingcxx . . $ autoSomeThing#<T>&& movecxx . . $ autoSomeThing#move(25acc3554b7d1642). () noexcept { return static_cast <autoSomeThingcxx . . $ autoSomeThing#<T>&&> (*this); }
476 /*
477 Returning autoSomeThing from a function works as hoped for:
478 autoPitch Sound_to_Pitch (Sound me) {
479 autoPitch thee = Pitch_create (...);
480 ...
481 return thee;
482 }
483 autoPitch pitch = Sound_to_Pitch (sound);
484 returns a moved `thee` in `pitch`. This works because return values from automatic (i.e. non-static) variables are r-values.
485
486 In function arguments, transfer of ownership works only explicitly:
487 autoPitch pitch = Pitch_create (...);
488 collection -> addItem_move (pitch.move()); // compiler error if you don't call move()
489 */
490
491 /*
492 The C++ language does allow us to do
493 Sampled sampled;
494 Pitch pitch = static_cast <Pitch> (sampled);
495 but not
496 autoSampled sampled;
497 autoPitch pitch = static_cast <autoPitch> (sampled.move());
498 or anything like that.
499
500 So we create a method that casts and moves at the same time:
501 autoPitch pitch = sampled.static_cast_move <structPitch>();
502 */
503 template <class Y> autoSomeThingcxx . . $ autoSomeThing#<Y> static_cast_movecxx . . $ autoSomeThing#static_cast_move(8f979c7064dbabcf).() noexcept {
504 T* nakedPointer_oldType = ourcxx . . $ `melder/melder.h:42:9`! releaseToAmbiguousOwnercxx . . $ autoSomeThing#releaseToAmbiguousOwner(258ee1c9e0a18a5c).(); // throw the object in the air...
505 Y* nakedPointer_newType = static_cast<Y*> (nakedPointer_oldType);
506 autoSomeThingcxx . . $ autoSomeThing#<Y> newObject;
507 newObject. adoptFromAmbiguousOwnercxx . . $ autoSomeThing#adoptFromAmbiguousOwner(6f97683fca3ece44). (nakedPointer_newType); // ...and catch it
508 return newObject;
509 }
510 };
511
512 typedef autoSomeThingcxx . . $ autoSomeThing#<structThingcxx . . $ structThing#> autoThingcxx . . $ autoThing#;
513
514 #define Thing_newcxx . . $ `sys/Thing.h:514:9`!(Klas) Thing_newFromClass (class##Klas).static_cast_move <struct##Klas>()
515 /*
516 Function:
517 return a new object of class 'klas'.
518 Postconditions:
519 result -> classInfo == class'klas';
520 other members are 0.
521 */
522
523 autoThingcxx . . $ autoThing# Thing_newFromClasscxx . . $ Thing_newFromClass(f67a3a815c0fee1d). (ClassInfocxx . . $ ClassInfo# klas);
524 /*
525 Function:
526 return a new object of class 'klas'.
527 Postconditions:
528 result -> classInfo == 'klas';
529 other members are 0.
530 */
531
532 autoThingcxx . . $ autoThing# Thing_newFromClassNamecxx . . $ Thing_newFromClassName(87ac77d99e238079). (conststring32cxx . . $ conststring32# className, int *out_formatVersion);
533 /*
534 Function:
535 return a new object of class 'className', or null if the class name is not recognized.
536 Postconditions:
537 result -> classInfo == class'className';
538 other members are 0.
539 Side effect:
540 see Thing_classFromClassName.
541 */
542
543 struct SharedThingcxx . . $ SharedThing# {
544 Thingcxx . . $ Thing# thingcxx . . $ SharedThing#thing.;
545 };
546 extern stdcxx . . $ std/::unordered_mapcxx . . $ std/unordered_map# <integercxx . . $ integer#, SharedThingcxx . . $ SharedThing#> theSharedThingscxx . . $ theSharedThings.;
547 void Thing_sharecxx . . $ Thing_share(97cddd01752945f6). (Thingcxx . . $ Thing# me);
548
549 template <class T>
550 class SomeThingHandlecxx . . $ SomeThingHandle# {
551 integercxx . . $ integer# _idcxx . . $ SomeThingHandle#_id.;
552 public:
553 SomeThingHandlecxx . . $ SomeThingHandle#`SomeThingHandle<T>`(ebd0a1552f8ce24f). (T *me) {
554 if (mycxx . . $ `melder/melder.h:44:9`! idOfExistingThing == 0)
555 Thing_sharecxx . . $ Thing_share(97cddd01752945f6). (me);
556 ourcxx . . $ `melder/melder.h:42:9`! _idcxx . . $ SomeThingHandle#_id. = mycxx . . $ `melder/melder.h:44:9`! idOfExistingThing;
557 }
558 T *getcxx . . $ SomeThingHandle#get(155dc7f103cdb9db).() {
559 auto it = theSharedThingscxx . . $ theSharedThings.. findcxx . . $ std/unordered_map#find(f711f2f81556fe93). (ourcxx . . $ `melder/melder.h:42:9`! _idcxx . . $ SomeThingHandle#_id.);
560 return it ==cxx . . $ std/__detail/`operator==`(6ab342297d2d0253). theSharedThingscxx . . $ theSharedThings..endcxx . . $ std/unordered_map#end(58ae48d364eea91c).() ? nullptr : it ->cxx . . $ std/__detail/_Node_iterator#`operator->`(35610babf8706b50). secondcxx . . $ std/pair#second.. thingcxx . . $ SharedThing#thing.;
561 }
562 explicit operatorcxx . . $ SomeThingHandle#`operator bool`(3482b152b9333168). bool () const noexcept {
563 return theSharedThingscxx . . $ theSharedThings.. findcxx . . $ std/unordered_map#find(f711f2f81556fe93). (ourcxx . . $ `melder/melder.h:42:9`! _idcxx . . $ SomeThingHandle#_id.) !=cxx . . $ std/__detail/`operator!=`(6ab342297d2d0253). theSharedThingscxx . . $ theSharedThings..endcxx . . $ std/unordered_map#end(58ae48d364eea91c).();
564 }
565 integercxx . . $ integer# getIdcxx . . $ SomeThingHandle#getId(da6335ded91d79c2). () {
566 return ourcxx . . $ `melder/melder.h:42:9`! _idcxx . . $ SomeThingHandle#_id.; // this can be an obsolete handle; that's fine (nice for debugging)
567 }
568 };
569
570 typedef SomeThingHandlecxx . . $ SomeThingHandle#<structThingcxx . . $ structThing#> ThingHandlecxx . . $ ThingHandle#;
571
572 #endif // _Thing_h_