diff options
| author | Tyge Løvset <[email protected]> | 2022-11-06 15:52:16 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-11-06 15:52:16 +0100 |
| commit | 5ec606e5dfdbaebe22717d094b58ee7f365ffd9c (patch) | |
| tree | 80bcfce7b82dd836724d793f3de3c224ac376154 /docs/ccommon_api.md | |
| parent | bdbdf76616281f305ffc0af7f347f3fd8eaf0016 (diff) | |
| download | STC-modified-5ec606e5dfdbaebe22717d094b58ee7f365ffd9c.tar.gz STC-modified-5ec606e5dfdbaebe22717d094b58ee7f365ffd9c.zip | |
Added c_autodrop() macro, and removed c_autobuf() which wasn't that useful (and was undocumented).
Diffstat (limited to 'docs/ccommon_api.md')
| -rw-r--r-- | docs/ccommon_api.md | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 7721a38d..8a11042b 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -3,19 +3,20 @@ The following macros are recommended to use, and they safe/have no side-effects. ## Scope macros (RAII) -### c_auto, c_with, c_scope, c_defer +### c_auto, c_autodrop, c_with, c_scope, c_defer General ***defer*** mechanics for resource acquisition. These macros allows you to specify the freeing of the resources at the point where the acquisition takes place. The **checkauto** utility described below, ensures that the `c_auto*` macros are used correctly. | Usage | Description | |:---------------------------------------|:----------------------------------------------------------| -| `c_auto (Type, var...)` | Same as `c_with (Type var=Type_init(), Type_drop(&var))` | | `c_with (Type var=init, drop)` | Declare `var`. Defer `drop...` to end of scope | | `c_with (Type var=init, pred, drop)` | Adds a predicate in order to exit early if init failed | -| `c_scope (init, drop...)` | Execute `init` and defer `drop...` to end of scope | +| `c_auto (Type, var1,...,var4)` | `c_with (Type var1=Type_init(), Type_drop(&var1))` ... | +| `c_autodrop (Type, var, init...)` | Like `c_with (Type var=init..., Type_drop(&var))` | +| `c_scope (init, drop)` | Execute `init` and defer `drop` to end of scope | | `c_defer (drop...)` | Defer `drop...` to end of scope | -| `continue` | Exit a `c_auto/c_with/c_scope...` without memory leaks | +| `continue` | Exit a block above without memory leaks | For multiple variables, use either multiple **c_with** in sequence, or declare variable outside scope and use **c_scope**. For convenience, **c_auto** support up to 4 variables. @@ -49,6 +50,12 @@ c_auto (cstr, s1, s2) printf("%s %s\n", cstr_str(&s1), cstr_str(&s2)); } +c_autodrop (cstr, str, cstr_new("Hello")) +{ + cstr_append(&str, " world"); + printf("%s\n", cstr_str(&str)); +} + // `c_scope` is like `c_with` but works with an already declared variable. static pthread_mutex_t mut; c_scope (pthread_mutex_lock(&mut), pthread_mutex_unlock(&mut)) |
