From 23f5994392dc3a3ad4a1b680f4453cba21e632eb Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 24 Sep 2021 07:08:56 +0200 Subject: Reworked csptr docs. Made static members to inline (ccommon.h). --- docs/csptr_api.md | 15 +++++++++++---- examples/sptr_ex.c | 9 ++++----- include/stc/ccommon.h | 4 ++-- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/docs/csptr_api.md b/docs/csptr_api.md index d1157162..7b44e4c1 100644 --- a/docs/csptr_api.md +++ b/docs/csptr_api.md @@ -24,13 +24,20 @@ See the c++ classes [std::shared_ptr](https://en.cppreference.com/w/cpp/memory/s #define i_valdel // destroy value func - defaults to empty destruct #include ``` -`X` should be replaced by the value of `i_tag` in all of the following documentation. When declaring a container with shared pointers, define the `i_val_csptr` with the csptr's `i_tag`. See example below. +`X` should be replaced by the value of `i_tag` in all of the following documentation. -## Methods +The *csptr_X_compare()*, *csptr_X_del()* methods are defined based on the `i_cmp` and `i_valdel` +macros specified. Use *csptr_X_clone(p)* when sharing ownership of the pointed-to object. + +When declaring a container with shared pointers, define the `i_val_csptr` with the csptr's `i_tag`. +See example. -The *csptr_X_compare()*, *csptr_X_del()* methods are defined based on the `i_cmp` and `i_valdel` macros specified. +Also for containers, make sure to pass the result of *csptr_X_make()* to *insert*, *push_back*, +or *push*, and not an *emplace* function. The *csptr_X_make()* method creates a **csptr** with +use-count 1, and *emplace* will ***clone*** it and increase the count, causing a memory leak. Use +*emplace* functions when sharing **csptr**s between containers or other existing shared pointers. -Use *csptr_X_clone(p)* when sharing ownership of the pointed-to object. For shared pointers stored in containers, define `i_val_csptr` to the shared pointers tag instead of a `i_val` macro. See example below. +## Methods ```c csptr_X csptr_X_init(); // empty constructor csptr_X csptr_X_make(i_val val); // make_shared constructor, like std::make_shared() diff --git a/examples/sptr_ex.c b/examples/sptr_ex.c index 4a0397ec..290833ff 100644 --- a/examples/sptr_ex.c +++ b/examples/sptr_ex.c @@ -38,14 +38,13 @@ void Song_del(Song* s) { void example3() { - c_auto (cvec_song, v, v2) { - csptr_song songs[] = { + c_auto (cvec_song, v, v2) + { + c_apply(cvec_song, push_back, &v, { csptr_song_make(Song_from("Bob Dylan", "The Times They Are A Changing")), csptr_song_make(Song_from("Aretha Franklin", "Bridge Over Troubled Water")), csptr_song_make(Song_from("Thalia", "Entre El Mar y Una Estrella")) - }; - c_forrange (i, c_arraylen(songs)) - cvec_song_push_back(&v, songs[i]); + }); c_foreach (s, cvec_song, v) if (!cstr_equalto(s.ref->get->artist, "Bob Dylan")) diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 2a1bea72..919194a8 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -45,8 +45,8 @@ # define STC_LIBRARY_ONLY(...) __VA_ARGS__ # define STC_STATIC_ONLY(...) #else -# define STC_API static -# define STC_DEF static +# define STC_API static inline +# define STC_DEF static inline # define STC_LIBRARY_ONLY(...) # define STC_STATIC_ONLY(...) __VA_ARGS__ #endif -- cgit v1.2.3