ObjFW
OFObject.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
3  * Jonathan Schleifer <js@heap.zone>
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 #include "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 #import "block.h"
34 
35 OF_ASSUME_NONNULL_BEGIN
36 
42 typedef enum {
50 
54 typedef enum {
60 
66 typedef struct {
68  size_t location;
70  size_t length;
71 } of_range_t;
72 
80 static OF_INLINE of_range_t OF_CONST_FUNC
81 of_range(size_t start, size_t length)
82 {
83  of_range_t range = { start, length };
84 
85  return range;
86 }
87 
91 typedef double of_time_interval_t;
92 
98 typedef struct {
100  float x;
102  float y;
103 } of_point_t;
104 
112 static OF_INLINE of_point_t OF_CONST_FUNC
113 of_point(float x, float y)
114 {
115  of_point_t point = { x, y };
116 
117  return point;
118 }
119 
125 typedef struct {
127  float width;
129  float height;
131 
139 static OF_INLINE of_dimension_t OF_CONST_FUNC
140 of_dimension(float width, float height)
141 {
142  of_dimension_t dimension = { width, height };
143 
144  return dimension;
145 }
146 
152 typedef struct {
158 
168 static OF_INLINE of_rectangle_t OF_CONST_FUNC
169 of_rectangle(float x, float y, float width, float height)
170 {
171  of_rectangle_t rectangle = {
172  of_point(x, y),
173  of_dimension(width, height)
174  };
175 
176  return rectangle;
177 }
178 
179 @class OFString;
180 @class OFThread;
181 
187 @protocol OFObject
193 - (Class)class;
194 
200 - (nullable Class)superclass;
201 
208 - (bool)isKindOfClass: (Class)class_;
209 
217 - (bool)isMemberOfClass: (Class)class_;
218 
226 - (bool)respondsToSelector: (SEL)selector;
227 
234 - (bool)conformsToProtocol: (Protocol *)protocol;
235 
242 - (nullable IMP)methodForSelector: (SEL)selector;
243 
250 - (nullable const char *)typeEncodingForSelector: (SEL)selector;
251 
258 - (nullable id)performSelector: (SEL)selector;
259 
268 - (nullable id)performSelector: (SEL)selector
269  withObject: (nullable id)object;
270 
281 - (nullable id)performSelector: (SEL)selector
282  withObject: (nullable id)object1
283  withObject: (nullable id)object2;
284 
297 - (bool)isEqual: (nullable id)object;
298 
311 - (uint32_t)hash;
312 
319 - retain;
320 
326 - (unsigned int)retainCount;
327 
334 - (void)release;
335 
342 - autorelease;
343 
349 - self;
350 
356 - (bool)isProxy;
357 
363 - (bool)allowsWeakReference;
364 
370 - (bool)retainWeakReference;
371 @end
372 
378 OF_ROOT_CLASS
379 @interface OFObject <OFObject>
380 {
381 @private
382  Class _isa;
383 }
384 
392 + (void)load;
393 
406 + (void)unload;
407 
417 + (void)initialize;
418 
428 + alloc;
429 
434 + new;
435 
441 + (Class)class;
442 
448 + (OFString *)className;
449 
457 + (bool)isSubclassOfClass: (Class)class_;
458 
464 + (nullable Class)superclass;
465 
473 + (bool)instancesRespondToSelector: (SEL)selector;
474 
481 + (bool)conformsToProtocol: (Protocol *)protocol;
482 
491 + (nullable IMP)instanceMethodForSelector: (SEL)selector;
492 
500 + (nullable const char *)typeEncodingForInstanceSelector: (SEL)selector;
501 
509 + (OFString *)description;
510 
518 + (nullable IMP)replaceClassMethod: (SEL)selector
519  withMethodFromClass: (Class)class_;
520 
529 + (nullable IMP)replaceInstanceMethod: (SEL)selector
530  withMethodFromClass: (Class)class_;
531 
544 + (nullable IMP)replaceClassMethod: (SEL)selector
545  withImplementation: (IMP)implementation
546  typeEncoding: (const char *)typeEncoding;
547 
560 + (nullable IMP)replaceInstanceMethod: (SEL)selector
561  withImplementation: (IMP)implementation
562  typeEncoding: (const char *)typeEncoding;
563 
582 + (void)inheritMethodsFromClass: (Class)class_;
583 
592 + (BOOL)resolveClassMethod: (SEL)selector;
593 
602 + (BOOL)resolveInstanceMethod: (SEL)selector;
603 
612 + copy;
613 
627 - init;
628 
634 - (OFString *)className;
635 
643 - (OFString *)description;
644 
654 - (nullable void *)allocMemoryWithSize: (size_t)size;
655 
667 - (nullable void *)allocMemoryWithSize: (size_t)size
668  count: (size_t)count;
669 
680 - (nullable void *)resizeMemory: (nullable void *)pointer
681  size: (size_t)size;
682 
695 - (nullable void *)resizeMemory: (nullable void *)pointer
696  size: (size_t)size
697  count: (size_t)count;
698 
706 - (void)freeMemory: (nullable void *)pointer;
707 
715 - (void)dealloc;
716 
723 - (void)performSelector: (SEL)selector
724  afterDelay: (of_time_interval_t)delay;
725 
735 - (void)performSelector: (SEL)selector
736  withObject: (nullable id)object
737  afterDelay: (of_time_interval_t)delay;
738 
750 - (void)performSelector: (SEL)selector
751  withObject: (nullable id)object1
752  withObject: (nullable id)object2
753  afterDelay: (of_time_interval_t)delay;
754 
755 #ifdef OF_HAVE_THREADS
756 
763 - (void)performSelector: (SEL)selector
764  onThread: (OFThread *)thread
765  waitUntilDone: (bool)waitUntilDone;
766 
777 - (void)performSelector: (SEL)selector
778  onThread: (OFThread *)thread
779  withObject: (nullable id)object
780  waitUntilDone: (bool)waitUntilDone;
781 
794 - (void)performSelector: (SEL)selector
795  onThread: (OFThread *)thread
796  withObject: (nullable id)object1
797  withObject: (nullable id)object2
798  waitUntilDone: (bool)waitUntilDone;
799 
806 - (void)performSelectorOnMainThread: (SEL)selector
807  waitUntilDone: (bool)waitUntilDone;
808 
818 - (void)performSelectorOnMainThread: (SEL)selector
819  withObject: (nullable id)object
820  waitUntilDone: (bool)waitUntilDone;
821 
833 - (void)performSelectorOnMainThread: (SEL)selector
834  withObject: (nullable id)object1
835  withObject: (nullable id)object2
836  waitUntilDone: (bool)waitUntilDone;
837 
846 - (void)performSelector: (SEL)selector
847  onThread: (OFThread *)thread
848  afterDelay: (of_time_interval_t)delay;
849 
860 - (void)performSelector: (SEL)selector
861  onThread: (OFThread *)thread
862  withObject: (nullable id)object
863  afterDelay: (of_time_interval_t)delay;
864 
877 - (void)performSelector: (SEL)selector
878  onThread: (OFThread *)thread
879  withObject: (nullable id)object1
880  withObject: (nullable id)object2
881  afterDelay: (of_time_interval_t)delay;
882 #endif
883 
895 - (nullable id)forwardingTargetForSelector: (SEL)selector;
896 
905 - (void)doesNotRecognizeSelector: (SEL)selector OF_NO_RETURN;
906 @end
907 
913 @protocol OFCopying
923 - copy;
924 @end
925 
934 @protocol OFMutableCopying
940 - mutableCopy;
941 @end
942 
950 @protocol OFComparing <OFObject>
957 - (of_comparison_result_t)compare: (id <OFComparing>)object;
958 @end
959 
960 #ifdef __cplusplus
961 extern "C" {
962 #endif
963 extern id of_alloc_object(Class class_, size_t extraSize,
964  size_t extraAlignment, void *_Nullable *_Nullable extra);
965 extern void OF_NO_RETURN_FUNC of_method_not_found(id self, SEL _cmd);
966 extern uint32_t of_hash_seed;
967 #ifdef __cplusplus
968 }
969 #endif
970 
971 OF_ASSUME_NONNULL_END
972 
973 #import "OFObject+KeyValueCoding.h"
974 #import "OFObject+Serialization.h"
bool retainWeakReference()
Retain a weak reference to this object.
bool allowsWeakReference()
Returns whether the class allows weak references.
Definition: OFObject.h:46
of_dimension_t size
Definition: OFObject.h:156
OFString * className()
Returns the name of the class as a string.
Definition: OFObject.m:282
A protocol for the creation of mutable copies.
Definition: OFObject.h:935
A point.
Definition: OFObject.h:98
A dimension.
Definition: OFObject.h:125
id copy()
Returns the class.
Definition: OFObject.m:1133
void dealloc()
Deallocates the object.
Definition: OFObject.m:1031
Definition: OFObject.h:48
A class for handling strings.
Definition: OFString.h:114
float x
Definition: OFObject.h:100
Definition: OFObject.h:44
Definition: OFObject.h:58
OFString * description()
Returns a description for the class, which is usually the class name.
Definition: OFObject.m:337
id self()
Returns the receiver.
float height
Definition: OFObject.h:129
float y
Definition: OFObject.h:102
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:127
id alloc()
Allocates memory for an instance of the class and sets up the memory pool for the object...
Definition: OFObject.m:267
A protocol for the creation of copies.
Definition: OFObject.h:914
double of_time_interval_t
A time interval in seconds.
Definition: OFObject.h:91
of_point_t origin
Definition: OFObject.h:154
A protocol for comparing objects.
size_t length
Definition: OFObject.h:70
A rectangle.
Definition: OFObject.h:152
Definition: OFObject.h:56
The root class for all other classes inside ObjFW.
Definition: OFObject.h:379
A range.
Definition: OFObject.h:66
void initialize()
A method which is called the moment before the first call to the class is being made.
Definition: OFObject.m:263
void unload()
A method which is called when the class is unloaded from the runtime.
Definition: OFObject.m:259
of_comparison_result_t
A result of a comparison.
Definition: OFObject.h:42
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:215
id autorelease()
Adds the object to the topmost OFAutoreleasePool of the thread&#39;s autorelease pool stack...
unsigned int retainCount()
Returns the retain count.
A class which provides portable threads.
Definition: OFThread.h:53
nullable Class superclass()
Returns the superclass of the class.
Definition: OFObject.m:297
void release()
Decreases the retain count.
bool isProxy()
Returns whether the object is a proxy object.
size_t location
Definition: OFObject.h:68
Class class()
Returns the class.
Definition: OFObject.m:277
of_byte_order_t
An enum for storing endianess.
Definition: OFObject.h:54