summaryrefslogtreecommitdiffhomepage
path: root/docs/cbox_api.md
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-12-19 12:21:44 +0100
committerTyge Løvset <[email protected]>2021-12-19 12:21:44 +0100
commit92b950333c6c7002bdbf1b60af44a249dc0cef9c (patch)
tree4b1acfcdba0bd940f829c53910587e27b5e0af90 /docs/cbox_api.md
parent183a89859ba9914ee0546e4482b40be199e52292 (diff)
downloadSTC-modified-92b950333c6c7002bdbf1b60af44a249dc0cef9c.tar.gz
STC-modified-92b950333c6c7002bdbf1b60af44a249dc0cef9c.zip
First commit for Version 3 of STC. Main changes are consistent rename of '_del' to '_drop' and '_compare' to '_cmp'.
Also i_key_ref (earlier i_key_sptr) and i_val_ref replaced by more general i_key_bind/i_val_bind.
Diffstat (limited to 'docs/cbox_api.md')
-rw-r--r--docs/cbox_api.md66
1 files changed, 33 insertions, 33 deletions
diff --git a/docs/cbox_api.md b/docs/cbox_api.md
index 83cec830..8580e7d7 100644
--- a/docs/cbox_api.md
+++ b/docs/cbox_api.md
@@ -1,12 +1,12 @@
# STC [cbox](../include/stc/cbox.h): (Boxed) Heap Allocated Objects
**cbox** is a A box is a smart pointer to a heap allocated value of type X. A **cbox** can
-be empty. The *cbox_X_compare()*, *cbox_X_del()* methods are defined based on the `i_cmp`
-and `i_valdel` macros specified. Use *cbox_X_clone(p)* to make a deep copy, which uses the
+be empty. The *cbox_X_cmp()*, *cbox_X_drop()* methods are defined based on the `i_cmp`
+and `i_valdrop` macros specified. Use *cbox_X_clone(p)* to make a deep copy, which uses the
`i_valfrom` macro if defined.
-When declaring a container of **cbox** values, it is recommended to define `i_val_ref` to the
-cbox type instead of defining `i_val`. This will auto-set `i_del`, `i_from`, and `i_cmp` using
+When declaring a container of **cbox** values, it is recommended to define `i_val_bind` to the
+cbox type instead of defining `i_val`. This will auto-set `i_drop`, `i_from`, and `i_cmp` using
functions defined by the specified **cbox**.
For containers, make sure to pass the result of create functions like *cbox_X_new()* **only** to
@@ -20,21 +20,21 @@ See similar c++ class [std::unique_ptr](https://en.cppreference.com/w/cpp/memory
```c
#define i_val // value: REQUIRED
#define i_cmp // three-way compare two i_val* : REQUIRED IF i_val is a non-integral type
-#define i_del // destroy value func - defaults to empty destruct
-#define i_from // create from raw/clone func - REQUIRED if i_del is defined,
+#define i_drop // destroy value func - defaults to empty destruct
+#define i_from // create from raw/clone func - REQUIRED if i_drop is defined,
// unless 'i_opt c_no_clone' is defined.
#define i_tag // type name tag, defaults to i_val
#include <stc/cbox.h>
```
`X` should be replaced by the value of `i_tag` in all of the following documentation.
-Define `i_opt` with `c_no_compare` if comparison between i_val's is not needed/available. Will then
+Define `i_opt` with `c_no_cmp` if comparison between i_val's is not needed/available. Will then
compare the pointer addresses when used. Additionally, `c_no_clone` or `i_is_fwd` may be defined.
## Methods
```c
cbox_X cbox_X_init(); // return an empty cbox
cbox_X cbox_X_new(i_val val); // allocate new heap object with val. Take ownership of val.
-cbox_X cbox_X_from(i_rawval raw); // like cbox_X_new(), but create owned value from raw.
+cbox_X cbox_X_from(i_valraw raw); // like cbox_X_new(), but create owned value from raw.
cbox_X cbox_X_with(i_val* p); // create a cbox from a pointer. Takes ownership of p.
cbox_X cbox_X_clone(cbox_X other); // return deep copied clone
@@ -42,14 +42,14 @@ cbox_X cbox_X_move(cbox_X* self); // transfer owners
void cbox_X_take(cbox_X* self, cbox_X other); // take ownership of other.
void cbox_X_copy(cbox_X* self, cbox_X other); // deep copy to self
-void cbox_X_del(cbox_X* self); // destruct the contained object and free's it.
+void cbox_X_drop(cbox_X* self); // destruct the contained object and free's it.
void cbox_X_reset(cbox_X* self);
void cbox_X_reset_new(cbox_X* self, i_val val); // assign new cbox with value. Takes ownership of val.
-void cbox_X_reset_from(cbox_X* self, i_rawval raw); // make and assign new cbox from raw value.
+void cbox_X_reset_from(cbox_X* self, i_valraw raw); // make and assign new cbox from raw value.
void cbox_X_reset_with(cbox_X* self, i_val* p); // create cbox with pointer p. Takes ownership of p.
-int cbox_X_compare(const cbox_X* x, const cbox_X* y); // compares pointer addresses if 'i_opt c_no_compare'
+int cbox_X_cmp(const cbox_X* x, const cbox_X* y); // compares pointer addresses if 'i_opt c_no_cmp'
// is defined. Otherwise uses 'i_cmp' or default compare.
```
## Types and constants
@@ -66,14 +66,14 @@ int cbox_X_compare(const cbox_X* x, const cbox_X* y); // compares pointe
#include <stdio.h>
#include <string.h>
-void int_del(int* x) {
- printf("del: %d\n", *x);
+void int_drop(int* x) {
+ printf("drop: %d\n", *x);
}
-// When 'i_del' is defined, you are also forced to define a clone function with
-// 'i_valfrom', as it is normally required when i_del destroys resources.
+// When 'i_drop' is defined, you are also forced to define a clone function with
+// 'i_from', as it is normally required when i_drop destroys resources.
//
-// If cloning is not needed, define 'i_opt c_no_clone' instead of 'i_valfrom'
+// If cloning is not needed, define 'i_opt c_no_clone' instead of 'i_from'
// both for the cbox type and the container of cbox elements. It will also
// disable emplace container functions.
//
@@ -81,22 +81,22 @@ void int_del(int* x) {
// define cloning internally.
#define i_val int
-#define i_del int_del // optional func, just to display elements destroyed
-#define i_valfrom c_default_clone
-#include <stc/cbox.h> // cbox_int
+#define i_drop int_drop // optional func, just to display elements destroyed
+#define i_from c_default_clone
+#include <stc/cbox.h> // cbox_int
-#define i_key_ref cbox_int // note: use i_key_ref 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_key_bind cbox_int // note: use i_key_bind 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_val_ref cbox_int // note: use i_val_ref 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_val_bind cbox_int // note: use i_val_bind 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>>)
int main()
{
- c_auto (cvec_int, vec) // declare and init vec, call del at scope exit
- c_auto (csset_int, set) // declare and init set, call del at scope exit
+ 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_apply(cvec_int, push_back, &vec, {
cbox_int_new(2021),
@@ -130,13 +130,13 @@ int main()
Output:
```
vec: 2021 2012 2022 2015
-del: 2015
-del: 2022
+drop: 2015
+drop: 2022
vec: 2021 2012
set: 2015 2021
Done
-del: 2021
-del: 2015
-del: 2021
-del: 2012
+drop: 2021
+drop: 2015
+drop: 2021
+drop: 2012
```