ObjFW  Diff

Differences From Artifact [6f74f6252f]:

To Artifact [e051be24f6]:

  • File src/OFObject.h — part of check-in [cbdd534337] at 2009-04-12 14:51:00 on branch trunk — Get rid of the dependency on Object.

    This allows libobjfw to run on ObjC2-only runtimes like the one on the
    iPhone. However, it's still relying on objc_msgSendv for plugins, which
    is unavailable in ObjC2-only runtimes, thus OFPlugins are unavailable
    on the iPhone until I write a replacement for objc_msgSendv. (user: js, size: 5627) [annotate] [blame] [check-ins using]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15




16
17






18





19

20




21
22



23


24

25




26
27








28










29

30




31
32



























33
34


35



36
37














38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import <objc/Object.h>

#include <stdint.h>





@protocol OFRetainRelease
/**






 * Increases the retain count.





 */

- retain;





/**



 * Decreases the retain cound and frees the object if it reaches 0.


 */

- (void)release;





/**








 * Adds the object to the autorelease pool that is on top of the thread's stack.










 */

- autorelease;





/**



























 * \return The retain count
 */


- (size_t)retainCount;



@end















@protocol OFHashable
/**
 * Compare two objects.
 * Classes containing data (like strings, arrays, lists etc.) should reimplement
 * this!
 *
 * \param obj The object which is tested for equality
 * \return A boolean whether the object is equal to the other object
 */
- (BOOL)isEqual: (id)obj;

/**
 * Calculate a hash for the object.
 * Classes containing data (like strings, arrays, lists etc.) should reimplement
 * this!
 *
 * \return A 32 bit hash for the object
 */
- (uint32_t)hash;
@end

/**
 * The OFObject class is the base class for all other classes inside ObjFW.
 */
@interface OFObject: Object <OFRetainRelease, OFHashable>
{
	void   **__memchunks;
	size_t __memchunks_size;
	size_t __retain_count;
}

/**
 * Initialize the already allocated object.
 * Also sets up the memory pool for the object.
 *
 * \return An initialized object
 */
- init;

/**
 * Frees the object and also frees all memory allocated via its memory pool.
 */
- free;

/**
 * Adds a pointer to the memory pool.
 * This is useful to add memory allocated by functions such as asprintf to the
 * pool so it gets freed automatically when the object is freed.
 *
 * \param ptr A pointer to add to the memory pool











<
|


>
>
>
>
|

>
>
>
>
>
>
|
>
>
>
>
>

>
|
>
>
>
>


>
>
>
|
>
>

>
|
>
>
>
>


>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>

>
|
>
>
>
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

>
>
|
>
>
>
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
|


















<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







1
2
3
4
5
6
7
8
9
10
11

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
























155
156
157
158
159
160
161
/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */


#include <stddef.h>
#include <stdint.h>

#import <objc/objc.h>
#ifndef __objc_INCLUDE_GNU
#import <objc/message.h>
#endif

/**
 * The OFObject class is the base class for all other classes inside ObjFW.
 */
@interface OFObject
{
	Class  isa;
}

/**
 * This code is executed once when a method of the class is called for the first
 * time.
 * Derived classes can override this to execute their own code on
 * initialization.
 */
+ initialize;

/**
 * Allocates memory for an instance of the class.
 */
+ alloc;

/**
 * Allocated memory for an instance of the class and initializes the instance.
 */
+ new;

/**
 * \return The class pointer
 */
+ (Class)class;

/**
 * \return The name of the class as a C string
 */
+ (const char*)name;

/**
 * Replace a method with a method from another class.
 *
 * \param selector The selector of the method to replace
 * \param class The class from which the new method should be taken
 * \return The old implementation
 */
+ (IMP)replaceMethod: (SEL)selector
 withMethodFromClass: (Class)class;

/**
 * Initialize the already allocated object.
 * Also sets up the memory pool for the object.
 *
 * \return An initialized object
 */
- init;

/**
 * \return A pointer to the class of the instance
 */
- (Class)class;

/**
 * \return The name of the instance's class as a C string
 */
- (const char*)name;

/**
 * \param class The class whose kind is checked
 *
 * \return A boolean whether the object is of the specified kind
 */
- (BOOL)isKindOf: (Class)class;

/**
 * \param selector The selector which should be checked
 *
 * \return A boolean whether the objects responds to the specified selector
 */
- (BOOL)respondsTo: (SEL)selector;

/**
 * \param selector The selector for which the method should be returned
 *
 * \return The implementation for the specified selector
 */
- (IMP)methodFor: (SEL)selector;

/**
 * This method is called when a method was called which isn't implemented.
 * It's possible to override it so the method call cann be forwarded to another
 * object.
 *
 * \param selector The selector which was called
 * \param args The arguments with which the selector was called
 * \return The return value of the call
 */
#ifdef __objc_INCLUDE_GNU
- (retval_t)forward: (SEL)selector
		   : (arglist_t)args;
#else
- (id)forward: (SEL)selector
	     : (marg_list)args;
#endif

/**
 * Perform the given selector with the given arguments.
 *
 * \param selector The selector to perform
 * \param args The arguments with which the selector is performed
 * \return The return value of the performed selector
 */
#ifdef __objc_INCLUDE_GNU
- (retval_t)performv: (SEL)selector
		    : (arglist_t)args;
#else
- performv: (SEL)selector
	  : (marg_list)args;
#endif

/**
 * Compare two objects.
 * Classes containing data (like strings, arrays, lists etc.) should reimplement
 * this!
 *
 * \param obj The object which is tested for equality
 * \return A boolean whether the object is equal to the other object
 */
- (BOOL)isEqual: (id)obj;

/**
 * Calculate a hash for the object.
 * Classes containing data (like strings, arrays, lists etc.) should reimplement
 * this!
 *
 * \return A 32 bit hash for the object
 */
- (uint32_t)hash;

























/**
 * Adds a pointer to the memory pool.
 * This is useful to add memory allocated by functions such as asprintf to the
 * pool so it gets freed automatically when the object is freed.
 *
 * \param ptr A pointer to add to the memory pool
133
134
135
136
137
138
139

























140

/**
 * Frees allocated memory and removes it from the memory pool.
 *
 * \param ptr A pointer to the allocated memory
 */
- freeMem: (void*)ptr;

























@end







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

/**
 * Frees allocated memory and removes it from the memory pool.
 *
 * \param ptr A pointer to the allocated memory
 */
- freeMem: (void*)ptr;

/**
 * Increases the retain count.
 */
- retain;

/**
 * Adds the object to the autorelease pool that is on top of the thread's stack.
 */
- autorelease;

/**
 * \return The retain count
 */
- (size_t)retainCount;

/**
 * Decreases the retain cound and frees the object if it reaches 0.
 */
- (void)release;

/**
 * Frees the object and also frees all memory allocated via its memory pool.
 */
- free;
@end