root/open-source/closures-in-objc/trunk/MyBlock.m

Revision 87, 3.3 kB (checked in by mhaecker, 1 year ago)

initial import

Line 
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is trying to emulate Smalltalk style Blocks.
15 *
16 * The Initial Developer of the Original Code is
17 * M-Soft, IT-Dienste.
18 * Portions created by the Initial Developer are Copyright (C) 2004
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 *   Martin HŠcker <mhaecker@mac.com>
23 *
24 * ***** END LICENSE BLOCK ***** */
25
26 #import "MyBlock.h"
27 #include <string.h>
28
29 #define MY_WRONG_RETURN_VALUE_EXCEPTION @"MyWrongReturnValueException"
30
31 @implementation MyBlock
32
33 #pragma mark -
34 #pragma mark Private methods
35
36 BOOL AreTypeStringsEqual(char *first, char *second) {
37     return (0 == strcmp(first, second));
38 }
39
40 - (void) assertReturnType: (char *) aType numberOfArguments: (int) anInt {
41     if (AreTypeStringsEqual(aType, returnType)
42             && anInt == numberOfArguments) {
43         return;
44     }
45     [NSException raise:MY_WRONG_RETURN_VALUE_EXCEPTION format:@"Wrong return-type or number of arguments, this block returns '%s' and gets %d arguments", returnType, numberOfArguments];
46 }
47
48 unsigned CountNumberOfArguments(char * aString) {
49     id arguments = [NSString stringWithCString: aString];
50     if ([arguments isEqualTo:@"void"]) {
51         return 0;
52     }
53     return [[arguments componentsSeparatedByString:@","] count];
54 }
55
56 #pragma mark -
57 #pragma mark Instance creation
58
59 + blockWithFunction: (void *) aFunctionPointer
60          returnType: (char *) aType
61           arguments: (char *)argumentString;
62 {
63     return [[[[self class] alloc] initWithFunction: aFunctionPointer returnType: aType arguments: argumentString] autorelease];
64 }
65
66
67 - initWithFunction: (void *) aFunctionPointer
68         returnType: (char *) aType
69          arguments: (char *)argumentString;
70 {
71     if (self = [super init]) {
72         func = aFunctionPointer;
73         returnType = aType;
74         numberOfArguments = CountNumberOfArguments(argumentString);
75     }
76     return self;
77 }
78
79 // TODO consider adding exceptions to @selector(retain) so nobody tries to store them for long
80 // this seems to collide with my wish to enable exactly this somehow...
81
82 #pragma mark -
83 #pragma mark Evaluating the block
84
85 // TODO I need some sort of error checking!
86 - value; {
87     [self assertReturnType: @encode(id) numberOfArguments: 0];
88     functionPointer aFunction = (functionPointer) func;
89     return aFunction();
90 }
91
92 - value: argument; {
93     [self assertReturnType: @encode(id) numberOfArguments: 1];
94     unaryFunctionPointer unary = (unaryFunctionPointer) func;
95     return unary(argument);
96 }
97
98 - value: firstArgument value: secondArgument; {
99     [self assertReturnType: @encode(id) numberOfArguments: 2];
100     binaryFunctionPointer binary = (binaryFunctionPointer) func;
101     return binary(firstArgument, secondArgument);
102 }
103
104 - (BOOL) test: argument; {
105     [self assertReturnType: @encode(BOOL) numberOfArguments: 1];
106     testingFunctionPointer function = (testingFunctionPointer) func;
107     return function(argument);
108 }
109
110
111 @end
Note: See TracBrowser for help on using the browser.