summaryrefslogtreecommitdiffhomepage
path: root/include/stc
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-10-19 15:30:20 +0200
committerTyge Løvset <[email protected]>2022-10-19 15:30:20 +0200
commitbb3a457e5c2c9e6b069ab3dfba10f9d21686b035 (patch)
tree5e0947fbf0be670f569595a2dbc28f24938e18c6 /include/stc
parent4c643c261e124460e6b7c41a9cf67c66a4213189 (diff)
downloadSTC-modified-bb3a457e5c2c9e6b069ab3dfba10f9d21686b035.tar.gz
STC-modified-bb3a457e5c2c9e6b069ab3dfba10f9d21686b035.zip
- Removed deprecated carc_make and cbox_make (replaced by carc_from, cbox_from)
- Some improvements to template.h - Many smaller improvements to examples and docs.
Diffstat (limited to 'include/stc')
-rw-r--r--include/stc/carc.h24
-rw-r--r--include/stc/cbox.h15
-rw-r--r--include/stc/template.h15
3 files changed, 30 insertions, 24 deletions
diff --git a/include/stc/carc.h b/include/stc/carc.h
index f0ab651b..6148a815 100644
--- a/include/stc/carc.h
+++ b/include/stc/carc.h
@@ -26,12 +26,13 @@
typedef struct { cstr name, last; } Person;
-Person Person_new(const char* name, const char* last) {
+Person Person_make(const char* name, const char* last) {
return (Person){.name = cstr_from(name), .last = cstr_from(last)};
}
void Person_drop(Person* p) {
printf("drop: %s %s\n", cstr_str(&p->name), cstr_str(&p->last));
- c_drop(cstr, &p->name, &p->last);
+ cstr_drop(&p->name);
+ cstr_drop(&p->last);
}
#define i_type ArcPers
@@ -40,10 +41,10 @@ void Person_drop(Person* p) {
#include <stc/carc.h>
int main() {
- ArcPers p = ArcPers_from(Person_new("John", "Smiths"));
+ ArcPers p = ArcPers_from(Person_make("John", "Smiths"));
ArcPers q = ArcPers_clone(p); // share the pointer
- printf("%s %s. uses: %" c_zu "\n", cstr_str(&q.get->name), cstr_str(&q.get->last), *q.use_count);
+ printf("%s %s. uses: %ld\n", cstr_str(&q.get->name), cstr_str(&q.get->last), *q.use_count);
c_drop(ArcPers, &p, &q);
}
*/
@@ -77,9 +78,12 @@ int main() {
#ifndef _i_prefix
#define _i_prefix carc_
#endif
-#if !(defined i_cmp || defined i_less || defined i_eq || defined i_hash)
+#if !(defined i_cmp || defined i_less)
#define _i_no_cmp
#endif
+#if !(defined i_eq || defined i_hash)
+ #define _i_no_hash
+#endif
#include "template.h"
typedef i_keyraw _cx_raw;
@@ -117,9 +121,6 @@ STC_INLINE _cx_self _cx_memb(_from)(_cx_value val) {
return ptr;
}
-STC_INLINE _cx_self _cx_memb(_make)(_cx_value val) // [deprecated]
- { return _cx_memb(_from)(val); }
-
STC_INLINE _cx_raw _cx_memb(_toraw)(const _cx_self* self)
{ return i_keyto(self->get); }
@@ -174,8 +175,8 @@ STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self ptr) {
}
STC_INLINE uint64_t _cx_memb(_value_hash)(const _cx_value* x) {
- #if defined _i_no_cmp
- return c_default_hash(&x);
+ #if defined _i_no_hash
+ return (uint64_t)x;
#else
_cx_raw rx = i_keyto(x);
return i_hash((&rx));
@@ -192,7 +193,7 @@ STC_INLINE int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) {
}
STC_INLINE bool _cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) {
- #if defined _i_no_cmp
+ #if defined _i_no_hash
return x == y;
#else
_cx_raw rx = i_keyto(x), ry = i_keyto(y);
@@ -211,4 +212,5 @@ STC_INLINE bool _cx_memb(_eq)(const _cx_self* x, const _cx_self* y)
#undef _i_atomic_inc
#undef _i_atomic_dec_and_test
+#undef _i_no_hash
#include "template.h"
diff --git a/include/stc/cbox.h b/include/stc/cbox.h
index fc300de3..410656c2 100644
--- a/include/stc/cbox.h
+++ b/include/stc/cbox.h
@@ -70,9 +70,12 @@ int main() {
#ifndef _i_prefix
#define _i_prefix cbox_
#endif
-#if !(defined i_cmp || defined i_less || defined i_eq || defined i_hash)
+#if !(defined i_cmp || defined i_less)
#define _i_no_cmp
#endif
+#if !(defined i_eq || defined i_hash)
+ #define _i_no_hash
+#endif
#include "template.h"
typedef i_keyraw _cx_raw;
@@ -95,9 +98,6 @@ STC_INLINE _cx_self _cx_memb(_from)(_cx_value val) {
_cx_self ptr = {c_alloc(_cx_value)};
*ptr.get = val; return ptr;
}
-// [deprecated]
-STC_INLINE _cx_self _cx_memb(_make)(_cx_value val)
- { return _cx_memb(_from)(val); }
STC_INLINE _cx_raw _cx_memb(_toraw)(const _cx_self* self)
{ return i_keyto(self->get); }
@@ -149,8 +149,8 @@ STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self other) {
}
STC_INLINE uint64_t _cx_memb(_value_hash)(const _cx_value* x) {
- #if defined _i_no_cmp
- return c_default_hash(&x);
+ #if defined _i_no_hash
+ return (uint64_t)x;
#else
_cx_raw rx = i_keyto(x);
return i_hash((&rx));
@@ -167,7 +167,7 @@ STC_INLINE int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) {
}
STC_INLINE bool _cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) {
- #if defined _i_no_cmp
+ #if defined _i_no_hash
return x == y;
#else
_cx_raw rx = i_keyto(x), ry = i_keyto(y);
@@ -184,4 +184,5 @@ STC_INLINE int _cx_memb(_cmp)(const _cx_self* x, const _cx_self* y)
STC_INLINE bool _cx_memb(_eq)(const _cx_self* x, const _cx_self* y)
{ return _cx_memb(_value_eq)(x->get, y->get); }
+#undef _i_no_hash
#include "template.h"
diff --git a/include/stc/template.h b/include/stc/template.h
index 04eea1d4..8ac14583 100644
--- a/include/stc/template.h
+++ b/include/stc/template.h
@@ -52,6 +52,12 @@
#define _i_expandby 1
#endif
+#if (defined i_valraw) ^ (defined i_valto)
+ #error "both i_valto and i_valraw must be defined, if any"
+#elif defined i_valfrom && !defined i_valraw
+ #error "i_valfrom defined without i_valraw"
+#endif
+
#if !(defined i_key || defined i_key_str || defined i_key_ssv || \
defined i_key_bind || defined i_key_arcbox)
#define _i_key_from_val
@@ -177,7 +183,9 @@
#elif !defined i_eq
#define i_eq c_default_eq
#endif
-#if !defined i_less && !defined i_cmp
+#if defined i_less && defined i_cmp
+ #error "Only one of i_less and i_cmp may be defined"
+#elif !defined i_less && !defined i_cmp
#define i_less c_default_less
#elif !defined i_less
#define i_less(x, y) (i_cmp(x, y)) < 0
@@ -224,11 +232,6 @@
#ifndef i_val
#error "i_val* must be defined for maps"
#endif
-#if defined i_valraw ^ defined i_valto
- #error "both i_valto and i_valraw must be defined, if any"
-#elif defined i_valfrom && !defined i_valraw
- #error "i_valfrom defined without i_valraw"
-#endif
#if !defined i_valclone && (defined i_valdrop || defined i_valraw)
#define _i_no_clone