diff options
| author | Tyge Løvset <[email protected]> | 2022-01-06 10:35:13 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-01-06 10:35:13 +0100 |
| commit | 26c449c95223173dcb9de7b84684099c55b41db1 (patch) | |
| tree | ce66f281059794322282a94cd16d303d5b30cab9 /docs/cbox_api.md | |
| parent | 7d770eac625d3cc48c51285ce089276bca08d82b (diff) | |
| download | STC-modified-26c449c95223173dcb9de7b84684099c55b41db1.tar.gz STC-modified-26c449c95223173dcb9de7b84684099c55b41db1.zip | |
Update cbox_api.md
Diffstat (limited to 'docs/cbox_api.md')
| -rw-r--r-- | docs/cbox_api.md | 57 |
1 files changed, 28 insertions, 29 deletions
diff --git a/docs/cbox_api.md b/docs/cbox_api.md index ba2c3b54..1e8c4ae9 100644 --- a/docs/cbox_api.md +++ b/docs/cbox_api.md @@ -36,7 +36,7 @@ compare the pointer addresses when used. Additionally, `c_no_clone` or `i_is_fwd ```c cbox_X cbox_X_init(); // return an empty cbox cbox_X cbox_X_new(i_valraw raw); // like cbox_X_from(), but create owned value from raw. - // nb! available only if i_valraw is defined. + // NB! available only if i_valraw is defined. cbox_X cbox_X_from(i_val val); // allocate new heap object with val. Take ownership of val. cbox_X cbox_X_from_ptr(i_val* p); // create a cbox from a pointer. Takes ownership of p. @@ -66,58 +66,57 @@ bool cbox_X_value_eq(const i_val* x, const i_val* y); // cbox_X_value_cm ```c #include <stdio.h> - -void int_drop(int* x) { // for demo. +void int_drop(int* x) { printf("drop: %d\n", *x); } +#define i_type IBox #define i_val int #define i_drop int_drop // optional func, just to display elements destroyed -#define i_from c_default_from -#include <stc/cbox.h> // cbox_int +#define i_from c_default_from // must specify because i_drop was defined. +#include <stc/cbox.h> -#define i_key_sptr cbox_int // note: use i_key_sptr instead of i_key -#define i_tag int // tag otherwise defaults to 'ref' -#include <stc/csset.h> // csset_int (like: std::set<std::unique_ptr<int>>) +#define i_type ISet +#define i_key_sptr IBox // NB: use i_key_sptr instead of i_key +#include <stc/csset.h> // ISet : std::set<std::unique_ptr<int>> -#define i_val_sptr cbox_int // note: use i_val_sptr instead of i_val -#define i_tag int // tag otherwise defaults to 'ref' -#include <stc/cvec.h> // cvec_int (like: std::vector<std::unique_ptr<int>>) +#define i_type IVec +#define i_val_sptr IBox // NB: use i_val_sptr instead of i_val +#include <stc/cvec.h> // IVec : std::vector<std::unique_ptr<int>> int main() { - c_auto (cvec_int, vec) // declare and init vec, call drop at scope exit - c_auto (csset_int, set) // declare and init set, call drop at scope exit + c_auto (IVec, vec) // declare and init vec, call drop at scope exit + c_auto (ISet, set) // similar { - c_apply(v, cvec_int_push_back(&vec, v), cbox_int, { - cbox_int_from(2021), - cbox_int_from(2012), - cbox_int_from(2022), - cbox_int_from(2015), + c_apply(v, IVec_push(&vec, v), IBox, { + IBox_from(2021), IBox_from(2012), IBox_from(2022), IBox_from(2015), }); + printf("vec:"); - c_foreach (i, cvec_int, vec) printf(" %d", *i.ref->get); + c_foreach (i, IVec, vec) + printf(" %d", *i.ref->get); puts(""); // add odd numbers from vec to set - c_foreach (i, cvec_int, vec) + c_foreach (i, IVec, vec) if (*i.ref->get & 1) { - csset_int_emplace(&set, *i.ref->get); // clone + ISet_emplace(&set, *i.ref->get); // clone // same as: - //csset_int_insert(&set, cbox_int_clone(*i.ref)); + //ISet_insert(&set, IBox_clone(*i.ref)); } - // erase the two last elements in vec - cvec_int_pop_back(&vec); - cvec_int_pop_back(&vec); + // pop the two last elements in vec + IVec_pop(&vec); + IVec_pop(&vec); printf("vec:"); - c_foreach (i, cvec_int, vec) printf(" %d", *i.ref->get); + c_foreach (i, IVec, vec) + printf(" %d", *i.ref->get); printf("\nset:"); - c_foreach (i, csset_int, set) printf(" %d", *i.ref->get); - - puts("\nDone"); + c_foreach (i, ISet, set) + printf(" %d", *i.ref->get); } } ``` |
