diff options
| author | Tyge Løvset <[email protected]> | 2021-12-14 11:20:17 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-12-14 11:20:17 +0100 |
| commit | c083936d8fa46b5df921dedae6ca15e1192d6612 (patch) | |
| tree | a8f2b0e78030e3c3db87487acb85ae8c75733a38 | |
| parent | 17280f8177736c35b34e41556a307ca51e68d1e2 (diff) | |
| download | STC-modified-c083936d8fa46b5df921dedae6ca15e1192d6612.tar.gz STC-modified-c083936d8fa46b5df921dedae6ca15e1192d6612.zip | |
cstr_printf() now returns int like printf(). Minor updates in sharedptr.c example.
| -rw-r--r-- | docs/cstr_api.md | 2 | ||||
| -rw-r--r-- | examples/sharedptr.c | 44 | ||||
| -rw-r--r-- | include/stc/cstr.h | 18 | ||||
| -rw-r--r-- | include/stc/template.h | 6 |
4 files changed, 33 insertions, 37 deletions
diff --git a/docs/cstr_api.md b/docs/cstr_api.md index 0004eeae..e54b3a4c 100644 --- a/docs/cstr_api.md +++ b/docs/cstr_api.md @@ -41,8 +41,8 @@ void cstr_clear(cstr* self); cstr* cstr_assign(cstr* self, const char* str); cstr* cstr_assign_n(cstr* self, const char* str, size_t n); // assign n first chars of str -cstr* cstr_printf(cstr* self, const char* fmt, ...); // printf() formatting cstr* cstr_copy(cstr* self, cstr s); // cstr_take(self, cstr_clone(s)) +int cstr_printf(cstr* self, const char* fmt, ...); // printf() formatting cstr* cstr_append(cstr* self, const char* str); cstr* cstr_append_s(cstr* self, cstr s); diff --git a/examples/sharedptr.c b/examples/sharedptr.c index a8e3e3ae..c3da5287 100644 --- a/examples/sharedptr.c +++ b/examples/sharedptr.c @@ -5,50 +5,50 @@ void int_del(int* x) { printf("del: %d\n", *x);
}
-// csptr implements its own clone method using ref counting, so 'i_valfrom'
-// should not be defined (ignored).
+// csptr implements its own clone method using reference counting,
+// so 'i_valfrom' need not be defined (will be ignored).
+
+#define i_type iref // set type name to be defined (instead of 'csptr_int')
#define i_val int
-#define i_del int_del // optional func, just to display elements destroyed
-#include <stc/csptr.h> // csptr_int
+#define i_del int_del // optional, just to display the elements destroyed
+#include <stc/csptr.h> // iref
-#define i_key_ref csptr_int // note: use i_key_ref instead of i_key
-#define i_tag int // tag otherwise defaults to 'ref'
-#include <stc/csset.h> // csset_int (like: std::set<std::shared_ptr<int>>)
+#define i_key_ref iref // note: use i_key_ref instead of i_key for csptr/cbox elements
+#include <stc/csset.h> // csset_iref (like: std::set<std::shared_ptr<int>>)
-#define i_val_ref csptr_int // Note: use i_val_ref instead of i_val
-#define i_tag int // tag otherwise defaults to 'ref'
-#include <stc/cvec.h> // cvec_int (like: std::vector<std::shared_ptr<int>>)
+#define i_val_ref iref // note: as above.
+#include <stc/cvec.h> // cvec_iref (like: std::vector<std::shared_ptr<int>>)
int main()
{
- c_auto (cvec_int, vec) // declare and init vec, call del at scope exit
- c_auto (csset_int, set) // declare and init set, call del at scope exit
+ c_auto (cvec_iref, vec) // declare and init vec, call cvec_iref_del() at scope exit
+ c_auto (csset_iref, set) // declare and init set, call csset_iref_del() at scope exit
{
const int years[] = {2021, 2012, 2022, 2015};
c_forrange (i, c_arraylen(years))
- cvec_int_push_back(&vec, csptr_int_new(years[i]));
+ cvec_iref_push_back(&vec, iref_new(years[i]));
printf("vec:");
- c_foreach (i, cvec_int, vec) printf(" %d", *i.ref->get);
+ c_foreach (i, cvec_iref, vec) printf(" %d", *i.ref->get);
puts("");
// add odd numbers from vec to set
- c_foreach (i, cvec_int, vec)
+ c_foreach (i, cvec_iref, vec)
if (*i.ref->get & 1)
- csset_int_emplace(&set, *i.ref); // copy shared pointer => increments counter.
+ csset_iref_emplace(&set, *i.ref); // copy shared pointer => increments counter.
// erase the two last elements in vec
- cvec_int_pop_back(&vec);
- cvec_int_pop_back(&vec);
+ cvec_iref_pop_back(&vec);
+ cvec_iref_pop_back(&vec);
printf("vec:");
- c_foreach (i, cvec_int, vec) printf(" %d", *i.ref->get);
+ c_foreach (i, cvec_iref, vec) printf(" %d", *i.ref->get);
printf("\nset:");
- c_foreach (i, csset_int, set) printf(" %d", *i.ref->get);
+ c_foreach (i, csset_iref, set) printf(" %d", *i.ref->get);
- c_autovar (csptr_int p = csptr_int_clone(vec.data[0]), csptr_int_del(&p)) {
- printf("\n%d is now owned by %zu objects\n", *p.get, *p.use_count);
+ c_autovar (iref p = iref_clone(vec.data[0]), iref_del(&p)) {
+ printf("\n%d is now owned by %ld objects\n", *p.get, *p.use_count);
}
puts("\nDone");
diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 41ab11ad..3d8d3097 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -54,7 +54,7 @@ STC_API cstr cstr_from_replace_all(const char* str, size_t str_len, STC_API size_t cstr_reserve(cstr* self, size_t cap);
STC_API void cstr_resize(cstr* self, size_t len, char fill);
STC_API cstr* cstr_assign_n(cstr* self, const char* str, size_t n);
-STC_API cstr* cstr_printf(cstr* self, const char* fmt, ...);
+STC_API int cstr_printf(cstr* self, const char* fmt, ...);
STC_API cstr* cstr_append_n(cstr* self, const char* str, size_t n);
STC_API void cstr_replace_n(cstr* self, size_t pos, size_t len, const char* str, size_t n);
STC_API void cstr_replace_all(cstr* self, const char* find, const char* replace);
@@ -216,7 +216,7 @@ cstr_from_n(const char* str, const size_t n) { # pragma warning(disable: 4996)
#endif
-STC_DEF void
+STC_DEF int
cstr_vfmt(cstr* self, const char* fmt, va_list args) {
va_list args2;
va_copy(args2, args);
@@ -224,7 +224,7 @@ cstr_vfmt(cstr* self, const char* fmt, va_list args) { cstr_reserve(self, len);
vsprintf(self->str, fmt, args2);
va_end(args2);
- _cstr_rep(self)->size = len;
+ return _cstr_rep(self)->size = len;
}
#if defined(__clang__)
@@ -242,14 +242,16 @@ cstr_from_fmt(const char* fmt, ...) { return ret;
}
-STC_DEF cstr*
+STC_DEF int
cstr_printf(cstr* self, const char* fmt, ...) {
cstr ret = cstr_null;
- va_list args; va_start(args, fmt);
- cstr_vfmt(&ret, fmt, args);
+ va_list args;
+ va_start(args, fmt);
+ int n = cstr_vfmt(&ret, fmt, args);
va_end(args);
- cstr_del(self); *self = ret;
- return self;
+ cstr_del(self);
+ *self = ret;
+ return n;
}
STC_DEF cstr*
diff --git a/include/stc/template.h b/include/stc/template.h index 757f9443..e788d4eb 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -72,9 +72,6 @@ #if defined i_key_ref
#define i_key i_key_ref
#define i_keyfrom c_PASTE(i_key, _clone)
- #ifndef i_tag
- #define i_tag ref
- #endif
#ifndef i_cmp
#define i_cmp c_PASTE(i_key, _compare)
#endif
@@ -108,9 +105,6 @@ #if defined i_val_ref
#define i_val i_val_ref
#define i_valfrom c_PASTE(i_val, _clone)
- #if !defined i_tag && !defined i_key
- #define i_tag ref
- #endif
#if !defined i_cmp && !defined i_key
#define i_cmp c_PASTE(i_val, _compare)
#endif
|
