ObjFW
OFObject.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2026 Jonathan Schleifer <js@nil.im>
3  *
4  * All rights reserved.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License version 3.0 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * version 3.0 for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * version 3.0 along with this program. If not, see
17  * <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "objfw-defs.h"
21 
22 #ifndef __STDC_LIMIT_MACROS
23 # define __STDC_LIMIT_MACROS
24 #endif
25 #ifndef __STDC_CONSTANT_MACROS
26 # define __STDC_CONSTANT_MACROS
27 #endif
28 
29 #include <stddef.h>
30 #include <stdint.h>
31 #include <stdbool.h>
32 #include <limits.h>
33 #include <math.h>
34 
35 #include "macros.h"
36 
37 #import "OFOnce.h"
38 
39 /*
40  * Some versions of MinGW require <winsock2.h> to be included before
41  * <windows.h>. Do this here to make sure this is always done in the correct
42  * order, even if another header includes just <windows.h>.
43  */
44 #ifdef __MINGW32__
45 # include <_mingw.h>
46 # ifdef __MINGW64_VERSION_MAJOR
47 # include <winsock2.h>
48 # include <windows.h>
49 # endif
50 #endif
51 
52 OF_ASSUME_NONNULL_BEGIN
53 
59 static const size_t OFNotFound = SIZE_MAX;
60 
64 typedef enum {
72 
81 typedef OFComparisonResult (*OFCompareFunction)(id _Nonnull left,
82  id _Nonnull right, void *_Nullable context);
83 
84 #ifdef OF_HAVE_BLOCKS
92 typedef OFComparisonResult (^OFComparator)(id _Nonnull left, id _Nonnull right);
93 #endif
94 
98 typedef enum {
104 #ifdef OF_BIG_ENDIAN
106 #else
108 #endif
110 
116 typedef struct OF_BOXABLE OFRange {
118  size_t location;
120  size_t length;
121 } OFRange;
122 
130 static OF_INLINE OFRange OF_CONST_FUNC
131 OFMakeRange(size_t start, size_t length)
132 {
133  OFRange range = { start, length };
134 
135  return range;
136 }
137 
145 static OF_INLINE bool
147 {
148  if (range1.location != range2.location)
149  return false;
150 
151  if (range1.length != range2.length)
152  return false;
153 
154  return true;
155 }
156 
163 static OF_INLINE size_t
165 {
166  if (range.length > SIZE_MAX - range.location) {
167  extern void OF_NO_RETURN_FUNC _OFThrowOutOfRangeException(void);
168  _OFThrowOutOfRangeException();
169  }
170 
171  return range.location + range.length;
172 }
173 
181 static OF_INLINE bool
182 OFLocationInRange(size_t location, OFRange range)
183 {
184  return (location >= range.location &&
185  location < OFEndOfRange(range));
186 }
187 
196 static OF_INLINE OFRange
198 {
199  OFRange range;
200 
201  if (range1.location >= OFEndOfRange(range2) ||
202  range2.location >= OFEndOfRange(range1))
203  return OFMakeRange(OFNotFound, 0);
204 
205  range.location = (range1.location > range2.location
206  ? range1.location : range2.location);
207  range.length = (OFEndOfRange(range1) < OFEndOfRange(range2))
208  ? OFEndOfRange(range1) - range.location
209  : OFEndOfRange(range2) - range.location;
210 
211  return range;
212 }
213 
223 static OF_INLINE OFRange
224 OFUnionRange(OFRange range1, OFRange range2)
225 {
226  OFRange range;
227 
228  if (range1.location > OFEndOfRange(range2) ||
229  range2.location > OFEndOfRange(range1))
230  return OFMakeRange(OFNotFound, 0);
231 
232  range.location = (range1.location < range2.location
233  ? range1.location : range2.location);
234  range.length = (OFEndOfRange(range1) > OFEndOfRange(range2))
235  ? OFEndOfRange(range1) - range.location
236  : OFEndOfRange(range2) - range.location;
237 
238  return range;
239 }
240 
244 typedef double OFTimeInterval;
245 
251 typedef struct OF_BOXABLE OFPoint {
253  float x;
255  float y;
256 } OFPoint;
257 
265 static OF_INLINE OFPoint OF_CONST_FUNC
266 OFMakePoint(float x, float y)
267 {
268  OFPoint point = { x, y };
269 
270  return point;
271 }
272 
280 static OF_INLINE bool
282 {
283  if (point1.x != point2.x)
284  return false;
285 
286  if (point1.y != point2.y)
287  return false;
288 
289  return true;
290 }
291 
297 typedef struct OF_BOXABLE OFSize {
299  float width;
301  float height;
302 } OFSize;
303 
311 static OF_INLINE OFSize OF_CONST_FUNC
312 OFMakeSize(float width, float height)
313 {
314  OFSize size = { width, height };
315 
316  return size;
317 }
318 
326 static OF_INLINE bool
328 {
329  if (size1.width != size2.width)
330  return false;
331 
332  if (size1.height != size2.height)
333  return false;
334 
335  return true;
336 }
337 
343 typedef struct OF_BOXABLE OFRect {
348 } OFRect;
349 
359 static OF_INLINE OFRect OF_CONST_FUNC
360 OFMakeRect(float x, float y, float width, float height)
361 {
362  OFRect rect = {
363  OFMakePoint(x, y),
364  OFMakeSize(width, height)
365  };
366 
367  return rect;
368 }
369 
377 static OF_INLINE bool
379 {
380  if (!OFEqualPoints(rect1.origin, rect2.origin))
381  return false;
382 
383  if (!OFEqualSizes(rect1.size, rect2.size))
384  return false;
385 
386  return true;
387 }
388 
397 static OF_INLINE OFRect
399 {
400  OFRect rect;
401 
402  rect.origin.x = (rect1.origin.x >= rect2.origin.x
403  ? rect1.origin.x : rect2.origin.x);
404  rect.origin.y = (rect1.origin.y >= rect2.origin.y
405  ? rect1.origin.y : rect2.origin.y);
406  rect.size.width = (rect1.origin.x + rect1.size.width <
407  rect2.origin.x + rect2.size.width
408  ? rect1.origin.x + rect1.size.width
409  : rect2.origin.x + rect2.size.width) - rect.origin.x;
410  rect.size.height = (rect1.origin.y + rect1.size.height <
411  rect2.origin.y + rect2.size.height
412  ? rect1.origin.y + rect1.size.height
413  : rect2.origin.y + rect2.size.height) - rect.origin.y;
414 
415  if (rect.size.width <= 0.0f || rect.size.height <= 0.0f) {
416  rect.origin.x = rect.origin.y = 0.0f;
417  rect.size.width = rect.size.height = 0.0f;
418  }
419 
420  return rect;
421 }
422 
428 typedef struct OF_BOXABLE OFVector3D {
430  float x;
432  float y;
434  float z;
435 } OFVector3D;
436 
445 static OF_INLINE OFVector3D OF_CONST_FUNC
446 OFMakeVector3D(float x, float y, float z)
447 {
448  OFVector3D vector = { x, y, z };
449 
450  return vector;
451 }
452 
460 static OF_INLINE bool
462 {
463  if (vector1.x != vector2.x)
464  return false;
465 
466  if (vector1.y != vector2.y)
467  return false;
468 
469  if (vector1.z != vector2.z)
470  return false;
471 
472  return true;
473 }
474 
482 static OF_INLINE OFVector3D
484 {
485  return OFMakeVector3D(vector1.x + vector2.x, vector1.y + vector2.y,
486  vector1.z + vector2.z);
487 }
488 
496 static OF_INLINE OFVector3D
498 {
499  return OFMakeVector3D(vector1.x - vector2.x, vector1.y - vector2.y,
500  vector1.z - vector2.z);
501 }
502 
510 static OF_INLINE OFVector3D
511 OFMultiplyVector3D(OFVector3D vector, float scalar)
512 {
513  return OFMakeVector3D(vector.x * scalar, vector.y * scalar,
514  vector.z * scalar);
515 }
516 
524 static OF_INLINE float
526 {
527  return vector1.x * vector2.x + vector1.y * vector2.y +
528  vector1.z * vector2.z;
529 }
530 
538 static OF_INLINE float
540 {
541  OFVector3D difference = OFSubtractVectors3D(vector1, vector2);
542 
543  return sqrtf(OFDotProductOfVectors3D(difference, difference));
544 }
545 
551 typedef struct OF_BOXABLE OFVector4D {
553  float x;
555  float y;
557  float z;
559  float w;
560 } OFVector4D;
561 
571 static OF_INLINE OFVector4D OF_CONST_FUNC
572 OFMakeVector4D(float x, float y, float z, float w)
573 {
574  OFVector4D vector = { x, y, z, w };
575 
576  return vector;
577 }
578 
586 static OF_INLINE bool
588 {
589  if (vector1.x != vector2.x)
590  return false;
591 
592  if (vector1.y != vector2.y)
593  return false;
594 
595  if (vector1.z != vector2.z)
596  return false;
597 
598  if (vector1.w != vector2.w)
599  return false;
600 
601  return true;
602 }
603 
611 static OF_INLINE OFVector4D
613 {
614  return OFMakeVector4D(vector1.x + vector2.x, vector1.y + vector2.y,
615  vector1.z + vector2.z, vector1.w + vector2.w);
616 }
617 
625 static OF_INLINE OFVector4D
627 {
628  return OFMakeVector4D(vector1.x - vector2.x, vector1.y - vector2.y,
629  vector1.z - vector2.z, vector1.w - vector2.w);
630 }
631 
639 static OF_INLINE OFVector4D
640 OFMultiplyVector4D(OFVector4D vector, float scalar)
641 {
642  return OFMakeVector4D(vector.x * scalar, vector.y * scalar,
643  vector.z * scalar, vector.w * scalar);
644 }
645 
653 static OF_INLINE float
655 {
656  return vector1.x * vector2.x + vector1.y * vector2.y +
657  vector1.z * vector2.z + vector1.w * vector2.w;
658 }
659 
667 static OF_INLINE float
669 {
670  OFVector4D difference = OFSubtractVectors4D(vector1, vector2);
671 
672  return sqrtf(OFDotProductOfVectors4D(difference, difference));
673 }
674 
681 static OF_INLINE void
682 OFHashAddByte(unsigned long *_Nonnull hash, unsigned char byte)
683 {
684  uint32_t tmp = (uint32_t)*hash;
685 
686  tmp += byte;
687  tmp += tmp << 10;
688  tmp ^= tmp >> 6;
689 
690  *hash = tmp;
691 }
692 
699 static OF_INLINE void
700 OFHashAddHash(unsigned long *_Nonnull hash, unsigned long otherHash)
701 {
702  OFHashAddByte(hash, (otherHash >> 24) & 0xFF);
703  OFHashAddByte(hash, (otherHash >> 16) & 0xFF);
704  OFHashAddByte(hash, (otherHash >> 8) & 0xFF);
705  OFHashAddByte(hash, otherHash & 0xFF);
706 }
707 
713 static OF_INLINE void
714 OFHashFinalize(unsigned long *_Nonnull hash)
715 {
716  uint32_t tmp = (uint32_t)*hash;
717 
718  tmp += tmp << 3;
719  tmp ^= tmp >> 11;
720  tmp += tmp << 15;
721 
722  *hash = tmp;
723 }
724 
725 @class OFMethodSignature;
726 @class OFString;
727 @class OFThread;
728 
734 @protocol OFObject
740 - (Class)class;
741 
747 - (nullable Class)superclass;
748 
761 - (unsigned long)hash;
762 
768 - (unsigned int)retainCount;
769 
775 - (bool)isProxy;
776 
783 - (bool)isKindOfClass: (Class)class_;
784 
792 - (bool)isMemberOfClass: (Class)class_;
793 
801 - (bool)respondsToSelector: (SEL)selector;
802 
809 - (bool)conformsToProtocol: (Protocol *)protocol;
810 
817 - (nullable IMP)methodForSelector: (SEL)selector;
818 
825 - (nullable id)performSelector: (SEL)selector;
826 
835 - (nullable id)performSelector: (SEL)selector withObject: (nullable id)object;
836 
847 - (nullable id)performSelector: (SEL)selector
848  withObject: (nullable id)object1
849  withObject: (nullable id)object2;
850 
863 - (nullable id)performSelector: (SEL)selector
864  withObject: (nullable id)object1
865  withObject: (nullable id)object2
866  withObject: (nullable id)object3;
867 
882 - (nullable id)performSelector: (SEL)selector
883  withObject: (nullable id)object1
884  withObject: (nullable id)object2
885  withObject: (nullable id)object3
886  withObject: (nullable id)object4;
887 
900 - (bool)isEqual: (nullable id)object;
901 
908 - (instancetype)retain;
909 
916 - (void)release;
917 
924 - (instancetype)autorelease;
925 
931 - (instancetype)self;
932 
938 - (bool)allowsWeakReference;
939 
945 - (bool)retainWeakReference;
946 @end
947 
953 OF_ROOT_CLASS
954 @interface OFObject <OFObject>
955 {
956 @private
957 #ifndef __clang_analyzer__
958  Class _isa;
959 #else
960  Class _isa __attribute__((__unused__));
961 #endif
962 }
963 
964 #ifdef OF_HAVE_CLASS_PROPERTIES
965 # ifndef __cplusplus
966 @property (class, readonly, nonatomic) Class class;
967 # else
968 @property (class, readonly, nonatomic, getter=class) Class class_;
969 # endif
970 @property (class, readonly, nonatomic) OFString *className;
971 @property (class, readonly, nullable, nonatomic) Class superclass;
972 @property (class, readonly, nonatomic) OFString *description;
973 #endif
974 
975 #ifndef __cplusplus
976 @property (readonly, nonatomic) Class class;
977 #else
978 @property (readonly, nonatomic, getter=class) Class class_;
979 #endif
980 @property OF_NULLABLE_PROPERTY (readonly, nonatomic) Class superclass;
981 @property (readonly, nonatomic) unsigned long hash;
982 @property (readonly, nonatomic) unsigned int retainCount;
983 @property (readonly, nonatomic) bool isProxy;
984 @property (readonly, nonatomic) bool allowsWeakReference;
985 
989 @property (readonly, nonatomic) OFString *className;
990 
997 @property (readonly, nonatomic) OFString *description;
998 
1010 + (void)load;
1011 
1023 + (void)unload;
1024 
1034 + (void)initialize;
1035 
1047 + (instancetype)alloc;
1048 
1054 + (Class)class;
1055 
1061 + (OFString *)className;
1062 
1070 + (bool)isSubclassOfClass: (Class)class_;
1071 
1077 + (nullable Class)superclass;
1078 
1086 + (bool)instancesRespondToSelector: (SEL)selector;
1087 
1094 + (bool)conformsToProtocol: (Protocol *)protocol;
1095 
1104 + (nullable IMP)instanceMethodForSelector: (SEL)selector;
1105 
1115 + (nullable OFMethodSignature *)
1116  instanceMethodSignatureForSelector: (SEL)selector;
1117 
1125 + (OFString *)description;
1126 
1134 + (nullable IMP)replaceClassMethod: (SEL)selector
1135  withMethodFromClass: (Class)class_;
1136 
1145 + (nullable IMP)replaceInstanceMethod: (SEL)selector
1146  withMethodFromClass: (Class)class_;
1147 
1166 + (void)inheritMethodsFromClass: (Class)class_;
1167 
1176 + (bool)resolveClassMethod: (SEL)selector;
1177 
1186 + (bool)resolveInstanceMethod: (SEL)selector;
1187 
1196 + (id)copy;
1197 
1229 - (instancetype)init;
1230 
1238 - (nullable OFMethodSignature *)methodSignatureForSelector: (SEL)selector;
1239 
1247 - (void)dealloc;
1248 
1255 - (void)performSelector: (SEL)selector afterDelay: (OFTimeInterval)delay;
1256 
1266 - (void)performSelector: (SEL)selector
1267  withObject: (nullable id)object
1268  afterDelay: (OFTimeInterval)delay;
1269 
1281 - (void)performSelector: (SEL)selector
1282  withObject: (nullable id)object1
1283  withObject: (nullable id)object2
1284  afterDelay: (OFTimeInterval)delay;
1285 
1299 - (void)performSelector: (SEL)selector
1300  withObject: (nullable id)object1
1301  withObject: (nullable id)object2
1302  withObject: (nullable id)object3
1303  afterDelay: (OFTimeInterval)delay;
1304 
1320 - (void)performSelector: (SEL)selector
1321  withObject: (nullable id)object1
1322  withObject: (nullable id)object2
1323  withObject: (nullable id)object3
1324  withObject: (nullable id)object4
1325  afterDelay: (OFTimeInterval)delay;
1326 
1327 #ifdef OF_HAVE_THREADS
1335 - (void)performSelector: (SEL)selector
1336  onThread: (OFThread *)thread
1337  waitUntilDone: (bool)waitUntilDone;
1338 
1349 - (void)performSelector: (SEL)selector
1350  onThread: (OFThread *)thread
1351  withObject: (nullable id)object
1352  waitUntilDone: (bool)waitUntilDone;
1353 
1366 - (void)performSelector: (SEL)selector
1367  onThread: (OFThread *)thread
1368  withObject: (nullable id)object1
1369  withObject: (nullable id)object2
1370  waitUntilDone: (bool)waitUntilDone;
1371 
1386 - (void)performSelector: (SEL)selector
1387  onThread: (OFThread *)thread
1388  withObject: (nullable id)object1
1389  withObject: (nullable id)object2
1390  withObject: (nullable id)object3
1391  waitUntilDone: (bool)waitUntilDone;
1392 
1409 - (void)performSelector: (SEL)selector
1410  onThread: (OFThread *)thread
1411  withObject: (nullable id)object1
1412  withObject: (nullable id)object2
1413  withObject: (nullable id)object3
1414  withObject: (nullable id)object4
1415  waitUntilDone: (bool)waitUntilDone;
1416 
1423 - (void)performSelectorOnMainThread: (SEL)selector
1424  waitUntilDone: (bool)waitUntilDone;
1425 
1435 - (void)performSelectorOnMainThread: (SEL)selector
1436  withObject: (nullable id)object
1437  waitUntilDone: (bool)waitUntilDone;
1438 
1450 - (void)performSelectorOnMainThread: (SEL)selector
1451  withObject: (nullable id)object1
1452  withObject: (nullable id)object2
1453  waitUntilDone: (bool)waitUntilDone;
1454 
1468 - (void)performSelectorOnMainThread: (SEL)selector
1469  withObject: (nullable id)object1
1470  withObject: (nullable id)object2
1471  withObject: (nullable id)object3
1472  waitUntilDone: (bool)waitUntilDone;
1473 
1489 - (void)performSelectorOnMainThread: (SEL)selector
1490  withObject: (nullable id)object1
1491  withObject: (nullable id)object2
1492  withObject: (nullable id)object3
1493  withObject: (nullable id)object4
1494  waitUntilDone: (bool)waitUntilDone;
1495 
1504 - (void)performSelector: (SEL)selector
1505  onThread: (OFThread *)thread
1506  afterDelay: (OFTimeInterval)delay;
1507 
1518 - (void)performSelector: (SEL)selector
1519  onThread: (OFThread *)thread
1520  withObject: (nullable id)object
1521  afterDelay: (OFTimeInterval)delay;
1522 
1535 - (void)performSelector: (SEL)selector
1536  onThread: (OFThread *)thread
1537  withObject: (nullable id)object1
1538  withObject: (nullable id)object2
1539  afterDelay: (OFTimeInterval)delay;
1540 
1555 - (void)performSelector: (SEL)selector
1556  onThread: (OFThread *)thread
1557  withObject: (nullable id)object1
1558  withObject: (nullable id)object2
1559  withObject: (nullable id)object3
1560  afterDelay: (OFTimeInterval)delay;
1561 
1578 - (void)performSelector: (SEL)selector
1579  onThread: (OFThread *)thread
1580  withObject: (nullable id)object1
1581  withObject: (nullable id)object2
1582  withObject: (nullable id)object3
1583  withObject: (nullable id)object4
1584  afterDelay: (OFTimeInterval)delay;
1585 #endif
1586 
1598 - (nullable id)forwardingTargetForSelector: (SEL)selector;
1599 
1609 - (void)doesNotRecognizeSelector: (SEL)selector OF_NO_RETURN;
1610 @end
1611 
1617 @protocol OFCopying
1627 - (id)copy;
1628 @end
1629 
1638 @protocol OFMutableCopying
1644 - (id)mutableCopy;
1645 @end
1646 
1655 @protocol OFComparing
1662 - (OFComparisonResult)compare: (id <OFComparing>)object;
1663 @end
1664 
1665 #ifdef __cplusplus
1666 extern "C" {
1667 #endif
1682 extern void *_Nullable OFAllocMemory(size_t count, size_t size)
1683  OF_MALLOC_FUNC OF_WARN_UNUSED_RESULT;
1684 
1699 extern void *_Nullable OFAllocZeroedMemory(size_t count, size_t size)
1700  OF_MALLOC_FUNC OF_WARN_UNUSED_RESULT;
1701 
1719 extern void *_Nullable OFResizeMemory(void *_Nullable pointer, size_t count,
1720  size_t size) OF_WARN_UNUSED_RESULT;
1721 
1729 extern void OFFreeMemory(void *_Nullable pointer);
1730 
1731 #ifdef OF_APPLE_RUNTIME
1732 extern void *_Null_unspecified objc_autoreleasePoolPush(void);
1733 extern void objc_autoreleasePoolPop(void *_Null_unspecified pool);
1734 extern id _Nullable objc_retain(id _Nullable object);
1735 extern id _Nullable objc_retainBlock(id _Nullable block);
1736 extern id _Nullable objc_retainAutorelease(id _Nullable object);
1737 extern void objc_release(id _Nullable object);
1738 extern id _Nullable objc_autorelease(id _Nullable object);
1739 extern id _Nullable objc_autoreleaseReturnValue(id _Nullable object);
1740 extern id _Nullable objc_retainAutoreleaseReturnValue(id _Nullable object);
1741 extern id _Nullable objc_retainAutoreleasedReturnValue(id _Nullable object);
1742 extern id _Nullable objc_storeStrong(id _Nullable *_Nonnull object,
1743  id _Nullable value);
1744 extern id _Nullable objc_storeWeak(id _Nullable *_Nonnull object,
1745  id _Nullable value);
1746 extern id _Nullable objc_loadWeakRetained(id _Nullable *_Nonnull object);
1747 extern _Nullable id objc_initWeak(id _Nullable *_Nonnull object,
1748  id _Nullable value);
1749 extern void objc_destroyWeak(id _Nullable *_Nonnull object);
1750 extern id _Nullable objc_loadWeak(id _Nullable *_Nonnull object);
1751 extern void objc_copyWeak(id _Nullable *_Nonnull dest,
1752  id _Nullable *_Nonnull src);
1753 extern void objc_moveWeak(id _Nullable *_Nonnull dest,
1754  id _Nullable *_Nonnull src);
1755 # ifdef OF_DECLARE_CONSTRUCT_INSTANCE
1756 extern id _Nullable objc_constructInstance(Class _Nullable class_,
1757  void *_Nullable bytes);
1758 extern void *_Nullable objc_destructInstance(id _Nullable object);
1759 # endif
1760 # ifdef OF_DECLARE_SET_ASSOCIATED_OBJECT
1761 typedef enum objc_associationPolicy {
1768 extern void objc_setAssociatedObject(id _Nonnull object,
1769  const void *_Nonnull key, id _Nullable value,
1770  objc_associationPolicy policy);
1771 extern id _Nullable objc_getAssociatedObject(id _Nonnull object,
1772  const void *_Nonnull key);
1773 extern void objc_removeAssociatedObjects(id _Nonnull object);
1774 # endif
1775 #endif
1776 
1789 extern id OFAllocObject(Class class_, size_t extraSize, size_t extraAlignment,
1790  void *_Nullable *_Nullable extra);
1791 
1814 extern void OF_NO_RETURN_FUNC OFMethodNotFound(id self, SEL _cmd);
1815 
1821 extern void OFHashInit(unsigned long *_Nonnull hash);
1822 
1828 extern uint16_t OFRandom16(void);
1829 
1835 extern uint32_t OFRandom32(void);
1836 
1842 extern uint64_t OFRandom64(void);
1843 #ifdef __cplusplus
1844 }
1845 #endif
1846 
1847 OF_ASSUME_NONNULL_END
1848 
1849 #import "OFBlock.h"
1850 #import "OFObject+KeyValueCoding.h"
static OF_INLINE OFRect OF_CONST_FUNC OFMakeRect(float x, float y, float width, float height)
Creates a new OFRect.
Definition: OFObject.h:360
static OF_INLINE void OFHashFinalize(unsigned long *hash)
Finalizes the specified hash.
Definition: OFObject.h:714
static OF_INLINE void OFHashAddByte(unsigned long *hash, unsigned char byte)
Adds the specified byte to the hash.
Definition: OFObject.h:682
static OF_INLINE OFVector4D OFMultiplyVector4D(OFVector4D vector, float scalar)
Multiplies the specified vector with a scalar.
Definition: OFObject.h:640
static OF_INLINE OFVector4D OFAddVectors4D(OFVector4D vector1, OFVector4D vector2)
Adds the two specified vectors.
Definition: OFObject.h:612
OFComparisonResult
A result of a comparison.
Definition: OFObject.h:64
@ OFOrderedAscending
Definition: OFObject.h:66
@ OFOrderedDescending
Definition: OFObject.h:70
@ OFOrderedSame
Definition: OFObject.h:68
static OF_INLINE OFRange OFUnionRange(OFRange range1, OFRange range2)
Returns the union of the two ranges if they are overlapping or adjacent, otherwise returns a range wi...
Definition: OFObject.h:224
void OFHashInit(unsigned long *hash)
Initializes the specified hash.
Definition: OFObject.m:276
static OF_INLINE size_t OFEndOfRange(OFRange range)
Returns the end of the range, which is its location + its length.
Definition: OFObject.h:164
static OF_INLINE OFVector3D OFAddVectors3D(OFVector3D vector1, OFVector3D vector2)
Adds the two specified vectors.
Definition: OFObject.h:483
static OF_INLINE bool OFEqualVectors4D(OFVector4D vector1, OFVector4D vector2)
Returns whether the two vectors are equal.
Definition: OFObject.h:587
OFComparisonResult(^ OFComparator)(id left, id right)
A comparator to compare two objects.
Definition: OFObject.h:92
static OF_INLINE float OFDotProductOfVectors4D(OFVector4D vector1, OFVector4D vector2)
Calculates the dot product of the two specified vectors.
Definition: OFObject.h:654
static OF_INLINE bool OFEqualRanges(OFRange range1, OFRange range2)
Returns whether the two ranges are equal.
Definition: OFObject.h:146
static OF_INLINE void OFHashAddHash(unsigned long *hash, unsigned long otherHash)
Adds the specified hash to the hash.
Definition: OFObject.h:700
static OF_INLINE bool OFEqualSizes(OFSize size1, OFSize size2)
Returns whether the two sizes are equal.
Definition: OFObject.h:327
static OF_INLINE OFSize OF_CONST_FUNC OFMakeSize(float width, float height)
Creates a new OFSize.
Definition: OFObject.h:312
uint32_t OFRandom32(void)
Returns 32 bit or non-cryptographical randomness.
Definition: OFObject.m:220
void * OFAllocZeroedMemory(size_t count, size_t size) OF_MALLOC_FUNC
Allocates memory for the specified number of items of the specified size and initializes it with zero...
Definition: OFObject.m:119
void * OFResizeMemory(void *pointer, size_t count, size_t size)
Resizes memory to the specified number of items of the specified size.
Definition: OFObject.m:138
void OFFreeMemory(void *pointer)
Frees memory allocated by OFAllocMemory, OFAllocZeroedMemory or OFResizeMemory.
Definition: OFObject.m:156
double OFTimeInterval
A time interval in seconds.
Definition: OFObject.h:244
static OF_INLINE OFRect OFIntersectionRect(OFRect rect1, OFRect rect2)
Returns the intersection of the two rectangles or a rectangle with x, y, width and height set to 0 if...
Definition: OFObject.h:398
static OF_INLINE OFVector3D OFMultiplyVector3D(OFVector3D vector, float scalar)
Multiplies the specified vector with a scalar.
Definition: OFObject.h:511
static OF_INLINE OFVector4D OFSubtractVectors4D(OFVector4D vector1, OFVector4D vector2)
Subtracts the second vector from the first vector.
Definition: OFObject.h:626
static OF_INLINE float OFDotProductOfVectors3D(OFVector3D vector1, OFVector3D vector2)
Calculates the dot product of the two specified vectors.
Definition: OFObject.h:525
static OF_INLINE bool OFLocationInRange(size_t location, OFRange range)
Returns whether the specified location is in the specified range.
Definition: OFObject.h:182
static OF_INLINE float OFDistanceOfVectors4D(OFVector4D vector1, OFVector4D vector2)
Calculates the distance between two vectors.
Definition: OFObject.h:668
uint64_t OFRandom64(void)
Returns 64 bit or non-cryptographical randomness.
Definition: OFObject.m:246
static const size_t OFNotFound
A special not found index.
Definition: OFObject.h:59
OFByteOrder
An enum for representing endianness.
Definition: OFObject.h:98
@ OFByteOrderBigEndian
Definition: OFObject.h:100
@ OFByteOrderLittleEndian
Definition: OFObject.h:102
@ OFByteOrderNative
Definition: OFObject.h:107
static OF_INLINE bool OFEqualVectors3D(OFVector3D vector1, OFVector3D vector2)
Returns whether the two vectors are equal.
Definition: OFObject.h:461
static OF_INLINE OFRange OFIntersectionRange(OFRange range1, OFRange range2)
Returns the intersection of the two ranges or OFNotFound and length 0 if they don't intersect.
Definition: OFObject.h:197
static OF_INLINE OFVector3D OFSubtractVectors3D(OFVector3D vector1, OFVector3D vector2)
Subtracts the second vector from the first vector.
Definition: OFObject.h:497
id OFAllocObject(Class class_, size_t extraSize, size_t extraAlignment, void **extra)
Allocates a new object.
Definition: OFObject.m:432
uint16_t OFRandom16(void)
Returns 16 bit or non-cryptographical randomness.
Definition: OFObject.m:190
static OF_INLINE bool OFEqualPoints(OFPoint point1, OFPoint point2)
Returns whether the two points are equal.
Definition: OFObject.h:281
static OF_INLINE bool OFEqualRects(OFRect rect1, OFRect rect2)
Returns whether the two rectangles are equal.
Definition: OFObject.h:378
static OF_INLINE float OFDistanceOfVectors3D(OFVector3D vector1, OFVector3D vector2)
Calculates the distance between two vectors.
Definition: OFObject.h:539
OFComparisonResult(* OFCompareFunction)(id left, id right, void *context)
A function to compare two objects.
Definition: OFObject.h:81
static OF_INLINE OFVector4D OF_CONST_FUNC OFMakeVector4D(float x, float y, float z, float w)
Creates a new OFVector4D.
Definition: OFObject.h:572
void OFMethodNotFound(id self, SEL _cmd)
This function is called when a method is not found.
Definition: OFObject.m:412
static OF_INLINE OFPoint OF_CONST_FUNC OFMakePoint(float x, float y)
Creates a new OFPoint.
Definition: OFObject.h:266
static OF_INLINE OFRange OF_CONST_FUNC OFMakeRange(size_t start, size_t length)
Creates a new OFRange.
Definition: OFObject.h:131
void * OFAllocMemory(size_t count, size_t size) OF_MALLOC_FUNC
Allocates memory for the specified number of items of the specified size.
Definition: OFObject.m:101
static OF_INLINE OFVector3D OF_CONST_FUNC OFMakeVector3D(float x, float y, float z)
Creates a new OFVector3D.
Definition: OFObject.h:446
id(* IMP)(id object, SEL selector,...)
A method implementation.
Definition: ObjFWRT.h:146
void *_Null_unspecified objc_autoreleasePoolPush(void)
Creates a new autorelease pool and puts it on top of the stack of autorelease pools.
Definition: autorelease-foundation.m:37
void objc_removeAssociatedObjects(id object)
Removes all associated objects for the specified object.
Definition: association.m:251
void * objc_destructInstance(id object)
Destructs the specified object.
Definition: instance.m:131
id objc_constructInstance(Class class_, void *bytes)
Constructs an instance of the specified class in the specified array of bytes.
Definition: instance.m:115
void objc_setAssociatedObject(id object, const void *key, id value, objc_associationPolicy policy)
Sets an associated object on the specified object for the specified key.
Definition: association.m:131
id objc_getAssociatedObject(id object, const void *key)
Returns the associated object on the specified object for the specified key.
Definition: association.m:208
void objc_autoreleasePoolPop(void *_Null_unspecified pool)
Drains the specified autorelease pool and all pools on top of it and removes it from the stack of aut...
const struct objc_protocol * Protocol
A protocol.
Definition: ObjFWRT.h:117
objc_associationPolicy
A policy for object association, see objc_setAssociatedObject.
Definition: ObjFWRT.h:183
@ OBJC_ASSOCIATION_RETAIN_NONATOMIC
Associate the object like a retained, nonatomic property.
Definition: ObjFWRT.h:187
@ OBJC_ASSOCIATION_COPY
Associate the object like a copied property.
Definition: ObjFWRT.h:193
@ OBJC_ASSOCIATION_RETAIN
Associate the object like a retained property.
Definition: ObjFWRT.h:189
@ OBJC_ASSOCIATION_ASSIGN
Associate the object like an assigned property.
Definition: ObjFWRT.h:185
@ OBJC_ASSOCIATION_COPY_NONATOMIC
Associate the object like a copied, nonatomic property.
Definition: ObjFWRT.h:191
A class for parsing type encodings and accessing them.
Definition: OFMethodSignature.h:33
The root class for all other classes inside ObjFW.
Definition: OFObject.h:956
OFString * description
A description for the object.
Definition: OFObject.h:998
OFString * className
The name of the object's class.
Definition: OFObject.h:990
instancetype init()
Initializes an already allocated object.
Definition: OFObject.m:671
void dealloc()
Deallocates the object.
Definition: OFObject.m:1270
id copy()
Returns the class.
Definition: OFObject.m:1326
void unload()
A method which is called when the class is unloaded from the runtime.
Definition: OFObject.m:507
instancetype alloc()
Allocates memory for an instance of the class and sets up the memory pool for the object.
Definition: OFObject.m:515
void initialize()
A method which is called the moment before the first call to the class is being made.
Definition: OFObject.m:511
void load()
A method which is called once when the class is loaded into the runtime.
Definition: OFObject.m:472
A class for handling strings.
Definition: OFString.h:143
A class which provides portable threads.
Definition: OFThread.h:66
A protocol for comparing objects.
Definition: OFObject.h:1656
A protocol for the creation of copies.
Definition: OFObject.h:1618
id copy()
Copies the object.
A protocol for the creation of mutable copies.
Definition: OFObject.h:1639
id mutableCopy()
Creates a mutable copy of the object.
instancetype autorelease()
Adds the object to the topmost autorelease pool of the thread's autorelease pool stack.
instancetype self()
Returns the receiver.
void release()
Decreases the retain count.
instancetype retain()
Increases the retain count.
bool retainWeakReference()
Retain a weak reference to this object.
A point in 2D space.
Definition: OFObject.h:251
float y
Definition: OFObject.h:255
float x
Definition: OFObject.h:253
A range.
Definition: OFObject.h:116
size_t length
Definition: OFObject.h:120
size_t location
Definition: OFObject.h:118
A rectangle.
Definition: OFObject.h:343
OFPoint origin
Definition: OFObject.h:345
OFSize size
Definition: OFObject.h:347
A size.
Definition: OFObject.h:297
float width
Definition: OFObject.h:299
float height
Definition: OFObject.h:301
A vector in 3D space.
Definition: OFObject.h:428
float x
Definition: OFObject.h:430
float y
Definition: OFObject.h:432
float z
Definition: OFObject.h:434
A vector in 4D space.
Definition: OFObject.h:551
float x
Definition: OFObject.h:553
float z
Definition: OFObject.h:557
float y
Definition: OFObject.h:555
float w
Definition: OFObject.h:559