ObjFW
ObjFWRT.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 #ifndef OBJFWRT_OBJFWRT_H
21 #define OBJFWRT_OBJFWRT_H
22 
23 #ifndef __STDC_LIMIT_MACROS
24 # define __STDC_LIMIT_MACROS
25 #endif
26 #ifndef __STDC_CONSTANT_MACROS
27 # define __STDC_CONSTANT_MACROS
28 #endif
29 
30 #include <stdbool.h>
31 #include <stddef.h>
32 #include <stdint.h>
33 
36 #ifndef __has_feature
37 # define __has_feature(x) 0
38 #endif
39 
40 #ifndef __has_attribute
41 # define __has_attribute(x) 0
42 #endif
43 
44 #if !__has_feature(nullability)
45 # ifndef _Nonnull
46 # define _Nonnull
47 # endif
48 # ifndef _Nullable
49 # define _Nullable
50 # endif
51 # ifndef _Null_unspecified
52 # define _Null_unspecified
53 # endif
54 #endif
55 
56 #if !__has_feature(objc_arc) && !defined(__unsafe_unretained)
57 # define __unsafe_unretained
58 #endif
59 
63 #define Nil (Class _Null_unspecified)0
64 
68 #define nil (id _Null_unspecified)0
69 
76 #define YES true
77 
84 #define NO false
85 
89 typedef struct objc_class *Class;
90 
94 typedef struct objc_object *id;
95 
102 typedef const struct objc_selector *SEL;
103 
109 typedef const struct objc_method *Method;
110 
114 #if defined(__OBJC__) && !defined(DOXYGEN)
115 @class Protocol;
116 #else
117 typedef const struct objc_protocol *Protocol;
118 #endif
119 
123 typedef const struct objc_ivar *Ivar;
124 
128 typedef const struct objc_property *objc_property_t;
129 
130 #if !defined(__wii__) && !defined(__amigaos__)
137 typedef bool BOOL;
138 #endif
139 
146 typedef id _Nullable (*IMP)(id _Nonnull object, SEL _Nonnull selector, ...);
147 
153 typedef void (*objc_uncaught_exception_handler)(id _Nullable exception);
154 
160 typedef void (*objc_enumeration_mutation_handler)(id _Nonnull object);
161 
165 struct objc_super {
169  id __unsafe_unretained _Nullable self;
173 #ifdef __cplusplus
174  Class _Nonnull class_;
175 #else
176  Class _Nonnull class;
177 #endif
178 };
179 
195 
196 #ifdef __cplusplus
197 extern "C" {
198 #endif
199 
206 extern SEL _Nonnull sel_registerName(const char *_Nonnull name);
207 
214 extern const char *_Nonnull sel_getName(SEL _Nonnull selector);
215 
226 extern bool sel_isEqual(SEL _Nonnull selector1, SEL _Nonnull selector2);
227 
236 extern Class _Nonnull objc_allocateClassPair(Class _Nullable superclass,
237  const char *_Nonnull name, size_t extraBytes);
238 
244 extern void objc_registerClassPair(Class _Nonnull class_);
245 
254 extern unsigned int objc_getClassList(Class _Nonnull *_Nullable buffer,
255  unsigned int count);
256 
268 extern Class _Nonnull *_Nonnull objc_copyClassList(
269  unsigned int *_Nullable length);
270 
277 extern bool class_isMetaClass(Class _Nullable class_);
278 
285 extern const char *_Nullable class_getName(Class _Nullable class_);
286 
293 extern Class _Nullable class_getSuperclass(Class _Nullable class_);
294 
301 extern unsigned long class_getInstanceSize(Class _Nullable class_);
302 
311 extern bool class_respondsToSelector(Class _Nullable class_,
312  SEL _Nonnull selector);
313 
322 extern bool class_conformsToProtocol(Class _Nullable class_,
323  Protocol *_Nonnull protocol);
324 
337 extern IMP _Nullable class_getMethodImplementation(Class _Nullable class_,
338  SEL _Nonnull selector);
339 
352 extern IMP _Nullable class_getMethodImplementation_stret(Class _Nullable class_,
353  SEL _Nonnull selector);
354 
362 extern Method _Nullable class_getInstanceMethod(Class _Nullable class_,
363  SEL _Nonnull selector);
364 
374 extern bool class_addMethod(Class _Nonnull class_, SEL _Nonnull selector,
375  IMP _Nonnull implementation, const char *_Nullable typeEncoding);
376 
387 extern IMP _Nullable class_replaceMethod(Class _Nonnull class_,
388  SEL _Nonnull selector, IMP _Nonnull implementation,
389  const char *_Nullable typeEncoding);
390 
397 extern Class _Nullable object_getClass(id _Nullable object);
398 
408 extern Class _Nullable object_setClass(id _Nullable object,
409  Class _Nonnull class_);
410 
417 extern const char *_Nullable object_getClassName(id _Nullable object);
418 
425 extern const char *_Nonnull protocol_getName(Protocol *_Nonnull protocol);
426 
434 extern bool protocol_isEqual(Protocol *_Nonnull protocol1,
435  Protocol *_Nonnull protocol2);
436 
444 extern bool protocol_conformsToProtocol(Protocol *_Nonnull protocol1,
445  Protocol *_Nonnull protocol2);
446 
456 extern Method _Nullable *_Nullable class_copyMethodList(Class _Nullable class_,
457  unsigned int *_Nullable outCount);
458 
465 extern SEL _Nonnull method_getName(Method _Nonnull method);
466 
473 extern const char *_Nullable method_getTypeEncoding(Method _Nonnull method);
474 
484 extern Ivar _Nullable *_Nullable class_copyIvarList(Class _Nullable class_,
485  unsigned int *_Nullable outCount);
486 
493 extern const char *_Nonnull ivar_getName(Ivar _Nonnull ivar);
494 
501 extern const char *_Nonnull ivar_getTypeEncoding(Ivar _Nonnull ivar);
502 
509 extern ptrdiff_t ivar_getOffset(Ivar _Nonnull ivar);
510 
520 extern objc_property_t _Nullable *_Nullable class_copyPropertyList(
521  Class _Nullable class_, unsigned int *_Nullable outCount);
522 
529 extern const char *_Nonnull property_getName(objc_property_t _Nonnull property);
530 
539 extern char *_Nullable property_copyAttributeValue(
540  objc_property_t _Nonnull property, const char *_Nonnull name);
541 
549 extern void objc_deinit(void);
550 
557 extern _Nullable objc_uncaught_exception_handler
559  objc_uncaught_exception_handler _Nullable handler);
560 
568 extern void objc_setForwardHandler(IMP _Nullable forward,
569  IMP _Nullable stretForward);
570 
577  objc_enumeration_mutation_handler _Nullable handler);
578 
588 extern id _Nullable objc_constructInstance(Class _Nullable class_,
589  void *_Nullable bytes);
590 
597 extern void *_Nullable objc_destructInstance(id _Nullable object);
598 
606 extern void *_Null_unspecified objc_autoreleasePoolPush(void);
607 
615 extern void objc_autoreleasePoolPop(void *_Null_unspecified pool);
616 
626 extern id _Nullable _objc_rootAutorelease(id _Nullable object);
627 
634 extern void objc_setTaggedPointerSecret(uintptr_t secret);
635 
642 extern int objc_registerTaggedPointerClass(Class _Nonnull class_);
643 
650 extern bool object_isTaggedPointer(id _Nullable object);
651 
658 extern uintptr_t object_getTaggedPointerValue(id _Nonnull object);
659 
667 extern id _Nullable objc_createTaggedPointer(int class_, uintptr_t value);
668 
678 extern void objc_setAssociatedObject(id _Nonnull object,
679  const void *_Nonnull key, id _Nullable value,
680  objc_associationPolicy policy);
681 
690 extern id _Nullable objc_getAssociatedObject(id _Nonnull object,
691  const void *_Nonnull key);
692 
698 extern void objc_removeAssociatedObjects(id _Nonnull object);
699 
700 /*
701  * Used by the compiler, but can also be called manually.
702  *
703  * These declarations are also required to prevent Clang's implicit
704  * declarations which include __declspec(dllimport) on Windows.
705  */
706 struct objc_module;
707 extern void __objc_exec_class(struct objc_module *_Nonnull module);
708 extern IMP _Nonnull objc_msg_lookup(id _Nullable object, SEL _Nonnull selector);
709 extern IMP _Nonnull objc_msg_lookup_stret(id _Nullable object,
710  SEL _Nonnull selector);
711 extern IMP _Nonnull objc_msg_lookup_super(struct objc_super *_Nonnull super,
712  SEL _Nonnull selector);
713 extern IMP _Nonnull objc_msg_lookup_super_stret(
714  struct objc_super *_Nonnull super, SEL _Nonnull selector);
715 extern Class _Nullable objc_lookUpClass(const char *_Nonnull name);
716 extern Class _Nullable objc_getClass(const char *_Nonnull name);
717 extern Class _Nonnull objc_getRequiredClass(const char *_Nonnull name);
718 extern Class _Nullable objc_lookup_class(const char *_Nonnull name);
719 extern Class _Nonnull objc_get_class(const char *_Nonnull name);
720 extern void objc_exception_throw(id _Nullable object);
721 extern int objc_sync_enter(id _Nullable object);
722 extern int objc_sync_exit(id _Nullable object);
723 extern id _Nullable objc_getProperty(id _Nonnull self, SEL _Nonnull _cmd,
724  ptrdiff_t offset, bool atomic);
725 extern void objc_setProperty(id _Nonnull self, SEL _Nonnull _cmd,
726  ptrdiff_t offset, id _Nullable value, bool atomic, signed char copy);
727 extern void objc_getPropertyStruct(void *_Nonnull dest,
728  const void *_Nonnull src, ptrdiff_t size, bool atomic, bool strong);
729 extern void objc_setPropertyStruct(void *_Nonnull dest,
730  const void *_Nonnull src, ptrdiff_t size, bool atomic, bool strong);
731 extern void objc_enumerationMutation(id _Nonnull object);
732 #ifndef OBJC_NO_PERSONALITY_DECLARATION
733 /*
734  * No objfw-defs.h or config.h is available for the installed runtime headers,
735  * so we don't know which exceptions we have.
736  */
737 extern int __gnu_objc_personality_v0(int version, int actions,
738  uint64_t exClass, void *_Nonnull ex, void *_Nonnull ctx);
739 extern int __gnu_objc_personality_sj0(int version, int actions,
740  uint64_t exClass, void *_Nonnull ex, void *_Nonnull ctx);
741 #endif
742 extern id _Nullable objc_retain(id _Nullable object);
743 extern id _Nullable objc_retainBlock(id _Nullable block);
744 extern id _Nullable objc_retainAutorelease(id _Nullable object);
745 extern void objc_release(id _Nullable object);
746 extern id _Nullable objc_autorelease(id _Nullable object);
747 extern id _Nullable objc_autoreleaseReturnValue(id _Nullable object);
748 extern id _Nullable objc_retainAutoreleaseReturnValue(id _Nullable object);
749 extern id _Nullable objc_retainAutoreleasedReturnValue(id _Nullable object);
750 extern id _Nullable objc_storeStrong(id _Nullable *_Nonnull object,
751  id _Nullable value);
752 extern id _Nullable objc_storeWeak(id _Nullable *_Nonnull object,
753  id _Nullable value);
754 extern id _Nullable objc_loadWeakRetained(id _Nullable *_Nonnull object);
755 extern _Nullable id objc_initWeak(id _Nullable *_Nonnull object,
756  id _Nullable value);
757 extern void objc_destroyWeak(id _Nullable *_Nonnull object);
758 extern id _Nullable objc_loadWeak(id _Nullable *_Nonnull object);
759 extern void objc_copyWeak(id _Nullable *_Nonnull dest,
760  id _Nullable *_Nonnull src);
761 extern void objc_moveWeak(id _Nullable *_Nonnull dest,
762  id _Nullable *_Nonnull src);
763 #ifdef __cplusplus
764 }
765 #endif
766 
767 #endif
Class class_getSuperclass(Class class_)
Returns the superclass of the specified class.
Definition: class.m:679
const char * method_getTypeEncoding(Method method)
Returns the type encoding of the specified method.
Definition: method.m:81
Class object_getClass(id object)
Returns the object's class.
Definition: class.m:866
IMP class_getMethodImplementation_stret(Class class_, SEL selector)
Returns the class's method implementation for the specified selector.
Definition: class.m:721
SEL method_getName(Method method)
Returns the name of the specified method.
Definition: method.m:75
const struct objc_property * objc_property_t
A property.
Definition: ObjFWRT.h:128
const char * object_getClassName(id object)
Returns the object's class name.
Definition: class.m:899
const char * ivar_getTypeEncoding(Ivar ivar)
Returns the type encoding of the specified instance variable.
Definition: ivar.m:72
IMP class_replaceMethod(Class class_, SEL selector, IMP implementation, const char *typeEncoding)
Replaces or adds the specified method of the class.
Definition: class.m:843
SEL sel_registerName(const char *name)
Registers a selector with the specified name with the runtime.
Definition: selector.m:75
Method class_getInstanceMethod(Class class_, SEL selector)
Returns the class's instance method for the specified selector.
Definition: class.m:798
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.m:70
Class object_setClass(id object, Class class_)
Sets the object's class.
Definition: class.m:882
void objc_deinit(void)
Deinitializes the Objective-C runtime.
Definition: init.m:39
const char * class_getName(Class class_)
Returns the name of the specified class.
Definition: class.m:670
const char * protocol_getName(Protocol *protocol)
Returns the name of the specified protocol.
Definition: protocol.m:31
ptrdiff_t ivar_getOffset(Ivar ivar)
Returns the offset of the specified instance variable.
Definition: ivar.m:78
uintptr_t object_getTaggedPointerValue(id object)
Returns the value of the specified tagged pointer.
Definition: tagged-pointer.m:80
void objc_removeAssociatedObjects(id object)
Removes all associated objects for the specified object.
Definition: association.m:228
unsigned int objc_getClassList(Class *buffer, unsigned int count)
Gets the list of all classes known to the runtime.
Definition: class.m:590
void objc_setTaggedPointerSecret(uintptr_t secret)
Sets the tagged pointer secret.
Definition: tagged-pointer.m:32
const struct objc_ivar * Ivar
An instance variable.
Definition: ObjFWRT.h:123
bool class_conformsToProtocol(Class class_, Protocol *protocol)
Returns whether the specified class conforms to the specified protocol.
Definition: protocol.m:60
char * property_copyAttributeValue(objc_property_t property, const char *name)
Copies the specified attribute value.
Definition: property.m:230
Class * objc_copyClassList(unsigned int *length)
Copies the list of all classes known to the runtime.
Definition: class.m:636
void * objc_destructInstance(id object)
Destructs the specified object.
Definition: instance.m:83
objc_uncaught_exception_handler objc_setUncaughtExceptionHandler(objc_uncaught_exception_handler handler)
Sets the handler for uncaught exceptions.
Definition: exception.m:781
Method * class_copyMethodList(Class class_, unsigned int *outCount)
Copies the method list of the specified class.
Definition: method.m:26
id _objc_rootAutorelease(id object)
Adds the specified object to the topmost autorelease pool.
Definition: autorelease.m:123
void(* objc_uncaught_exception_handler)(id exception)
A handler for uncaught exceptions.
Definition: ObjFWRT.h:153
objc_property_t * class_copyPropertyList(Class class_, unsigned int *outCount)
Copies the property list of the specified class.
Definition: property.m:172
id objc_constructInstance(Class class_, void *bytes)
Constructs an instance of the specified class in the specified array of bytes.
Definition: instance.m:67
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:116
void objc_setEnumerationMutationHandler(objc_enumeration_mutation_handler handler)
Sets the handler for mutations during enumeration.
Definition: misc.m:56
bool protocol_isEqual(Protocol *protocol1, Protocol *protocol2)
Returns whether two protocols are equal.
Definition: protocol.m:37
bool object_isTaggedPointer(id object)
Returns whether the specified object is a tagged pointer.
Definition: tagged-pointer.m:58
const char * sel_getName(SEL selector)
Returns the name of the specified selector.
Definition: selector.m:119
void(* objc_enumeration_mutation_handler)(id object)
A handler for mutation during enumeration.
Definition: ObjFWRT.h:160
bool protocol_conformsToProtocol(Protocol *protocol1, Protocol *protocol2)
Returns whether the first protocol conforms to the second protocol.
Definition: protocol.m:44
bool BOOL
An Objective-C boolean. Either YES or NO.
Definition: ObjFWRT.h:137
bool sel_isEqual(SEL selector1, SEL selector2)
Checks two selectors for equality.
Definition: selector.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:186
unsigned long class_getInstanceSize(Class class_)
Returns the instance size of the specified class.
Definition: class.m:688
const struct objc_method * Method
A method.
Definition: ObjFWRT.h:109
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...
bool class_isMetaClass(Class class_)
Returns whether the specified class is a metaclass.
Definition: class.m:661
const struct objc_selector * SEL
A selector.
Definition: ObjFWRT.h:102
IMP class_getMethodImplementation(Class class_, SEL selector)
Returns the class's method implementation for the specified selector.
Definition: class.m:697
id objc_createTaggedPointer(int class_, uintptr_t value)
Creates a new tagged pointer.
Definition: tagged-pointer.m:90
bool class_addMethod(Class class_, SEL selector, IMP implementation, const char *typeEncoding)
Adds the specified method to the class.
Definition: class.m:824
bool class_respondsToSelector(Class class_, SEL selector)
Returns whether the specified class responds to the specified selector.
Definition: lookup.m:126
Class objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes)
Allocates a new class and its metaclass.
Definition: class.m:476
int objc_registerTaggedPointerClass(Class class_)
Registers a class for tagged pointers.
Definition: tagged-pointer.m:38
const struct objc_protocol * Protocol
A protocol.
Definition: ObjFWRT.h:117
void objc_registerClassPair(Class class_)
Registers an already allocated class pair.
Definition: class.m:513
const char * property_getName(objc_property_t property)
Returns the name of the specified property.
Definition: property.m:224
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
void objc_setForwardHandler(IMP forward, IMP stretForward)
Sets the forwarding handler for unimplemented methods.
Definition: lookup.m:119
Ivar * class_copyIvarList(Class class_, unsigned int *outCount)
Copies the instance variable list of the specified class.
Definition: ivar.m:26
const char * ivar_getName(Ivar ivar)
Returns the name of the specified instance variable.
Definition: ivar.m:66
A pointer to a class.
Definition: private.h:37
A pointer to any object.
Definition: private.h:66
A struct representing a call to super.
Definition: ObjFWRT.h:165