From f1258489d15a03c01129697ee61777ed598a4adc Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sun, 17 May 2026 17:09:05 +0100 Subject: [PATCH] fx: implement a proper value-type mechanism --- fx/CMakeLists.txt | 2 +- fx/bool.c | 79 +--------- fx/bstr.c | 16 +- fx/cstr.c | 133 ++++++++++++++++ fx/double.c | 280 --------------------------------- fx/encoding.c | 60 +++++++ fx/endian.c | 2 + fx/float/double.c | 18 +++ fx/float/float.c | 18 +++ fx/hash/sha1.c | 29 ++-- fx/include/fx/bstr.h | 4 + fx/include/fx/cstr.h | 35 +++++ fx/include/fx/double.h | 40 ----- fx/include/fx/encoding.h | 6 +- fx/include/fx/endian.h | 20 +-- fx/include/fx/float.h | 25 +++ fx/include/fx/int-primitives.h | 17 ++ fx/include/fx/int.h | 159 ++++++++++++++++--- fx/include/fx/macros.h | 10 +- fx/include/fx/object.h | 1 + fx/include/fx/pointer.h | 19 +++ fx/include/fx/string.h | 16 +- fx/include/fx/stringstream.h | 1 + fx/include/fx/type.h | 1 + fx/include/fx/uint.h | 32 ---- fx/include/fx/value-type.h | 53 +++++++ fx/include/fx/value.h | 114 ++++++++------ fx/int.c | 272 -------------------------------- fx/int/byte.c | 18 +++ fx/int/i16.c | 55 +++++++ fx/int/i32.c | 58 +++++++ fx/int/i64.c | 62 ++++++++ fx/int/int.c | 18 +++ fx/int/iptr.c | 18 +++ fx/int/long.c | 18 +++ fx/int/longlong.c | 18 +++ fx/int/sbyte.c | 18 +++ fx/int/short.c | 19 +++ fx/int/size.c | 18 +++ fx/int/u16.c | 56 +++++++ fx/int/u32.c | 58 +++++++ fx/int/u64.c | 62 ++++++++ fx/int/uint.c | 18 +++ fx/int/ulong.c | 18 +++ fx/int/ulonglong.c | 18 +++ fx/int/uptr.c | 18 +++ fx/int/ushort.c | 18 +++ fx/object.c | 5 + fx/pointer.c | 18 +++ fx/string.c | 82 +--------- fx/stringstream.c | 29 ++-- fx/test/values.c | 10 ++ fx/type.c | 29 ++-- fx/uint.c | 174 -------------------- fx/value-type.c | 30 ++++ fx/value.c | 30 ++-- 56 files changed, 1343 insertions(+), 1112 deletions(-) create mode 100644 fx/cstr.c delete mode 100644 fx/double.c create mode 100644 fx/float/double.c create mode 100644 fx/float/float.c create mode 100644 fx/include/fx/cstr.h delete mode 100644 fx/include/fx/double.h create mode 100644 fx/include/fx/float.h create mode 100644 fx/include/fx/int-primitives.h create mode 100644 fx/include/fx/pointer.h delete mode 100644 fx/include/fx/uint.h create mode 100644 fx/include/fx/value-type.h delete mode 100644 fx/int.c create mode 100644 fx/int/byte.c create mode 100644 fx/int/i16.c create mode 100644 fx/int/i32.c create mode 100644 fx/int/i64.c create mode 100644 fx/int/int.c create mode 100644 fx/int/iptr.c create mode 100644 fx/int/long.c create mode 100644 fx/int/longlong.c create mode 100644 fx/int/sbyte.c create mode 100644 fx/int/short.c create mode 100644 fx/int/size.c create mode 100644 fx/int/u16.c create mode 100644 fx/int/u32.c create mode 100644 fx/int/u64.c create mode 100644 fx/int/uint.c create mode 100644 fx/int/ulong.c create mode 100644 fx/int/ulonglong.c create mode 100644 fx/int/uptr.c create mode 100644 fx/int/ushort.c create mode 100644 fx/pointer.c create mode 100644 fx/test/values.c delete mode 100644 fx/uint.c create mode 100644 fx/value-type.c diff --git a/fx/CMakeLists.txt b/fx/CMakeLists.txt index 7bfdaef..cbcfb61 100644 --- a/fx/CMakeLists.txt +++ b/fx/CMakeLists.txt @@ -1,2 +1,2 @@ -set(source_dirs hash) +set(source_dirs hash int float) export_fx_namespace_details(fx) diff --git a/fx/bool.c b/fx/bool.c index 47ca426..d544c0b 100644 --- a/fx/bool.c +++ b/fx/bool.c @@ -1,84 +1,19 @@ #include -#include - -/*** PRIVATE DATA *************************************************************/ - -struct fx_bool_p { - bool b_value; -}; - -/*** PRIVATE FUNCTIONS ********************************************************/ - -static void bool_set_value(struct fx_bool_p *i, bool v) -{ - i->b_value = v; -} - -static bool bool_get_value(struct fx_bool_p *i) -{ - return i->b_value; -} - -/*** PUBLIC FUNCTIONS *********************************************************/ - -fx_bool *fx_bool_create(bool value) -{ - fx_bool *i = fx_object_create(FX_TYPE_BOOL); - if (!i) { - return NULL; - } - - struct fx_bool_p *p = fx_object_get_private(i, FX_TYPE_BOOL); - p->b_value = value; - - return i; -} - -void fx_bool_set_value(fx_bool *i, bool v) -{ - FX_CLASS_DISPATCH_STATIC_V(FX_TYPE_BOOL, bool_set_value, i, v); -} - -bool fx_bool_get_value(const fx_bool *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_BOOL, bool_get_value, i); -} - -/*** VIRTUAL FUNCTIONS ********************************************************/ - -static void bool_init(fx_object *obj, void *priv) -{ - struct fx_bool_p *i = priv; - - i->b_value = false; -} - -static void bool_fini(fx_object *obj, void *priv) -{ -} - -static void bool_to_string(const fx_object *obj, fx_stream *out) -{ - struct fx_bool_p *i = fx_object_get_private(obj, FX_TYPE_BOOL); - if (i->b_value == false) { - fx_stream_write_cstr(out, "false", NULL); - } else { - fx_stream_write_cstr(out, "true", NULL); - } -} +#include +#include /*** CLASS DEFINITION *********************************************************/ FX_TYPE_CLASS_BEGIN(fx_bool) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) - FX_INTERFACE_ENTRY(to_string) = bool_to_string; + FX_INTERFACE_ENTRY(to_string) = NULL; FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_CLASS_END(fx_bool) FX_TYPE_DEFINITION_BEGIN(fx_bool) - FX_TYPE_ID(0x9c8453bf, 0xfc92, 0x4b0a, 0xbcaf, 0xd0c6cdba9310); + __FX_VALUE_TYPE_ID(BOOL); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.bool"); FX_TYPE_CLASS(fx_bool_class); - FX_TYPE_INSTANCE_PRIVATE(struct fx_bool_p); - FX_TYPE_INSTANCE_INIT(bool_init); - FX_TYPE_INSTANCE_FINI(bool_fini); + FX_TYPE_INSTANCE_PRIVATE(bool); FX_TYPE_DEFINITION_END(fx_bool) diff --git a/fx/bstr.c b/fx/bstr.c index c0a5043..c64ec32 100644 --- a/fx/bstr.c +++ b/fx/bstr.c @@ -244,6 +244,15 @@ enum fx_status fx_bstr_write_cstr( return bstr_puts(str, s, nr_written); } +enum fx_status fx_bstr_write_wstr( + struct fx_bstr *str, + const fx_wchar *s, + size_t *nr_written) +{ + /* TODO */ + return FX_ERR_NOT_SUPPORTED; +} + enum fx_status fx_bstr_write_cstr_list( struct fx_bstr *str, const char **strs, @@ -348,8 +357,11 @@ enum fx_status fx_bstr_write_fmt( { va_list arg; va_start(arg, format); - enum fx_status result - = fx_bstr_write_vfmt(str, nr_written, format, arg); + enum fx_status result = fx_bstr_write_vfmt( + str, + nr_written, + format, + arg); va_end(arg); return result; diff --git a/fx/cstr.c b/fx/cstr.c new file mode 100644 index 0000000..8a5fccd --- /dev/null +++ b/fx/cstr.c @@ -0,0 +1,133 @@ +#include +#include +#include + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_cstr) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_cstr) + +FX_TYPE_DEFINITION_BEGIN(fx_cstr) + __FX_VALUE_TYPE_ID(CSTR); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.cstr"); + FX_TYPE_CLASS(fx_cstr_class); + FX_TYPE_INSTANCE_PRIVATE(char *); +FX_TYPE_DEFINITION_END(fx_cstr) + +/*** MISC FUNCTIONS ***********************************************************/ + +char *fx_strdup(const char *s) +{ + size_t len = strlen(s); + char *p = malloc(len + 1); + if (!p) { + return NULL; + } + + memcpy(p, s, len); + p[len] = '\0'; + + return p; +} + +size_t fx_strlen(const char *s, fx_strlen_flags flags) +{ + if (!(flags & (FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD))) { + return strlen(s); + } + + size_t out = 0; + for (size_t i = 0; s[i]; i++) { + if (s[i] == '\033' && (flags & FX_STRLEN_IGNORE_ESC)) { + while (!isalpha(s[i]) && s[i]) { + i++; + } + + continue; + } + + if (s[i] == '[' && (flags & FX_STRLEN_IGNORE_MOD)) { + i++; + if (s[i] == '[') { + out++; + continue; + } + + while (s[i] != ']' && s[i]) { + i++; + } + + continue; + } + + out++; + } + + return out; +} + +int fx_wstrcmp(const fx_wchar *a, const fx_wchar *b) +{ + int i; + for (i = 0; a[i] == b[i]; i++) + if (a[i] == '\0') + return 0; + + return a[i] - b[i]; +} + +int fx_wastrcmp(const char *a, const fx_wchar *b) +{ + while (1) { + fx_wchar ch_a = fx_wchar_utf8_codepoint_decode(a); + fx_wchar ch_b = *b; + + if (ch_a != ch_b) { + return ch_a - ch_b; + } + + a += fx_wchar_utf8_codepoint_stride(a); + b++; + } + + return 0; +} + +fx_wchar *fx_wstrdup(const fx_wchar *s) +{ + size_t len = fx_wstrlen(s); + fx_wchar *buf = calloc(len + 1, sizeof(fx_wchar)); + if (!buf) { + return NULL; + } + + memcpy(buf, s, len * sizeof(fx_wchar)); + + return buf; +} + +size_t fx_wstrlen(const fx_wchar *s) +{ + size_t len; + for (len = 0; s[len] != 0; len++) + ; + return len; +} + +size_t fx_wstrcspn(const fx_wchar *dest, const fx_wchar *src) +{ + size_t i = 0; + for (i = 0; dest[i]; i++) { + for (size_t ii = 0; src[ii]; ii++) { + if (dest[i] == src[ii]) { + return i; + } + } + } + + return i; +} diff --git a/fx/double.c b/fx/double.c deleted file mode 100644 index cc3c4f1..0000000 --- a/fx/double.c +++ /dev/null @@ -1,280 +0,0 @@ -#include -#include -#include -#include - -/*** PRIVATE DATA *************************************************************/ - -enum double_type { - DOUBLE_REGULAR = 0, - DOUBLE_INF_POSITIVE, - DOUBLE_INF_NEGATIVE, - DOUBLE_NAN_POSITIVE, - DOUBLE_NAN_NEGATIVE, -}; - -struct fx_double_p { - enum double_type i_type; - double i_value; -}; - -/*** PRIVATE FUNCTIONS ********************************************************/ - -static bool double_get_value(const struct fx_double_p *i) -{ - return i->i_value; -} - -static void double_set_value(struct fx_double_p *i, double v) -{ - i->i_type = DOUBLE_REGULAR; - i->i_value = v; -} - -static void double_set_value_nan(struct fx_double_p *i) -{ - i->i_type = DOUBLE_NAN_POSITIVE; - i->i_value = 0; -} - -static void double_set_value_nan_negative(struct fx_double_p *i) -{ - i->i_type = DOUBLE_NAN_NEGATIVE; - i->i_value = 0; -} - -static void double_set_value_inf(struct fx_double_p *i) -{ - i->i_type = DOUBLE_INF_POSITIVE; - i->i_value = 0; -} - -static void double_set_value_inf_negative(struct fx_double_p *i) -{ - i->i_type = DOUBLE_INF_NEGATIVE; - i->i_value = 0; -} - -static bool double_is_nan(struct fx_double_p *i) -{ - return i->i_type == DOUBLE_NAN_POSITIVE - || i->i_type == DOUBLE_NAN_NEGATIVE; -} - -static bool double_is_nan_positive(struct fx_double_p *i) -{ - return i->i_type == DOUBLE_NAN_POSITIVE; -} - -static bool double_is_nan_negative(struct fx_double_p *i) -{ - return i->i_type == DOUBLE_NAN_NEGATIVE; -} - -static bool double_is_inf(struct fx_double_p *i) -{ - return i->i_type == DOUBLE_INF_POSITIVE - || i->i_type == DOUBLE_INF_NEGATIVE; -} - -static bool double_is_inf_positive(struct fx_double_p *i) -{ - return i->i_type == DOUBLE_INF_POSITIVE; -} - -static bool double_is_inf_negative(struct fx_double_p *i) -{ - return i->i_type == DOUBLE_INF_NEGATIVE; -} - -/*** PUBLIC FUNCTIONS *********************************************************/ - -fx_double *fx_double_create(double value) -{ - fx_double *i = fx_object_create(FX_TYPE_DOUBLE); - if (!i) { - return NULL; - } - - struct fx_double_p *p = fx_object_get_private(i, FX_TYPE_DOUBLE); - p->i_type = DOUBLE_REGULAR; - p->i_value = value; - - return i; -} - -fx_double *fx_double_create_nan(void) -{ - fx_double *i = fx_object_create(FX_TYPE_DOUBLE); - if (!i) { - return NULL; - } - - struct fx_double_p *p = fx_object_get_private(i, FX_TYPE_DOUBLE); - p->i_type = DOUBLE_NAN_POSITIVE; - p->i_value = 0; - - return i; -} - -fx_double *fx_double_create_nan_negative(void) -{ - fx_double *i = fx_object_create(FX_TYPE_DOUBLE); - if (!i) { - return NULL; - } - - struct fx_double_p *p = fx_object_get_private(i, FX_TYPE_DOUBLE); - p->i_type = DOUBLE_NAN_NEGATIVE; - p->i_value = 0; - - return i; -} - -fx_double *fx_double_create_inf(void) -{ - fx_double *i = fx_object_create(FX_TYPE_DOUBLE); - if (!i) { - return NULL; - } - - struct fx_double_p *p = fx_object_get_private(i, FX_TYPE_DOUBLE); - p->i_type = DOUBLE_INF_POSITIVE; - p->i_value = 0; - - return i; -} - -fx_double *fx_double_create_inf_negative(void) -{ - fx_double *i = fx_object_create(FX_TYPE_DOUBLE); - if (!i) { - return NULL; - } - - struct fx_double_p *p = fx_object_get_private(i, FX_TYPE_DOUBLE); - p->i_type = DOUBLE_INF_NEGATIVE; - p->i_value = 0; - - return i; -} - -double fx_double_get_value(const fx_double *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DOUBLE, double_get_value, i); -} - -void fx_double_set_value(fx_double *i, double v) -{ - FX_CLASS_DISPATCH_STATIC_V(FX_TYPE_DOUBLE, double_set_value, i, v); -} - -void fx_double_set_value_nan(fx_double *i) -{ - FX_CLASS_DISPATCH_STATIC_V0(FX_TYPE_DOUBLE, double_set_value_nan, i); -} - -void fx_double_set_value_nan_negative(fx_double *i) -{ - FX_CLASS_DISPATCH_STATIC_V0( - FX_TYPE_DOUBLE, - double_set_value_nan_negative, - i); -} - -void fx_double_set_value_inf(fx_double *i) -{ - FX_CLASS_DISPATCH_STATIC_V0(FX_TYPE_DOUBLE, double_set_value_inf, i); -} - -void fx_double_set_value_inf_negative(fx_double *i) -{ - FX_CLASS_DISPATCH_STATIC_V0( - FX_TYPE_DOUBLE, - double_set_value_inf_negative, - i); -} - -bool fx_double_is_nan(const fx_double *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DOUBLE, double_is_nan, i); -} - -bool fx_double_is_nan_positive(const fx_double *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DOUBLE, double_is_nan_positive, i); -} - -bool fx_double_is_nan_negative(const fx_double *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DOUBLE, double_is_nan_negative, i); -} - -bool fx_double_is_inf(const fx_double *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DOUBLE, double_is_inf, i); -} - -bool fx_double_is_inf_positive(const fx_double *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DOUBLE, double_is_inf_positive, i); -} - -bool fx_double_is_inf_negative(const fx_double *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DOUBLE, double_is_inf_negative, i); -} - -/*** VIRTUAL FUNCTIONS ********************************************************/ - -static void double_init(fx_object *obj, void *priv) -{ - struct fx_double_p *i = priv; - - i->i_type = DOUBLE_REGULAR; - i->i_value = 0; -} - -static void double_fini(fx_object *obj, void *priv) -{ -} - -static void double_to_string(const fx_object *obj, fx_stream *out) -{ - struct fx_double_p *i = fx_object_get_private(obj, FX_TYPE_DOUBLE); - switch (i->i_type) { - case DOUBLE_REGULAR: - fx_stream_write_fmt(out, NULL, "%" PRIdPTR, i->i_value); - break; - case DOUBLE_INF_POSITIVE: - fx_stream_write_cstr(out, "inf", NULL); - break; - case DOUBLE_INF_NEGATIVE: - fx_stream_write_cstr(out, "-inf", NULL); - break; - case DOUBLE_NAN_POSITIVE: - fx_stream_write_cstr(out, "NaN", NULL); - break; - case DOUBLE_NAN_NEGATIVE: - fx_stream_write_cstr(out, "-NaN", NULL); - break; - default: - break; - } -} - -/*** CLASS DEFINITION *********************************************************/ - -FX_TYPE_CLASS_BEGIN(fx_double) - FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) - FX_INTERFACE_ENTRY(to_string) = double_to_string; - FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) -FX_TYPE_CLASS_END(fx_double) - -FX_TYPE_DEFINITION_BEGIN(fx_double) - FX_TYPE_ID(0x3b20f57a, 0x2ddf, 0x4682, 0x81c4, 0x4fe404a6524e); - FX_TYPE_CLASS(fx_double_class); - FX_TYPE_INSTANCE_PRIVATE(struct fx_double_p); - FX_TYPE_INSTANCE_INIT(double_init); - FX_TYPE_INSTANCE_FINI(double_fini); -FX_TYPE_DEFINITION_END(fx_double) diff --git a/fx/encoding.c b/fx/encoding.c index 0d3c15e..19d9e7d 100644 --- a/fx/encoding.c +++ b/fx/encoding.c @@ -1177,6 +1177,66 @@ bool fx_wchar_is_alpha(fx_wchar c) } } +static bool wchar_is_control(fx_wchar c) +{ + if (c >= 0x0000 && c <= 0x001F) { + return true; + } + + if (c == 0x007F) { + return true; + } + + if (c >= 0x0080 && c <= 0x009F) { + return true; + } + + switch (c) { + case 0x00AD: + case 0x034F: + case 0x070F: + case 0x180E: + return true; + default: + break; + } + + if (c >= 0x200B && c <= 0x200F) { + return true; + } + + if (c >= 0x202A && c <= 0x202E) { + return true; + } + + if (c >= 0x2060 && c <= 0x2064) { + return true; + } + + if (c >= 0x2066 && c <= 0x206F) { + return true; + } + + if (c == 0xFEFF) { + return true; + } + + if (c >= 0xFFF9 && c <= 0xFFFB) { + return true; + } + + if (c >= 0x13430 && c <= 0x13438) { + return true; + } + + return false; +} + +bool fx_wchar_is_graph(fx_wchar c) +{ + return !wchar_is_control(c); +} + bool fx_wchar_is_hex_digit(fx_wchar c) { return iswxdigit(c); diff --git a/fx/endian.c b/fx/endian.c index a6c6fac..2e60a23 100644 --- a/fx/endian.c +++ b/fx/endian.c @@ -1,4 +1,5 @@ #include +#if 0 fx_i16 fx_i16_htob(uint16_t v) { @@ -211,3 +212,4 @@ uint64_t fx_i64_stoh(fx_i64 v) return x; } +#endif diff --git a/fx/float/double.c b/fx/float/double.c new file mode 100644 index 0000000..5f5f6b2 --- /dev/null +++ b/fx/float/double.c @@ -0,0 +1,18 @@ +#include +#include + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_double) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_double) + +FX_TYPE_DEFINITION_BEGIN(fx_double) + __FX_VALUE_TYPE_ID(DOUBLE); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.double"); + FX_TYPE_CLASS(fx_double_class); + FX_TYPE_INSTANCE_PRIVATE(double); +FX_TYPE_DEFINITION_END(fx_double) diff --git a/fx/float/float.c b/fx/float/float.c new file mode 100644 index 0000000..66bc39e --- /dev/null +++ b/fx/float/float.c @@ -0,0 +1,18 @@ +#include +#include + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_float) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_float) + +FX_TYPE_DEFINITION_BEGIN(fx_float) + __FX_VALUE_TYPE_ID(FLOAT); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.float"); + FX_TYPE_CLASS(fx_float_class); + FX_TYPE_INSTANCE_PRIVATE(float); +FX_TYPE_DEFINITION_END(fx_float) diff --git a/fx/hash/sha1.c b/fx/hash/sha1.c index 2d1a0b6..08cc5c5 100644 --- a/fx/hash/sha1.c +++ b/fx/hash/sha1.c @@ -30,20 +30,20 @@ A million repetitions of "a" /* blk0() and blk() perform the initial expand. */ /* I got the idea of expanding during the round function from SSLeay */ -#if defined(LITTLE_ENDIAN) +#if FX_ENDIAN == 0 #define blk0(i) \ (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) \ | (rol(block->l[i], 8) & 0x00FF00FF)) -#elif defined(BIG_ENDIAN) +#elif FX_ENDIAN == 1 #define blk0(i) block->l[i] #else #error "Endianness not defined!" #endif #define blk(i) \ - (block->l[i & 15] \ - = rol(block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] \ - ^ block->l[(i + 2) & 15] ^ block->l[i & 15], \ - 1)) + (block->l[i & 15] = rol( \ + block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] \ + ^ block->l[(i + 2) & 15] ^ block->l[i & 15], \ + 1)) /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ #define R0(v, w, x, y, z, i) \ @@ -235,11 +235,10 @@ static void sha_finish(struct fx_hash_ctx *context, void *out, size_t max) unsigned char c; for (i = 0; i < 8; i++) { - finalcount[i] - = (unsigned char)((context->ctx_state.sha1 - .count[(i >= 4 ? 0 : 1)] - >> ((3 - (i & 3)) * 8)) - & 255); /* Endian independent */ + finalcount[i] = (unsigned char)((context->ctx_state.sha1.count[( + i >= 4 ? 0 : 1)] + >> ((3 - (i & 3)) * 8)) + & 255); /* Endian independent */ } char digest[FX_DIGEST_LENGTH_SHA1]; @@ -251,10 +250,10 @@ static void sha_finish(struct fx_hash_ctx *context, void *out, size_t max) } sha_update(context, finalcount, 8); /* Should cause a SHA1Transform() */ for (i = 0; i < 20; i++) { - digest[i] - = (unsigned char)((context->ctx_state.sha1.state[i >> 2] - >> ((3 - (i & 3)) * 8)) - & 255); + digest[i] = (unsigned char)((context->ctx_state.sha1 + .state[i >> 2] + >> ((3 - (i & 3)) * 8)) + & 255); } memcpy(out, digest, fx_min(size_t, sizeof digest, max)); diff --git a/fx/include/fx/bstr.h b/fx/include/fx/bstr.h index 05c43bc..b40b264 100644 --- a/fx/include/fx/bstr.h +++ b/fx/include/fx/bstr.h @@ -1,6 +1,7 @@ #ifndef FX_CORE_BSTR_H_ #define FX_CORE_BSTR_H_ +#include #include #include #include @@ -77,6 +78,9 @@ FX_API fx_status fx_bstr_write_vfmt( const char *format, va_list arg); +FX_API fx_status +fx_bstr_write_wstr(fx_bstr *strv, const fx_wchar *str, size_t *nr_written); + FX_API char *fx_bstr_rope(const struct fx_rope *rope, size_t *nr_written); FX_API char *fx_bstr_fmt(size_t *nr_written, const char *format, ...); FX_API char *fx_bstr_vfmt(size_t *nr_written, const char *format, va_list arg); diff --git a/fx/include/fx/cstr.h b/fx/include/fx/cstr.h new file mode 100644 index 0000000..78a67d2 --- /dev/null +++ b/fx/include/fx/cstr.h @@ -0,0 +1,35 @@ +#ifndef FX_CSTR_H_ +#define FX_CSTR_H_ + +#include +#include + +FX_DECLS_BEGIN; + +typedef enum fx_strlen_flags { + FX_STRLEN_NORMAL = 0, + FX_STRLEN_IGNORE_ESC = 0x01u, + FX_STRLEN_IGNORE_MOD = 0x02u, + FX_STRLEN_CODEPOINTS = 0x04u, +} fx_strlen_flags; + +#define FX_TYPE_CSTR (fx_cstr_get_type()) + +FX_DECLARE_TYPE(fx_cstr); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_cstr) +FX_TYPE_CLASS_DECLARATION_END(fx_cstr) + +FX_API fx_type_id fx_cstr_get_type(void); + +FX_API char *fx_strdup(const char *s); +FX_API size_t fx_strlen(const char *s, fx_strlen_flags flags); +FX_API int fx_wstrcmp(const fx_wchar *a, const fx_wchar *b); +FX_API int fx_wastrcmp(const char *a, const fx_wchar *b); +FX_API fx_wchar *fx_wstrdup(const fx_wchar *s); +FX_API size_t fx_wstrlen(const fx_wchar *s); +FX_API size_t fx_wstrcspn(const fx_wchar *dest, const fx_wchar *src); + +FX_DECLS_END; + +#endif diff --git a/fx/include/fx/double.h b/fx/include/fx/double.h deleted file mode 100644 index a373ad3..0000000 --- a/fx/include/fx/double.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef FX_DOUBLE_H_ -#define FX_DOUBLE_H_ - -#include - -FX_DECLS_BEGIN; - -#define FX_TYPE_DOUBLE (fx_double_get_type()) - -FX_DECLARE_TYPE(fx_double); - -FX_TYPE_CLASS_DECLARATION_BEGIN(fx_double) -FX_TYPE_CLASS_DECLARATION_END(fx_double) - -FX_API fx_type_id fx_double_get_type(void); - -FX_API fx_double *fx_double_create(double value); -FX_API fx_double *fx_double_create_nan(void); -FX_API fx_double *fx_double_create_nan_negative(void); -FX_API fx_double *fx_double_create_inf(void); -FX_API fx_double *fx_double_create_inf_negative(void); - -FX_API void fx_double_set_value(fx_double *i, double v); -FX_API void fx_double_set_value_nan(fx_double *i); -FX_API void fx_double_set_value_nan_negative(fx_double *i); -FX_API void fx_double_set_value_inf(fx_double *i); -FX_API void fx_double_set_value_inf_negative(fx_double *i); - -FX_API bool fx_double_is_nan(const fx_double *i); -FX_API bool fx_double_is_nan_positive(const fx_double *i); -FX_API bool fx_double_is_nan_negative(const fx_double *i); -FX_API bool fx_double_is_inf(const fx_double *i); -FX_API bool fx_double_is_inf_positive(const fx_double *i); -FX_API bool fx_double_is_inf_negative(const fx_double *i); - -FX_API double fx_double_get_value(const fx_double *i); - -FX_DECLS_END; - -#endif diff --git a/fx/include/fx/encoding.h b/fx/include/fx/encoding.h index 3b08222..d367f9c 100644 --- a/fx/include/fx/encoding.h +++ b/fx/include/fx/encoding.h @@ -11,6 +11,7 @@ typedef int32_t fx_wchar; FX_API bool fx_wchar_is_alpha(fx_wchar c); FX_API bool fx_wchar_is_number(fx_wchar c); +FX_API bool fx_wchar_is_graph(fx_wchar c); static inline bool fx_wchar_is_bin_digit(fx_wchar c) { return c >= '0' && c <= '1'; @@ -35,8 +36,7 @@ FX_API fx_wchar fx_wchar_utf8_codepoint_decode(const char *s); FX_API unsigned int fx_wchar_utf8_codepoint_encode(fx_wchar c, char s[4]); FX_API unsigned int fx_wchar_utf8_codepoint_stride(const char *s); FX_API size_t fx_wchar_utf8_codepoint_count(const char *s, size_t nr_bytes); -FX_API size_t fx_wchar_utf8_string_encoded_size( - const fx_wchar *s, - size_t nr_codepoints); +FX_API size_t +fx_wchar_utf8_string_encoded_size(const fx_wchar *s, size_t nr_codepoints); #endif diff --git a/fx/include/fx/endian.h b/fx/include/fx/endian.h index efb9bc9..2af7c18 100644 --- a/fx/include/fx/endian.h +++ b/fx/include/fx/endian.h @@ -4,6 +4,7 @@ #include #include +#if 0 typedef struct { union { unsigned char i_bytes[sizeof(uint16_t)]; @@ -27,23 +28,6 @@ typedef struct { uint64_t i_uval; }; } fx_i64; - -FX_API fx_i16 fx_i16_htob(uint16_t v); -FX_API fx_i16 fx_i16_htos(uint16_t v); - -FX_API uint16_t fx_i16_btoh(fx_i16 v); -FX_API uint16_t fx_i16_stoh(fx_i16 v); - -FX_API fx_i32 fx_i32_htob(uint32_t v); -FX_API fx_i32 fx_i32_htos(uint32_t v); - -FX_API uint32_t fx_i32_btoh(fx_i32 v); -FX_API uint32_t fx_i32_stoh(fx_i32 v); - -FX_API fx_i64 fx_i64_htob(uint64_t v); -FX_API fx_i64 fx_i64_htos(uint64_t v); - -FX_API uint64_t fx_i64_btoh(fx_i64 v); -FX_API uint64_t fx_i64_stoh(fx_i64 v); +#endif #endif diff --git a/fx/include/fx/float.h b/fx/include/fx/float.h new file mode 100644 index 0000000..e7b5c5b --- /dev/null +++ b/fx/include/fx/float.h @@ -0,0 +1,25 @@ +#ifndef FX_DOUBLE_H_ +#define FX_DOUBLE_H_ + +#include + +FX_DECLS_BEGIN; + +#define FX_TYPE_FLOAT (fx_float_get_type()) +#define FX_TYPE_DOUBLE (fx_double_get_type()) + +FX_DECLARE_TYPE(fx_float); +FX_DECLARE_TYPE(fx_double); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_float) +FX_TYPE_CLASS_DECLARATION_END(fx_float) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_double) +FX_TYPE_CLASS_DECLARATION_END(fx_double) + +FX_API fx_type_id fx_float_get_type(void); +FX_API fx_type_id fx_double_get_type(void); + +FX_DECLS_END; + +#endif diff --git a/fx/include/fx/int-primitives.h b/fx/include/fx/int-primitives.h new file mode 100644 index 0000000..f115f59 --- /dev/null +++ b/fx/include/fx/int-primitives.h @@ -0,0 +1,17 @@ +#ifndef FX_INT_PRIMITIVES_H_ +#define FX_INT_PRIMITIVES_H_ + +#include + +typedef int8_t sbyte; +typedef uint8_t byte; +typedef int16_t i16; +typedef uint16_t u16; +typedef int32_t i32; +typedef uint32_t u32; +typedef int64_t i64; +typedef uint64_t u64; +typedef intptr_t iptr; +typedef uintptr_t uptr; + +#endif diff --git a/fx/include/fx/int.h b/fx/include/fx/int.h index c2a42a0..d1fc1de 100644 --- a/fx/include/fx/int.h +++ b/fx/include/fx/int.h @@ -1,39 +1,162 @@ #ifndef FX_INT_H_ #define FX_INT_H_ +#include #include +#include +#include FX_DECLS_BEGIN; -#define FX_TYPE_INT (fx_int_get_type()) +#define FX_TYPE_I16 (fx_i16_get_type()) +#define FX_TYPE_U16 (fx_u16_get_type()) +#define FX_TYPE_I32 (fx_i32_get_type()) +#define FX_TYPE_U32 (fx_u32_get_type()) +#define FX_TYPE_I64 (fx_i64_get_type()) +#define FX_TYPE_U64 (fx_u64_get_type()) +#define FX_TYPE_IPTR (fx_iptr_get_type()) +#define FX_TYPE_UPTR (fx_uptr_get_type()) +#define FX_TYPE_SBYTE (fx_sbyte_get_type()) +#define FX_TYPE_BYTE (fx_byte_get_type()) +#define FX_TYPE_SHORT (fx_short_get_type()) +#define FX_TYPE_USHORT (fx_ushort_get_type()) +#define FX_TYPE_INT (fx_int_get_type()) +#define FX_TYPE_UINT (fx_uint_get_type()) +#define FX_TYPE_LONG (fx_long_get_type()) +#define FX_TYPE_ULONG (fx_ulong_get_type()) +#define FX_TYPE_LONGLONG (fx_longlong_get_type()) +#define FX_TYPE_ULONGLONG (fx_ulonglong_get_type()) +#define FX_TYPE_SIZE (fx_size_get_type()) + +FX_DECLARE_TYPE(fx_i16); +FX_DECLARE_TYPE(fx_u16); +FX_DECLARE_TYPE(fx_i32); +FX_DECLARE_TYPE(fx_u32); +FX_DECLARE_TYPE(fx_i64); +FX_DECLARE_TYPE(fx_u64); +FX_DECLARE_TYPE(fx_iptr); +FX_DECLARE_TYPE(fx_uptr); + +FX_DECLARE_TYPE(fx_sbyte); +FX_DECLARE_TYPE(fx_byte); +FX_DECLARE_TYPE(fx_short); +FX_DECLARE_TYPE(fx_ushort); FX_DECLARE_TYPE(fx_int); +FX_DECLARE_TYPE(fx_uint); +FX_DECLARE_TYPE(fx_long); +FX_DECLARE_TYPE(fx_ulong); +FX_DECLARE_TYPE(fx_longlong); +FX_DECLARE_TYPE(fx_ulonglong); +FX_DECLARE_TYPE(fx_size); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_i16) +FX_TYPE_CLASS_DECLARATION_END(fx_i16) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_u16) +FX_TYPE_CLASS_DECLARATION_END(fx_u16) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_i32) +FX_TYPE_CLASS_DECLARATION_END(fx_i32) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_u32) +FX_TYPE_CLASS_DECLARATION_END(fx_u32) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_i64) +FX_TYPE_CLASS_DECLARATION_END(fx_i64) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_u64) +FX_TYPE_CLASS_DECLARATION_END(fx_u64) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iptr) +FX_TYPE_CLASS_DECLARATION_END(fx_iptr) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_uptr) +FX_TYPE_CLASS_DECLARATION_END(fx_uptr) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_sbyte) +FX_TYPE_CLASS_DECLARATION_END(fx_sbyte) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_byte) +FX_TYPE_CLASS_DECLARATION_END(fx_byte) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_short) +FX_TYPE_CLASS_DECLARATION_END(fx_short) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_ushort) +FX_TYPE_CLASS_DECLARATION_END(fx_ushort) FX_TYPE_CLASS_DECLARATION_BEGIN(fx_int) FX_TYPE_CLASS_DECLARATION_END(fx_int) +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_uint) +FX_TYPE_CLASS_DECLARATION_END(fx_uint) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_long) +FX_TYPE_CLASS_DECLARATION_END(fx_long) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_ulong) +FX_TYPE_CLASS_DECLARATION_END(fx_ulong) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_longlong) +FX_TYPE_CLASS_DECLARATION_END(fx_longlong) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_ulonglong) +FX_TYPE_CLASS_DECLARATION_END(fx_ulonglong) + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_size) +FX_TYPE_CLASS_DECLARATION_END(fx_size) + +FX_API fx_type_id fx_i16_get_type(void); +FX_API fx_type_id fx_u16_get_type(void); +FX_API fx_type_id fx_i32_get_type(void); +FX_API fx_type_id fx_u32_get_type(void); +FX_API fx_type_id fx_i64_get_type(void); +FX_API fx_type_id fx_u64_get_type(void); +FX_API fx_type_id fx_iptr_get_type(void); +FX_API fx_type_id fx_uptr_get_type(void); + +FX_API fx_type_id fx_sbyte_get_type(void); +FX_API fx_type_id fx_byte_get_type(void); +FX_API fx_type_id fx_short_get_type(void); +FX_API fx_type_id fx_ushort_get_type(void); FX_API fx_type_id fx_int_get_type(void); +FX_API fx_type_id fx_uint_get_type(void); +FX_API fx_type_id fx_long_get_type(void); +FX_API fx_type_id fx_ulong_get_type(void); +FX_API fx_type_id fx_longlong_get_type(void); +FX_API fx_type_id fx_ulonglong_get_type(void); +FX_API fx_type_id fx_size_get_type(void); -FX_API fx_int *fx_int_create(intptr_t value); -FX_API fx_int *fx_int_create_nan(void); -FX_API fx_int *fx_int_create_nan_negative(void); -FX_API fx_int *fx_int_create_inf(void); -FX_API fx_int *fx_int_create_inf_negative(void); +FX_API i16 fx_i16_htob(i16 v); +FX_API i16 fx_i16_htos(i16 v); +FX_API i16 fx_i16_btoh(i16 v); +FX_API i16 fx_i16_stoh(i16 v); -FX_API void fx_int_set_value(fx_int *i, intptr_t v); -FX_API void fx_int_set_value_nan(fx_int *i); -FX_API void fx_int_set_value_nan_negative(fx_int *i); -FX_API void fx_int_set_value_inf(fx_int *i); -FX_API void fx_int_set_value_inf_negative(fx_int *i); +FX_API u16 fx_u16_htob(u16 v); +FX_API u16 fx_u16_htos(u16 v); +FX_API u16 fx_u16_btoh(u16 v); +FX_API u16 fx_u16_stoh(u16 v); -FX_API bool fx_int_is_nan(const fx_int *i); -FX_API bool fx_int_is_nan_positive(const fx_int *i); -FX_API bool fx_int_is_nan_negative(const fx_int *i); -FX_API bool fx_int_is_inf(const fx_int *i); -FX_API bool fx_int_is_inf_positive(const fx_int *i); -FX_API bool fx_int_is_inf_negative(const fx_int *i); +FX_API i32 fx_i32_htob(i32 v); +FX_API i32 fx_i32_htos(i32 v); +FX_API i32 fx_i32_btoh(i32 v); +FX_API i32 fx_i32_stoh(i32 v); -FX_API intptr_t fx_int_get_value(const fx_int *i); +FX_API u32 fx_u32_htob(u32 v); +FX_API u32 fx_u32_htos(u32 v); +FX_API u32 fx_u32_btoh(u32 v); +FX_API u32 fx_u32_stoh(u32 v); + +FX_API i64 fx_i64_htob(i64 v); +FX_API i64 fx_i64_htos(i64 v); +FX_API i64 fx_i64_btoh(i64 v); +FX_API i64 fx_i64_stoh(i64 v); + +FX_API u64 fx_u64_htob(u64 v); +FX_API u64 fx_u64_htos(u64 v); +FX_API u64 fx_u64_btoh(u64 v); +FX_API u64 fx_u64_stoh(u64 v); FX_DECLS_END; diff --git a/fx/include/fx/macros.h b/fx/include/fx/macros.h index 8f44d27..6b27a53 100644 --- a/fx/include/fx/macros.h +++ b/fx/include/fx/macros.h @@ -5,7 +5,6 @@ #include #include #include -#include #include #define __FX_IFACE_I0(p, x) p##x @@ -28,7 +27,7 @@ #define FX_TYPE_CONSTRUCTOR(name, impl, flags, ...) \ do { \ if (ty) { \ - fx_value_type args[] = {__VA_ARGS__}; \ + fx_type_id args[] = {__VA_ARGS__}; \ fx_function *func = fx_function_create( \ name, \ FX_FUNCTION_F_CONSTRUCTOR \ @@ -36,14 +35,14 @@ (fx_function_impl)impl, \ args, \ sizeof args / sizeof args[0], \ - FX_VALUE_TYPE_POINTER); \ + FX_TYPE_OBJECT); \ fx_type_add_function(ty, func); \ } \ } while (0) #define FX_TYPE_METHOD(return_type, name, impl, flags, ...) \ do { \ if (ty) { \ - fx_value_type args[] = {__VA_ARGS__}; \ + fx_type_id args[] = {__VA_ARGS__}; \ fx_function *func = fx_function_create( \ name, \ (flags), \ @@ -117,6 +116,9 @@ #define FX_TYPE_ID(a, b, c, d, e) \ fx_type_id_init(&type_info->ty_id, a, b, c, d, e) +#define __FX_VALUE_TYPE_ID(ty_name) \ + type_info->ty_id.a.p00 = __FX_VALUE_TYPE_BASEID; \ + type_info->ty_id.a.p01 = __FX_VALUE_TYPE_##ty_name #define FX_TYPE_NAME(n) type_info->ty_name = (n) #define FX_TYPE_EXTENDS(parent_id) \ fx_type_id_copy(parent_id, &type_info->ty_parent_id) diff --git a/fx/include/fx/object.h b/fx/include/fx/object.h index 5c0994a..cbbc69c 100644 --- a/fx/include/fx/object.h +++ b/fx/include/fx/object.h @@ -20,6 +20,7 @@ typedef struct _fx_object_class { } fx_object_class; FX_API fx_type_id fx_object_get_type(void); +FX_API fx_type_id fx_object_query_type(const fx_object *obj); FX_API void *fx_object_get_private(const fx_object *object, fx_type_id type); FX_API void *fx_object_get_protected(const fx_object *object, fx_type_id type); diff --git a/fx/include/fx/pointer.h b/fx/include/fx/pointer.h new file mode 100644 index 0000000..2df6065 --- /dev/null +++ b/fx/include/fx/pointer.h @@ -0,0 +1,19 @@ +#ifndef FX_POINTER_H_ +#define FX_POINTER_H_ + +#include + +FX_DECLS_BEGIN; + +#define FX_TYPE_POINTER (fx_pointer_get_type()) + +FX_DECLARE_TYPE(fx_pointer); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_pointer) +FX_TYPE_CLASS_DECLARATION_END(fx_pointer) + +FX_API fx_type_id fx_pointer_get_type(void); + +FX_DECLS_END; + +#endif diff --git a/fx/include/fx/string.h b/fx/include/fx/string.h index 3869519..1873513 100644 --- a/fx/include/fx/string.h +++ b/fx/include/fx/string.h @@ -2,6 +2,7 @@ #define FX_STRING_H_ #include +#include #include #include #include @@ -25,16 +26,6 @@ FX_TYPE_CLASS_DECLARATION_END(fx_string) FX_TYPE_CLASS_DECLARATION_BEGIN(fx_string_iterator) FX_TYPE_CLASS_DECLARATION_END(fx_string_iterator) -#define FX_CSTR(s) (fx_string_create_from_cstr(s)) -#define FX_RV_CSTR(s) (FX_RV(fx_string_create_from_cstr(s))) - -typedef enum fx_strlen_flags { - FX_STRLEN_NORMAL = 0, - FX_STRLEN_IGNORE_ESC = 0x01u, - FX_STRLEN_IGNORE_MOD = 0x02u, - FX_STRLEN_CODEPOINTS = 0x04u, -} fx_strlen_flags; - typedef enum fx_string_tokenise_flags { FX_STRING_TOK_F_NORMAL = 0x00u, FX_STRING_TOK_F_INCLUDE_EMPTY_TOKENS = 0x01u, @@ -132,11 +123,6 @@ FX_API bool fx_string_iterator_next(fx_string_iterator *it); // FX_API fx_status fx_string_iterator_erase(fx_string_iterator *it); FX_API bool fx_string_iterator_is_valid(const fx_string_iterator *it); -FX_API char *fx_strdup(const char *s); -FX_API size_t fx_strlen(const char *s, fx_strlen_flags flags); -FX_API fx_wchar *fx_wstrdup(const fx_wchar *s); -FX_API size_t fx_wstrlen(const fx_wchar *s); - FX_API uint64_t fx_string_hash(const fx_string *s); FX_DECLS_END; diff --git a/fx/include/fx/stringstream.h b/fx/include/fx/stringstream.h index 6cbefe0..c35ad37 100644 --- a/fx/include/fx/stringstream.h +++ b/fx/include/fx/stringstream.h @@ -31,6 +31,7 @@ FX_API const char *fx_stringstream_ptr(const fx_stringstream *strv); FX_API char *fx_stringstream_steal(fx_stringstream *strv); FX_API size_t fx_stringstream_get_length(const fx_stringstream *strv); +FX_API size_t fx_stringstream_get_available(const fx_stringstream *strv); FX_DECLS_END; diff --git a/fx/include/fx/type.h b/fx/include/fx/type.h index a533e02..54346ad 100644 --- a/fx/include/fx/type.h +++ b/fx/include/fx/type.h @@ -10,6 +10,7 @@ #include #include +#define FX_TYPE_VOID ((fx_type_id)NULL) #define FX_TYPE_MAX_INTERFACES 64 struct _fx_class; diff --git a/fx/include/fx/uint.h b/fx/include/fx/uint.h deleted file mode 100644 index ace4553..0000000 --- a/fx/include/fx/uint.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef FX_UINT_H_ -#define FX_UINT_H_ - -#include - -FX_DECLS_BEGIN; - -#define FX_TYPE_UINT (fx_uint_get_type()) - -FX_DECLARE_TYPE(fx_uint); - -FX_TYPE_CLASS_DECLARATION_BEGIN(fx_uint) -FX_TYPE_CLASS_DECLARATION_END(fx_uint) - -FX_API fx_type_id fx_uint_get_type(void); - -FX_API fx_uint *fx_uint_create(uintptr_t value); -FX_API fx_uint *fx_uint_create_nan(void); -FX_API fx_uint *fx_uint_create_inf(void); - -FX_API void fx_uint_set_value(fx_uint *i, uintptr_t v); -FX_API void fx_uint_set_value_nan(fx_uint *i); -FX_API void fx_uint_set_value_inf(fx_uint *i); - -FX_API bool fx_uint_is_nan(const fx_uint *i); -FX_API bool fx_uint_is_inf(const fx_uint *i); - -FX_API uintptr_t fx_uint_get_value(const fx_uint *i); - -FX_DECLS_END; - -#endif diff --git a/fx/include/fx/value-type.h b/fx/include/fx/value-type.h new file mode 100644 index 0000000..17895d2 --- /dev/null +++ b/fx/include/fx/value-type.h @@ -0,0 +1,53 @@ +#ifndef FX_VALUE_TYPE_H_ +#define FX_VALUE_TYPE_H_ + +#include + +FX_DECLS_BEGIN; + +#define __FX_VALUE_TYPE_BASEID 0xe58f43c1c468899c + +enum { + __FX_VALUE_TYPE_BOOL = 1, + __FX_VALUE_TYPE_I16, + __FX_VALUE_TYPE_U16, + __FX_VALUE_TYPE_I32, + __FX_VALUE_TYPE_U32, + __FX_VALUE_TYPE_I64, + __FX_VALUE_TYPE_U64, + __FX_VALUE_TYPE_IPTR, + __FX_VALUE_TYPE_UPTR, + + __FX_VALUE_TYPE_SBYTE, + __FX_VALUE_TYPE_BYTE, + __FX_VALUE_TYPE_SHORT, + __FX_VALUE_TYPE_USHORT, + __FX_VALUE_TYPE_INT, + __FX_VALUE_TYPE_UINT, + __FX_VALUE_TYPE_LONG, + __FX_VALUE_TYPE_ULONG, + __FX_VALUE_TYPE_LONGLONG, + __FX_VALUE_TYPE_ULONGLONG, + __FX_VALUE_TYPE_SIZE, + + __FX_VALUE_TYPE_FLOAT, + __FX_VALUE_TYPE_DOUBLE, + + __FX_VALUE_TYPE_CSTR, + __FX_VALUE_TYPE_POINTER, +}; + +#define FX_TYPE_VALUE_TYPE (fx_value_type_get_type()) + +FX_DECLARE_TYPE(fx_value_type); + +FX_TYPE_CLASS_DECLARATION_BEGIN(fx_value_type) +FX_TYPE_CLASS_DECLARATION_END(fx_value_type) + +FX_DECLS_END; + +FX_API fx_type_id fx_value_type_get_type(void); +FX_API bool fx_type_is_value_type(fx_type_id ty); +FX_API unsigned int __fx_type_get_value_type(fx_type_id ty); + +#endif diff --git a/fx/include/fx/value.h b/fx/include/fx/value.h index 7d24873..210617e 100644 --- a/fx/include/fx/value.h +++ b/fx/include/fx/value.h @@ -1,66 +1,84 @@ #ifndef FX_VALUE_H_ #define FX_VALUE_H_ +#include +#include +#include +#include #include +#include #include #include #define FX_VALUE_EMPTY ((fx_value) {}) -#define FX_VALUE_BOOL(v) \ +#define __FX_VALUE_CREATE(type, member, value) \ ((fx_value) { \ - .v_type.t_primitive = FX_VALUE_TYPE_BOOL, \ - .v_bool = (v), \ - }) -#define FX_VALUE_INT(v) \ - ((fx_value) { \ - .v_type.t_primitive = FX_VALUE_TYPE_INT, \ - .v_int = (v), \ - }) -#define FX_VALUE_UINT(v) \ - ((fx_value) { \ - .v_type.t_primitive = FX_VALUE_TYPE_UINT, \ - .v_uint = (v), \ - }) -#define FX_VALUE_DOUBLE(v) \ - ((fx_value) { \ - .v_type.t_primitive = FX_VALUE_TYPE_DOUBLE, \ - .v_double = (v), \ - }) -#define FX_VALUE_CSTR(v) \ - ((fx_value) { \ - .v_type.t_primitive = FX_VALUE_TYPE_CSTR, \ - .v_cstr = (v), \ - }) -#define FX_VALUE_POINTER(v) \ - ((fx_value) { \ - .v_type.t_primitive = FX_VALUE_TYPE_POINTER, \ - .v_pointer = (v), \ + .v_type = FX_TYPE_##type, \ + .member = (value), \ }) +#define FX_BOOL(v) __FX_VALUE_CREATE(BOOL, v_bool, v) +#define FX_I16(v) __FX_VALUE_CREATE(I16, v_i16, v) +#define FX_U16(v) __FX_VALUE_CREATE(U16, v_u16, v) +#define FX_I32(v) __FX_VALUE_CREATE(I32, v_i32, v) +#define FX_U32(v) __FX_VALUE_CREATE(U32, v_u32, v) +#define FX_I64(v) __FX_VALUE_CREATE(I64, v_i64, v) +#define FX_U64(v) __FX_VALUE_CREATE(U64, v_u64, v) +#define FX_IPTR(v) __FX_VALUE_CREATE(IPTR, v_iptr, v) +#define FX_UPTR(v) __FX_VALUE_CREATE(UPTR, v_uptr, v) -typedef enum fx_value_type { - FX_VALUE_TYPE_NONE = 0, - FX_VALUE_TYPE_BOOL, - FX_VALUE_TYPE_INT, - FX_VALUE_TYPE_UINT, - FX_VALUE_TYPE_DOUBLE, - FX_VALUE_TYPE_CSTR, - FX_VALUE_TYPE_POINTER, +#define FX_SBYTE(v) __FX_VALUE_CREATE(SBYTE, v_sbyte, v) +#define FX_BYTE(v) __FX_VALUE_CREATE(BYTE, v_byte, v) +#define FX_SHORT(v) __FX_VALUE_CREATE(SHORT, v_short, v) +#define FX_USHORT(v) __FX_VALUE_CREATE(USHORT, v_ushort, v) +#define FX_INT(v) __FX_VALUE_CREATE(INT, v_int, v) +#define FX_UINT(v) __FX_VALUE_CREATE(UINT, v_uint, v) +#define FX_LONG(v) __FX_VALUE_CREATE(LONG, v_long, v) +#define FX_ULONG(v) __FX_VALUE_CREATE(ULONG, v_ulong, v) +#define FX_LONGLONG(v) __FX_VALUE_CREATE(LONGLONG, v_int, v) +#define FX_ULONGLONG(v) __FX_VALUE_CREATE(ULONGLONG, v_uint, v) +#define FX_SIZE(v) __FX_VALUE_CREATE(SIZE, v_size, v) - /* any value greater than this represents an object fx_type_id */ - __FX_VALUE_TYPE_OBJECT_BOUNDARY = 1024, -} fx_value_type; +#define FX_FLOAT(v) __FX_VALUE_CREATE(FLOAT, v_float, v) +#define FX_DOUBLE(v) __FX_VALUE_CREATE(DOUBLE, v_double, v) + +#define FX_CSTR(v) __FX_VALUE_CREATE(CSTR, v_cstr, v) +#define FX_POINTER(v) __FX_VALUE_CREATE(POINTER, v_pointer, v) +#define FX_VALUE_OBJECT(v) \ + ((fx_value) { \ + .v_type = fx_object_query_type((fx_object *)v), \ + .v_object = (fx_object *)(v), \ + }) typedef struct fx_value { - union { - fx_value_type t_primitive; - fx_type_id t_object; - } v_type; + fx_type_id v_type; union { bool v_bool; - intptr_t v_int; - uintptr_t v_uint; + + i16 v_i16; + u16 v_u16; + i32 v_i32; + u32 v_u32; + i64 v_i64; + u64 v_u64; + iptr v_iptr; + uptr v_uptr; + + sbyte v_sbyte; + byte v_byte; + short v_short; + unsigned short v_ushort; + int v_int; + unsigned int v_uint; + long v_long; + unsigned long v_ulong; + long long v_longlong; + unsigned long long v_ulonglong; + size_t v_size; + + float v_float; double v_double; + const char *v_cstr; void *v_pointer; fx_object *v_object; @@ -68,11 +86,15 @@ typedef struct fx_value { } fx_value; FX_API void fx_value_init(fx_value *v, fx_type_id type); -FX_API void fx_value_init_primitive(fx_value *v, fx_value_type type); FX_API void fx_value_copy(fx_value *dst, fx_value *src); FX_API void fx_value_copy_array(fx_value *dst, fx_value *src, size_t count); +static inline bool fx_value_is_type(const fx_value *value, fx_type_id ty) +{ + return fx_type_id_compare(value->v_type, ty) == 0; +} + FX_API void fx_value_unset(fx_value *v); FX_API void fx_value_unset_array(fx_value *v, size_t length); diff --git a/fx/int.c b/fx/int.c deleted file mode 100644 index 5b63c68..0000000 --- a/fx/int.c +++ /dev/null @@ -1,272 +0,0 @@ -#include -#include -#include -#include - -/*** PRIVATE DATA *************************************************************/ - -enum int_type { - INT_REGULAR = 0, - INT_INF_POSITIVE, - INT_INF_NEGATIVE, - INT_NAN_POSITIVE, - INT_NAN_NEGATIVE, -}; - -struct fx_int_p { - enum int_type i_type; - intptr_t i_value; -}; - -/*** PRIVATE FUNCTIONS ********************************************************/ - -static bool int_get_value(const struct fx_int_p *i) -{ - return i->i_value; -} - -static void int_set_value(struct fx_int_p *i, intptr_t v) -{ - i->i_type = INT_REGULAR; - i->i_value = v; -} - -static void int_set_value_nan(struct fx_int_p *i) -{ - i->i_type = INT_NAN_POSITIVE; - i->i_value = 0; -} - -static void int_set_value_nan_negative(struct fx_int_p *i) -{ - i->i_type = INT_NAN_NEGATIVE; - i->i_value = 0; -} - -static void int_set_value_inf(struct fx_int_p *i) -{ - i->i_type = INT_INF_POSITIVE; - i->i_value = 0; -} - -static void int_set_value_inf_negative(struct fx_int_p *i) -{ - i->i_type = INT_INF_NEGATIVE; - i->i_value = 0; -} - -static bool int_is_nan(struct fx_int_p *i) -{ - return i->i_type == INT_NAN_POSITIVE || i->i_type == INT_NAN_NEGATIVE; -} - -static bool int_is_nan_positive(struct fx_int_p *i) -{ - return i->i_type == INT_NAN_POSITIVE; -} - -static bool int_is_nan_negative(struct fx_int_p *i) -{ - return i->i_type == INT_NAN_NEGATIVE; -} - -static bool int_is_inf(struct fx_int_p *i) -{ - return i->i_type == INT_INF_POSITIVE || i->i_type == INT_INF_NEGATIVE; -} - -static bool int_is_inf_positive(struct fx_int_p *i) -{ - return i->i_type == INT_INF_POSITIVE; -} - -static bool int_is_inf_negative(struct fx_int_p *i) -{ - return i->i_type == INT_INF_NEGATIVE; -} - -/*** PUBLIC FUNCTIONS *********************************************************/ - -fx_int *fx_int_create(intptr_t value) -{ - fx_int *i = fx_object_create(FX_TYPE_INT); - if (!i) { - return NULL; - } - - struct fx_int_p *p = fx_object_get_private(i, FX_TYPE_INT); - p->i_type = INT_REGULAR; - p->i_value = value; - - return i; -} - -fx_int *fx_int_create_nan(void) -{ - fx_int *i = fx_object_create(FX_TYPE_INT); - if (!i) { - return NULL; - } - - struct fx_int_p *p = fx_object_get_private(i, FX_TYPE_INT); - p->i_type = INT_NAN_POSITIVE; - p->i_value = 0; - - return i; -} - -fx_int *fx_int_create_nan_negative(void) -{ - fx_int *i = fx_object_create(FX_TYPE_INT); - if (!i) { - return NULL; - } - - struct fx_int_p *p = fx_object_get_private(i, FX_TYPE_INT); - p->i_type = INT_NAN_NEGATIVE; - p->i_value = 0; - - return i; -} - -fx_int *fx_int_create_inf(void) -{ - fx_int *i = fx_object_create(FX_TYPE_INT); - if (!i) { - return NULL; - } - - struct fx_int_p *p = fx_object_get_private(i, FX_TYPE_INT); - p->i_type = INT_INF_POSITIVE; - p->i_value = 0; - - return i; -} - -fx_int *fx_int_create_inf_negative(void) -{ - fx_int *i = fx_object_create(FX_TYPE_INT); - if (!i) { - return NULL; - } - - struct fx_int_p *p = fx_object_get_private(i, FX_TYPE_INT); - p->i_type = INT_INF_NEGATIVE; - p->i_value = 0; - - return i; -} - -intptr_t fx_int_get_value(const fx_int *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_INT, int_get_value, i); -} - -void fx_int_set_value(fx_int *i, intptr_t v) -{ - FX_CLASS_DISPATCH_STATIC_V(FX_TYPE_INT, int_set_value, i, v); -} - -void fx_int_set_value_nan(fx_int *i) -{ - FX_CLASS_DISPATCH_STATIC_V0(FX_TYPE_INT, int_set_value_nan, i); -} - -void fx_int_set_value_nan_negative(fx_int *i) -{ - FX_CLASS_DISPATCH_STATIC_V0(FX_TYPE_INT, int_set_value_nan_negative, i); -} - -void fx_int_set_value_inf(fx_int *i) -{ - FX_CLASS_DISPATCH_STATIC_V0(FX_TYPE_INT, int_set_value_inf, i); -} - -void fx_int_set_value_inf_negative(fx_int *i) -{ - FX_CLASS_DISPATCH_STATIC_V0(FX_TYPE_INT, int_set_value_inf_negative, i); -} - -bool fx_int_is_nan(const fx_int *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_INT, int_is_nan, i); -} - -bool fx_int_is_nan_positive(const fx_int *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_INT, int_is_nan_positive, i); -} - -bool fx_int_is_nan_negative(const fx_int *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_INT, int_is_nan_negative, i); -} - -bool fx_int_is_inf(const fx_int *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_INT, int_is_inf, i); -} - -bool fx_int_is_inf_positive(const fx_int *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_INT, int_is_inf_positive, i); -} - -bool fx_int_is_inf_negative(const fx_int *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_INT, int_is_inf_negative, i); -} - -/*** VIRTUAL FUNCTIONS ********************************************************/ - -static void int_init(fx_object *obj, void *priv) -{ - struct fx_int_p *i = priv; - - i->i_type = INT_REGULAR; - i->i_value = 0; -} - -static void int_fini(fx_object *obj, void *priv) -{ -} - -static void int_to_string(const fx_object *obj, fx_stream *out) -{ - struct fx_int_p *i = fx_object_get_private(obj, FX_TYPE_INT); - switch (i->i_type) { - case INT_REGULAR: - fx_stream_write_fmt(out, NULL, "%" PRIdPTR, i->i_value); - break; - case INT_INF_POSITIVE: - fx_stream_write_cstr(out, "inf", NULL); - break; - case INT_INF_NEGATIVE: - fx_stream_write_cstr(out, "-inf", NULL); - break; - case INT_NAN_POSITIVE: - fx_stream_write_cstr(out, "NaN", NULL); - break; - case INT_NAN_NEGATIVE: - fx_stream_write_cstr(out, "-NaN", NULL); - break; - default: - break; - } -} - -/*** CLASS DEFINITION *********************************************************/ - -FX_TYPE_CLASS_BEGIN(fx_int) - FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) - FX_INTERFACE_ENTRY(to_string) = int_to_string; - FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) -FX_TYPE_CLASS_END(fx_int) - -FX_TYPE_DEFINITION_BEGIN(fx_int) - FX_TYPE_ID(0x3b20f57a, 0x2ddf, 0x4682, 0x81c4, 0x4fe404a6524e); - FX_TYPE_CLASS(fx_int_class); - FX_TYPE_INSTANCE_PRIVATE(struct fx_int_p); - FX_TYPE_INSTANCE_INIT(int_init); - FX_TYPE_INSTANCE_FINI(int_fini); -FX_TYPE_DEFINITION_END(fx_int) diff --git a/fx/int/byte.c b/fx/int/byte.c new file mode 100644 index 0000000..2d3232f --- /dev/null +++ b/fx/int/byte.c @@ -0,0 +1,18 @@ +#include +#include + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_byte) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_byte) + +FX_TYPE_DEFINITION_BEGIN(fx_byte) + __FX_VALUE_TYPE_ID(BYTE); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.byte"); + FX_TYPE_CLASS(fx_byte_class); + FX_TYPE_INSTANCE_PRIVATE(byte); +FX_TYPE_DEFINITION_END(fx_byte) diff --git a/fx/int/i16.c b/fx/int/i16.c new file mode 100644 index 0000000..608170e --- /dev/null +++ b/fx/int/i16.c @@ -0,0 +1,55 @@ +#include +#include + +#define SWAP16(v) (((v << 8) & 0xFF00) | ((v >> 8) & 0x00FF)) + +i16 fx_i16_htob(i16 v) +{ +#if FX_ENDIAN == 1 + return v; +#else + return SWAP16(v); +#endif +} + +i16 fx_i16_htos(i16 v) +{ +#if FX_ENDIAN == 0 + return v; +#else + return SWAP16(v); +#endif +} + +i16 fx_i16_btoh(i16 v) +{ +#if FX_ENDIAN == 1 + return v; +#else + return SWAP16(v); +#endif +} + +i16 fx_i16_stoh(i16 v) +{ +#if FX_ENDIAN == 0 + return v; +#else + return SWAP16(v); +#endif +} +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_i16) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_i16) + +FX_TYPE_DEFINITION_BEGIN(fx_i16) + __FX_VALUE_TYPE_ID(I16); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.i16"); + FX_TYPE_CLASS(fx_i16_class); + FX_TYPE_INSTANCE_PRIVATE(i16); +FX_TYPE_DEFINITION_END(fx_i16) diff --git a/fx/int/i32.c b/fx/int/i32.c new file mode 100644 index 0000000..48dbb2d --- /dev/null +++ b/fx/int/i32.c @@ -0,0 +1,58 @@ +#include +#include + +#define SWAP32(v) \ + (((v << 24) & 0xFF000000) | ((v << 8) & 0x00FF0000) \ + | ((v >> 8) & 0x0000FF00) | ((v >> 24) & 0x000000FF)) + +i32 fx_i32_htob(i32 v) +{ +#if FX_ENDIAN == 1 + return v; +#else + return SWAP32(v); +#endif +} + +i32 fx_i32_htos(i32 v) +{ +#if FX_ENDIAN == 0 + return v; +#else + return SWAP32(v); +#endif +} + +i32 fx_i32_btoh(i32 v) +{ +#if FX_ENDIAN == 1 + return v; +#else + return SWAP32(v); +#endif +} + +i32 fx_i32_stoh(i32 v) +{ +#if FX_ENDIAN == 0 + return v; +#else + return SWAP32(v); +#endif +} + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_i32) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_i32) + +FX_TYPE_DEFINITION_BEGIN(fx_i32) + __FX_VALUE_TYPE_ID(I32); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.i32"); + FX_TYPE_CLASS(fx_i32_class); + FX_TYPE_INSTANCE_PRIVATE(i32); +FX_TYPE_DEFINITION_END(fx_i32) diff --git a/fx/int/i64.c b/fx/int/i64.c new file mode 100644 index 0000000..ac1b99d --- /dev/null +++ b/fx/int/i64.c @@ -0,0 +1,62 @@ +#include +#include + +/* clang-format off */ +#define SWAP64(v) \ + (((v << 56) & 0xFF00000000000000) | ((v >> 56) & 0x00000000000000FF) \ + |((v << 40) & 0x00FF000000000000) | ((v >> 40) & 0x000000000000FF00) \ + |((v << 24) & 0x0000FF0000000000) | ((v >> 24) & 0x0000000000FF0000) \ + |((v << 8) & 0x000000FF00000000) | ((v >> 8) & 0x00000000FF000000)) +/* clang-format on */ + +i64 fx_i64_htob(i64 v) +{ +#if FX_ENDIAN == 1 + return v; +#else + return SWAP64(v); +#endif +} + +i64 fx_i64_htos(i64 v) +{ +#if FX_ENDIAN == 0 + return v; +#else + return SWAP64(v); +#endif +} + +i64 fx_i64_btoh(i64 v) +{ +#if FX_ENDIAN == 1 + return v; +#else + return SWAP64(v); +#endif +} + +i64 fx_i64_stoh(i64 v) +{ +#if FX_ENDIAN == 0 + return v; +#else + return SWAP64(v); +#endif +} + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_i64) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_i64) + +FX_TYPE_DEFINITION_BEGIN(fx_i64) + __FX_VALUE_TYPE_ID(I64); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.i64"); + FX_TYPE_CLASS(fx_i64_class); + FX_TYPE_INSTANCE_PRIVATE(i64); +FX_TYPE_DEFINITION_END(fx_i64) diff --git a/fx/int/int.c b/fx/int/int.c new file mode 100644 index 0000000..632e4f1 --- /dev/null +++ b/fx/int/int.c @@ -0,0 +1,18 @@ +#include +#include + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_int) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_int) + +FX_TYPE_DEFINITION_BEGIN(fx_int) + __FX_VALUE_TYPE_ID(INT); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.int"); + FX_TYPE_CLASS(fx_int_class); + FX_TYPE_INSTANCE_PRIVATE(int); +FX_TYPE_DEFINITION_END(fx_int) diff --git a/fx/int/iptr.c b/fx/int/iptr.c new file mode 100644 index 0000000..5796ce5 --- /dev/null +++ b/fx/int/iptr.c @@ -0,0 +1,18 @@ +#include +#include + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_iptr) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_iptr) + +FX_TYPE_DEFINITION_BEGIN(fx_iptr) + __FX_VALUE_TYPE_ID(IPTR); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.iptr"); + FX_TYPE_CLASS(fx_iptr_class); + FX_TYPE_INSTANCE_PRIVATE(iptr); +FX_TYPE_DEFINITION_END(fx_iptr) diff --git a/fx/int/long.c b/fx/int/long.c new file mode 100644 index 0000000..c103fad --- /dev/null +++ b/fx/int/long.c @@ -0,0 +1,18 @@ +#include +#include + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_long) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_long) + +FX_TYPE_DEFINITION_BEGIN(fx_long) + __FX_VALUE_TYPE_ID(LONG); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.long"); + FX_TYPE_CLASS(fx_long_class); + FX_TYPE_INSTANCE_PRIVATE(long); +FX_TYPE_DEFINITION_END(fx_long) diff --git a/fx/int/longlong.c b/fx/int/longlong.c new file mode 100644 index 0000000..338be0f --- /dev/null +++ b/fx/int/longlong.c @@ -0,0 +1,18 @@ +#include +#include + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_longlong) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_longlong) + +FX_TYPE_DEFINITION_BEGIN(fx_longlong) + __FX_VALUE_TYPE_ID(LONGLONG); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.longlong"); + FX_TYPE_CLASS(fx_longlong_class); + FX_TYPE_INSTANCE_PRIVATE(long long); +FX_TYPE_DEFINITION_END(fx_longlong) diff --git a/fx/int/sbyte.c b/fx/int/sbyte.c new file mode 100644 index 0000000..51102cd --- /dev/null +++ b/fx/int/sbyte.c @@ -0,0 +1,18 @@ +#include +#include + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_sbyte) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_sbyte) + +FX_TYPE_DEFINITION_BEGIN(fx_sbyte) + __FX_VALUE_TYPE_ID(SBYTE); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.sbyte"); + FX_TYPE_CLASS(fx_sbyte_class); + FX_TYPE_INSTANCE_PRIVATE(sbyte); +FX_TYPE_DEFINITION_END(fx_sbyte) diff --git a/fx/int/short.c b/fx/int/short.c new file mode 100644 index 0000000..d35377c --- /dev/null +++ b/fx/int/short.c @@ -0,0 +1,19 @@ + +#include +#include + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_short) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_short) + +FX_TYPE_DEFINITION_BEGIN(fx_short) + __FX_VALUE_TYPE_ID(SHORT); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.short"); + FX_TYPE_CLASS(fx_short_class); + FX_TYPE_INSTANCE_PRIVATE(short); +FX_TYPE_DEFINITION_END(fx_short) diff --git a/fx/int/size.c b/fx/int/size.c new file mode 100644 index 0000000..35f6403 --- /dev/null +++ b/fx/int/size.c @@ -0,0 +1,18 @@ +#include +#include + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_size) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_size) + +FX_TYPE_DEFINITION_BEGIN(fx_size) + __FX_VALUE_TYPE_ID(SIZE); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.size"); + FX_TYPE_CLASS(fx_size_class); + FX_TYPE_INSTANCE_PRIVATE(size_t); +FX_TYPE_DEFINITION_END(fx_size) diff --git a/fx/int/u16.c b/fx/int/u16.c new file mode 100644 index 0000000..7854336 --- /dev/null +++ b/fx/int/u16.c @@ -0,0 +1,56 @@ +#include +#include + +#define SWAP16(v) (((v << 8) & 0xFF00) | ((v >> 8) & 0x00FF)) + +u16 fx_u16_htob(u16 v) +{ +#if FX_ENDIAN == 1 + return v; +#else + return SWAP16(v); +#endif +} + +u16 fx_u16_htos(u16 v) +{ +#if FX_ENDIAN == 0 + return v; +#else + return SWAP16(v); +#endif +} + +u16 fx_u16_btoh(u16 v) +{ +#if FX_ENDIAN == 1 + return v; +#else + return SWAP16(v); +#endif +} + +u16 fx_u16_stoh(u16 v) +{ +#if FX_ENDIAN == 0 + return v; +#else + return SWAP16(v); +#endif +} + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_u16) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_u16) + +FX_TYPE_DEFINITION_BEGIN(fx_u16) + __FX_VALUE_TYPE_ID(U16); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.u16"); + FX_TYPE_CLASS(fx_u16_class); + FX_TYPE_INSTANCE_PRIVATE(u16); +FX_TYPE_DEFINITION_END(fx_u16) diff --git a/fx/int/u32.c b/fx/int/u32.c new file mode 100644 index 0000000..a47dd4a --- /dev/null +++ b/fx/int/u32.c @@ -0,0 +1,58 @@ +#include +#include + +#define SWAP32(v) \ + (((v << 24) & 0xFF000000) | ((v << 8) & 0x00FF0000) \ + | ((v >> 8) & 0x0000FF00) | ((v >> 24) & 0x000000FF)) + +u32 fx_u32_htob(u32 v) +{ +#if FX_ENDIAN == 1 + return v; +#else + return SWAP32(v); +#endif +} + +u32 fx_u32_htos(u32 v) +{ +#if FX_ENDIAN == 0 + return v; +#else + return SWAP32(v); +#endif +} + +u32 fx_u32_btoh(u32 v) +{ +#if FX_ENDIAN == 1 + return v; +#else + return SWAP32(v); +#endif +} + +u32 fx_u32_stoh(u32 v) +{ +#if FX_ENDIAN == 0 + return v; +#else + return SWAP32(v); +#endif +} + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_u32) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_u32) + +FX_TYPE_DEFINITION_BEGIN(fx_u32) + __FX_VALUE_TYPE_ID(U32); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.u32"); + FX_TYPE_CLASS(fx_u32_class); + FX_TYPE_INSTANCE_PRIVATE(u32); +FX_TYPE_DEFINITION_END(fx_u32) diff --git a/fx/int/u64.c b/fx/int/u64.c new file mode 100644 index 0000000..c27afc9 --- /dev/null +++ b/fx/int/u64.c @@ -0,0 +1,62 @@ +#include +#include + +/* clang-format off */ +#define SWAP64(v) \ + (((v << 56) & 0xFF00000000000000) | ((v >> 56) & 0x00000000000000FF) \ + |((v << 40) & 0x00FF000000000000) | ((v >> 40) & 0x000000000000FF00) \ + |((v << 24) & 0x0000FF0000000000) | ((v >> 24) & 0x0000000000FF0000) \ + |((v << 8) & 0x000000FF00000000) | ((v >> 8) & 0x00000000FF000000)) +/* clang-format on */ + +u64 fx_u64_htob(u64 v) +{ +#if FX_ENDIAN == 1 + return v; +#else + return SWAP64(v); +#endif +} + +u64 fx_u64_htos(u64 v) +{ +#if FX_ENDIAN == 0 + return v; +#else + return SWAP64(v); +#endif +} + +u64 fx_u64_btoh(u64 v) +{ +#if FX_ENDIAN == 1 + return v; +#else + return SWAP64(v); +#endif +} + +u64 fx_u64_stoh(u64 v) +{ +#if FX_ENDIAN == 0 + return v; +#else + return SWAP64(v); +#endif +} + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_u64) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_u64) + +FX_TYPE_DEFINITION_BEGIN(fx_u64) + __FX_VALUE_TYPE_ID(U64); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.u64"); + FX_TYPE_CLASS(fx_u64_class); + FX_TYPE_INSTANCE_PRIVATE(u64); +FX_TYPE_DEFINITION_END(fx_u64) diff --git a/fx/int/uint.c b/fx/int/uint.c new file mode 100644 index 0000000..ba74279 --- /dev/null +++ b/fx/int/uint.c @@ -0,0 +1,18 @@ +#include +#include + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_uint) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_uint) + +FX_TYPE_DEFINITION_BEGIN(fx_uint) + __FX_VALUE_TYPE_ID(UINT); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.uint"); + FX_TYPE_CLASS(fx_uint_class); + FX_TYPE_INSTANCE_PRIVATE(unsigned int); +FX_TYPE_DEFINITION_END(fx_uint) diff --git a/fx/int/ulong.c b/fx/int/ulong.c new file mode 100644 index 0000000..ab17ffe --- /dev/null +++ b/fx/int/ulong.c @@ -0,0 +1,18 @@ +#include +#include + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_ulong) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_ulong) + +FX_TYPE_DEFINITION_BEGIN(fx_ulong) + __FX_VALUE_TYPE_ID(ULONG); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.ulong"); + FX_TYPE_CLASS(fx_ulong_class); + FX_TYPE_INSTANCE_PRIVATE(unsigned long); +FX_TYPE_DEFINITION_END(fx_ulong) diff --git a/fx/int/ulonglong.c b/fx/int/ulonglong.c new file mode 100644 index 0000000..527bece --- /dev/null +++ b/fx/int/ulonglong.c @@ -0,0 +1,18 @@ +#include +#include + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_ulonglong) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_ulonglong) + +FX_TYPE_DEFINITION_BEGIN(fx_ulonglong) + __FX_VALUE_TYPE_ID(ULONGLONG); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.ulonglong"); + FX_TYPE_CLASS(fx_ulonglong_class); + FX_TYPE_INSTANCE_PRIVATE(unsigned long long); +FX_TYPE_DEFINITION_END(fx_ulonglong) diff --git a/fx/int/uptr.c b/fx/int/uptr.c new file mode 100644 index 0000000..b93ea1d --- /dev/null +++ b/fx/int/uptr.c @@ -0,0 +1,18 @@ +#include +#include + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_uptr) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_uptr) + +FX_TYPE_DEFINITION_BEGIN(fx_uptr) + __FX_VALUE_TYPE_ID(UPTR); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.uptr"); + FX_TYPE_CLASS(fx_uptr_class); + FX_TYPE_INSTANCE_PRIVATE(uptr); +FX_TYPE_DEFINITION_END(fx_uptr) diff --git a/fx/int/ushort.c b/fx/int/ushort.c new file mode 100644 index 0000000..6cfa512 --- /dev/null +++ b/fx/int/ushort.c @@ -0,0 +1,18 @@ +#include +#include + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_ushort) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_ushort) + +FX_TYPE_DEFINITION_BEGIN(fx_ushort) + __FX_VALUE_TYPE_ID(USHORT); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.ushort"); + FX_TYPE_CLASS(fx_ushort_class); + FX_TYPE_INSTANCE_PRIVATE(unsigned short); +FX_TYPE_DEFINITION_END(fx_ushort) diff --git a/fx/object.c b/fx/object.c index 75b6da3..33db6db 100644 --- a/fx/object.c +++ b/fx/object.c @@ -21,6 +21,11 @@ FX_TYPE_DEFINITION_BEGIN(fx_object) FX_TYPE_CLASS(fx_object_class); FX_TYPE_DEFINITION_END(fx_object) +fx_type_id fx_object_query_type(const struct _fx_object *obj) +{ + return &obj->obj_type->ty_id; +} + fx_result fx_object_instantiate( struct fx_type_info *type, struct _fx_object **out_object) diff --git a/fx/pointer.c b/fx/pointer.c new file mode 100644 index 0000000..9bde9a3 --- /dev/null +++ b/fx/pointer.c @@ -0,0 +1,18 @@ +#include +#include + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_pointer) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_pointer) + +FX_TYPE_DEFINITION_BEGIN(fx_pointer) + __FX_VALUE_TYPE_ID(POINTER); + FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_NAME("fx.pointer"); + FX_TYPE_CLASS(fx_pointer_class); + FX_TYPE_INSTANCE_PRIVATE(void *); +FX_TYPE_DEFINITION_END(fx_pointer) diff --git a/fx/string.c b/fx/string.c index d60d799..578b949 100644 --- a/fx/string.c +++ b/fx/string.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -1718,18 +1719,18 @@ FX_TYPE_CLASS_BEGIN(fx_string) FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin; FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) - FX_TYPE_CONSTRUCTOR("create", fx_string_create, 0, FX_VALUE_TYPE_NONE); + FX_TYPE_CONSTRUCTOR("create", fx_string_create, 0, FX_TYPE_VOID); FX_TYPE_CONSTRUCTOR( "create_from_cstr", fx_string_create_from_cstr, 1, - FX_VALUE_TYPE_CSTR); + FX_TYPE_CSTR); FX_TYPE_METHOD( - FX_VALUE_TYPE_CSTR, + FX_TYPE_CSTR, "get_cstr", fx_string_get_cstr, 0, - FX_VALUE_TYPE_POINTER); + FX_TYPE_STRING); FX_TYPE_CLASS_END(fx_string) FX_TYPE_DEFINITION_BEGIN(fx_string) @@ -1763,76 +1764,3 @@ FX_TYPE_DEFINITION_BEGIN(fx_string_iterator) FX_TYPE_CLASS(fx_string_iterator_class); FX_TYPE_INSTANCE_PRIVATE(struct fx_string_iterator_p); FX_TYPE_DEFINITION_END(fx_string_iterator) - -/*** MISC FUNCTIONS ***********************************************************/ - -char *fx_strdup(const char *s) -{ - size_t len = strlen(s); - char *p = malloc(len + 1); - if (!p) { - return NULL; - } - - memcpy(p, s, len); - p[len] = '\0'; - - return p; -} - -size_t fx_strlen(const char *s, fx_strlen_flags flags) -{ - if (!(flags & (FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD))) { - return strlen(s); - } - - size_t out = 0; - for (size_t i = 0; s[i]; i++) { - if (s[i] == '\033' && (flags & FX_STRLEN_IGNORE_ESC)) { - while (!isalpha(s[i]) && s[i]) { - i++; - } - - continue; - } - - if (s[i] == '[' && (flags & FX_STRLEN_IGNORE_MOD)) { - i++; - if (s[i] == '[') { - out++; - continue; - } - - while (s[i] != ']' && s[i]) { - i++; - } - - continue; - } - - out++; - } - - return out; -} - -fx_wchar *fx_wstrdup(const fx_wchar *s) -{ - size_t len = fx_wstrlen(s); - fx_wchar *buf = calloc(len + 1, sizeof(fx_wchar)); - if (!buf) { - return NULL; - } - - memcpy(buf, s, len * sizeof(fx_wchar)); - - return buf; -} - -size_t fx_wstrlen(const fx_wchar *s) -{ - size_t len; - for (len = 0; s[len] != 0; len++) - ; - return len; -} diff --git a/fx/stringstream.c b/fx/stringstream.c index 562060c..71a6ce7 100644 --- a/fx/stringstream.c +++ b/fx/stringstream.c @@ -58,6 +58,10 @@ static enum fx_status __gets( ss->ss_ptr += to_copy; + if (ss->ss_ptr == ss->ss_len) { + ss->ss_ptr = ss->ss_len = 0; + } + if (nr_read) { *nr_read = to_copy; } @@ -163,8 +167,9 @@ fx_stringstream *fx_stringstream_create_with_buffer(char *buf, size_t max) } fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM); - struct fx_stringstream_p *p - = fx_object_get_private(s, FX_TYPE_STRINGSTREAM); + struct fx_stringstream_p *p = fx_object_get_private( + s, + FX_TYPE_STRINGSTREAM); cfg->s_mode = FX_STREAM_READ | FX_STREAM_WRITE | Z__FX_STREAM_STATIC; @@ -184,8 +189,9 @@ fx_stringstream *fx_stringstream_create(void) } fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM); - struct fx_stringstream_p *p - = fx_object_get_private(s, FX_TYPE_STRINGSTREAM); + struct fx_stringstream_p *p = fx_object_get_private( + s, + FX_TYPE_STRINGSTREAM); cfg->s_mode = FX_STREAM_READ | FX_STREAM_WRITE | Z__FX_STREAM_STATIC; @@ -264,8 +270,9 @@ static void stringstream_fini(fx_object *obj, void *priv) enum fx_status stream_getc(fx_stream *stream, fx_wchar *c) { - struct fx_stringstream_p *s - = fx_object_get_private(stream, FX_TYPE_STRINGSTREAM); + struct fx_stringstream_p *s = fx_object_get_private( + stream, + FX_TYPE_STRINGSTREAM); enum fx_status status = __getc(s, c); @@ -278,8 +285,9 @@ enum fx_status stream_read( size_t count, size_t *nr_read) { - struct fx_stringstream_p *s - = fx_object_get_private(stream, FX_TYPE_STRINGSTREAM); + struct fx_stringstream_p *s = fx_object_get_private( + stream, + FX_TYPE_STRINGSTREAM); enum fx_status status = __gets(s, buf, count, nr_read); @@ -292,8 +300,9 @@ enum fx_status stream_write( size_t count, size_t *nr_written) { - struct fx_stringstream_p *s - = fx_object_get_private(stream, FX_TYPE_STRINGSTREAM); + struct fx_stringstream_p *s = fx_object_get_private( + stream, + FX_TYPE_STRINGSTREAM); enum fx_status status = __puts(s, (const char *)buf, count, nr_written); diff --git a/fx/test/values.c b/fx/test/values.c new file mode 100644 index 0000000..cae993a --- /dev/null +++ b/fx/test/values.c @@ -0,0 +1,10 @@ +#include +#include +#include + +int main(void) +{ + fx_value v = FX_I32(1024); + printf("%u\n", __fx_type_get_value_type(FX_TYPE_I64)); + return 0; +} diff --git a/fx/type.c b/fx/type.c index 5288222..bce3f36 100644 --- a/fx/type.c +++ b/fx/type.c @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -111,25 +112,19 @@ static struct fx_type_component *create_type_component( return c; } -void fx_type_id_init( - union fx_type_id *out, - uint32_t a, - uint16_t b, - uint16_t c, - uint16_t d, - uint64_t e) +void fx_type_id_init(union fx_type_id *out, u32 a, u16 b, u16 c, u16 d, u64 e) { - fx_i32 x_a = fx_i32_htob(a); - fx_i16 x_b = fx_i16_htob(b); - fx_i16 x_c = fx_i16_htob(c); - fx_i16 x_d = fx_i16_htob(d); - fx_i64 x_e = fx_i64_htob(e); + a = fx_u32_htob(a); + b = fx_u16_htob(b); + c = fx_u16_htob(c); + d = fx_u16_htob(d); + e = fx_u64_htob(e); - memcpy(&out->b[0], x_a.i_bytes, sizeof x_a.i_bytes); - memcpy(&out->b[4], x_b.i_bytes, sizeof x_b.i_bytes); - memcpy(&out->b[6], x_c.i_bytes, sizeof x_c.i_bytes); - memcpy(&out->b[8], x_d.i_bytes, sizeof x_d.i_bytes); - memcpy(&out->b[10], &x_e.i_bytes[2], sizeof x_e.i_bytes - 2); + memcpy(&out->b[0], &a, sizeof a); + memcpy(&out->b[4], &b, sizeof b); + memcpy(&out->b[6], &c, sizeof c); + memcpy(&out->b[8], &d, sizeof d); + memcpy(&out->b[10], (byte *)&e + 2, sizeof e - 2); } static void initialise_type_component( diff --git a/fx/uint.c b/fx/uint.c deleted file mode 100644 index 4ee0d26..0000000 --- a/fx/uint.c +++ /dev/null @@ -1,174 +0,0 @@ -#include -#include -#include -#include - -/*** PRIVATE DATA *************************************************************/ - -enum uint_type { - UINT_REGULAR = 0, - UINT_INF, - UINT_NAN, -}; - -struct fx_uint_p { - enum uint_type i_type; - uintptr_t i_value; -}; - -/*** PRIVATE FUNCTIONS ********************************************************/ - -static uintptr_t uint_get_value(const struct fx_uint_p *i) -{ - return i->i_value; -} - -static void uint_set_value(struct fx_uint_p *i, uintptr_t v) -{ - i->i_type = UINT_REGULAR; - i->i_value = v; -} - -static void uint_set_value_nan(struct fx_uint_p *i) -{ - i->i_type = UINT_NAN; - i->i_value = 0; -} - -static void uint_set_value_inf(struct fx_uint_p *i) -{ - i->i_type = UINT_INF; - i->i_value = 0; -} - -static bool uint_is_nan(struct fx_uint_p *i) -{ - return i->i_type == UINT_NAN; -} - -static bool uint_is_inf(struct fx_uint_p *i) -{ - return i->i_type == UINT_INF; -} - -/*** PUBLIC FUNCTIONS *********************************************************/ - -fx_uint *fx_uint_create(uintptr_t value) -{ - fx_uint *i = fx_object_create(FX_TYPE_UINT); - if (!i) { - return NULL; - } - - struct fx_uint_p *p = fx_object_get_private(i, FX_TYPE_UINT); - p->i_type = UINT_REGULAR; - p->i_value = value; - - return i; -} - -fx_uint *fx_uint_create_nan(void) -{ - fx_uint *i = fx_object_create(FX_TYPE_UINT); - if (!i) { - return NULL; - } - - struct fx_uint_p *p = fx_object_get_private(i, FX_TYPE_UINT); - p->i_type = UINT_NAN; - p->i_value = 0; - - return i; -} - -fx_uint *fx_uint_create_inf(void) -{ - fx_uint *i = fx_object_create(FX_TYPE_UINT); - if (!i) { - return NULL; - } - - struct fx_uint_p *p = fx_object_get_private(i, FX_TYPE_UINT); - p->i_type = UINT_INF; - p->i_value = 0; - - return i; -} - -uintptr_t fx_uint_get_value(const fx_uint *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_UINT, uint_get_value, i); -} - -void fx_uint_set_value(fx_uint *i, uintptr_t v) -{ - FX_CLASS_DISPATCH_STATIC_V(FX_TYPE_UINT, uint_set_value, i, v); -} - -void fx_uint_set_value_nan(fx_uint *i) -{ - FX_CLASS_DISPATCH_STATIC_V0(FX_TYPE_UINT, uint_set_value_nan, i); -} - -void fx_uint_set_value_inf(fx_uint *i) -{ - FX_CLASS_DISPATCH_STATIC_V0(FX_TYPE_UINT, uint_set_value_inf, i); -} - -bool fx_uint_is_nan(const fx_uint *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_UINT, uint_is_nan, i); -} - -bool fx_uint_is_inf(const fx_uint *i) -{ - FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_UINT, uint_is_inf, i); -} - -/*** VIRTUAL FUNCTIONS ********************************************************/ - -static void uint_init(fx_object *obj, void *priv) -{ - struct fx_uint_p *i = priv; - - i->i_type = UINT_REGULAR; - i->i_value = 0; -} - -static void uint_fini(fx_object *obj, void *priv) -{ -} - -static void uint_to_string(const fx_object *obj, fx_stream *out) -{ - struct fx_uint_p *i = fx_object_get_private(obj, FX_TYPE_UINT); - switch (i->i_type) { - case UINT_REGULAR: - fx_stream_write_fmt(out, NULL, "%" PRIdPTR, i->i_value); - break; - case UINT_INF: - fx_stream_write_cstr(out, "inf", NULL); - break; - case UINT_NAN: - fx_stream_write_cstr(out, "NaN", NULL); - break; - default: - break; - } -} - -/*** CLASS DEFINITION *********************************************************/ - -FX_TYPE_CLASS_BEGIN(fx_uint) - FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) - FX_INTERFACE_ENTRY(to_string) = uint_to_string; - FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) -FX_TYPE_CLASS_END(fx_uint) - -FX_TYPE_DEFINITION_BEGIN(fx_uint) - FX_TYPE_ID(0x79aee484, 0x5dd5, 0x463a, 0xb0c8, 0x60873abdf088); - FX_TYPE_CLASS(fx_uint_class); - FX_TYPE_INSTANCE_PRIVATE(struct fx_uint_p); - FX_TYPE_INSTANCE_INIT(uint_init); - FX_TYPE_INSTANCE_FINI(uint_fini); -FX_TYPE_DEFINITION_END(fx_uint) diff --git a/fx/value-type.c b/fx/value-type.c new file mode 100644 index 0000000..54940f6 --- /dev/null +++ b/fx/value-type.c @@ -0,0 +1,30 @@ +#include + +bool fx_type_is_value_type(fx_type_id ty) +{ + return ty->a.p00 == __FX_VALUE_TYPE_BASEID; +} + +unsigned int __fx_type_get_value_type(fx_type_id ty) +{ + if (ty->a.p00 != __FX_VALUE_TYPE_BASEID) { + return 0; + } + + return ty->a.p01; +} + +/*** CLASS DEFINITION *********************************************************/ + +FX_TYPE_CLASS_BEGIN(fx_value_type) + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) + FX_INTERFACE_ENTRY(to_string) = NULL; + FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) +FX_TYPE_CLASS_END(fx_value_type) + +FX_TYPE_DEFINITION_BEGIN(fx_value_type) + type_info->ty_id.a.p00 = __FX_VALUE_TYPE_BASEID; + type_info->ty_id.a.p01 = 0; + FX_TYPE_NAME("fx.value-type"); + FX_TYPE_CLASS(fx_value_type_class); +FX_TYPE_DEFINITION_END(fx_value_type) diff --git a/fx/value.c b/fx/value.c index dcc461b..c76859b 100644 --- a/fx/value.c +++ b/fx/value.c @@ -1,23 +1,19 @@ #include -#include +#include #include #include -#include +#include #include void fx_value_init(fx_value *v, fx_type_id type) { } -void fx_value_init_primitive(fx_value *v, fx_value_type type) -{ -} - void fx_value_copy(fx_value *dst, fx_value *src) { memcpy(dst, src, sizeof *dst); - if (dst->v_type.t_primitive > __FX_VALUE_TYPE_OBJECT_BOUNDARY) { + if (!fx_type_is_value_type(dst->v_type)) { fx_object_ref(dst->v_object); } } @@ -31,9 +27,11 @@ void fx_value_copy_array(fx_value *dst, fx_value *src, size_t count) void fx_value_unset(fx_value *v) { +#if 0 if (v->v_type.t_primitive > __FX_VALUE_TYPE_OBJECT_BOUNDARY) { fx_object_unref(v->v_object); } +#endif memset(v, 0x0, sizeof *v); } @@ -73,6 +71,7 @@ void fx_value_set_object(fx_value *v, fx_object *obj) { } +#if 0 bool fx_value_is_bool(const fx_value *v) { if (v->v_type.t_primitive == FX_VALUE_TYPE_BOOL) { @@ -223,17 +222,16 @@ double fx_value_get_double(const fx_value *v) return 0; } +#endif const char *fx_value_get_cstr(const fx_value *v) { - if (v->v_type.t_primitive == FX_VALUE_TYPE_CSTR) { + if (v->v_type == FX_TYPE_CSTR) { return v->v_cstr; } - if (v->v_type.t_primitive > __FX_VALUE_TYPE_OBJECT_BOUNDARY) { - if (fx_type_id_compare(v->v_type.t_object, FX_TYPE_STRING)) { - return fx_string_get_cstr(v->v_object); - } + if (v->v_type == FX_TYPE_STRING) { + return fx_string_get_cstr(v->v_object); } return NULL; @@ -241,7 +239,7 @@ const char *fx_value_get_cstr(const fx_value *v) void *fx_value_get_pointer(fx_value *v) { - if (v->v_type.t_primitive == FX_VALUE_TYPE_POINTER) { + if (__fx_type_get_value_type(v->v_type) == __FX_VALUE_TYPE_POINTER) { return v->v_pointer; } @@ -250,7 +248,7 @@ void *fx_value_get_pointer(fx_value *v) const void *fx_value_get_pointer_c(const fx_value *v) { - if (v->v_type.t_primitive == FX_VALUE_TYPE_POINTER) { + if (__fx_type_get_value_type(v->v_type) == __FX_VALUE_TYPE_POINTER) { return v->v_pointer; } @@ -259,7 +257,7 @@ const void *fx_value_get_pointer_c(const fx_value *v) fx_object *fx_value_get_object(fx_value *v) { - if (v->v_type.t_primitive > __FX_VALUE_TYPE_OBJECT_BOUNDARY) { + if (!fx_type_is_value_type(v->v_type)) { return v->v_object; } @@ -268,7 +266,7 @@ fx_object *fx_value_get_object(fx_value *v) const fx_object *fx_value_get_object_c(const fx_value *v) { - if (v->v_type.t_primitive > __FX_VALUE_TYPE_OBJECT_BOUNDARY) { + if (!fx_type_is_value_type(v->v_type)) { return v->v_object; }