ObjFW
Loading...
Searching...
No Matches
OFObject.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008-2024 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
34#include "macros.h"
35
36#import "OFOnce.h"
37
38/*
39 * Some versions of MinGW require <winsock2.h> to be included before
40 * <windows.h>. Do this here to make sure this is always done in the correct
41 * order, even if another header includes just <windows.h>.
42 */
43#ifdef __MINGW32__
44# include <_mingw.h>
45# ifdef __MINGW64_VERSION_MAJOR
46# include <winsock2.h>
47# include <windows.h>
48# endif
49#endif
50
51OF_ASSUME_NONNULL_BEGIN
52
66
75typedef OFComparisonResult (*OFCompareFunction)(id _Nonnull left,
76 id _Nonnull right, void *_Nullable context);
77
78#ifdef OF_HAVE_BLOCKS
86typedef OFComparisonResult (^OFComparator)(id _Nonnull left, id _Nonnull right);
87#endif
88
104
110typedef struct OF_BOXABLE OFRange {
112 size_t location;
114 size_t length;
115} OFRange;
116
124static OF_INLINE OFRange OF_CONST_FUNC
125OFMakeRange(size_t start, size_t length)
126{
127 OFRange range = { start, length };
128
129 return range;
130}
131
139static OF_INLINE bool
141{
142 if (range1.location != range2.location)
143 return false;
144
145 if (range1.length != range2.length)
146 return false;
147
148 return true;
149}
150
154typedef double OFTimeInterval;
155
161typedef struct OF_BOXABLE OFPoint {
163 float x;
165 float y;
166} OFPoint;
167
175static OF_INLINE OFPoint OF_CONST_FUNC
176OFMakePoint(float x, float y)
177{
178 OFPoint point = { x, y };
179
180 return point;
181}
182
190static OF_INLINE bool
192{
193 if (point1.x != point2.x)
194 return false;
195
196 if (point1.y != point2.y)
197 return false;
198
199 return true;
200}
201
207typedef struct OF_BOXABLE OFSize {
209 float width;
211 float height;
212} OFSize;
213
221static OF_INLINE OFSize OF_CONST_FUNC
222OFMakeSize(float width, float height)
223{
224 OFSize size = { width, height };
225
226 return size;
227}
228
236static OF_INLINE bool
238{
239 if (size1.width != size2.width)
240 return false;
241
242 if (size1.height != size2.height)
243 return false;
244
245 return true;
246}
247
253typedef struct OF_BOXABLE OFRect {
258} OFRect;
259
269static OF_INLINE OFRect OF_CONST_FUNC
270OFMakeRect(float x, float y, float width, float height)
271{
272 OFRect rect = {
273 OFMakePoint(x, y),
274 OFMakeSize(width, height)
275 };
276
277 return rect;
278}
279
287static OF_INLINE bool
289{
290 if (!OFEqualPoints(rect1.origin, rect2.origin))
291 return false;
292
293 if (!OFEqualSizes(rect1.size, rect2.size))
294 return false;
295
296 return true;
297}
298
304typedef struct OF_BOXABLE OFVector3D {
306 float x;
308 float y;
310 float z;
311} OFVector3D;
312
321static OF_INLINE OFVector3D OF_CONST_FUNC
322OFMakeVector3D(float x, float y, float z)
323{
324 OFVector3D vector = { x, y, z };
325
326 return vector;
327}
328
336static OF_INLINE bool
338{
339 if (vector1.x != vector2.x)
340 return false;
341
342 if (vector1.y != vector2.y)
343 return false;
344
345 if (vector1.z != vector2.z)
346 return false;
347
348 return true;
349}
350
356typedef struct OF_BOXABLE OFVector4D {
358 float x;
360 float y;
362 float z;
364 float w;
365} OFVector4D;
366
376static OF_INLINE OFVector4D OF_CONST_FUNC
377OFMakeVector4D(float x, float y, float z, float w)
378{
379 OFVector4D vector = { x, y, z, w };
380
381 return vector;
382}
383
391static OF_INLINE bool
393{
394 if (vector1.x != vector2.x)
395 return false;
396
397 if (vector1.y != vector2.y)
398 return false;
399
400 if (vector1.z != vector2.z)
401 return false;
402
403 if (vector1.w != vector2.w)
404 return false;
405
406 return true;
407}
408
415static OF_INLINE void
416OFHashAddByte(unsigned long *_Nonnull hash, unsigned char byte)
417{
418 uint32_t tmp = (uint32_t)*hash;
419
420 tmp += byte;
421 tmp += tmp << 10;
422 tmp ^= tmp >> 6;
423
424 *hash = tmp;
425}
426
433static OF_INLINE void
434OFHashAddHash(unsigned long *_Nonnull hash, unsigned long otherHash)
435{
436 OFHashAddByte(hash, (otherHash >> 24) & 0xFF);
437 OFHashAddByte(hash, (otherHash >> 16) & 0xFF);
438 OFHashAddByte(hash, (otherHash >> 8) & 0xFF);
439 OFHashAddByte(hash, otherHash & 0xFF);
440}
441
447static OF_INLINE void
448OFHashFinalize(unsigned long *_Nonnull hash)
449{
450 uint32_t tmp = (uint32_t)*hash;
451
452 tmp += tmp << 3;
453 tmp ^= tmp >> 11;
454 tmp += tmp << 15;
455
456 *hash = tmp;
457}
458
459static const size_t OFNotFound = SIZE_MAX;
460
461@class OFMethodSignature;
462@class OFString;
463@class OFThread;
464
470@protocol OFObject
476- (Class)class;
477
483- (nullable Class)superclass;
484
497- (unsigned long)hash;
498
504- (unsigned int)retainCount;
505
511- (bool)isProxy;
512
519- (bool)isKindOfClass: (Class)class_;
520
528- (bool)isMemberOfClass: (Class)class_;
529
537- (bool)respondsToSelector: (SEL)selector;
538
545- (bool)conformsToProtocol: (Protocol *)protocol;
546
553- (nullable IMP)methodForSelector: (SEL)selector;
554
561- (nullable id)performSelector: (SEL)selector;
562
571- (nullable id)performSelector: (SEL)selector withObject: (nullable id)object;
572
583- (nullable id)performSelector: (SEL)selector
584 withObject: (nullable id)object1
585 withObject: (nullable id)object2;
586
599- (nullable id)performSelector: (SEL)selector
600 withObject: (nullable id)object1
601 withObject: (nullable id)object2
602 withObject: (nullable id)object3;
603
618- (nullable id)performSelector: (SEL)selector
619 withObject: (nullable id)object1
620 withObject: (nullable id)object2
621 withObject: (nullable id)object3
622 withObject: (nullable id)object4;
623
636- (bool)isEqual: (nullable id)object;
637
644- (instancetype)retain;
645
652- (void)release;
653
660- (instancetype)autorelease;
661
667- (instancetype)self;
668
674- (bool)allowsWeakReference;
675
681- (bool)retainWeakReference;
682@end
683
689OF_ROOT_CLASS
690@interface OFObject <OFObject>
691{
692@private
693#ifndef __clang_analyzer__
694 Class _isa;
695#else
696 Class _isa __attribute__((__unused__));
697#endif
698}
699
700#ifdef OF_HAVE_CLASS_PROPERTIES
701# ifndef __cplusplus
702@property (class, readonly, nonatomic) Class class;
703# else
704@property (class, readonly, nonatomic, getter=class) Class class_;
705# endif
706@property (class, readonly, nonatomic) OFString *className;
707@property (class, readonly, nullable, nonatomic) Class superclass;
708@property (class, readonly, nonatomic) OFString *description;
709#endif
710
711#ifndef __cplusplus
712@property (readonly, nonatomic) Class class;
713#else
714@property (readonly, nonatomic, getter=class) Class class_;
715#endif
716@property OF_NULLABLE_PROPERTY (readonly, nonatomic) Class superclass;
717@property (readonly, nonatomic) unsigned long hash;
718@property (readonly, nonatomic) unsigned int retainCount;
719@property (readonly, nonatomic) bool isProxy;
720@property (readonly, nonatomic) bool allowsWeakReference;
721
725@property (readonly, nonatomic) OFString *className;
733@property (readonly, nonatomic) OFString *description;
742+ (void)load;
743
755+ (void)unload;
756
766+ (void)initialize;
767
779+ (instancetype)alloc;
780
786+ (Class)class;
787
794
802+ (bool)isSubclassOfClass: (Class)class_;
803
809+ (nullable Class)superclass;
810
818+ (bool)instancesRespondToSelector: (SEL)selector;
819
826+ (bool)conformsToProtocol: (Protocol *)protocol;
827
836+ (nullable IMP)instanceMethodForSelector: (SEL)selector;
837
847+ (nullable OFMethodSignature *)
848 instanceMethodSignatureForSelector: (SEL)selector;
849
858
866+ (nullable IMP)replaceClassMethod: (SEL)selector
867 withMethodFromClass: (Class)class_;
868
877+ (nullable IMP)replaceInstanceMethod: (SEL)selector
878 withMethodFromClass: (Class)class_;
879
898+ (void)inheritMethodsFromClass: (Class)class_;
899
908+ (bool)resolveClassMethod: (SEL)selector;
909
918+ (bool)resolveInstanceMethod: (SEL)selector;
919
928+ (id)copy;
929
961- (instancetype)init;
962
970- (nullable OFMethodSignature *)methodSignatureForSelector: (SEL)selector;
971
979- (void)dealloc;
980
987- (void)performSelector: (SEL)selector afterDelay: (OFTimeInterval)delay;
988
998- (void)performSelector: (SEL)selector
999 withObject: (nullable id)object
1000 afterDelay: (OFTimeInterval)delay;
1001
1013- (void)performSelector: (SEL)selector
1014 withObject: (nullable id)object1
1015 withObject: (nullable id)object2
1016 afterDelay: (OFTimeInterval)delay;
1017
1031- (void)performSelector: (SEL)selector
1032 withObject: (nullable id)object1
1033 withObject: (nullable id)object2
1034 withObject: (nullable id)object3
1035 afterDelay: (OFTimeInterval)delay;
1036
1052- (void)performSelector: (SEL)selector
1053 withObject: (nullable id)object1
1054 withObject: (nullable id)object2
1055 withObject: (nullable id)object3
1056 withObject: (nullable id)object4
1057 afterDelay: (OFTimeInterval)delay;
1058
1059#ifdef OF_HAVE_THREADS
1067- (void)performSelector: (SEL)selector
1068 onThread: (OFThread *)thread
1069 waitUntilDone: (bool)waitUntilDone;
1070
1081- (void)performSelector: (SEL)selector
1082 onThread: (OFThread *)thread
1083 withObject: (nullable id)object
1084 waitUntilDone: (bool)waitUntilDone;
1085
1098- (void)performSelector: (SEL)selector
1099 onThread: (OFThread *)thread
1100 withObject: (nullable id)object1
1101 withObject: (nullable id)object2
1102 waitUntilDone: (bool)waitUntilDone;
1103
1118- (void)performSelector: (SEL)selector
1119 onThread: (OFThread *)thread
1120 withObject: (nullable id)object1
1121 withObject: (nullable id)object2
1122 withObject: (nullable id)object3
1123 waitUntilDone: (bool)waitUntilDone;
1124
1141- (void)performSelector: (SEL)selector
1142 onThread: (OFThread *)thread
1143 withObject: (nullable id)object1
1144 withObject: (nullable id)object2
1145 withObject: (nullable id)object3
1146 withObject: (nullable id)object4
1147 waitUntilDone: (bool)waitUntilDone;
1148
1155- (void)performSelectorOnMainThread: (SEL)selector
1156 waitUntilDone: (bool)waitUntilDone;
1157
1167- (void)performSelectorOnMainThread: (SEL)selector
1168 withObject: (nullable id)object
1169 waitUntilDone: (bool)waitUntilDone;
1170
1182- (void)performSelectorOnMainThread: (SEL)selector
1183 withObject: (nullable id)object1
1184 withObject: (nullable id)object2
1185 waitUntilDone: (bool)waitUntilDone;
1186
1200- (void)performSelectorOnMainThread: (SEL)selector
1201 withObject: (nullable id)object1
1202 withObject: (nullable id)object2
1203 withObject: (nullable id)object3
1204 waitUntilDone: (bool)waitUntilDone;
1205
1221- (void)performSelectorOnMainThread: (SEL)selector
1222 withObject: (nullable id)object1
1223 withObject: (nullable id)object2
1224 withObject: (nullable id)object3
1225 withObject: (nullable id)object4
1226 waitUntilDone: (bool)waitUntilDone;
1227
1236- (void)performSelector: (SEL)selector
1237 onThread: (OFThread *)thread
1238 afterDelay: (OFTimeInterval)delay;
1239
1250- (void)performSelector: (SEL)selector
1251 onThread: (OFThread *)thread
1252 withObject: (nullable id)object
1253 afterDelay: (OFTimeInterval)delay;
1254
1267- (void)performSelector: (SEL)selector
1268 onThread: (OFThread *)thread
1269 withObject: (nullable id)object1
1270 withObject: (nullable id)object2
1271 afterDelay: (OFTimeInterval)delay;
1272
1287- (void)performSelector: (SEL)selector
1288 onThread: (OFThread *)thread
1289 withObject: (nullable id)object1
1290 withObject: (nullable id)object2
1291 withObject: (nullable id)object3
1292 afterDelay: (OFTimeInterval)delay;
1293
1310- (void)performSelector: (SEL)selector
1311 onThread: (OFThread *)thread
1312 withObject: (nullable id)object1
1313 withObject: (nullable id)object2
1314 withObject: (nullable id)object3
1315 withObject: (nullable id)object4
1316 afterDelay: (OFTimeInterval)delay;
1317#endif
1318
1330- (nullable id)forwardingTargetForSelector: (SEL)selector;
1331
1341- (void)doesNotRecognizeSelector: (SEL)selector OF_NO_RETURN;
1342@end
1343
1349@protocol OFCopying
1359- (id)copy;
1360@end
1361
1370@protocol OFMutableCopying
1376- (id)mutableCopy;
1377@end
1378
1387@protocol OFComparing
1394- (OFComparisonResult)compare: (id <OFComparing>)object;
1395@end
1396
1397#ifdef __cplusplus
1398extern "C" {
1399#endif
1414extern void *_Nullable OFAllocMemory(size_t count, size_t size)
1415 OF_WARN_UNUSED_RESULT;
1416
1431extern void *_Nullable OFAllocZeroedMemory(size_t count, size_t size)
1432 OF_WARN_UNUSED_RESULT;
1433
1451extern void *_Nullable OFResizeMemory(void *_Nullable pointer, size_t count,
1452 size_t size) OF_WARN_UNUSED_RESULT;
1453
1461extern void OFFreeMemory(void *_Nullable pointer);
1462
1463#ifdef OF_APPLE_RUNTIME
1464extern void *_Null_unspecified objc_autoreleasePoolPush(void);
1465extern void objc_autoreleasePoolPop(void *_Null_unspecified pool);
1466# ifndef __OBJC2__
1467extern id _Nullable objc_constructInstance(Class _Nullable class_,
1468 void *_Nullable bytes);
1469extern void *_Nullable objc_destructInstance(id _Nullable object);
1470typedef enum objc_associationPolicy {
1477extern void objc_setAssociatedObject(id _Nonnull object,
1478 const void *_Nonnull key, id _Nullable value,
1479 objc_associationPolicy policy);
1480extern id _Nullable objc_getAssociatedObject(id _Nonnull object,
1481 const void *_Nonnull key);
1482extern void objc_removeAssociatedObjects(id _Nonnull object);
1483# endif
1484#endif
1485
1498extern id OFAllocObject(Class class_, size_t extraSize, size_t extraAlignment,
1499 void *_Nullable *_Nullable extra);
1500
1523extern void OF_NO_RETURN_FUNC OFMethodNotFound(id self, SEL _cmd);
1524
1530extern void OFHashInit(unsigned long *_Nonnull hash);
1531
1537extern uint16_t OFRandom16(void);
1538
1544extern uint32_t OFRandom32(void);
1545
1551extern uint64_t OFRandom64(void);
1552#ifdef __cplusplus
1553}
1554#endif
1555
1556OF_ASSUME_NONNULL_END
1557
1558#import "OFBlock.h"
1559#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:270
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:149
static OF_INLINE void OFHashFinalize(unsigned long *hash)
Finalizes the specified hash.
Definition OFObject.h:448
static OF_INLINE void OFHashAddByte(unsigned long *hash, unsigned char byte)
Adds the specified byte to the hash.
Definition OFObject.h:416
void * OFAllocMemory(size_t count, size_t size)
Allocates memory for the specified number of items of the specified size.
Definition OFObject.m:112
OFComparisonResult
A result of a comparison.
Definition OFObject.h:58
@ OFOrderedAscending
Definition OFObject.h:60
@ OFOrderedDescending
Definition OFObject.h:64
@ OFOrderedSame
Definition OFObject.h:62
void OFHashInit(unsigned long *hash)
Initializes the specified hash.
Definition OFObject.m:314
static OF_INLINE bool OFEqualVectors4D(OFVector4D vector1, OFVector4D vector2)
Returns whether the two vectors are equal.
Definition OFObject.h:392
OFComparisonResult(^ OFComparator)(id left, id right)
A comparator to compare two objects.
Definition OFObject.h:86
static OF_INLINE bool OFEqualRanges(OFRange range1, OFRange range2)
Returns whether the two ranges are equal.
Definition OFObject.h:140
static OF_INLINE void OFHashAddHash(unsigned long *hash, unsigned long otherHash)
Adds the specified hash to the hash.
Definition OFObject.h:434
static OF_INLINE bool OFEqualSizes(OFSize size1, OFSize size2)
Returns whether the two sizes are equal.
Definition OFObject.h:237
static OF_INLINE OFSize OF_CONST_FUNC OFMakeSize(float width, float height)
Creates a new OFSize.
Definition OFObject.h:222
uint32_t OFRandom32(void)
Returns 32 bit or non-cryptographical randomness.
Definition OFObject.m:258
void OFFreeMemory(void *pointer)
Frees memory allocated by OFAllocMemory, OFAllocZeroedMemory or OFResizeMemory.
Definition OFObject.m:167
double OFTimeInterval
A time interval in seconds.
Definition OFObject.h:154
uint64_t OFRandom64(void)
Returns 64 bit or non-cryptographical randomness.
Definition OFObject.m:284
OFByteOrder
An enum for representing endianness.
Definition OFObject.h:92
@ OFByteOrderBigEndian
Definition OFObject.h:94
@ OFByteOrderLittleEndian
Definition OFObject.h:96
@ OFByteOrderNative
Definition OFObject.h:101
void * OFAllocZeroedMemory(size_t count, size_t size)
Allocates memory for the specified number of items of the specified size and initializes it with zero...
Definition OFObject.m:130
static OF_INLINE bool OFEqualVectors3D(OFVector3D vector1, OFVector3D vector2)
Returns whether the two vectors are equal.
Definition OFObject.h:337
id OFAllocObject(Class class_, size_t extraSize, size_t extraAlignment, void **extra)
Allocates a new object.
Definition OFObject.m:408
uint16_t OFRandom16(void)
Returns 16 bit or non-cryptographical randomness.
Definition OFObject.m:228
static OF_INLINE bool OFEqualPoints(OFPoint point1, OFPoint point2)
Returns whether the two points are equal.
Definition OFObject.h:191
static OF_INLINE bool OFEqualRects(OFRect rect1, OFRect rect2)
Returns whether the two rectangles are equal.
Definition OFObject.h:288
OFComparisonResult(* OFCompareFunction)(id left, id right, void *context)
A function to compare two objects.
Definition OFObject.h:75
static OF_INLINE OFVector4D OF_CONST_FUNC OFMakeVector4D(float x, float y, float z, float w)
Creates a new OFVector4D.
Definition OFObject.h:377
void OFMethodNotFound(id self, SEL _cmd)
This function is called when a method is not found.
Definition OFObject.m:388
static OF_INLINE OFPoint OF_CONST_FUNC OFMakePoint(float x, float y)
Creates a new OFPoint.
Definition OFObject.h:176
static OF_INLINE OFRange OF_CONST_FUNC OFMakeRange(size_t start, size_t length)
Creates a new OFRange.
Definition OFObject.h:125
static OF_INLINE OFVector3D OF_CONST_FUNC OFMakeVector3D(float x, float y, float z)
Creates a new OFVector3D.
Definition OFObject.h:322
id(* IMP)(id object, SEL selector,...)
A method implementation.
Definition ObjFWRT.h:146
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:692
OFString * description
A description for the object.
Definition OFObject.h:734
OFString * className
The name of the object's class.
Definition OFObject.h:726
instancetype init()
Initializes an already allocated object.
Definition OFObject.m:696
void dealloc()
Deallocates the object.
Definition OFObject.m:1339
id copy()
Returns the class.
Definition OFObject.m:1407
void unload()
A method which is called when the class is unloaded from the runtime.
Definition OFObject.m:540
instancetype alloc()
Allocates memory for an instance of the class and sets up the memory pool for the object.
Definition OFObject.m:548
void initialize()
A method which is called the moment before the first call to the class is being made.
Definition OFObject.m:544
void load()
A method which is called once when the class is loaded into the runtime.
Definition OFObject.m:505
A class for handling strings.
Definition OFString.h:139
A class which provides portable threads.
Definition OFThread.h:66
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:161
float y
Definition OFObject.h:165
float x
Definition OFObject.h:163
A range.
Definition OFObject.h:110
size_t length
Definition OFObject.h:114
size_t location
Definition OFObject.h:112
A rectangle.
Definition OFObject.h:253
OFPoint origin
Definition OFObject.h:255
OFSize size
Definition OFObject.h:257
A size.
Definition OFObject.h:207
float width
Definition OFObject.h:209
float height
Definition OFObject.h:211
A vector in 3D space.
Definition OFObject.h:304
float x
Definition OFObject.h:306
float y
Definition OFObject.h:308
float z
Definition OFObject.h:310
A vector in 4D space.
Definition OFObject.h:356
float x
Definition OFObject.h:358
float z
Definition OFObject.h:362
float y
Definition OFObject.h:360
float w
Definition OFObject.h:364