1 | // This file must have the following defined before it is included: |
2 | // T defined to the type to test (int, float, etc) |
3 | // T_CSTR a C string representation of the type T ("int", "float") |
4 | // T_VALUE_1 defined to a valid initializer value for TEST_TYPE (7 for int, 2.0 for float) |
5 | // T_VALUE_2, T_VALUE_3, T_VALUE_4 defined to a valid initializer value for TEST_TYPE that is different from TEST_VALUE_1 |
6 | // T_PRINTF_FORMAT defined if T can be printed with printf |
7 | // |
8 | // An example for integers is below |
9 | #if 0 |
10 | |
11 | #define T int |
12 | #define T_CSTR "int" |
13 | #define T_VALUE_1 11001110 |
14 | #define T_VALUE_2 22002220 |
15 | #define T_VALUE_3 33003330 |
16 | #define T_VALUE_4 44044440 |
17 | #define T_PRINTF_FORMAT "%i" |
18 | |
19 | #include "basic_type.cpp" |
20 | |
21 | #endif |
22 | |
23 | #ifdef TEST_BLOCK_CAPTURED_VARS |
24 | #include <dispatch/dispatch.h> |
25 | #endif |
26 | #include <cstdint> |
27 | #include <cstdio> |
28 | #include <cstdlib> |
29 | |
30 | class a_class |
31 | { |
32 | public: |
33 | a_class (const T& a, const T& b) : |
34 | m_a (a), |
35 | m_b (b) |
36 | { |
37 | } |
38 | |
39 | ~a_class () |
40 | { |
41 | } |
42 | |
43 | const T& |
44 | get_a() const |
45 | { |
46 | return m_a; |
47 | } |
48 | |
49 | void |
50 | set_a (const T& a) |
51 | { |
52 | m_a = a; |
53 | } |
54 | |
55 | const T& |
56 | get_b() const |
57 | { |
58 | return m_b; |
59 | } |
60 | |
61 | void |
62 | set_b (const T& b) |
63 | { |
64 | m_b = b; |
65 | } |
66 | |
67 | protected: |
68 | T m_a; |
69 | T m_b; |
70 | }; |
71 | |
72 | typedef struct a_struct_tag { |
73 | T a; |
74 | T b; |
75 | } a_struct_t; |
76 | |
77 | |
78 | typedef union a_union_zero_tag { |
79 | T a; |
80 | double a_double; |
81 | } a_union_zero_t; |
82 | |
83 | typedef struct a_union_nonzero_tag { |
84 | double a_double; |
85 | a_union_zero_t u; |
86 | } a_union_nonzero_t; |
87 | |
88 | |
89 | int |
90 | main (int argc, char const *argv[]) |
91 | { |
92 | FILE *out = stdout; |
93 | |
94 | // By default, output to stdout |
95 | // If a filename is provided as the command line argument, |
96 | // output to that file. |
97 | if (argc == 2 && argv[1] && argv[1][0] != '\0') |
98 | { |
99 | out = fopen (filename: argv[1], modes: "w" ); |
100 | } |
101 | |
102 | T a = T_VALUE_1; |
103 | T* a_ptr = &a; |
104 | T& a_ref = a; |
105 | T a_array_bounded[2] = { T_VALUE_1, T_VALUE_2 }; |
106 | T a_array_unbounded[] = { T_VALUE_1, T_VALUE_2 }; |
107 | |
108 | a_class a_class_instance (T_VALUE_1, T_VALUE_2); |
109 | a_class *a_class_ptr = &a_class_instance; |
110 | a_class &a_class_ref = a_class_instance; |
111 | |
112 | a_struct_t a_struct = { T_VALUE_1, T_VALUE_2 }; |
113 | a_struct_t *a_struct_ptr = &a_struct; |
114 | a_struct_t &a_struct_ref = a_struct; |
115 | |
116 | // Create a union with type T at offset zero |
117 | a_union_zero_t a_union_zero; |
118 | a_union_zero.a = T_VALUE_1; |
119 | a_union_zero_t *a_union_zero_ptr = &a_union_zero; |
120 | a_union_zero_t &a_union_zero_ref = a_union_zero; |
121 | |
122 | // Create a union with type T at a non-zero offset |
123 | a_union_nonzero_t a_union_nonzero; |
124 | a_union_nonzero.u.a = T_VALUE_1; |
125 | a_union_nonzero_t *a_union_nonzero_ptr = &a_union_nonzero; |
126 | a_union_nonzero_t &a_union_nonzero_ref = a_union_nonzero; |
127 | |
128 | a_struct_t a_struct_array_bounded[2] = {{ T_VALUE_1, T_VALUE_2 }, { T_VALUE_3, T_VALUE_4 }}; |
129 | a_struct_t a_struct_array_unbounded[] = {{ T_VALUE_1, T_VALUE_2 }, { T_VALUE_3, T_VALUE_4 }}; |
130 | a_union_zero_t a_union_zero_array_bounded[2]; |
131 | a_union_zero_array_bounded[0].a = T_VALUE_1; |
132 | a_union_zero_array_bounded[1].a = T_VALUE_2; |
133 | a_union_zero_t a_union_zero_array_unbounded[] = {{ T_VALUE_1 }, { T_VALUE_2 }}; |
134 | |
135 | #ifdef T_PRINTF_FORMAT |
136 | fprintf (stream: out, format: "%s: a = '" T_PRINTF_FORMAT "'\n" , T_CSTR, a); |
137 | fprintf (stream: out, format: "%s*: %p => *a_ptr = '" T_PRINTF_FORMAT "'\n" , T_CSTR, a_ptr, *a_ptr); |
138 | fprintf (stream: out, format: "%s&: @%p => a_ref = '" T_PRINTF_FORMAT "'\n" , T_CSTR, &a_ref, a_ref); |
139 | |
140 | fprintf (stream: out, format: "%s[2]: a_array_bounded[0] = '" T_PRINTF_FORMAT "'\n" , T_CSTR, a_array_bounded[0]); |
141 | fprintf (stream: out, format: "%s[2]: a_array_bounded[1] = '" T_PRINTF_FORMAT "'\n" , T_CSTR, a_array_bounded[1]); |
142 | |
143 | fprintf (stream: out, format: "%s[]: a_array_unbounded[0] = '" T_PRINTF_FORMAT "'\n" , T_CSTR, a_array_unbounded[0]); |
144 | fprintf (stream: out, format: "%s[]: a_array_unbounded[1] = '" T_PRINTF_FORMAT "'\n" , T_CSTR, a_array_unbounded[1]); |
145 | |
146 | fprintf (stream: out, format: "(a_class) a_class_instance.m_a = '" T_PRINTF_FORMAT "'\n" , a_class_instance.get_a()); |
147 | fprintf (stream: out, format: "(a_class) a_class_instance.m_b = '" T_PRINTF_FORMAT "'\n" , a_class_instance.get_b()); |
148 | fprintf (stream: out, format: "(a_class*) a_class_ptr = %p, a_class_ptr->m_a = '" T_PRINTF_FORMAT "'\n" , a_class_ptr, a_class_ptr->get_a()); |
149 | fprintf (stream: out, format: "(a_class*) a_class_ptr = %p, a_class_ptr->m_b = '" T_PRINTF_FORMAT "'\n" , a_class_ptr, a_class_ptr->get_b()); |
150 | fprintf (stream: out, format: "(a_class&) a_class_ref = %p, a_class_ref.m_a = '" T_PRINTF_FORMAT "'\n" , &a_class_ref, a_class_ref.get_a()); |
151 | fprintf (stream: out, format: "(a_class&) a_class_ref = %p, a_class_ref.m_b = '" T_PRINTF_FORMAT "'\n" , &a_class_ref, a_class_ref.get_b()); |
152 | |
153 | fprintf (stream: out, format: "(a_struct_t) a_struct.a = '" T_PRINTF_FORMAT "'\n" , a_struct.a); |
154 | fprintf (stream: out, format: "(a_struct_t) a_struct.b = '" T_PRINTF_FORMAT "'\n" , a_struct.b); |
155 | fprintf (stream: out, format: "(a_struct_t*) a_struct_ptr = %p, a_struct_ptr->a = '" T_PRINTF_FORMAT "'\n" , a_struct_ptr, a_struct_ptr->a); |
156 | fprintf (stream: out, format: "(a_struct_t*) a_struct_ptr = %p, a_struct_ptr->b = '" T_PRINTF_FORMAT "'\n" , a_struct_ptr, a_struct_ptr->b); |
157 | fprintf (stream: out, format: "(a_struct_t&) a_struct_ref = %p, a_struct_ref.a = '" T_PRINTF_FORMAT "'\n" , &a_struct_ref, a_struct_ref.a); |
158 | fprintf (stream: out, format: "(a_struct_t&) a_struct_ref = %p, a_struct_ref.b = '" T_PRINTF_FORMAT "'\n" , &a_struct_ref, a_struct_ref.b); |
159 | |
160 | fprintf (stream: out, format: "(a_union_zero_t) a_union_zero.a = '" T_PRINTF_FORMAT "'\n" , a_union_zero.a); |
161 | fprintf (stream: out, format: "(a_union_zero_t*) a_union_zero_ptr = %p, a_union_zero_ptr->a = '" T_PRINTF_FORMAT "'\n" , a_union_zero_ptr, a_union_zero_ptr->a); |
162 | fprintf (stream: out, format: "(a_union_zero_t&) a_union_zero_ref = %p, a_union_zero_ref.a = '" T_PRINTF_FORMAT "'\n" , &a_union_zero_ref, a_union_zero_ref.a); |
163 | |
164 | fprintf (stream: out, format: "(a_union_nonzero_t) a_union_nonzero.u.a = '" T_PRINTF_FORMAT "'\n" , a_union_nonzero.u.a); |
165 | fprintf (stream: out, format: "(a_union_nonzero_t*) a_union_nonzero_ptr = %p, a_union_nonzero_ptr->u.a = '" T_PRINTF_FORMAT "'\n" , a_union_nonzero_ptr, a_union_nonzero_ptr->u.a); |
166 | fprintf (stream: out, format: "(a_union_nonzero_t&) a_union_nonzero_ref = %p, a_union_nonzero_ref.u.a = '" T_PRINTF_FORMAT "'\n" , &a_union_nonzero_ref, a_union_nonzero_ref.u.a); |
167 | |
168 | fprintf (stream: out, format: "(a_struct_t[2]) a_struct_array_bounded[0].a = '" T_PRINTF_FORMAT "'\n" , a_struct_array_bounded[0].a); |
169 | fprintf (stream: out, format: "(a_struct_t[2]) a_struct_array_bounded[0].b = '" T_PRINTF_FORMAT "'\n" , a_struct_array_bounded[0].b); |
170 | fprintf (stream: out, format: "(a_struct_t[2]) a_struct_array_bounded[1].a = '" T_PRINTF_FORMAT "'\n" , a_struct_array_bounded[1].a); |
171 | fprintf (stream: out, format: "(a_struct_t[2]) a_struct_array_bounded[1].b = '" T_PRINTF_FORMAT "'\n" , a_struct_array_bounded[1].b); |
172 | |
173 | fprintf (stream: out, format: "(a_struct_t[]) a_struct_array_unbounded[0].a = '" T_PRINTF_FORMAT "'\n" , a_struct_array_unbounded[0].a); |
174 | fprintf (stream: out, format: "(a_struct_t[]) a_struct_array_unbounded[0].b = '" T_PRINTF_FORMAT "'\n" , a_struct_array_unbounded[0].b); |
175 | fprintf (stream: out, format: "(a_struct_t[]) a_struct_array_unbounded[1].a = '" T_PRINTF_FORMAT "'\n" , a_struct_array_unbounded[1].a); |
176 | fprintf (stream: out, format: "(a_struct_t[]) a_struct_array_unbounded[1].b = '" T_PRINTF_FORMAT "'\n" , a_struct_array_unbounded[1].b); |
177 | |
178 | fprintf (stream: out, format: "(a_union_zero_t[2]) a_union_zero_array_bounded[0].a = '" T_PRINTF_FORMAT "'\n" , a_union_zero_array_bounded[0].a); |
179 | fprintf (stream: out, format: "(a_union_zero_t[2]) a_union_zero_array_bounded[1].a = '" T_PRINTF_FORMAT "'\n" , a_union_zero_array_bounded[1].a); |
180 | |
181 | fprintf (stream: out, format: "(a_union_zero_t[]) a_union_zero_array_unbounded[0].a = '" T_PRINTF_FORMAT "'\n" , a_union_zero_array_unbounded[0].a); |
182 | fprintf (stream: out, format: "(a_union_zero_t[]) a_union_zero_array_unbounded[1].a = '" T_PRINTF_FORMAT "'\n" , a_union_zero_array_unbounded[1].a); |
183 | |
184 | #endif |
185 | puts(s: "About to exit, break here to check values..." ); // Here is the line we will break on to check variables. |
186 | |
187 | #ifdef TEST_BLOCK_CAPTURED_VARS |
188 | void (^myBlock)() = ^() { |
189 | fprintf (out, "%s: a = '" T_PRINTF_FORMAT "'\n" , T_CSTR, a); |
190 | fprintf (out, "%s*: %p => *a_ptr = '" T_PRINTF_FORMAT "'\n" , T_CSTR, a_ptr, *a_ptr); |
191 | fprintf (out, "%s&: @%p => a_ref = '" T_PRINTF_FORMAT "'\n" , T_CSTR, &a_ref, a_ref); |
192 | |
193 | fprintf (out, "(a_class) a_class_instance.m_a = '" T_PRINTF_FORMAT "'\n" , a_class_instance.get_a()); |
194 | fprintf (out, "(a_class) a_class_instance.m_b = '" T_PRINTF_FORMAT "'\n" , a_class_instance.get_b()); |
195 | fprintf (out, "(a_class*) a_class_ptr = %p, a_class_ptr->m_a = '" T_PRINTF_FORMAT "'\n" , a_class_ptr, a_class_ptr->get_a()); |
196 | fprintf (out, "(a_class*) a_class_ptr = %p, a_class_ptr->m_b = '" T_PRINTF_FORMAT "'\n" , a_class_ptr, a_class_ptr->get_b()); |
197 | fprintf (out, "(a_class&) a_class_ref = %p, a_class_ref.m_a = '" T_PRINTF_FORMAT "'\n" , &a_class_ref, a_class_ref.get_a()); |
198 | fprintf (out, "(a_class&) a_class_ref = %p, a_class_ref.m_b = '" T_PRINTF_FORMAT "'\n" , &a_class_ref, a_class_ref.get_b()); |
199 | |
200 | fprintf (out, "(a_struct_t) a_struct.a = '" T_PRINTF_FORMAT "'\n" , a_struct.a); |
201 | fprintf (out, "(a_struct_t) a_struct.b = '" T_PRINTF_FORMAT "'\n" , a_struct.b); |
202 | fprintf (out, "(a_struct_t*) a_struct_ptr = %p, a_struct_ptr->a = '" T_PRINTF_FORMAT "'\n" , a_struct_ptr, a_struct_ptr->a); |
203 | fprintf (out, "(a_struct_t*) a_struct_ptr = %p, a_struct_ptr->b = '" T_PRINTF_FORMAT "'\n" , a_struct_ptr, a_struct_ptr->b); |
204 | fprintf (out, "(a_struct_t&) a_struct_ref = %p, a_struct_ref.a = '" T_PRINTF_FORMAT "'\n" , &a_struct_ref, a_struct_ref.a); |
205 | fprintf (out, "(a_struct_t&) a_struct_ref = %p, a_struct_ref.b = '" T_PRINTF_FORMAT "'\n" , &a_struct_ref, a_struct_ref.b); |
206 | |
207 | fprintf (out, "(a_union_zero_t) a_union_zero.a = '" T_PRINTF_FORMAT "'\n" , a_union_zero.a); |
208 | fprintf (out, "(a_union_zero_t*) a_union_zero_ptr = %p, a_union_zero_ptr->a = '" T_PRINTF_FORMAT "'\n" , a_union_zero_ptr, a_union_zero_ptr->a); |
209 | fprintf (out, "(a_union_zero_t&) a_union_zero_ref = %p, a_union_zero_ref.a = '" T_PRINTF_FORMAT "'\n" , &a_union_zero_ref, a_union_zero_ref.a); |
210 | |
211 | fprintf (out, "(a_union_nonzero_t) a_union_nonzero.u.a = '" T_PRINTF_FORMAT "'\n" , a_union_nonzero.u.a); |
212 | fprintf (out, "(a_union_nonzero_t*) a_union_nonzero_ptr = %p, a_union_nonzero_ptr->u.a = '" T_PRINTF_FORMAT "'\n" , a_union_nonzero_ptr, a_union_nonzero_ptr->u.a); |
213 | fprintf (out, "(a_union_nonzero_t&) a_union_nonzero_ref = %p, a_union_nonzero_ref.u.a = '" T_PRINTF_FORMAT "'\n" , &a_union_nonzero_ref, a_union_nonzero_ref.u.a); |
214 | |
215 | fprintf (out, "That's All Folks!\n" ); // Break here to test block captured variables. |
216 | }; |
217 | |
218 | myBlock(); |
219 | #endif |
220 | |
221 | if (out != stdout) |
222 | fclose (stream: out); |
223 | |
224 | return 0; |
225 | } |
226 | |