summaryrefslogtreecommitdiffhomepage
path: root/examples/box2.c
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-12-14 19:50:10 +0100
committerTyge Løvset <[email protected]>2021-12-14 19:50:10 +0100
commit6749cc21a2045d307c239d82891cb860687dfd2a (patch)
tree6987b4cd03caa9b1ad8ab29bd97a7d8d15fcaa67 /examples/box2.c
parentc083936d8fa46b5df921dedae6ca15e1192d6612 (diff)
downloadSTC-modified-6749cc21a2045d307c239d82891cb860687dfd2a.tar.gz
STC-modified-6749cc21a2045d307c239d82891cb860687dfd2a.zip
Added and renamed some examples.
Diffstat (limited to 'examples/box2.c')
-rw-r--r--examples/box2.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/examples/box2.c b/examples/box2.c
new file mode 100644
index 00000000..79304609
--- /dev/null
+++ b/examples/box2.c
@@ -0,0 +1,86 @@
+// 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_compare
+#include <stc/cbox.h>
+
+#define i_val cbox_Point
+#define i_opt c_no_compare
+#define i_tag BoxPoint
+#include <stc/cbox.h>
+
+#define i_val Rectangle
+#define i_opt c_no_compare
+#include <stc/cbox.h>
+
+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));
+ }
+} \ No newline at end of file