Adding self checks in toll free bridged objects so that they are subclassable.

This commit is contained in:
Derek Hammer committed 2010-09-06 12:07:50 -05:00
1 parent e704d42adb
commit a09878dcb4
6 files changed
+64

No files matched your search

+1
View File
@@ -108,6 +108,7 @@ CPEnumerationReverse = 1 << 1;
*/
+ (id)alloc
{
if ([self class] !== CPArray && [self class] !== CPMutableArray) return [super alloc];
return [];
}
+1
View File
@@ -84,6 +84,7 @@
*/
+ (id)alloc
{
if ([self class] !== CPDictionary && [self class] !== CPMutableDictionary) return [super alloc];
return new CFMutableDictionary();
}
+1
View File
@@ -54,6 +54,7 @@ if (input == nil)
*/
+ (id)alloc
{
if ([self class] !== CPException) return [super alloc];
return new Error();
}
+1
View File
@@ -43,6 +43,7 @@ var __placeholder = new Number(),
+ (id)alloc
{
if ([self class] !== CPNumber) return [super alloc];
return __placeholder;
}
+1
View File
@@ -91,6 +91,7 @@ var CPStringRegexSpecialCharacters = [
*/
+ (id)alloc
{
if ([self class] !== CPString) return [super alloc];
return new String;
}
+59
View File
@@ -0,0 +1,59 @@
@implementation SubclassTollFreeTest : OJTestCase
- (void)testThatSubclassTollFreeDoesAllowForSubclassing
{
[OJAssert assert:@"a" equals:[[[MyDict alloc] init] newMessage]];
[OJAssert assert:@"a" equals:[[[MyString alloc] init] newMessage]];
[OJAssert assert:@"a" equals:[[[MyNum alloc] init] newMessage]];
[OJAssert assert:@"a" equals:[[[MyException alloc] init] newMessage]];
[OJAssert assert:@"a" equals:[[[MyArray alloc] init] newMessage]];
}
@end
@import <Foundation/CPDictionary.j>
@implementation MyDict : CPDictionary
- (id)newMessage
{
return "a";
}
@end
@implementation MyNum : CPNumber
- (id)newMessage
{
return "a";
}
@end
@implementation MyString : CPString
- (id)newMessage
{
return "a";
}
@end
@implementation MyException : CPException
- (id)newMessage
{
return "a";
}
@end
@implementation MyArray : CPArray
- (id)newMessage
{
return "a";
}
@end