mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-08-25 21:17:03 +00:00
Previously, Cappuccino was using preprocessor macros internally for the CGPoint/Size/Rect/Inset/Affine functions, as well as for CPRange. These macros had the same name as the corresponding function, but began with _. The functions were actually defined using the macros.
The motivation behind using macros was to increase performance by reducing function calls. However, there were a number of problems with this approach:
- There was an artificial dichotomy between _CG macros and the corresponding CG functions. We never completely replaced CG function calls with _CG macros. In fact, they were often mixed up in the same file. There was an extra burden on the programmer to remember to use the macro instead of the function.
- If a method call was passed as an argument to a macro, performance could actually be significantly *worse* than a function call. For example, _CGGetRectMakeCopy([view frame]) would expand to `{ origin:{ x:[view frame].origin.x, y:[view frame].origin.y }, size:{ width:[view frame].size.width, height:[view frame].size.height } }`. So instead of a single objj_msgSend and a single simple function call, we ended up with 4 objj_msgSend calls, which are way more expensive than simple function calls.
- Because of this expansion problem, to use macros efficiently required us to remember to use variables for all macro parameters. This didn't happen, and shouldn't have to happen.
- Finally, with modern Javascript engines, function call overhead is so small that it really isn't worth using the macros.
This commit eliminates the _CGGeometry, CGAffineTransformation and CPRange macros and replaces them with function calls.
BREAKING CHANGE:
The macros are no longer available. They could only be used with compiled code, but if there is any user code that used them, they will have to be replaced with the corresponding functions.
105 lines
2.4 KiB
Plaintext
105 lines
2.4 KiB
Plaintext
@import <AppKit/AppKit.j>
|
|
|
|
@implementation CPBrowserTest : OJTestCase
|
|
{
|
|
CPBrowser browser;
|
|
CPBrowserDelegate delegate;
|
|
}
|
|
|
|
- (void)setUp
|
|
{
|
|
browser = [[CPBrowser alloc] initWithFrame:CGRectMake(0, 0, 500, 300)];
|
|
delegate = [CPBrowserDelegate new];
|
|
[delegate setEntries:[".1", ".1.1", ".1.2", ".1.2.1", ".1.2.2", ".2", ".3", ".3.1"]];
|
|
[browser setDelegate:delegate];
|
|
}
|
|
|
|
/*!
|
|
Verify that the items are loaded into rows in their proper columns.
|
|
*/
|
|
- (void)testRows
|
|
{
|
|
[self assert:".1" equals:[browser itemAtRow:0 inColumn:0]];
|
|
[self assert:".2" equals:[browser itemAtRow:1 inColumn:0]];
|
|
[self assert:".3" equals:[browser itemAtRow:2 inColumn:0]];
|
|
|
|
// Only one column so far.
|
|
[self assert:nil equals:[browser itemAtRow:0 inColumn:1]];
|
|
|
|
// Drill down.
|
|
[browser selectRowIndexes:[CPIndexSet indexSetWithIndex:0] inColumn:0];
|
|
[browser addColumn];
|
|
[self assert:".1.1" equals:[browser itemAtRow:0 inColumn:1]];
|
|
[self assert:".1.2" equals:[browser itemAtRow:1 inColumn:1]];
|
|
}
|
|
|
|
- (void)testCoding
|
|
{
|
|
// This should preferably not crash.
|
|
var decoded = [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:browser]];
|
|
|
|
// This basic test will serve to verify that the decoded object is not broken.
|
|
browser = decoded;
|
|
[self testRows];
|
|
}
|
|
|
|
@end
|
|
|
|
@implementation CPBrowserDelegate : CPObject
|
|
{
|
|
CPArray entries @accessors;
|
|
}
|
|
|
|
- (CPArray)childrenOfPrefix:(CPString)theItem
|
|
{
|
|
if (!theItem)
|
|
theItem = "";
|
|
|
|
var matcher = new RegExp("^" + theItem + "\\.\\d$"),
|
|
children = [];
|
|
for (var i = 0; i < entries.length; i++)
|
|
if (matcher.exec(entries[i]))
|
|
children.push(entries[i]);
|
|
|
|
return children;
|
|
}
|
|
|
|
- (id)browser:(id)aBrowser numberOfChildrenOfItem:(id)theItem
|
|
{
|
|
return [[self childrenOfPrefix:theItem] count];
|
|
}
|
|
|
|
- (id)browser:(id)aBrowser child:(int)theIndex ofItem:(id)theItem
|
|
{
|
|
return [self childrenOfPrefix:theItem][theIndex];
|
|
}
|
|
|
|
- (id)browser:(id)aBrowser objectValueForItem:(id)theItem
|
|
{
|
|
return theItem;
|
|
}
|
|
|
|
- (id)browser:(id)aBrowser isLeafItem:(id)theItem
|
|
{
|
|
return ![[self childrenOfPrefix:theItem] count];
|
|
}
|
|
|
|
- (id)initWithCoder:(CPCoder)aCoder
|
|
{
|
|
self = [super init];
|
|
|
|
if (self)
|
|
{
|
|
entries = [aCoder decodeObjectForKey:"entries"];
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (void)encodeWithCoder:(CPCoder)aCoder
|
|
{
|
|
[aCoder encodeObject:entries forKey:"entries"];
|
|
}
|
|
|
|
@end
|