ObjFW  Check-in [b10efe7b2b]

Overview
Comment:Add +[setImplementation:forMethod:].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b10efe7b2bc2e109cc39a4725a301f208c2a27482e44b8811d4e5f7420777562
User & Date: js on 2009-06-10 15:31:28
Other Links: manifest | tags
Context
2009-06-15
19:22
Remove whitespaces at EOL. check-in: 172e662b71 user: js tags: trunk
2009-06-10
15:31
Add +[setImplementation:forMethod:]. check-in: b10efe7b2b user: js tags: trunk
00:02
Autorelease array returned in -[splitWithDelimiter] properly. check-in: a0b7eb7a66 user: js tags: trunk
Changes

Modified src/OFObject.h from [ca450e2d5c] to [b4d0f545f8].

56
57
58
59
60
61
62










63
64
65
66
67
68
69
/**
 * \param protocol The protocol which should be checked for conformance
 *
 * \return A boolean whether the class conforms to the specified protocol
 */
+ (BOOL)conformsTo: (Protocol*)protocol;











/**
 * 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
 */







>
>
>
>
>
>
>
>
>
>







56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
 * \param protocol The protocol which should be checked for conformance
 *
 * \return A boolean whether the class conforms to the specified protocol
 */
+ (BOOL)conformsTo: (Protocol*)protocol;

/**
 * Replace a method implementation with another implementation.
 *
 * \param selector The selector of the method to replace
 * \param imp The new implementation for the method
 * \return The old implementation
 */
+ (IMP)setImplementation: (IMP)newimp
	       forMethod: (SEL)selector;

/**
 * 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
 */

Modified src/OFObject.m from [da7bffba83] to [afcd5f1e07].

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















162
163
164
165
166
167
168
		if (class_conformsToProtocol(c, protocol))
			return YES;

	return NO;
#endif
}


+  (IMP)replaceMethod: (SEL)selector
  withMethodFromClass: (Class)class;
{
#ifdef __objc_INCLUDE_GNU
	Method_t method = class_get_instance_method(self, selector);
	IMP oldimp, newimp;

	if (method == NULL)
		@throw [OFInvalidArgumentException newWithClass: self
						    andSelector: _cmd];

	oldimp = method_get_imp(method);
	newimp = method_get_imp(class_get_instance_method(class, selector));

	if (oldimp == (IMP)0 || newimp == (IMP)0)
		@throw [OFInvalidArgumentException newWithClass: self
						    andSelector: _cmd];

	method->method_imp = newimp;

	/* Update the dtable if necessary */
	if (sarray_get_safe(((Class)self)->dtable,
	    (sidx)method->method_name->sel_id))
		sarray_at_put_safe(((Class)self)->dtable,
		    (sidx)method->method_name->sel_id, method->method_imp);

	return oldimp;
#else
	Method method = class_getInstanceMethod(self, selector);
	IMP imp = class_getMethodImplementation(class, selector);

	if (method == NULL || imp == NULL)
		@throw [OFInvalidArgumentException newWithClass: self
						    andSelector: _cmd];

	return method_setImplementation(method, imp);
#endif















}

- init
{
	return self;
}








>
|
<



|





|
<
<
<













|
<

|



|

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







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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
		if (class_conformsToProtocol(c, protocol))
			return YES;

	return NO;
#endif
}

+ (IMP)setImplementation: (IMP)newimp
	       forMethod: (SEL)selector

{
#ifdef __objc_INCLUDE_GNU
	Method_t method = class_get_instance_method(self, selector);
	IMP oldimp;

	if (method == NULL)
		@throw [OFInvalidArgumentException newWithClass: self
						    andSelector: _cmd];

	if ((oldimp = method_get_imp(method)) == (IMP)0 || newimp == (IMP)0)



		@throw [OFInvalidArgumentException newWithClass: self
						    andSelector: _cmd];

	method->method_imp = newimp;

	/* Update the dtable if necessary */
	if (sarray_get_safe(((Class)self)->dtable,
	    (sidx)method->method_name->sel_id))
		sarray_at_put_safe(((Class)self)->dtable,
		    (sidx)method->method_name->sel_id, method->method_imp);

	return oldimp;
#else
	Method method;


	if ((method = class_getInstanceMethod(self, selector)) == NULL)
		@throw [OFInvalidArgumentException newWithClass: self
						    andSelector: _cmd];

	return method_setImplementation(method, newimp);
#endif
}

+  (IMP)replaceMethod: (SEL)selector
  withMethodFromClass: (Class)class;
{
	IMP newimp;

#ifdef __objc_INCLUDE_GNU
	newimp = method_get_imp(class_get_instance_method(class, selector));
#else
	newimp = class_getMethodImplementation(class, selector);
#endif

	return [self setImplementation: newimp
			     forMethod: selector];
}

- init
{
	return self;
}