ObjFW
OFObject.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015
3  * Jonathan Schleifer <js@webkeks.org>
4  *
5  * All rights reserved.
6  *
7  * This file is part of ObjFW. It may be distributed under the terms of the
8  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
9  * the packaging of this file.
10  *
11  * Alternatively, it may be distributed under the terms of the GNU General
12  * Public License, either version 2 or 3, which can be found in the file
13  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
14  * file.
15  */
16 
17 #import "objfw-defs.h"
18 
19 #ifndef __STDC_LIMIT_MACROS
20 # define __STDC_LIMIT_MACROS
21 #endif
22 #ifndef __STDC_CONSTANT_MACROS
23 # define __STDC_CONSTANT_MACROS
24 #endif
25 
26 #include <stddef.h>
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include <limits.h>
30 
31 #import "macros.h"
32 #import "autorelease.h"
33 
34 OF_ASSUME_NONNULL_BEGIN
35 
41 typedef enum {
49 
53 typedef enum {
59 
65 typedef struct {
67  size_t location;
69  size_t length;
70 } of_range_t;
71 
79 static OF_INLINE of_range_t OF_CONST_FUNC
80 of_range(size_t start, size_t length)
81 {
82  of_range_t range = { start, length };
83 
84  return range;
85 }
86 
90 typedef double of_time_interval_t;
91 
97 typedef struct {
99  float x;
101  float y;
102 } of_point_t;
103 
111 static OF_INLINE of_point_t OF_CONST_FUNC
112 of_point(float x, float y)
113 {
114  of_point_t point = { x, y };
115 
116  return point;
117 }
118 
124 typedef struct {
126  float width;
128  float height;
130 
138 static OF_INLINE of_dimension_t OF_CONST_FUNC
139 of_dimension(float width, float height)
140 {
141  of_dimension_t dimension = { width, height };
142 
143  return dimension;
144 }
145 
151 typedef struct {
157 
167 static OF_INLINE of_rectangle_t OF_CONST_FUNC
168 of_rectangle(float x, float y, float width, float height)
169 {
170  of_rectangle_t rectangle = {
171  of_point(x, y),
172  of_dimension(width, height)
173  };
174 
175  return rectangle;
176 }
177 
178 @class OFString;
179 @class OFThread;
180 
186 @protocol OFObject
192 - (Class)class;
193 
199 - (Class)superclass;
200 
207 - (bool)isKindOfClass: (Class)class_;
208 
216 - (bool)isMemberOfClass: (Class)class_;
217 
225 - (bool)respondsToSelector: (SEL)selector;
226 
233 - (bool)conformsToProtocol: (Protocol*)protocol;
234 
241 - (nullable IMP)methodForSelector: (SEL)selector;
242 
249 - (nullable const char*)typeEncodingForSelector: (SEL)selector;
250 
257 - (nullable id)performSelector: (SEL)selector;
258 
267 - (nullable id)performSelector: (SEL)selector
268  withObject: (nullable id)object;
269 
280 - (nullable id)performSelector: (SEL)selector
281  withObject: (nullable id)object1
282  withObject: (nullable id)object2;
283 
296 - (bool)isEqual: (nullable id)object;
297 
310 - (uint32_t)hash;
311 
318 - retain;
319 
325 - (unsigned int)retainCount;
326 
333 - (void)release;
334 
341 - autorelease;
342 
348 - self;
349 
355 - (bool)isProxy;
356 @end
357 
363 OF_ROOT_CLASS
364 @interface OFObject <OFObject>
365 {
366 @private
367  Class _isa;
368 }
369 
377 + (void)load;
378 
391 + (void)unload;
392 
402 + (void)initialize;
403 
413 + alloc;
414 
419 + new;
420 
426 + (Class)class;
427 
433 + (OFString*)className;
434 
442 + (bool)isSubclassOfClass: (Class)class_;
443 
449 + (Class)superclass;
450 
458 + (bool)instancesRespondToSelector: (SEL)selector;
459 
466 + (bool)conformsToProtocol: (Protocol*)protocol;
467 
476 + (nullable IMP)instanceMethodForSelector: (SEL)selector;
477 
485 + (nullable const char*)typeEncodingForInstanceSelector: (SEL)selector;
486 
495 
503 + (nullable IMP)replaceClassMethod: (SEL)selector
504  withMethodFromClass: (Class)class_;
505 
514 + (nullable IMP)replaceInstanceMethod: (SEL)selector
515  withMethodFromClass: (Class)class_;
516 
529 + (nullable IMP)replaceClassMethod: (SEL)selector
530  withImplementation: (IMP)implementation
531  typeEncoding: (const char*)typeEncoding;
532 
545 + (nullable IMP)replaceInstanceMethod: (SEL)selector
546  withImplementation: (IMP)implementation
547  typeEncoding: (const char*)typeEncoding;
548 
567 + (void)inheritMethodsFromClass: (Class)class_;
568 
577 + (bool)resolveClassMethod: (SEL)selector;
578 
587 + (bool)resolveInstanceMethod: (SEL)selector;
588 
597 + copy;
598 
612 - init;
613 
619 - (OFString*)className;
620 
629 
638 - (void*)allocMemoryWithSize: (size_t)size;
639 
650 - (void*)allocMemoryWithSize: (size_t)size
651  count: (size_t)count;
652 
663 - (nullable void*)resizeMemory: (nullable void*)pointer
664  size: (size_t)size;
665 
678 - (nullable void*)resizeMemory: (nullable void*)pointer
679  size: (size_t)size
680  count: (size_t)count;
681 
689 - (void)freeMemory: (nullable void*)pointer;
690 
698 - (void)dealloc;
699 
706 - (void)performSelector: (SEL)selector
707  afterDelay: (of_time_interval_t)delay;
708 
718 - (void)performSelector: (SEL)selector
719  withObject: (nullable id)object
720  afterDelay: (of_time_interval_t)delay;
721 
733 - (void)performSelector: (SEL)selector
734  withObject: (nullable id)object1
735  withObject: (nullable id)object2
736  afterDelay: (of_time_interval_t)delay;
737 
738 #ifdef OF_HAVE_THREADS
739 
746 - (void)performSelector: (SEL)selector
747  onThread: (OFThread*)thread
748  waitUntilDone: (bool)waitUntilDone;
749 
760 - (void)performSelector: (SEL)selector
761  onThread: (OFThread*)thread
762  withObject: (nullable id)object
763  waitUntilDone: (bool)waitUntilDone;
764 
777 - (void)performSelector: (SEL)selector
778  onThread: (OFThread*)thread
779  withObject: (nullable id)object1
780  withObject: (nullable id)object2
781  waitUntilDone: (bool)waitUntilDone;
782 
789 - (void)performSelectorOnMainThread: (SEL)selector
790  waitUntilDone: (bool)waitUntilDone;
791 
801 - (void)performSelectorOnMainThread: (SEL)selector
802  withObject: (nullable id)object
803  waitUntilDone: (bool)waitUntilDone;
804 
816 - (void)performSelectorOnMainThread: (SEL)selector
817  withObject: (nullable id)object1
818  withObject: (nullable id)object2
819  waitUntilDone: (bool)waitUntilDone;
820 
829 - (void)performSelector: (SEL)selector
830  onThread: (OFThread*)thread
831  afterDelay: (of_time_interval_t)delay;
832 
843 - (void)performSelector: (SEL)selector
844  onThread: (OFThread*)thread
845  withObject: (nullable id)object
846  afterDelay: (of_time_interval_t)delay;
847 
860 - (void)performSelector: (SEL)selector
861  onThread: (OFThread*)thread
862  withObject: (nullable id)object1
863  withObject: (nullable id)object2
864  afterDelay: (of_time_interval_t)delay;
865 #endif
866 
877 - (nullable id)forwardingTargetForSelector: (SEL)selector;
878 
887 - (void)doesNotRecognizeSelector: (SEL)selector OF_NO_RETURN;
888 @end
889 
895 @protocol OFCopying
905 - copy;
906 @end
907 
916 @protocol OFMutableCopying
922 - mutableCopy;
923 @end
924 
932 @protocol OFComparing <OFObject>
939 - (of_comparison_result_t)compare: (id <OFComparing>)object;
940 @end
941 
942 #ifdef __cplusplus
943 extern "C" {
944 #endif
945 extern id of_alloc_object(Class class_, size_t extraSize,
946  size_t extraAlignment, void *OF_NULLABLE *OF_NULLABLE extra);
947 extern void OF_NO_RETURN_FUNC of_method_not_found(id self, SEL _cmd);
948 extern uint32_t of_hash_seed;
949 #ifdef __cplusplus
950 }
951 #endif
952 
953 OF_ASSUME_NONNULL_END
954 
955 #import "OFObject+Serialization.h"
id new()
Allocates memory for a new instance and calls init on it.
Definition: OFObject.m:265
Definition: OFObject.h:45
of_dimension_t size
Definition: OFObject.h:155
OFString * className()
Returns the name of the class as a string.
Definition: OFObject.m:275
A protocol for the creation of mutable copies.
Definition: OFObject.h:917
A point.
Definition: OFObject.h:97
A dimension.
Definition: OFObject.h:124
id copy()
Returns the class.
Definition: OFObject.m:1118
void dealloc()
Deallocates the object.
Definition: OFObject.m:1016
Definition: OFObject.h:47
A class for handling strings.
Definition: OFString.h:91
float x
Definition: OFObject.h:99
Definition: OFObject.h:43
id copy()
Copies the object.
Definition: OFObject.h:57
OFString * description()
Returns a description for the class, which is usually the class name.
Definition: OFObject.m:332
id self()
Returns the receiver.
float height
Definition: OFObject.h:128
float y
Definition: OFObject.h:101
id init()
Initializes an already allocated object.
Definition: OFObject.m:488
uint32_t hash()
Calculates a hash for the object.
float width
Definition: OFObject.h:126
id alloc()
Allocates memory for an instance of the class and sets up the memory pool for the object...
Definition: OFObject.m:260
A protocol for the creation of copies.
Definition: OFObject.h:896
double of_time_interval_t
A time interval in seconds.
Definition: OFObject.h:90
of_point_t origin
Definition: OFObject.h:153
A protocol for comparing objects.
Definition: OFObject.h:933
size_t length
Definition: OFObject.h:69
A rectangle.
Definition: OFObject.h:151
Definition: OFObject.h:55
The root class for all other classes inside ObjFW.
Definition: OFObject.h:364
A range.
Definition: OFObject.h:65
void initialize()
A method which is called the moment before the first call to the class is being made.
Definition: OFObject.m:256
void unload()
A method which is called when the class is unloaded from the runtime.
Definition: OFObject.m:252
of_comparison_result_t
A result of a comparison.
Definition: OFObject.h:41
id retain()
Increases the retain count.
void load()
A method which is called once when the class is loaded into the runtime.
Definition: OFObject.m:217
id autorelease()
Adds the object to the topmost OFAutoreleasePool of the thread's autorelease pool stack...
unsigned int retainCount()
Returns the retain count.
A class which provides portable threads.
Definition: OFThread.h:53
Class superclass()
Returns the superclass of the class.
Definition: OFObject.m:292
void release()
Decreases the retain count.
bool isProxy()
Returns whether the object is a proxy object.
size_t location
Definition: OFObject.h:67
Class class()
Returns the class.
Definition: OFObject.m:270
id mutableCopy()
Creates a mutable copy of the object.
of_byte_order_t
An enum for storing endianess.
Definition: OFObject.h:53