1/* 2 * Copyright (c) 2010 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LLVM_LICENSE_HEADER@ 5 */ 6 7/* 8 * Block_return.c 9 * Examples 10 * 11 * Created by Blaine Garst on 1/31/08. 12 * Copyright 2008 Apple. All rights reserved. 13 * 14 */ 15 16 17#include "driver.h" 18 19// 20// Example closure code generated for specific Blocks 21// 22 23// 24// Computing and accessing a result value 25// Now that Blocks don't keep return values, not much to see here. 26// 27 28#if __BLOCKS__ 29int result_value_example_real(int verbose) { 30 int (^myClosure)(void) = myClosure = ^ (void) { return 12; }; 31 32 // invoke it 33 // extract return value 34 int result = myClosure(); 35 if (result == 12) { 36 if (verbose) printf("result extracted successfully\n"); 37 return 0; 38 } 39 else { 40 printf("oops, result not extracted properly, should be 12 but is %d\n", result); 41 return 1; 42 } 43 44} 45 46#endif __BLOCKS__ 47 48struct result_value_example_struct { 49 struct Block_basic base; 50}; 51 52// thunk(s) generated 53 54int invoke_result_value_example(struct result_value_example_struct *aBlock) { 55 return 12; 56} 57 58 59int result_value_example(int verbose) { 60 struct result_value_example_struct onStack = { 61 { 0, 0, sizeof(struct result_value_example_struct), 62 (void (*)(void *))invoke_result_value_example, 63 } 64 }; 65 struct result_value_example_struct *myClosure = &onStack; 66 67 int result = (*(int (*)(struct return_value_example_struct*))myClosure->base.Block_invoke)(myClosure); 68 69 if (result == 12) { 70 if (verbose) printf("result extracted successfully\n"); 71 return 0; 72 } 73 else { 74 printf("oops, result not extracted properly, should be 12 but is %d\n", result); 75 return 1; 76 } 77} 78 79