ObjFW  Check-in [2828853e17]

Overview
Comment:More enhancements in +[OFObject inheritMethodsFromClass:].

Inherits class and instance methods now, handles superclasses, etc.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 2828853e17d66b88a9161f38206f9be8ac122f3b39a98e9df85e3fa8bf44b7bf
User & Date: js on 2011-07-30 23:31:56
Other Links: manifest | tags
Context
2011-07-31
02:07
Implement adding methods for the GNU runtime. check-in: db5f8c2c1b user: js tags: trunk
2011-07-30
23:31
More enhancements in +[OFObject inheritMethodsFromClass:]. check-in: 2828853e17 user: js tags: trunk
23:14
+[addClassMethod:withTypeEncoding:implementation:] to OFObject. check-in: 6083048589 user: js tags: trunk
Changes

Modified src/OFObject.h from [1644680d03] to [0f97f0d9d0].

391
392
393
394
395
396
397
398
399
400
401
402
403





404
405
406
407
408
409
410
411
412
413
414
415
416
 * \return Whether the method has been added
 */
+ (BOOL)addInstanceMethod: (SEL)selector
	 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.
 *
 * Derived classes may override this, but need to do self = [super init] before
 * they do any initialization themselves. init may never return nil, instead
 * an exception (for example OFInitializationFailed) should be thrown.







|
|




>
>
>
>
>





|







391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
 * \return Whether the method has been added
 */
+ (BOOL)addInstanceMethod: (SEL)selector
	 withTypeEncoding: (const char*)typeEncoding
	   implementation: (IMP)implementation;

/**
 * \brief Adds all 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.
 *
 * All methods from the superclasses of the specified class will also be added.
 *
 * If the specified class is a superclass of the receiving class, nothing is
 * done.
 *
 * 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)inheritMethodsFromClass: (Class)class;

/**
 * \brief Initializes an already allocated object.
 *
 * Derived classes may override this, but need to do self = [super init] before
 * they do any initialization themselves. init may never return nil, instead
 * an exception (for example OFInitializationFailed) should be thrown.

Modified src/OFObject.m from [6537d810e0] to [3cd936beff].

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
509
510
511
512
513
514
515
516
517


518
519
520
521
522
523
524
	    typeEncoding);
#else
	@throw [OFNotImplementedException newWithClass: self
					      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
{
	return self;
}








|

|
|




>
>
>
>
>
>
>
>

>
>
>
>
>
>
>




|


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







|





|









>
>







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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
	    typeEncoding);
#else
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
#endif
}

+ (void)inheritMethodsFromClass: (Class)class
{
	OFAutoreleasePool *pool;
	OFMutableSet *classMethods, *instanceMethods;
	OFIntrospection *introspection;
	OFMethod **cArray;
	size_t i, count;

	if ([self isSubclassOfClass: class])
		return;

	pool = [[OFAutoreleasePool alloc] init];

	classMethods = [OFMutableSet set];
	instanceMethods = [OFMutableSet set];

	introspection = [OFIntrospection introspectionWithClass: self];

	cArray = [[introspection classMethods] cArray];
	count = [[introspection classMethods] count];

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

	cArray = [[introspection instanceMethods] cArray];
	count = [[introspection instanceMethods] count];

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

	introspection = [OFIntrospection introspectionWithClass: class];

	cArray = [[introspection classMethods] cArray];
	count = [[introspection classMethods] count];

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

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

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

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

	cArray = [[introspection instanceMethods] cArray];
	count = [[introspection instanceMethods] count];

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

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

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

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

	[pool release];

	[self inheritMethodsFromClass: [class superclass]];
}

- init
{
	return self;
}