From fa5c3a40738d5fca515ab1d581566355b8e05430 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 19 May 2022 22:37:11 +0200 Subject: Some extra test on template params. Update shape.c example. --- docs/ccommon_api.md | 2 +- examples/shape.c | 21 +++++++++++++++------ include/stc/ccommon.h | 2 +- include/stc/template.h | 20 ++++++++++++-------- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 95f1c6bb..249f2523 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -223,7 +223,7 @@ c_drop(cstr, &a, &b); ### General predefined template parameter functions ``` int c_default_cmp(const Type*, const Type*); -Type c_default_from(Type val); // simple copy +Type c_default_clone(Type val); // simple copy Type c_default_toraw(const Type* val); // dereference val void c_default_drop(Type* val); // does nothing diff --git a/examples/shape.c b/examples/shape.c index ec1998c0..69c8ca70 100644 --- a/examples/shape.c +++ b/examples/shape.c @@ -83,8 +83,8 @@ c_vtable(Shape, Polygon, api); static void Polygon_drop(Shape** shape) { - puts("drop poly"); c_self(shape, Polygon, api); + puts("drop poly"); PVec_drop(&self->points); } @@ -123,18 +123,27 @@ void testShape(Shape** shape) (*shape)->draw(shape); } +#define i_type Shapes +#define i_val Shape** +#define i_opt c_no_clone +#define i_valdrop(x) Shape_delete(*x) +#include + int main(void) { - c_autovar (Shape** tria = NULL, Shape_delete(tria)) - c_autovar (Shape** poly = Polygon_new(), Shape_delete(poly)) + c_auto (Shapes, shapes) { - tria = Triangle_new((Point){5, 7}, (Point){12, 7}, (Point){12, 20}); + Shape** tria = Triangle_new((Point){5, 7}, (Point){12, 7}, (Point){12, 20}); + Shape** poly = Polygon_new(); c_apply(p, Polygon_addPoint(poly, p), Point, { {5, 7}, {12, 7}, {12, 20}, {82, 33}, {17, 56}, }); - testShape(tria); - testShape(poly); + Shapes_push(&shapes, tria); + Shapes_push(&shapes, poly); + + c_foreach (i, Shapes, shapes) + testShape(*i.ref); } } diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 549dd969..850bc511 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -95,7 +95,7 @@ #define c_memcmp_eq(x, y) (memcmp(x, y, sizeof *(x)) == 0) #define c_default_hash(x) c_fasthash(x, sizeof *(x)) -#define c_default_from(v) (v) +#define c_default_clone(v) (v) #define c_default_toraw(vp) (*(vp)) #define c_default_drop(vp) ((void) (vp)) #define c_derived_keyclone(v) i_keyfrom((i_keyto((&(v))))) diff --git a/include/stc/template.h b/include/stc/template.h index 6af10ace..4d2f0f58 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -138,11 +138,13 @@ #endif #if !defined i_key - #error "no i_key or i_val provided" + #error "no i_key or i_val defined" #elif defined i_keyraw ^ defined i_keyto - #error "if i_keyraw is defined, i_keyto must be defined, and vice versa" + #error "both i_keyraw and i_keyto must be defined, if any" +#elif defined i_keyfrom && !defined i_keyraw + #error "i_keyfrom defined without i_keyraw" #elif defined i_from || defined i_drop - #error "i_from / i_drop are not supported. Define i_keyfrom/i_valfrom and-or i_keydrop/i_valdrop instead." + #error "i_from / i_drop not supported. Define i_keyfrom/i_valfrom and/or i_keydrop/i_valdrop instead" #endif #ifndef i_tag @@ -155,7 +157,7 @@ #define i_keyraw i_key #endif #ifndef i_keyfrom - #define i_keyfrom c_default_from + #define i_keyfrom c_default_clone #else #define _i_has_from #endif @@ -163,7 +165,7 @@ #define i_keyto c_default_toraw #endif #ifndef i_keyclone - #define i_keyclone(key) (key) + #define i_keyclone c_default_clone #endif #ifndef i_keydrop #define i_keydrop c_default_drop @@ -214,7 +216,9 @@ #endif #if defined i_valraw ^ defined i_valto - #error "if i_valraw is defined, i_valto must be defined, and vice versa" + #error "both i_valto and i_valraw must be defined, if any" +#elif defined i_valfrom && !defined i_valraw + #error "i_valfrom defined without i_valraw" #endif #if !defined i_valclone && (defined i_valdrop || defined i_valraw) @@ -224,7 +228,7 @@ #define i_valraw i_val #endif #ifndef i_valfrom - #define i_valfrom c_default_from + #define i_valfrom c_default_clone #else #define _i_has_from #endif @@ -232,7 +236,7 @@ #define i_valto c_default_toraw #endif #ifndef i_valclone - #define i_valclone(val) (val) + #define i_valclone c_default_clone #endif #ifndef i_valdrop #define i_valdrop c_default_drop -- cgit v1.2.3