From 7b5ad58d20dcf3561662a294183fceab0d0ee73c Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 23 Sep 2021 08:13:46 +0200 Subject: Some more documentation in README.md. Added support for i_del template arg/define for destroy function. For maps, i_keydel / i_valdel is still required and error is given if i_del is defined. --- README.md | 6 +++++- docs/carray_api.md | 3 ++- docs/cvec_api.md | 2 ++ include/stc/template.h | 11 +++++++++++ tests/test_new_list.c | 12 ++++++------ tests/test_new_pque.c | 6 +++--- 6 files changed, 29 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 4fd34197..48ad3533 100644 --- a/README.md +++ b/README.md @@ -272,7 +272,11 @@ c_autovar (cstr s = cstr_lit("a string literal"), cstr_del(&s)) // cstr_lit() f ``` This is made possible because the type configuration may be given an optional conversion/"rawvalue"-type as template parameter, along with a back and forth conversion -methods to the container value type. By default, *rawvalue has the same type as value*. +methods to the container value type. + +Hence, `i_val x = ..., y = i_valfrom(i_valto(&x))` works as a *clone* function, where the output of +`i_valto()` is type `i_valraw`. Function `i_valfrom()` is a *clone* function when `i_valraw/i_valto` is +undefined (i_valraw defaults to `i_val`). Same for `i_key`. Rawvalues are also beneficial for **find()** and *map insertions*. The **emplace()** methods constructs *cstr*-objects from the rawvalues, but only when required: diff --git a/docs/carray_api.md b/docs/carray_api.md index 5a86240f..b567d8aa 100644 --- a/docs/carray_api.md +++ b/docs/carray_api.md @@ -11,7 +11,8 @@ See the c++ class [boost::multi_array](https://www.boost.org/doc/libs/release/li ```c #define i_tag // defaults to i_val name #define i_val // value: REQUIRED -#define i_valfrom // clone func i_val => i_val - defaults to plain copy +#define i_valfrom // func Raw => i_val - defaults to plain copy +#define i_valto // func i_val => Raw - defaults to plain copy #define i_valdel // destroy value func - defaults to empty destruct #include // or diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 294d3a59..3b1ff265 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -23,6 +23,8 @@ See the c++ class [std::vector](https://en.cppreference.com/w/cpp/container/vect ``` `X` should be replaced by the value of `i_tag` in all of the following documentation. +`i_del` may be defined instead of `i_valdel` (or `i_keydel`) for all non-map containers. + ## Methods ```c diff --git a/include/stc/template.h b/include/stc/template.h index efb6f2f8..c207ae56 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -104,6 +104,14 @@ #define i_valraw const char* #endif +#if defined i_del && defined i_isset + #define i_keydel i_del +#elif defined i_del && !defined i_key + #define i_valdel i_del +#elif defined i_del + #error i_del not supported for maps, define i_keydel / i_valdel instead. +#endif + #ifdef i_key #ifndef i_val #define i_val i_key @@ -131,6 +139,8 @@ #ifndef i_keydel #define i_keydel c_default_del #endif +#elif defined i_keydel || defined i_keyfrom || defined i_keyraw || defined i_hash || defined i_equ + #warning i_key-related definitions without i_key are ignored. #endif #ifndef i_tag @@ -160,6 +170,7 @@ #undef i_imp #undef i_fwd #undef i_cmp +#undef i_del #undef i_equ #undef i_hash #undef i_val diff --git a/tests/test_new_list.c b/tests/test_new_list.c index f0983b76..a38936b0 100644 --- a/tests/test_new_list.c +++ b/tests/test_new_list.c @@ -1,5 +1,5 @@ -#include "cstr.h" -#include "forward.h" +#include +#include forward_clist(i32, int); forward_clist(pnt, struct Point); @@ -12,7 +12,7 @@ struct MyStruct { #define F_tag i32 #define i_val int -#include "clist.h" +#include struct Point { int x, y; } typedef Point; int point_compare(const Point* a, const Point* b) { @@ -22,13 +22,13 @@ int point_compare(const Point* a, const Point* b) { #define F_tag pnt #define i_val Point #define i_cmp point_compare -#include "clist.h" +#include #define i_val float -#include "clist.h" +#include #define i_val_str -#include "clist.h" +#include int main() diff --git a/tests/test_new_pque.c b/tests/test_new_pque.c index b9750c44..1fb90123 100644 --- a/tests/test_new_pque.c +++ b/tests/test_new_pque.c @@ -8,10 +8,10 @@ struct MyStruct { }; #define i_val int -#include "cstack.h" +#include #define i_val int -#include "cpque.h" +#include struct Point { int x, y; } typedef Point; @@ -22,7 +22,7 @@ int Point_cmp(const Point* a, const Point* b) { #define F_tag pnt // F: was forward declared. #define i_val Point #define i_cmp Point_cmp -#include "cpque.h" +#include #include -- cgit v1.2.3