diff options
| -rw-r--r-- | docs/csptr_api.md | 31 | ||||
| -rw-r--r-- | include/stc/csptr.h | 10 |
2 files changed, 21 insertions, 20 deletions
diff --git a/docs/csptr_api.md b/docs/csptr_api.md index 7776d345..beafa5b9 100644 --- a/docs/csptr_api.md +++ b/docs/csptr_api.md @@ -36,20 +36,21 @@ Use *csptr_X_clone(p)* when sharing ownership of the pointed-to object. See exam The *csptr_X_compare()*, *csptr_X_equals()* and *csptr_X_del()* methods are defined based on the *valeCompare* and *valueDel* arguments passed to the **using**-macro. ```c -csptr_X csptr_X_from(Value* p); // constructor -csptr_X csptr_X_make(Value val); // make_shared +csptr_X csptr_X_from(Value* p); // constructor +csptr_X csptr_X_make(Value val); // make_shared; fast + void csptr_X_reset(csptr_X* self); -void csptr_X_reset_with(csptr_X* self, Value* p); +csptr_X_value_t* csptr_X_reset_from(csptr_X* self, Value* p); // slower than reset_make(). +csptr_X_value_t* csptr_X_reset_make(csptr_X* self, Value val); // assign new sptr with value -csptr_X_value_t* csptr_X_assign(csptr_X* self, Value val); // assign new sptr with value -csptr_X_value_t* csptr_X_copy(csptr_X* self, csptr_X ptr); // assign a shared copy +csptr_X_value_t* csptr_X_copy(csptr_X* self, CX other); // copy shared (increase use count) +csptr_X csptr_X_clone(csptr_X ptr); // clone shared (increase use count) -csptr_X csptr_X_clone(csptr_X ptr); // share the pointer (increase use count) -void csptr_X_move(csptr_X* self); // transfer ownership instead of sharing. -void csptr_X_del(csptr_X* self); // destructor: decrease use count, destroy at 0 +csptr_X csptr_X_move(csptr_X* self); // fast transfer ownership to another sptr. +void csptr_X_del(csptr_X* self); // destruct: decrease use count, free at 0 -int csptr_X_compare(csptr_X* x, csptr_X* y); -bool csptr_X_equals(csptr_X* x, csptr_X* y); +int csptr_X_compare(const csptr_X* x, const csptr_X* y); +bool csptr_X_equals(const csptr_X* x, const csptr_X* y); ``` ## Types and constants @@ -77,17 +78,17 @@ void Person_del(Person* p) { c_del(cstr, &p->name, &p->last); } -using_csptr(pe, Person, c_no_compare, Person_del); +using_csptr(person, Person, c_no_compare, Person_del); int main() { - csptr_pe p = csptr_pe_make(Person_init("John", "Smiths")); - csptr_pe q = csptr_pe_clone(p); // means: share the pointer + csptr_person p = csptr_person_make(Person_init("John", "Smiths")); + csptr_person q = csptr_person_clone(p); // means: share the pointer printf("Person: %s %s. uses: %zu\n", p.get->name.str, p.get->last.str, *p.use_count); - csptr_pe_del(&p); + csptr_person_del(&p); printf("Last man standing: %s %s. uses: %zu\n", q.get->name.str, q.get->last.str, *q.use_count); - csptr_pe_del(&q); + csptr_person_del(&q); } ``` Output: diff --git a/include/stc/csptr.h b/include/stc/csptr.h index 02518869..4637963e 100644 --- a/include/stc/csptr.h +++ b/include/stc/csptr.h @@ -139,14 +139,14 @@ typedef long atomic_count_t; } \
\
STC_INLINE CX##_value_t* \
- CX##_reset_with(CX* self, CX##_value_t* p) { \
+ CX##_reset_from(CX* self, CX##_value_t* p) { \
CX##_del(self); \
*self = CX##_from(p); \
return self->get; \
} \
\
STC_INLINE CX##_value_t* \
- CX##_assign(CX* self, CX##_value_t val) { \
+ CX##_reset_make(CX* self, CX##_value_t val) { \
CX##_del(self); \
*self = CX##_make(val); \
return self->get; \
@@ -155,18 +155,18 @@ typedef long atomic_count_t; STC_INLINE CX##_value_t* \
CX##_copy(CX* self, CX ptr) { \
CX##_del(self); \
- if (ptr.use_count) c_atomic_increment(ptr.use_count); \
*self = ptr; \
+ if (self->use_count) c_atomic_increment(self->use_count); \
return self->get; \
} \
\
STC_INLINE int \
- CX##_compare(CX* x, CX* y) { \
+ CX##_compare(const CX* x, const CX* y) { \
return valueCompare(x->get, y->get); \
} \
\
STC_INLINE bool \
- CX##_equals(CX* x, CX* y) { \
+ CX##_equals(const CX* x, const CX* y) { \
return valueCompare(x->get, y->get) == 0; \
} \
struct stc_trailing_semicolon
|
