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.
168 lines
4.6 KiB
Plaintext
Executable File
168 lines
4.6 KiB
Plaintext
Executable File
/*
|
|
* CPRange.j
|
|
* Foundation
|
|
*
|
|
* Created by Francisco Tolmasky.
|
|
* Copyright 2008, 280 North, Inc.
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
|
|
/*!
|
|
@addtogroup foundation
|
|
@{
|
|
*/
|
|
|
|
/*!
|
|
Makes a CPRange.
|
|
@param location the location for new range
|
|
@param length the length of the new range
|
|
@group CPRange
|
|
@return CPRange the new range object
|
|
*/
|
|
function CPMakeRange(location, length)
|
|
{
|
|
return { location:location, length:length };
|
|
}
|
|
|
|
/*!
|
|
Makes a copy of a CPRange.
|
|
@param aRange the CPRange to copy
|
|
@group CPRange
|
|
@return CPRange the copy of the range
|
|
*/
|
|
function CPMakeRangeCopy(aRange)
|
|
{
|
|
return { location:aRange.location, length:aRange.length };
|
|
}
|
|
|
|
/*!
|
|
Determines if a range is empty \c length is 0.
|
|
@param aRange the range to test
|
|
@group CPRange
|
|
@return YES if the range is empty
|
|
*/
|
|
function CPEmptyRange(aRange)
|
|
{
|
|
return aRange.length === 0;
|
|
}
|
|
|
|
/*!
|
|
Finds the range maximum. (\c location + length)
|
|
@param aRange the range to calculate a maximum from
|
|
@group CPRange
|
|
@return int the range maximum
|
|
*/
|
|
function CPMaxRange(aRange)
|
|
{
|
|
return aRange.location + aRange.length;
|
|
}
|
|
|
|
/*!
|
|
Determines if two CPRanges are equal.
|
|
@param lhsRange the first CPRange
|
|
@param rhsRange the second CPRange
|
|
@return BOOL \c YES if the two CPRanges are equal.
|
|
*/
|
|
function CPEqualRanges(lhsRange, rhsRange)
|
|
{
|
|
return ((lhsRange.location === rhsRange.location) && (lhsRange.length === rhsRange.length));
|
|
}
|
|
|
|
/*!
|
|
Determines if a number is within a specified CPRange.
|
|
@param aLocation the number to check
|
|
@param aRange the CPRange to check within
|
|
@group CPRange
|
|
@return BOOL \c YES if \c aLocation is within the range
|
|
*/
|
|
function CPLocationInRange(aLocation, aRange)
|
|
{
|
|
return ((aLocation >= aRange.location) && (aLocation < CPMaxRange(aRange)));
|
|
}
|
|
|
|
/*!
|
|
Creates a new range with the minimum \c location and a \c length
|
|
that extends to the maximum \c length.
|
|
@param lhsRange the first CPRange
|
|
@param rhsRange the second CPRange
|
|
@group CPRange
|
|
@return CPRange the new CPRange
|
|
*/
|
|
function CPUnionRange(lhsRange, rhsRange)
|
|
{
|
|
var location = MIN(lhsRange.location, rhsRange.location);
|
|
|
|
return CPMakeRange(location, MAX(CPMaxRange(lhsRange), CPMaxRange(rhsRange)) - location);
|
|
}
|
|
|
|
/*!
|
|
Creates a new CPRange that spans the common range of two CPRanges
|
|
@param lhsRange the first CPRange
|
|
@param rhsRange the second CPRange
|
|
@group CPRange
|
|
@return CPRange the new CPRange
|
|
*/
|
|
function CPIntersectionRange(lhsRange, rhsRange)
|
|
{
|
|
if (CPMaxRange(lhsRange) < rhsRange.location || CPMaxRange(rhsRange) < lhsRange.location)
|
|
return CPMakeRange(0, 0);
|
|
|
|
var location = MAX(lhsRange.location, rhsRange.location);
|
|
|
|
return CPMakeRange(location, MIN(CPMaxRange(lhsRange), CPMaxRange(rhsRange)) - location);
|
|
}
|
|
|
|
/*!
|
|
Checks if a range completely contains another range. In other words, if one range is the "super range" of another.
|
|
@param lhsRange the containing range
|
|
@param rhsRange the range we are testing to see if lhsRange contains it
|
|
@group CPRange
|
|
@return BOOL whether or not lhsRange completely contains rhsRange
|
|
*/
|
|
function CPRangeInRange(lhsRange, rhsRange)
|
|
{
|
|
return (lhsRange.location <= rhsRange.location && CPMaxRange(lhsRange) >= CPMaxRange(rhsRange));
|
|
}
|
|
|
|
/*!
|
|
Returns a string describing a range.
|
|
@param aRange the range to describe
|
|
@group CPRange
|
|
@return CPString a describing string
|
|
*/
|
|
function CPStringFromRange(aRange)
|
|
{
|
|
return "{" + aRange.location + ", " + aRange.length + "}";
|
|
}
|
|
|
|
/*!
|
|
Creates a CPRange from the contents of a CPString.
|
|
@param aString the string to create a CPRange from
|
|
@group CPRange
|
|
@return CPRange the new range
|
|
*/
|
|
function CPRangeFromString(aString)
|
|
{
|
|
var comma = aString.indexOf(',');
|
|
|
|
return { location:parseInt(aString.substr(1, comma - 1)), length:parseInt(aString.substring(comma + 1, aString.length)) };
|
|
}
|
|
|
|
/*!
|
|
@}
|
|
*/
|
|
|