diff options
Diffstat (limited to 'misc/examples/box2.c')
| -rw-r--r-- | misc/examples/box2.c | 74 |
1 files changed, 36 insertions, 38 deletions
diff --git a/misc/examples/box2.c b/misc/examples/box2.c index 943b2ae8..f7d21976 100644 --- a/misc/examples/box2.c +++ b/misc/examples/box2.c @@ -1,36 +1,31 @@ -// https://doc.rust-lang.org/rust-by-example/std/box.html - -#include <stdlib.h> +// example: https://doc.rust-lang.org/rust-by-example/std/box.html #include <stdio.h> -#include <string.h> -#include <stc/ccommon.h> -struct { +typedef struct { double x; double y; -} typedef Point; +} Point; // A Rectangle can be specified by where its top left and bottom right // corners are in space -struct { +typedef struct { Point top_left; Point bottom_right; -} typedef Rectangle; +} Rectangle; #define i_val Point -#define i_opt c_no_cmp +#define i_no_cmp #include <stc/cbox.h> // cbox_Point #define i_val Rectangle -#define i_opt c_no_cmp +#define i_no_cmp #include <stc/cbox.h> // cbox_Rectangle // Box in box: -#define i_valboxed cbox_Point // NB: use i_valboxed when value is a cbox or carc! - // it will auto define i_valdrop, i_valfrom, and i_cmp. -#define i_tag BoxPoint -#define i_opt c_no_cmp -#include <stc/cbox.h> // cbox_BoxPoint +#define i_valboxed cbox_Point // NB: use i_valboxed when value is a cbox or carc! +#define i_type BoxBoxPoint +#define i_no_cmp +#include <stc/cbox.h> // BoxBoxPoint Point origin(void) { return (Point){ .x=1.0, .y=2.0 }; @@ -38,7 +33,7 @@ Point origin(void) { cbox_Point boxed_origin(void) { // Allocate this point on the heap, and return a pointer to it - return cbox_Point_make((Point){ .x=2.0, .y=3.0 }); + return cbox_Point_make((Point){ .x=1.0, .y=2.0 }); } @@ -47,44 +42,47 @@ int main(void) { Point point = origin(); Rectangle rectangle = (Rectangle){ .top_left = origin(), - .bottom_right = (Point){ .x=3.0, .y=-4.0 } + .bottom_right = { .x=3.0, .y=-4.0 } }; - // Declare auto-deleted box objects + // Declare RAII'ed box objects c_auto (cbox_Rectangle, boxed_rectangle) c_auto (cbox_Point, boxed_point) - c_auto (cbox_BoxPoint, box_in_a_box, boxbox2) + c_auto (BoxBoxPoint, box_in_a_box) { // Heap allocated rectangle boxed_rectangle = cbox_Rectangle_make((Rectangle){ .top_left = origin(), - .bottom_right = (Point){ .x=3.0, .y=-4.0 } + .bottom_right = { .x=3.0, .y=-4.0 } }); // The output of functions can be boxed - boxed_point = cbox_Point_from(origin()); + boxed_point = cbox_Point_make(origin()); // Double indirection - box_in_a_box = cbox_BoxPoint_make(boxed_origin()); - boxbox2 = cbox_BoxPoint_from(point); // !! - printf("boxbox2: x=%f\n", boxbox2.get->get->x); - - printf("Point occupies %" c_ZU " bytes on the stack\n", - sizeof(point)); - printf("Rectangle occupies %" c_ZU " bytes on the stack\n", - sizeof(rectangle)); + //box_in_a_box = BoxBoxPoint_make(boxed_origin()); + //printf("box_in_a_box: x = %g\n", box_in_a_box.get->get->x); + + // Can use from(raw) and toraw instead: + box_in_a_box = BoxBoxPoint_from(origin()); + printf("box_in_a_box: x = %g\n", BoxBoxPoint_toraw(&box_in_a_box).x); + + printf("Point occupies %d bytes on the stack\n", + (int)sizeof(point)); + printf("Rectangle occupies %d bytes on the stack\n", + (int)sizeof(rectangle)); // box size == pointer size - printf("Boxed point occupies %" c_ZU " bytes on the stack\n", - sizeof(boxed_point)); - printf("Boxed rectangle occupies %" c_ZU " bytes on the stack\n", - sizeof(boxed_rectangle)); - printf("Boxed box occupies %" c_ZU " bytes on the stack\n", - sizeof(box_in_a_box)); + printf("Boxed point occupies %d bytes on the stack\n", + (int)sizeof(boxed_point)); + printf("Boxed rectangle occupies %d bytes on the stack\n", + (int)sizeof(boxed_rectangle)); + printf("Boxed box occupies %d bytes on the stack\n", + (int)sizeof(box_in_a_box)); // Copy the data contained in `boxed_point` into `unboxed_point` Point unboxed_point = *boxed_point.get; - printf("Unboxed point occupies %" c_ZU " bytes on the stack\n", - sizeof(unboxed_point)); + printf("Unboxed point occupies %d bytes on the stack\n", + (int)sizeof(unboxed_point)); } } |
