ObjFW  Check-in [cace873874]

Overview
Comment:Improve +[inheritInstanceMethodsFromClass:].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: cace87387421ec1e93a2a85469410a5acc413d038395b6a16f7d3016da945128
User & Date: js on 2011-07-30 02:14:47
Other Links: manifest | tags
Context
2011-07-30
17:43
Fix a typo. check-in: 5613f7fc46 user: js tags: trunk
02:14
Improve +[inheritInstanceMethodsFromClass:]. check-in: cace873874 user: js tags: trunk
2011-07-29
21:54
Add +[inheritInstanceMethodsFromClass:] to OFObject. check-in: bf0f10966f user: js tags: trunk
Changes

Modified src/OFObject.h from [250cefa1d6] to [1644680d03].

394
395
396
397
398
399
400
401
402



403
404
405
406
407
408
409
	 withTypeEncoding: (const char*)typeEncoding
	   implementation: (IMP)implementation;

/**
 * \brief Adds all instance methods from the specified class to the class that
 *	  is the receiver.
 *
 * Existing methods will not be overriden, so that it behaves similar to normal
 * inheritance.



 *
 * \param class The class from which the instance methods should be inherited
 */
+ (void)inheritInstanceMethodsFromClass: (Class)class;

/**
 * \brief Initializes an already allocated object.







|
|
>
>
>







394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
	 withTypeEncoding: (const char*)typeEncoding
	   implementation: (IMP)implementation;

/**
 * \brief Adds all instance methods from the specified class to the class that
 *	  is the receiver.
 *
 * Methods implemented by the receiving class itself will not be overridden,
 * however methods implemented by its superclass will. Therefore it behaves
 * similar as if the specified class is the superclass of the receiver.
 *
 * The specified class may not use instance variables and has to use accessors.
 *
 * \param class The class from which the instance methods should be inherited
 */
+ (void)inheritInstanceMethodsFromClass: (Class)class;

/**
 * \brief Initializes an already allocated object.

Modified src/OFObject.m from [c64aa46779] to [e21e0eaee6].

24
25
26
27
28
29
30

31
32
33
34
35
36
37

#include <unistd.h>

#include <assert.h>

#import "OFObject.h"
#import "OFArray.h"

#import "OFIntrospection.h"
#import "OFAutoreleasePool.h"

#import "OFAllocFailedException.h"
#import "OFEnumerationMutationException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"







>







24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

#include <unistd.h>

#include <assert.h>

#import "OFObject.h"
#import "OFArray.h"
#import "OFSet.h"
#import "OFIntrospection.h"
#import "OFAutoreleasePool.h"

#import "OFAllocFailedException.h"
#import "OFEnumerationMutationException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
461
462
463
464
465
466
467

468
469
470







471
472
473
474
475
476
477
478



479
480
481
482




483
484
485
486
487
488
489
490
491
492
					      selector: _cmd];
#endif
}

+ (void)inheritInstanceMethodsFromClass: (Class)class
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

	OFIntrospection *introspection;
	OFMethod **cArray;
	size_t i, count;








	introspection = [OFIntrospection introspectionWithClass: class];
	cArray = [[introspection instanceMethods] cArray];
	count = [[introspection instanceMethods] count];

	for (i = 0; i < count; i++) {
		SEL selector;
		IMP implementation;




		selector = [cArray[i] selector];
		implementation = [class instanceMethodForSelector: selector];





		[self addInstanceMethod: selector
		       withTypeEncoding: [cArray[i] typeEncoding]
			 implementation: implementation];
	}

	[pool release];
}

- init
{







>



>
>
>
>
>
>
>








>
>
>




>
>
>
>
|
|
|







462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
					      selector: _cmd];
#endif
}

+ (void)inheritInstanceMethodsFromClass: (Class)class
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMutableSet *set = [OFMutableSet set];
	OFIntrospection *introspection;
	OFMethod **cArray;
	size_t i, count;

	introspection = [OFIntrospection introspectionWithClass: self];
	cArray = [[introspection instanceMethods] cArray];
	count = [[introspection instanceMethods] count];

	for (i = 0; i < count; i++)
		[set addObject: [cArray[i] name]];

	introspection = [OFIntrospection introspectionWithClass: class];
	cArray = [[introspection instanceMethods] cArray];
	count = [[introspection instanceMethods] count];

	for (i = 0; i < count; i++) {
		SEL selector;
		IMP implementation;

		if ([set containsObject: [cArray[i] name]])
			continue;

		selector = [cArray[i] selector];
		implementation = [class instanceMethodForSelector: selector];

		if ([self respondsToSelector: selector])
			[self setImplementation: implementation
			      forInstanceMethod: selector];
		else
			[self addInstanceMethod: selector
			       withTypeEncoding: [cArray[i] typeEncoding]
				 implementation: implementation];
	}

	[pool release];
}

- init
{