|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. ! 3: * ! 4: * @APPLE_LICENSE_HEADER_START@ ! 5: * ! 6: * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights ! 7: * Reserved. This file contains Original Code and/or Modifications of ! 8: * Original Code as defined in and that are subject to the Apple Public ! 9: * Source License Version 1.0 (the 'License'). You may not use this file ! 10: * except in compliance with the License. Please obtain a copy of the ! 11: * License at http://www.apple.com/publicsource and read it before using ! 12: * this file. ! 13: * ! 14: * The Original Code and all software distributed under the License are ! 15: * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER ! 16: * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, ! 17: * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, ! 18: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the ! 19: * License for the specific language governing rights and limitations ! 20: * under the License." ! 21: * ! 22: * @APPLE_LICENSE_HEADER_END@ ! 23: */ ! 24: ! 25: #import <objc/Object.h> ! 26: #import <objc/objc-runtime.h> ! 27: #import <stdio.h> ! 28: ! 29: @interface Forwarder : Object ! 30: { ! 31: id delegate; ! 32: } ! 33: ! 34: - initWithDelegate: anObject; ! 35: - forward: (SEL) sel : (marg_list) args; ! 36: @end ! 37: ! 38: @implementation Forwarder ! 39: ! 40: - initWithDelegate: anObject ! 41: { ! 42: [super init]; ! 43: delegate = anObject; ! 44: return self; ! 45: } ! 46: ! 47: - forward: (SEL) sel : (marg_list) args ! 48: { ! 49: Method method = class_getInstanceMethod ([delegate class], sel); ! 50: unsigned int size = method_getSizeOfArguments (method); ! 51: id result; ! 52: static int dummy = 0; ! 53: ! 54: fprintf (stderr, "[Forwarder forward: \"%s\" : %u]\n", ! 55: sel_getName (sel), size); ! 56: ! 57: result = objc_msgSendv (delegate, sel, size, args); ! 58: ! 59: fprintf (stderr, "objc_msgSendv() returned\n"); ! 60: ! 61: /* This computation causes d1 to get trashed. */ ! 62: dummy = dummy / 2 + size + 3; ! 63: return result; ! 64: } ! 65: ! 66: @end ! 67: ! 68: struct foo ! 69: { ! 70: int one; ! 71: int two; ! 72: int three; ! 73: int four; ! 74: }; ! 75: ! 76: @interface Delegate : Object ! 77: { ! 78: int x; ! 79: float f; ! 80: double d; ! 81: struct foo s; ! 82: } ! 83: ! 84: - (int) x; ! 85: - (void) setX: (int) anInt; ! 86: - (float) f; ! 87: - (void) setF: (float) aFloat; ! 88: - (double) d; ! 89: - (void) setD: (double) aDouble; ! 90: - (struct foo) s; ! 91: - (void) setS: (struct foo) aStruct; ! 92: @end ! 93: ! 94: @implementation Delegate ! 95: ! 96: - (int) x ! 97: { ! 98: fprintf (stderr, "[Delegate x]\n"); ! 99: ! 100: return x; ! 101: } ! 102: ! 103: - (void) setX: (int) anInt ! 104: { ! 105: fprintf (stderr, "[Delegate setX: %d]\n", anInt); ! 106: ! 107: x = anInt; ! 108: } ! 109: ! 110: - (float) f ! 111: { ! 112: fprintf (stderr, "[Delegate f]\n"); ! 113: ! 114: return f; ! 115: } ! 116: ! 117: - (void) setF: (float) aFloat ! 118: { ! 119: fprintf (stderr, "[Delegate setF: %f]\n", aFloat); ! 120: ! 121: f = aFloat; ! 122: } ! 123: ! 124: - (double) d ! 125: { ! 126: fprintf (stderr, "[Delegate d]\n"); ! 127: ! 128: return d; ! 129: } ! 130: ! 131: - (void) setD: (double) aDouble ! 132: { ! 133: fprintf (stderr, "[Delegate setD: %f]\n", aDouble); ! 134: ! 135: d = aDouble; ! 136: } ! 137: ! 138: - (struct foo) s ! 139: { ! 140: fprintf (stderr, "[Delegate s]\n"); ! 141: ! 142: return s; ! 143: } ! 144: ! 145: - (void) setS: (struct foo) aStruct ! 146: { ! 147: fprintf (stderr, "[Delegate setS: {%d, %d, %d, %d}]\n", ! 148: aStruct.one, aStruct.two, aStruct.three, aStruct.four); ! 149: ! 150: s = aStruct; ! 151: } ! 152: ! 153: @end ! 154: ! 155: void main (void) ! 156: { ! 157: id forwarder = [[Forwarder alloc] initWithDelegate: [[Delegate alloc] init]]; ! 158: struct foo s = {1, 2, 3, 4}, t; ! 159: ! 160: fprintf (stderr, "About to forward setX: 13\n"); ! 161: [forwarder setX: 13]; ! 162: fprintf (stderr, "Forwarding succeeded!\n"); ! 163: if ([forwarder x] == 13) ! 164: fprintf (stderr, "passed for int\n"); ! 165: else ! 166: fprintf (stderr, "failed for int\n"); ! 167: ! 168: [forwarder setF: 13.0]; ! 169: if ([forwarder f] == 13.0) ! 170: fprintf (stderr, "passed for float\n"); ! 171: else ! 172: fprintf (stderr, "failed for float\n"); ! 173: ! 174: [forwarder setD: 13.0]; ! 175: if ([forwarder d] == 13.0) ! 176: fprintf (stderr, "passed for double\n"); ! 177: else ! 178: fprintf (stderr, "failed for double\n"); ! 179: ! 180: [forwarder setS: s]; ! 181: t = [forwarder s]; ! 182: if (memcmp (&t, &s, sizeof (struct foo)) == 0) ! 183: fprintf (stderr, "passed for struct\n"); ! 184: else ! 185: fprintf (stderr, "failed for struct\n"); ! 186: ! 187: fprintf (stderr, "done\n"); ! 188: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.