| 1 |
#ifndef _Thing_h_ |
| 2 |
#define _Thing_h_ |
| 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" |
| 25 |
/* The macros for struct and class definitions: */ |
| 26 |
#include "oo.h" |
| 27 |
|
| 28 |
#define _Thing_auto_DEBUG 0 |
| 29 |
|
| 30 |
typedef struct structClassInfo *ClassInfo; |
| 31 |
struct structClassInfo { |
| 32 |
/* |
| 33 |
The following five fields are statically initialized by the Thing_implement() macro. |
| 34 |
*/ |
| 35 |
conststring32 className; |
| 36 |
ClassInfo semanticParent; |
| 37 |
integer size; |
| 38 |
Thing (* _new) (); // objects have to be constructed via this function, because it calls C++ "new", which initializes the C++ class pointer |
| 39 |
integer 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 |
integer sequentialUniqueIdOfReadableClass; |
| 45 |
/* |
| 46 |
The following field is initialized by Thing_dummyObject(), which is used only rarely. |
| 47 |
*/ |
| 48 |
Thing dummyObject; |
| 49 |
}; |
| 50 |
|
| 51 |
#define Thing_declare(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_define(klas,syntacticParentKlas) \ |
| 61 |
Thing_declare (klas); \ |
| 62 |
typedef struct##syntacticParentKlas klas##_Parent; \ |
| 63 |
struct struct##klas : public struct##syntacticParentKlas |
| 64 |
|
| 65 |
#define Thing_implement(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_pureVirtual(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 structThing *Thing; // we need an explicit "struct" here, because this is a forward declaration |
| 79 |
typedef struct structThing *mutableThing; |
| 80 |
typedef const struct structThing *constThing; |
| 81 |
extern ClassInfo classThing; |
| 82 |
extern struct structClassInfo theClassInfo_Thing; |
| 83 |
struct structThing { |
| 84 |
ClassInfo classInfo; // the Praat class pointer (every object also has a C++ class pointer initialized by C++ "new") |
| 85 |
autostring32 name; |
| 86 |
integer idOfExistingThing; // 0 for most Things |
| 87 |
void * operator new (size_t size) { return Melder_calloc (char, (int64) size); } |
| 88 |
void operator delete (void *ptr, size_t /* size */) { Melder_free (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 ~structThing () noexcept { } |
| 98 |
|
| 99 |
virtual void v9_destroy () 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_info (); |
| 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_assertInvariants () { } |
| 124 |
/* |
| 125 |
derived::v1_assertInvariants typically calls base::v1_assertInvariants at start |
| 126 |
*/ |
| 127 |
friend void Thing_assertInvariants (Thing me) { |
| 128 |
my v1_assertInvariants (); |
| 129 |
} |
| 130 |
virtual void v_checkConstraints () { } |
| 131 |
/* |
| 132 |
derived::v_checkConstraints typically calls base::v_checkConstraints at start |
| 133 |
*/ |
| 134 |
virtual void v_nameChanged () { } |
| 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_copyPreferencesToInstance () { } |
| 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_repairPreferences () { } |
| 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 forget(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_nozero(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 |
conststring32 Thing_className (Thing me); |
| 174 |
/* Return your class name. */ |
| 175 |
|
| 176 |
bool Thing_isa (Thing me, 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_isSubclass (ClassInfo klas, 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_info (Thing me); |
| 191 |
void Thing_infoWithIdAndFile (Thing me, integer id, 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_recognizeClassesByName (ClassInfo readableClass); |
| 210 |
template <typename... ClassInfoType> |
| 211 |
void Thing_recognizeClassesByName (ClassInfoType... classInfos) { |
| 212 |
( Thing_recognizeClassesByName (classInfos), ... ); // folded over comma |
| 213 |
} |
| 214 |
void Thing_recognizeClassByOtherName (ClassInfo readableClass, conststring32 otherName); |
| 215 |
|
| 216 |
void Thing_listReadableClasses (); |
| 217 |
|
| 218 |
ClassInfo Thing_classFromClassName (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_dummyObject(klas) \ |
| 230 |
((klas) _Thing_dummyObject (class##klas)) |
| 231 |
Thing _Thing_dummyObject (ClassInfo classInfo); |
| 232 |
|
| 233 |
conststring32 Thing_getName (Thing me); |
| 234 |
/* Return a pointer to your internal name (which can be null). */ |
| 235 |
conststring32 Thing_messageName (constThing me); |
| 236 |
conststring32 Thing_messageNameAndAddress (Thing me); // for debugging |
| 237 |
|
| 238 |
void Thing_setName (Thing me, 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_cast(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_0(Klas,var,expr) \ |
| 251 |
Klas var = static_cast <Klas> (expr); /* The compiler checks this. */ \ |
| 252 |
Thing_isa (var, class##Klas) |
| 253 |
|
| 254 |
void Thing_swap (Thing me, 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_forget (Thing me); |
| 270 |
void _Thing_forget_nozero (Thing me); |
| 271 |
|
| 272 |
/* For debugging. */ |
| 273 |
|
| 274 |
extern std::atomic <integer> theTotalNumberOfThings; |
| 275 |
/* This number is 0 initially, increments at every successful `new', and decrements at every `forget'. */ |
| 276 |
|
| 277 |
template <class T> |
| 278 |
class autoSomeThing { |
| 279 |
T *ptr; |
| 280 |
public: |
| 281 |
/* |
| 282 |
A default constructor, as in |
| 283 |
autoPitch pitch; |
| 284 |
should initialize the pointer to null. |
| 285 |
*/ |
| 286 |
autoSomeThing () : ptr (nullptr) { |
| 287 |
#if _Thing_auto_DEBUG |
| 288 |
fprintf (Melder_stderr, "default constructor\n"); |
| 289 |
#endif |
| 290 |
} |
| 291 |
autoSomeThing (ClassInfo classInfo) { |
| 292 |
our ptr = classInfo -> _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 |
~autoSomeThing () noexcept { |
| 301 |
#if _Thing_auto_DEBUG |
| 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 (our ptr) { |
| 306 |
_Thing_forget (our ptr); |
| 307 |
our ptr = nullptr; |
| 308 |
} |
| 309 |
} |
| 310 |
T* get () const noexcept { |
| 311 |
return our 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* operator-> () const noexcept { // as r-value |
| 333 |
return our 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 releaseToUser () noexcept { |
| 356 |
our 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 adoptFromAmbiguousOwner (T* newPtr) noexcept { |
| 366 |
our reset(); |
| 367 |
our ptr = newPtr; |
| 368 |
} |
| 369 |
T* releaseToAmbiguousOwner () noexcept { |
| 370 |
T* temp = our ptr; |
| 371 |
our ptr = nullptr; // make the pointer non-automatic again |
| 372 |
return temp; |
| 373 |
} |
| 374 |
void reset () noexcept { |
| 375 |
_Thing_forget (our ptr); |
| 376 |
our ptr = nullptr; |
| 377 |
} |
| 378 |
void _zero () noexcept { |
| 379 |
our ptr = nullptr; |
| 380 |
} |
| 381 |
explicit operator bool () const noexcept { |
| 382 |
return !! our ptr; |
| 383 |
} |
| 384 |
bool operator== (autoSomeThing<T> other) const noexcept { |
| 385 |
return other. ptr == our ptr; |
| 386 |
} |
| 387 |
bool operator!= (autoSomeThing<T> other) const noexcept { |
| 388 |
return other. ptr != our 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 |
autoSomeThing<T> (const autoSomeThing<T>&) = delete; // disable copy constructor from an l-value of class T* |
| 403 |
template <class Y> autoSomeThing<T> (const autoSomeThing<Y>&) = delete; // disable copy constructor from an l-value of a descendant class of T* |
| 404 |
autoSomeThing<T>& operator= (const autoSomeThing<T>&) = delete; // disable copy assignment from an l-value of class T* |
| 405 |
template <class Y> autoSomeThing<T>& operator= (const 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 |
autoSomeThing<T> (autoSomeThing<T>&& other) noexcept : ptr (other. ptr) { |
| 413 |
#if _Thing_auto_DEBUG |
| 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. ptr = nullptr; |
| 419 |
} |
| 420 |
template <class Y> autoSomeThing<T> (autoSomeThing<Y>&& other) noexcept : ptr (other.get()) { |
| 421 |
#if _Thing_auto_DEBUG |
| 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. _zero(); |
| 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 |
autoSomeThing<T>& operator= (autoSomeThing<T>&& other) noexcept { |
| 436 |
if (other. ptr != our ptr) { |
| 437 |
#if _Thing_auto_DEBUG |
| 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 (our ptr) |
| 442 |
_Thing_forget (our ptr); |
| 443 |
our ptr = other. ptr; |
| 444 |
#if _Thing_auto_DEBUG |
| 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. ptr = nullptr; |
| 449 |
} |
| 450 |
return *this; |
| 451 |
} |
| 452 |
template <class Y> autoSomeThing<T>& operator= (autoSomeThing<Y>&& other) noexcept { |
| 453 |
if (other.get() != our ptr) { |
| 454 |
#if _Thing_auto_DEBUG |
| 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 (our ptr) |
| 459 |
_Thing_forget (our ptr); |
| 460 |
our ptr = other.get(); |
| 461 |
#if _Thing_auto_DEBUG |
| 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. _zero(); |
| 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 |
autoSomeThing<T>&& move () noexcept { return static_cast <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> autoSomeThing<Y> static_cast_move() noexcept { |
| 504 |
T* nakedPointer_oldType = our releaseToAmbiguousOwner(); // throw the object in the air... |
| 505 |
Y* nakedPointer_newType = static_cast<Y*> (nakedPointer_oldType); |
| 506 |
autoSomeThing<Y> newObject; |
| 507 |
newObject. adoptFromAmbiguousOwner (nakedPointer_newType); // ...and catch it |
| 508 |
return newObject; |
| 509 |
} |
| 510 |
}; |
| 511 |
|
| 512 |
typedef autoSomeThing<structThing> autoThing; |
| 513 |
|
| 514 |
#define Thing_new(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 |
autoThing Thing_newFromClass (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 |
autoThing Thing_newFromClassName (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 SharedThing { |
| 544 |
Thing thing; |
| 545 |
}; |
| 546 |
extern std::unordered_map <integer, SharedThing> theSharedThings; |
| 547 |
void Thing_share (Thing me); |
| 548 |
|
| 549 |
template <class T> |
| 550 |
class SomeThingHandle { |
| 551 |
integer _id; |
| 552 |
public: |
| 553 |
SomeThingHandle (T *me) { |
| 554 |
if (my idOfExistingThing == 0) |
| 555 |
Thing_share (me); |
| 556 |
our _id = my idOfExistingThing; |
| 557 |
} |
| 558 |
T *get() { |
| 559 |
auto it = theSharedThings. find (our _id); |
| 560 |
return it == theSharedThings.end() ? nullptr : it -> second. thing; |
| 561 |
} |
| 562 |
explicit operator bool () const noexcept { |
| 563 |
return theSharedThings. find (our _id) != theSharedThings.end(); |
| 564 |
} |
| 565 |
integer getId () { |
| 566 |
return our _id; // this can be an obsolete handle; that's fine (nice for debugging) |
| 567 |
} |
| 568 |
}; |
| 569 |
|
| 570 |
typedef SomeThingHandle<structThing> ThingHandle; |
| 571 |
|
| 572 |
#endif // _Thing_h_ |