diff options
| author | Tyge Løvset <[email protected]> | 2020-12-09 11:46:15 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-12-09 11:46:15 +0100 |
| commit | 30a400f71df76c077c97590b7a60daba98bef396 (patch) | |
| tree | b8bd823f0bcb8170c3c8c66c881c73573f8fa85c | |
| parent | b28159c2b1d4f477f78a2434a9a25caeb2a2e898 (diff) | |
| download | STC-modified-30a400f71df76c077c97590b7a60daba98bef396.tar.gz STC-modified-30a400f71df76c077c97590b7a60daba98bef396.zip | |
Renamed cptr_X type to cuptr_X, as it is similar to a unique_ptr, rather than a bare pointer. More docs too.
| -rw-r--r-- | docs/cptr_api.md | 34 | ||||
| -rw-r--r-- | examples/ptr.c | 4 | ||||
| -rw-r--r-- | stc/cptr.h | 33 |
3 files changed, 37 insertions, 34 deletions
diff --git a/docs/cptr_api.md b/docs/cptr_api.md index 4bdfd617..eec9c899 100644 --- a/docs/cptr_api.md +++ b/docs/cptr_api.md @@ -1,27 +1,29 @@ -# Container cptr: Shared Ptr +# Module cuptr: Smart Pointers -This describes the API of the type **cptr** and the shared pointer type **csptr**. The **cptr** is a helper for pointers in containers, while **csptr** is similar to c++ std::shared_ptr. +This describes the API of the type **cuptr** and the shared pointer type **csptr**. The **cuptr** is meant to be used like a c++ std::unique_ptr, while **csptr** is similar to c++ std::shared_ptr. + +The **cuptr** type is meant to be used as elements of containers, because the pointed to elements will automatically be deleted when the container is deleted. On its own, it offers little over regular pointers. **csptr** however, is useful when you want to track and delete the last usage of a pointer. **csptr** uses atomic usage counting, via the *csptr_X_share(sp)* and *csptr_X_del(&sp)* methods. ## Declaration ```c -#define using_cptr(X, Value, valueDestroy=c_default_del, - valueCompare=c_default_compare) +#define using_cuptr(X, Value, valueDestroy=c_default_del, + valueCompare=c_default_compare) #define using_csptr(X, Value, valueDestroy=c_default_del, valueCompare=c_default_compare) ``` -The macro `using_cptr()` must be instantiated in the global scope. `X` is a type tag name and will -affect the names of all cptr types and methods. E.g. declaring `using_cptr(my, cvec_my);`, +The macro `using_cuptr()` must be instantiated in the global scope. `X` is a type tag name and will +affect the names of all cuptr types and methods. E.g. declaring `using_cuptr(my, cvec_my);`, `X` should be replaced by `my` in all of the following documentation. Types | Type name | Type definition | Used to represent... | |:--------------------|:---------------------------------------|:-------------------------| -| `cptr_X` | Depends on underlying container type | The cptr type | -| `cptr_X_value_t` | " | The cptr element type | -| `cptr_X_iter_t` | " | cptr iterator | +| `cuptr_X` | Depends on underlying container type | The cuptr type | +| `cuptr_X_value_t` | " | The cuptr element type | +| `cuptr_X_iter_t` | " | cuptr iterator | | Type name | Type definition | Used to represent... | @@ -42,11 +44,11 @@ All cptr definitions and prototypes may be included in your C source file by inc ## Methods ```c -cptr_X cptr_X_init(void); -void cptr_X_del(cptr_X* self); -void cptr_X_reset(cptr_X* self, cptr_X_value_t* ptr); +cuptr_X cuptr_X_init(void); +void cuptr_X_del(cuptr_X* self); +void cuptr_X_reset(cuptr_X* self, cuptr_X_value_t* ptr); -int cptr_X_compare(cptr_X* x, cptr_X* y); +int cuptr_X_compare(cuptr_X* x, cuptr_X* y); ``` ```c @@ -57,7 +59,7 @@ void csptr_X_del(csptr_X* self); void csptr_X_reset(csptr_X* self, csptr_X_value_t* p); int csptr_X_compare(csptr_X* x, csptr_X* y); -int csptr_pointer_compare(cptr_X_value_t* x, cptr_X_value_t* y); +int csptr_pointer_compare(csptr_X_value_t* x, csptr_X_value_t* y); ``` ## Example @@ -81,8 +83,8 @@ void Person_del(Person* p) { using_cmap(pv, int, Person, Person_del); // mapped: Person -using_cptr(pp, Person, Person_del, c_no_compare); -using_cmap(pp, int, cptr_pp, cptr_pp_del); // mapped: Person* +using_cuptr(pp, Person, Person_del, c_no_compare); +using_cmap(pp, int, cuptr_pp, cuptr_pp_del); // mapped: Person* using_csptr(ps, Person, Person_del, c_no_compare); using_cmap(ps, int, csptr_ps, csptr_ps_del); // mapped: csptr<Person> diff --git a/examples/ptr.c b/examples/ptr.c index 3ffb9292..4e8f11aa 100644 --- a/examples/ptr.c +++ b/examples/ptr.c @@ -18,8 +18,8 @@ int Person_compare(const Person* p, const Person* q) { }
using_cvec(pe, Person, Person_del, Person_compare);
-using_cptr(pe, Person, Person_del, Person_compare);
-using_cvec(pp, Person*, cptr_pe_del, cptr_pe_compare);
+using_cuptr(pe, Person, Person_del, Person_compare);
+using_cvec(pp, Person*, cuptr_pe_del, cuptr_pe_compare);
int main() {
puts("Vec of Person *:");
@@ -23,7 +23,8 @@ #ifndef CPTR__H__
#define CPTR__H__
-/* cptr: pointers in containers, csptr: std::unique_ptr -like type:
+/* cuptr: std::unique_ptr -like type */
+/*
#include <stc/cptr.h>
#include <stc/cstr.h>
#include <stc/cvec.h>
@@ -43,8 +44,8 @@ int Person_compare(const Person* p, const Person* q) { return cmp == 0 ? strcmp(p->last.str, q->last.str) : cmp;
}
-using_cptr(pe, Person, Person_del, Person_compare);
-using_cvec(pe, Person*, cptr_pe_del, cptr_pe_compare);
+using_cuptr(pe, Person, Person_del, Person_compare);
+using_cvec(pe, Person*, cuptr_pe_del, cuptr_pe_compare);
int main() {
cvec_pe vec = cvec_pe_init();
@@ -58,35 +59,35 @@ int main() { */
#include "ccommon.h"
-#define using_cptr(...) c_MACRO_OVERLOAD(using_cptr, __VA_ARGS__)
+#define using_cuptr(...) c_MACRO_OVERLOAD(using_cuptr, __VA_ARGS__)
-#define using_cptr_2(X, Value) \
- using_cptr_3(X, Value, c_default_del)
+#define using_cuptr_2(X, Value) \
+ using_cuptr_3(X, Value, c_default_del)
-#define using_cptr_3(X, Value, valueDestroy) \
- using_cptr_4(X, Value, valueDestroy, c_default_compare)
+#define using_cuptr_3(X, Value, valueDestroy) \
+ using_cuptr_4(X, Value, valueDestroy, c_default_compare)
-#define using_cptr_4(X, Value, valueDestroy, valueCompare) \
- typedef Value cptr_##X##_value_t; \
- typedef cptr_##X##_value_t *cptr_##X; \
+#define using_cuptr_4(X, Value, valueDestroy, valueCompare) \
+ typedef Value cuptr_##X##_value_t; \
+ typedef cuptr_##X##_value_t *cuptr_##X; \
\
STC_INLINE void \
- cptr_##X##_del(cptr_##X* self) { \
+ cuptr_##X##_del(cuptr_##X* self) { \
valueDestroy(*self); \
c_free(*self); \
} \
\
STC_INLINE void \
- cptr_##X##_reset(cptr_##X* self, cptr_##X##_value_t* p) { \
- cptr_##X##_del(self); \
+ cuptr_##X##_reset(cuptr_##X* self, cuptr_##X##_value_t* p) { \
+ cuptr_##X##_del(self); \
*self = p; \
} \
\
STC_INLINE int \
- cptr_##X##_compare(cptr_##X* x, cptr_##X* y) { \
+ cuptr_##X##_compare(cuptr_##X* x, cuptr_##X* y) { \
return valueCompare(*x, *y); \
} \
- typedef cptr_##X cptr_##X##_t
+ typedef cuptr_##X cuptr_##X##_t
|
