diff options
| -rw-r--r-- | docs/csptr_api.md | 27 | ||||
| -rw-r--r-- | include/stc/csptr.h | 23 |
2 files changed, 25 insertions, 25 deletions
diff --git a/docs/csptr_api.md b/docs/csptr_api.md index 7776d345..6080f714 100644 --- a/docs/csptr_api.md +++ b/docs/csptr_api.md @@ -38,18 +38,19 @@ The *csptr_X_compare()*, *csptr_X_equals()* and *csptr_X_del()* methods are defi ```c csptr_X csptr_X_from(Value* p); // constructor csptr_X csptr_X_make(Value val); // make_shared + 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); +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 +void csptr_X_move(csptr_X* self); // transfer ownership instead of sharing. +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 4b35e98c..60b9997f 100644 --- a/include/stc/csptr.h +++ b/include/stc/csptr.h @@ -32,23 +32,22 @@ typedef struct { cstr name, last; } Person;
-Person* Person_make(Person* p, const char* name, const char* last) {
- p->name = cstr_from(name), p->last = cstr_from(last);
- return p;
+Person Person_init(const char* name, const char* last) {
+ return (Person){.name = cstr_from(name), .last = cstr_from(last)};
}
void Person_del(Person* p) {
printf("del: %s\n", p->name.str);
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_from(Person_make(c_new(Person), "John", "Smiths"));
- csptr_pe q = csptr_pe_clone(p); // share the pointer
+ csptr_person p = csptr_person_make(Person_init("John", "Smiths"));
+ csptr_person q = csptr_person_clone(p); // share the pointer
printf("%s %s. uses: %zu\n", q.get->name.str, q.get->last.str, *q.use_count);
- c_del(csptr_pe, &p, &q);
+ c_del(csptr_person, &p, &q);
}
*/
typedef long atomic_count_t;
@@ -140,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; \
@@ -156,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
|
