Home | History | Annotate | Download | only in types
      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 
     27 class a_class
     28 {
     29 public:
     30     a_class (const T& a, const T& b) :
     31         m_a (a),
     32         m_b (b)
     33     {
     34     }
     35 
     36     ~a_class ()
     37     {
     38     }
     39 
     40     const T&
     41     get_a() const
     42     {
     43         return m_a;
     44     }
     45 
     46     void
     47     set_a (const T& a)
     48     {
     49         m_a = a;
     50     }
     51 
     52     const T&
     53     get_b() const
     54     {
     55         return m_b;
     56     }
     57 
     58     void
     59     set_b (const T& b)
     60     {
     61         m_b = b;
     62     }
     63 
     64 protected:
     65     T m_a;
     66     T m_b;
     67 };
     68 
     69 typedef struct a_struct_tag {
     70     T a;
     71     T b;
     72 } a_struct_t;
     73 
     74 
     75 typedef union a_union_zero_tag {
     76     T a;
     77     double a_double;
     78 } a_union_zero_t;
     79 
     80 typedef struct a_union_nonzero_tag {
     81   double a_double;
     82   a_union_zero_t u;
     83 } a_union_nonzero_t;
     84 
     85 
     86 #include <stdint.h>
     87 #include <stdio.h>
     88 
     89 int
     90 main (int argc, char const *argv[])
     91 {
     92     T a = T_VALUE_1;
     93     T* a_ptr = &a;
     94     T& a_ref = a;
     95     T a_array_bounded[2] = { T_VALUE_1, T_VALUE_2 };
     96     T a_array_unbounded[] = { T_VALUE_1, T_VALUE_2 };
     97 
     98     a_class a_class_instance (T_VALUE_1, T_VALUE_2);
     99     a_class *a_class_ptr = &a_class_instance;
    100     a_class &a_class_ref = a_class_instance;
    101 
    102     a_struct_t a_struct = { T_VALUE_1, T_VALUE_2 };
    103     a_struct_t *a_struct_ptr = &a_struct;
    104     a_struct_t &a_struct_ref = a_struct;
    105 
    106     // Create a union with type T at offset zero
    107     a_union_zero_t a_union_zero;
    108     a_union_zero.a = T_VALUE_1;
    109     a_union_zero_t *a_union_zero_ptr = &a_union_zero;
    110     a_union_zero_t &a_union_zero_ref = a_union_zero;
    111 
    112     // Create a union with type T at a non-zero offset
    113     a_union_nonzero_t a_union_nonzero;
    114     a_union_nonzero.u.a = T_VALUE_1;
    115     a_union_nonzero_t *a_union_nonzero_ptr = &a_union_nonzero;
    116     a_union_nonzero_t &a_union_nonzero_ref = a_union_nonzero;
    117 
    118     a_struct_t a_struct_array_bounded[2]  = {{ T_VALUE_1, T_VALUE_2 }, { T_VALUE_3, T_VALUE_4 }};
    119     a_struct_t a_struct_array_unbounded[] = {{ T_VALUE_1, T_VALUE_2 }, { T_VALUE_3, T_VALUE_4 }};
    120     a_union_zero_t a_union_zero_array_bounded[2];
    121     a_union_zero_array_bounded[0].a = T_VALUE_1;
    122     a_union_zero_array_bounded[1].a = T_VALUE_2;
    123     a_union_zero_t a_union_zero_array_unbounded[] = {{ T_VALUE_1 }, { T_VALUE_2 }};
    124 
    125 #ifdef T_PRINTF_FORMAT
    126     printf ("%s: a = '" T_PRINTF_FORMAT "'\n", T_CSTR, a);
    127     printf ("%s*: %p => *a_ptr = '" T_PRINTF_FORMAT "'\n", T_CSTR, a_ptr, *a_ptr);
    128     printf ("%s&: @%p => a_ref = '" T_PRINTF_FORMAT "'\n", T_CSTR, &a_ref, a_ref);
    129 
    130     printf ("%s[2]: a_array_bounded[0] = '" T_PRINTF_FORMAT "'\n", T_CSTR, a_array_bounded[0]);
    131     printf ("%s[2]: a_array_bounded[1] = '" T_PRINTF_FORMAT "'\n", T_CSTR, a_array_bounded[1]);
    132 
    133     printf ("%s[]: a_array_unbounded[0] = '" T_PRINTF_FORMAT "'\n", T_CSTR, a_array_unbounded[0]);
    134     printf ("%s[]: a_array_unbounded[1] = '" T_PRINTF_FORMAT "'\n", T_CSTR, a_array_unbounded[1]);
    135 
    136     printf ("(a_class) a_class_instance.m_a = '" T_PRINTF_FORMAT "'\n", a_class_instance.get_a());
    137     printf ("(a_class) a_class_instance.m_b = '" T_PRINTF_FORMAT "'\n", a_class_instance.get_b());
    138     printf ("(a_class*) a_class_ptr = %p, a_class_ptr->m_a = '" T_PRINTF_FORMAT "'\n", a_class_ptr, a_class_ptr->get_a());
    139     printf ("(a_class*) a_class_ptr = %p, a_class_ptr->m_b = '" T_PRINTF_FORMAT "'\n", a_class_ptr, a_class_ptr->get_b());
    140     printf ("(a_class&) a_class_ref = %p, a_class_ref.m_a = '" T_PRINTF_FORMAT "'\n", &a_class_ref, a_class_ref.get_a());
    141     printf ("(a_class&) a_class_ref = %p, a_class_ref.m_b = '" T_PRINTF_FORMAT "'\n", &a_class_ref, a_class_ref.get_b());
    142 
    143     printf ("(a_struct_t) a_struct.a = '" T_PRINTF_FORMAT "'\n", a_struct.a);
    144     printf ("(a_struct_t) a_struct.b = '" T_PRINTF_FORMAT "'\n", a_struct.b);
    145     printf ("(a_struct_t*) a_struct_ptr = %p, a_struct_ptr->a = '" T_PRINTF_FORMAT "'\n", a_struct_ptr, a_struct_ptr->a);
    146     printf ("(a_struct_t*) a_struct_ptr = %p, a_struct_ptr->b = '" T_PRINTF_FORMAT "'\n", a_struct_ptr, a_struct_ptr->b);
    147     printf ("(a_struct_t&) a_struct_ref = %p, a_struct_ref.a = '" T_PRINTF_FORMAT "'\n", &a_struct_ref, a_struct_ref.a);
    148     printf ("(a_struct_t&) a_struct_ref = %p, a_struct_ref.b = '" T_PRINTF_FORMAT "'\n", &a_struct_ref, a_struct_ref.b);
    149 
    150     printf ("(a_union_zero_t) a_union_zero.a = '" T_PRINTF_FORMAT "'\n", a_union_zero.a);
    151     printf ("(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);
    152     printf ("(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);
    153 
    154     printf ("(a_union_nonzero_t) a_union_nonzero.u.a = '" T_PRINTF_FORMAT "'\n", a_union_nonzero.u.a);
    155     printf ("(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);
    156     printf ("(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);
    157 
    158     printf ("(a_struct_t[2]) a_struct_array_bounded[0].a = '" T_PRINTF_FORMAT "'\n", a_struct_array_bounded[0].a);
    159     printf ("(a_struct_t[2]) a_struct_array_bounded[0].b = '" T_PRINTF_FORMAT "'\n", a_struct_array_bounded[0].b);
    160     printf ("(a_struct_t[2]) a_struct_array_bounded[1].a = '" T_PRINTF_FORMAT "'\n", a_struct_array_bounded[1].a);
    161     printf ("(a_struct_t[2]) a_struct_array_bounded[1].b = '" T_PRINTF_FORMAT "'\n", a_struct_array_bounded[1].b);
    162 
    163     printf ("(a_struct_t[]) a_struct_array_unbounded[0].a = '" T_PRINTF_FORMAT "'\n", a_struct_array_unbounded[0].a);
    164     printf ("(a_struct_t[]) a_struct_array_unbounded[0].b = '" T_PRINTF_FORMAT "'\n", a_struct_array_unbounded[0].b);
    165     printf ("(a_struct_t[]) a_struct_array_unbounded[1].a = '" T_PRINTF_FORMAT "'\n", a_struct_array_unbounded[1].a);
    166     printf ("(a_struct_t[]) a_struct_array_unbounded[1].b = '" T_PRINTF_FORMAT "'\n", a_struct_array_unbounded[1].b);
    167 
    168     printf ("(a_union_zero_t[2]) a_union_zero_array_bounded[0].a = '" T_PRINTF_FORMAT "'\n", a_union_zero_array_bounded[0].a);
    169     printf ("(a_union_zero_t[2]) a_union_zero_array_bounded[1].a = '" T_PRINTF_FORMAT "'\n", a_union_zero_array_bounded[1].a);
    170 
    171     printf ("(a_union_zero_t[]) a_union_zero_array_unbounded[0].a = '" T_PRINTF_FORMAT "'\n", a_union_zero_array_unbounded[0].a);
    172     printf ("(a_union_zero_t[]) a_union_zero_array_unbounded[1].a = '" T_PRINTF_FORMAT "'\n", a_union_zero_array_unbounded[1].a);
    173 
    174 #endif
    175     puts("About to exit, break here to check values..."); // Here is the line we will break on to check variables.
    176 
    177 #ifdef TEST_BLOCK_CAPTURED_VARS
    178     void (^myBlock)() = ^() {
    179         printf ("%s: a = '" T_PRINTF_FORMAT "'\n", T_CSTR, a);
    180         printf ("%s*: %p => *a_ptr = '" T_PRINTF_FORMAT "'\n", T_CSTR, a_ptr, *a_ptr);
    181         printf ("%s&: @%p => a_ref = '" T_PRINTF_FORMAT "'\n", T_CSTR, &a_ref, a_ref);
    182 
    183         printf ("(a_class) a_class_instance.m_a = '" T_PRINTF_FORMAT "'\n", a_class_instance.get_a());
    184         printf ("(a_class) a_class_instance.m_b = '" T_PRINTF_FORMAT "'\n", a_class_instance.get_b());
    185         printf ("(a_class*) a_class_ptr = %p, a_class_ptr->m_a = '" T_PRINTF_FORMAT "'\n", a_class_ptr, a_class_ptr->get_a());
    186         printf ("(a_class*) a_class_ptr = %p, a_class_ptr->m_b = '" T_PRINTF_FORMAT "'\n", a_class_ptr, a_class_ptr->get_b());
    187         printf ("(a_class&) a_class_ref = %p, a_class_ref.m_a = '" T_PRINTF_FORMAT "'\n", &a_class_ref, a_class_ref.get_a());
    188         printf ("(a_class&) a_class_ref = %p, a_class_ref.m_b = '" T_PRINTF_FORMAT "'\n", &a_class_ref, a_class_ref.get_b());
    189 
    190         printf ("(a_struct_t) a_struct.a = '" T_PRINTF_FORMAT "'\n", a_struct.a);
    191         printf ("(a_struct_t) a_struct.b = '" T_PRINTF_FORMAT "'\n", a_struct.b);
    192         printf ("(a_struct_t*) a_struct_ptr = %p, a_struct_ptr->a = '" T_PRINTF_FORMAT "'\n", a_struct_ptr, a_struct_ptr->a);
    193         printf ("(a_struct_t*) a_struct_ptr = %p, a_struct_ptr->b = '" T_PRINTF_FORMAT "'\n", a_struct_ptr, a_struct_ptr->b);
    194         printf ("(a_struct_t&) a_struct_ref = %p, a_struct_ref.a = '" T_PRINTF_FORMAT "'\n", &a_struct_ref, a_struct_ref.a);
    195         printf ("(a_struct_t&) a_struct_ref = %p, a_struct_ref.b = '" T_PRINTF_FORMAT "'\n", &a_struct_ref, a_struct_ref.b);
    196 
    197         printf ("(a_union_zero_t) a_union_zero.a = '" T_PRINTF_FORMAT "'\n", a_union_zero.a);
    198         printf ("(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);
    199         printf ("(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);
    200 
    201         printf ("(a_union_nonzero_t) a_union_nonzero.u.a = '" T_PRINTF_FORMAT "'\n", a_union_nonzero.u.a);
    202         printf ("(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);
    203         printf ("(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);
    204 
    205         printf ("That's All Folks!\n"); // Break here to test block captured variables.
    206     };
    207 
    208     myBlock();
    209 #endif
    210     return 0;
    211 }
    212