Revision: 5785
          http://skim-app.svn.sourceforge.net/skim-app/?rev=5785&view=rev
Author:   hofman
Date:     2009-09-22 23:54:03 +0000 (Tue, 22 Sep 2009)

Log Message:
-----------
add category with class factory methods for some common pointer functions we use

Added Paths:
-----------
    trunk/NSPointerFunctions_SKExtensions.h
    trunk/NSPointerFunctions_SKExtensions.m

Added: trunk/NSPointerFunctions_SKExtensions.h
===================================================================
--- trunk/NSPointerFunctions_SKExtensions.h                             (rev 0)
+++ trunk/NSPointerFunctions_SKExtensions.h     2009-09-22 23:54:03 UTC (rev 
5785)
@@ -0,0 +1,51 @@
+//
+//  NSPointerFunctions_SKExtensions.h
+//  Skim
+//
+//  Created by Christiaan on 9/23/09.
+/*
+ This software is Copyright (c) 2009
+ Christiaan Hofman. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ - Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in
+    the documentation and/or other materials provided with the
+    distribution.
+
+ - Neither the name of Christiaan Hofman nor the names of any
+    contributors may be used to endorse or promote products derived
+    from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import <Cocoa/Cocoa.h>
+
+
+...@interface NSPointerFunctions (SKExtensions)
+
++ (id)strongObjectsPointerFunctions;
++ (id)integerPointerFunctions;
++ (id)floatPointerFunctions;
++ (id)rectPointerFunctions;
++ (id)rangePointerFunctions;
++ (id)caseInsensitiveStringPointerFunctions;
+
+...@end

Added: trunk/NSPointerFunctions_SKExtensions.m
===================================================================
--- trunk/NSPointerFunctions_SKExtensions.m                             (rev 0)
+++ trunk/NSPointerFunctions_SKExtensions.m     2009-09-22 23:54:03 UTC (rev 
5785)
@@ -0,0 +1,113 @@
+//
+//  NSPointerFunctions_SKExtensions.m
+//  Skim
+//
+//  Created by Christiaan on 9/23/09.
+/*
+ This software is Copyright (c) 2009
+ Christiaan Hofman. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ - Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in
+    the documentation and/or other materials provided with the
+    distribution.
+
+ - Neither the name of Christiaan Hofman nor the names of any
+    contributors may be used to endorse or promote products derived
+    from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "NSPointerFunctions_SKExtensions.h"
+
+
+...@implementation NSPointerFunctions (SKExtensions)
+
+static NSUInteger floatSizeFunction(const void *item) { return 
sizeof(CGFloat); }
+
+static NSUInteger rectSizeFunction(const void *item) { return sizeof(NSRect); }
+
+static NSUInteger rangeSizeFunction(const void *item) { return 
sizeof(NSRange); }
+
+#define STACK_BUFFER_SIZE 256
+
+static BOOL caseInsensitiveStringEqual(const void *item1, const void *item2, 
NSUInteger (*size)(const void *item)) {
+    return (CFStringGetLength(item1) == CFStringGetLength(item2) && 
CFStringCompareWithOptions(item1, item2, CFRangeMake(0, 
CFStringGetLength(item1)), kCFCompareCaseInsensitive) == kCFCompareEqualTo);
+}
+
+static NSUInteger caseInsensitiveStringHash(const void *item, NSUInteger 
(*size)(const void *item)) {
+    if(item == NULL) return 0;
+    
+    uint32_t hash = 0;
+    CFAllocatorRef allocator = CFGetAllocator(item);
+    CFIndex len = CFStringGetLength(item);
+    
+    // use a generous length, in case the lowercase changes the number of 
characters
+    UniChar *buffer, stackBuffer[STACK_BUFFER_SIZE];
+    if (len + 10 >= STACK_BUFFER_SIZE)
+        buffer = (UniChar *)CFAllocatorAllocate(allocator, (len + 10) * 
sizeof(UniChar), 0);
+    else
+        buffer = stackBuffer;
+    CFStringGetCharacters(item, CFRangeMake(0, len), buffer);
+    
+    // If we create the string with external characters, 
CFStringGetCharactersPtr is guaranteed to succeed; since we're going to call 
CFStringGetCharacters anyway in fastHash if CFStringGetCharactsPtr fails, let's 
do it now when we lowercase the string
+    CFMutableStringRef mutableString = 
CFStringCreateMutableWithExternalCharactersNoCopy(allocator, buffer, len, len + 
10, (buffer != stackBuffer ? allocator : kCFAllocatorNull));
+    CFStringLowercase(mutableString, NULL);
+    hash = CFHash(mutableString);
+    // if we used the allocator, this should free the buffer for us
+    CFRelease(mutableString);
+    return hash;
+}
+
++ (id)strongObjectsPointerFunctions {
+    return [NSPointerFunctions 
pointerFunctionsWithOptions:NSPointerFunctionsStrongMemory | 
NSPointerFunctionsObjectPersonality];
+}
+
++ (id)integerPointerFunctions {
+    return [NSPointerFunctions 
pointerFunctionsWithOptions:NSPointerFunctionsOpaqueMemory | 
NSPointerFunctionsIntegerPersonality];
+}
+
++ (id)floatPointerFunctions {
+    NSPointerFunctions *pointerFunctions = [NSPointerFunctions 
pointerFunctionsWithOptions:NSPointerFunctionsMallocMemory | 
NSPointerFunctionsCopyIn | NSPointerFunctionsStructPersonality];
+    [pointerFunctions setSizeFunction:&floatSizeFunction];
+    return pointerFunctions;
+}
+
++ (id)rectPointerFunctions {
+    NSPointerFunctions *pointerFunctions = [NSPointerFunctions 
pointerFunctionsWithOptions:NSPointerFunctionsMallocMemory | 
NSPointerFunctionsCopyIn | NSPointerFunctionsStructPersonality];
+    [pointerFunctions setSizeFunction:&rectSizeFunction];
+    return pointerFunctions;
+}
+
++ (id)rangePointerFunctions {
+    NSPointerFunctions *pointerFunctions = [NSPointerFunctions 
pointerFunctionsWithOptions:NSPointerFunctionsMallocMemory | 
NSPointerFunctionsCopyIn | NSPointerFunctionsStructPersonality];
+    [pointerFunctions setSizeFunction:&rangeSizeFunction];
+    return pointerFunctions;
+}
+
++ (id)caseInsensitiveStringPointerFunctions {
+    NSPointerFunctions *pointerFunctions = [NSPointerFunctions 
pointerFunctionsWithOptions:NSPointerFunctionsStrongMemory | 
NSPointerFunctionsObjectPersonality];
+    [pointerFunctions setIsEqualFunction:&caseInsensitiveStringEqual];
+    [pointerFunctions setHashFunction:&caseInsensitiveStringHash];
+    return pointerFunctions;
+}
+
+...@end


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to