diff options
| author | _Tradam <[email protected]> | 2023-09-08 01:29:47 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-09-08 01:29:47 +0000 |
| commit | 3c76c7f3d5db3f9586a90d03f8fbb02d79de9acd (patch) | |
| tree | afbe4b540967223911f7c5de36559b82154f02f3 /include/stc/cbox.h | |
| parent | 0841165881871ee01b782129be681209aeed2423 (diff) | |
| parent | 1a72205fe05c2375cfd380dd8381a8460d9ed8d1 (diff) | |
| download | STC-modified-modified.tar.gz STC-modified-modified.zip | |
Diffstat (limited to 'include/stc/cbox.h')
| -rw-r--r-- | include/stc/cbox.h | 132 |
1 files changed, 67 insertions, 65 deletions
diff --git a/include/stc/cbox.h b/include/stc/cbox.h index ca88fc66..55ce9711 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -23,7 +23,9 @@ */ /* cbox: heap allocated boxed type +#define i_implement #include <stc/cstr.h> +#include <stc/algo/raii.h> // c_auto typedef struct { cstr name, email; } Person; @@ -40,11 +42,11 @@ void Person_drop(Person* p) { c_drop(cstr, &p->name, &p->email); } -#define i_keyclass Person // bind Person clone+drop fn's #define i_type PBox +#define i_valclass Person // bind Person clone+drop fn's #include <stc/cbox.h> -int main() { +int main(void) { c_auto (PBox, p, q) { p = PBox_from(Person_from("John Smiths", "[email protected]")); @@ -56,142 +58,142 @@ int main() { } } */ -#include "ccommon.h" +#include "priv/linkage.h" #ifndef CBOX_H_INCLUDED #define CBOX_H_INCLUDED +#include "ccommon.h" #include "forward.h" #include <stdlib.h> #include <string.h> -#define cbox_NULL {NULL} +#define cbox_null {0} #endif // CBOX_H_INCLUDED -#ifndef _i_prefix #define _i_prefix cbox_ -#endif -#ifdef i_eq -#define _i_eq -#endif +#define _i_cbox #include "priv/template.h" typedef i_keyraw _cx_raw; #ifndef i_is_forward -_cx_deftypes(_c_cbox_types, _cx_self, i_key); +_cx_DEFTYPES(_c_cbox_types, _cx_Self, i_key); #endif // constructors (take ownership) -STC_INLINE _cx_self _cx_memb(_init)(void) - { return c_LITERAL(_cx_self){NULL}; } +STC_INLINE _cx_Self _cx_MEMB(_init)(void) + { return c_LITERAL(_cx_Self){NULL}; } -STC_INLINE long _cx_memb(_use_count)(const _cx_self* self) +STC_INLINE long _cx_MEMB(_use_count)(const _cx_Self* self) { return (long)(self->get != NULL); } -STC_INLINE _cx_self _cx_memb(_from_ptr)(_cx_value* p) - { return c_LITERAL(_cx_self){p}; } +STC_INLINE _cx_Self _cx_MEMB(_from_ptr)(_cx_value* p) + { return c_LITERAL(_cx_Self){p}; } // c++: std::make_unique<i_key>(val) -STC_INLINE _cx_self _cx_memb(_make)(_cx_value val) { - _cx_self ptr = {_i_alloc(_cx_value)}; +STC_INLINE _cx_Self _cx_MEMB(_make)(_cx_value val) { + _cx_Self ptr = {_i_alloc(_cx_value)}; *ptr.get = val; return ptr; } -STC_INLINE _cx_raw _cx_memb(_toraw)(const _cx_self* self) +STC_INLINE _cx_raw _cx_MEMB(_toraw)(const _cx_Self* self) { return i_keyto(self->get); } // destructor -STC_INLINE void _cx_memb(_drop)(_cx_self* self) { +STC_INLINE void _cx_MEMB(_drop)(_cx_Self* self) { if (self->get) { i_keydrop(self->get); i_free(self->get); } } -STC_INLINE _cx_self _cx_memb(_move)(_cx_self* self) { - _cx_self ptr = *self; +STC_INLINE _cx_Self _cx_MEMB(_move)(_cx_Self* self) { + _cx_Self ptr = *self; self->get = NULL; return ptr; } -STC_INLINE _cx_value* _cx_memb(_release)(_cx_self* self) - { return _cx_memb(_move)(self).get; } +STC_INLINE _cx_value* _cx_MEMB(_release)(_cx_Self* self) + { return _cx_MEMB(_move)(self).get; } -STC_INLINE void _cx_memb(_reset)(_cx_self* self) { - _cx_memb(_drop)(self); +STC_INLINE void _cx_MEMB(_reset)(_cx_Self* self) { + _cx_MEMB(_drop)(self); self->get = NULL; } // take ownership of p -STC_INLINE void _cx_memb(_reset_to)(_cx_self* self, _cx_value* p) { - _cx_memb(_drop)(self); +STC_INLINE void _cx_MEMB(_reset_to)(_cx_Self* self, _cx_value* p) { + _cx_MEMB(_drop)(self); self->get = p; } #ifndef i_no_emplace -STC_INLINE _cx_self _cx_memb(_from)(_cx_raw raw) - { return _cx_memb(_make)(i_keyfrom(raw)); } +STC_INLINE _cx_Self _cx_MEMB(_from)(_cx_raw raw) + { return _cx_MEMB(_make)(i_keyfrom(raw)); } #else -STC_INLINE _cx_self _cx_memb(_from)(_cx_value val) - { return _cx_memb(_make)(val); } +STC_INLINE _cx_Self _cx_MEMB(_from)(_cx_value val) + { return _cx_MEMB(_make)(val); } #endif #if !defined i_no_clone - STC_INLINE _cx_self _cx_memb(_clone)(_cx_self other) { + STC_INLINE _cx_Self _cx_MEMB(_clone)(_cx_Self other) { if (!other.get) return other; - _cx_self out = {_i_alloc(i_key)}; + _cx_Self out = {_i_alloc(i_key)}; *out.get = i_keyclone((*other.get)); return out; } #endif // !i_no_clone // take ownership of unowned -STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self unowned) { - _cx_memb(_drop)(self); +STC_INLINE void _cx_MEMB(_take)(_cx_Self* self, _cx_Self unowned) { + _cx_MEMB(_drop)(self); *self = unowned; } // transfer ownership from moved; set moved to NULL -STC_INLINE void _cx_memb(_assign)(_cx_self* self, _cx_self* moved) { +STC_INLINE void _cx_MEMB(_assign)(_cx_Self* self, _cx_Self* moved) { if (moved->get == self->get) return; - _cx_memb(_drop)(self); + _cx_MEMB(_drop)(self); *self = *moved; moved->get = NULL; } -#ifndef i_no_cmp -STC_INLINE int _cx_memb(_raw_cmp)(const _cx_raw* rx, const _cx_raw* ry) - { return i_cmp(rx, ry); } +#if defined i_use_cmp + STC_INLINE int _cx_MEMB(_raw_cmp)(const _cx_raw* rx, const _cx_raw* ry) + { return i_cmp(rx, ry); } -STC_INLINE int _cx_memb(_cmp)(const _cx_self* self, const _cx_self* other) { - _cx_raw rx = i_keyto(self->get), ry = i_keyto(other->get); - return i_cmp((&rx), (&ry)); -} -#endif + STC_INLINE int _cx_MEMB(_cmp)(const _cx_Self* self, const _cx_Self* other) { + _cx_raw rx = i_keyto(self->get), ry = i_keyto(other->get); + return i_cmp((&rx), (&ry)); + } -#ifdef _i_eq -STC_INLINE bool _cx_memb(_raw_eq)(const _cx_raw* rx, const _cx_raw* ry) - { return i_eq(rx, ry); } + STC_INLINE bool _cx_MEMB(_raw_eq)(const _cx_raw* rx, const _cx_raw* ry) + { return i_eq(rx, ry); } -STC_INLINE bool _cx_memb(_eq)(const _cx_self* self, const _cx_self* other) { - _cx_raw rx = i_keyto(self->get), ry = i_keyto(other->get); - return i_eq((&rx), (&ry)); -} -#elif !defined i_no_cmp -STC_INLINE bool _cx_memb(_raw_eq)(const _cx_raw* rx, const _cx_raw* ry) - { return i_cmp(rx, ry) == 0; } + STC_INLINE bool _cx_MEMB(_eq)(const _cx_Self* self, const _cx_Self* other) { + _cx_raw rx = i_keyto(self->get), ry = i_keyto(other->get); + return i_eq((&rx), (&ry)); + } -STC_INLINE bool _cx_memb(_eq)(const _cx_self* self, const _cx_self* other) - { return _cx_memb(_cmp)(self, other) == 0; } -#endif + #ifndef i_no_hash + STC_INLINE uint64_t _cx_MEMB(_raw_hash)(const _cx_raw* rx) + { return i_hash(rx); } -#ifndef i_no_hash -STC_INLINE uint64_t _cx_memb(_raw_hash)(const _cx_raw* rx) - { return i_hash(rx); } + STC_INLINE uint64_t _cx_MEMB(_hash)(const _cx_Self* self) + { _cx_raw rx = i_keyto(self->get); return i_hash((&rx)); } + #endif // i_no_hash -STC_INLINE uint64_t _cx_memb(_hash)(const _cx_self* self) - { _cx_raw rx = i_keyto(self->get); return i_hash((&rx)); } -#endif +#else + + STC_INLINE int _cx_MEMB(_cmp)(const _cx_Self* self, const _cx_Self* other) { + return c_default_cmp(&self->get, &other->get); + } + STC_INLINE bool _cx_MEMB(_eq)(const _cx_Self* self, const _cx_Self* other) { + return self->get == other->get; + } + STC_INLINE uint64_t _cx_MEMB(_hash)(const _cx_Self* self) + { return c_default_hash(&self->get); } +#endif // i_use_cmp -#undef _i_eq #include "priv/template2.h" +#undef _i_cbox |
