summaryrefslogtreecommitdiffhomepage
path: root/examples/box2.c
blob: 5549dadea20a2b40e38f58bc49aeafe0a958b43c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// https://doc.rust-lang.org/rust-by-example/std/box.html

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stc/ccommon.h>

struct {
    double x;
    double y;
} typedef Point;

// A Rectangle can be specified by where its top left and bottom right 
// corners are in space
struct {
    Point top_left;
    Point bottom_right;
} typedef Rectangle;

#define i_val Point
#define i_opt c_no_cmp
#include <stc/cbox.h> // cbox_Point

#define i_val Rectangle
#define i_opt c_no_cmp
#include <stc/cbox.h> // cbox_Rectangle

// Box in box:
#define i_val_bind cbox_Point  // NB: adviced to use i_val_arc when value is a cbox or csptr!
                                // it will auto-set i_drop, i_from, i_cmp for you.
#define i_opt c_no_cmp
#define i_tag BoxPoint
#include <stc/cbox.h> // cbox_BoxPoint

Point origin(void) {
    return (Point){ .x=0.0, .y=0.0 };
}

cbox_Point boxed_origin(void) {
    // Allocate this point on the heap, and return a pointer to it
    return cbox_Point_new((Point){ .x=0.0, .y=0.0 });
}


int main(void) {
    // (all the type annotations are superfluous)
    // Stack allocated variables
    Point point = origin();
    Rectangle rectangle = (Rectangle){
        .top_left = origin(),
        .bottom_right = (Point){ .x=3.0, .y=-4.0 }
    };

    // Heap allocated rectangle
    c_auto (cbox_Rectangle, boxed_rectangle)
    c_auto (cbox_Point, boxed_point)
    c_auto (cbox_BoxPoint, box_in_a_box)
    {
        boxed_rectangle = cbox_Rectangle_new((Rectangle){
            .top_left = origin(),
            .bottom_right = (Point){ .x=3.0, .y=-4.0 }
        });

        // The output of functions can be boxed
        boxed_point = cbox_Point_new(origin());

        // Double indirection
        box_in_a_box = cbox_BoxPoint_new(boxed_origin());

        printf("Point occupies %zu bytes on the stack\n",
                sizeof(point));
        printf("Rectangle occupies %zu bytes on the stack\n",
                sizeof(rectangle));

        // box size == pointer size
        printf("Boxed point occupies %zu bytes on the stack\n",
                sizeof(boxed_point));
        printf("Boxed rectangle occupies %zu bytes on the stack\n",
                sizeof(boxed_rectangle));
        printf("Boxed box occupies %zu bytes on the stack\n",
                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 %zu bytes on the stack\n",
                sizeof(unboxed_point));
    }
}