fx: implement updated interfaces in builtin classes

This commit is contained in:
2026-05-25 17:27:14 +01:00
parent 5d0aa577d6
commit 6efe473c11
13 changed files with 691 additions and 293 deletions
+8 -27
View File
@@ -4,31 +4,21 @@
#include <fx/macros.h> #include <fx/macros.h>
#include <fx/misc.h> #include <fx/misc.h>
#include <fx/status.h> #include <fx/status.h>
#include <fx/value.h>
#include <stdbool.h> #include <stdbool.h>
FX_DECLS_BEGIN; FX_DECLS_BEGIN;
#define fx_foreach(type, var, iterator) \ #define fx_foreach(var, iterator) \
for (type var = (type)fx_iterator_get_value(iterator).v_int; \ for (fx_value var = fx_iterator_get_value(iterator); \
FX_OK(fx_iterator_get_status(iterator)); \ FX_OK(fx_iterator_get_status(iterator)); \
fx_iterator_move_next(iterator), \ fx_iterator_move_next(iterator), \
var = (type)fx_iterator_get_value(iterator).v_int) var = fx_iterator_get_value(iterator))
#define fx_foreach_ptr(type, var, iterator) \ #define fx_foreach_ptr(type, var, iterator) \
for (type *var = (type *)fx_iterator_get_value(iterator).v_ptr; \ for (type *var = (type *)fx_iterator_get_value(iterator).v_ptr; \
FX_OK(fx_iterator_get_status(iterator)); \ FX_OK(fx_iterator_get_status(iterator)); \
fx_iterator_move_next(iterator), \ fx_iterator_move_next(iterator), \
var = (type *)fx_iterator_get_value(iterator).v_ptr) var = (type *)fx_iterator_get_value(iterator).v_ptr)
#define fx_foreach_c(type, var, iterator) \
for (type var = (type)fx_iterator_get_cvalue(iterator).v_int; \
FX_OK(fx_iterator_get_status(iterator)); \
fx_iterator_move_next(iterator), \
var = (type)fx_iterator_get_cvalue(iterator).v_int)
#define fx_foreach_cptr(type, var, iterator) \
for (const type *var \
= (const type *)fx_iterator_get_cvalue(iterator).v_cptr; \
FX_OK(fx_iterator_get_status(iterator)); \
fx_iterator_move_next(iterator), \
var = (const type *)fx_iterator_get_cvalue(iterator).v_cptr)
#define FX_ITERATOR_VALUE_INT(v) ((fx_iterator_value) {.v_int = (v)}) #define FX_ITERATOR_VALUE_INT(v) ((fx_iterator_value) {.v_int = (v)})
#define FX_ITERATOR_VALUE_PTR(v) ((fx_iterator_value) {.v_ptr = (v)}) #define FX_ITERATOR_VALUE_PTR(v) ((fx_iterator_value) {.v_ptr = (v)})
@@ -39,12 +29,6 @@ FX_DECLS_BEGIN;
#define FX_TYPE_ITERATOR (fx_iterator_get_type()) #define FX_TYPE_ITERATOR (fx_iterator_get_type())
#define FX_TYPE_ITERABLE (fx_iterable_get_type()) #define FX_TYPE_ITERABLE (fx_iterable_get_type())
typedef union fx_iterator_value {
uintptr_t v_int;
void *v_ptr;
const void *v_cptr;
} fx_iterator_value;
__FX_DECLARE_TYPE(fx_iterator); __FX_DECLARE_TYPE(fx_iterator);
FX_DECLARE_TYPE(fx_iterable); FX_DECLARE_TYPE(fx_iterable);
@@ -52,8 +36,7 @@ FX_DECLARE_TYPE(fx_iterable);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterator) FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterator)
fx_status (*it_move_next)(const fx_iterator *); fx_status (*it_move_next)(const fx_iterator *);
fx_status (*it_erase)(fx_iterator *); fx_status (*it_erase)(fx_iterator *);
fx_iterator_value (*it_get_value)(fx_iterator *); fx_value (*it_get_value)(const fx_iterator *);
const fx_iterator_value (*it_get_cvalue)(const fx_iterator *);
FX_TYPE_CLASS_DECLARATION_END(fx_iterator) FX_TYPE_CLASS_DECLARATION_END(fx_iterator)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterable) FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterable)
@@ -77,13 +60,11 @@ FX_API fx_iterator *fx_iterator_begin(fx_iterable *it);
FX_API const fx_iterator *fx_iterator_cbegin(const fx_iterable *it); FX_API const fx_iterator *fx_iterator_cbegin(const fx_iterable *it);
FX_API fx_status fx_iterator_get_status(const fx_iterator *it); FX_API fx_status fx_iterator_get_status(const fx_iterator *it);
FX_API fx_status fx_iterator_set_status( FX_API fx_status
const fx_iterator *it, fx_iterator_set_status(const fx_iterator *it, fx_status status);
fx_status status);
FX_API fx_status fx_iterator_move_next(const fx_iterator *it); FX_API fx_status fx_iterator_move_next(const fx_iterator *it);
FX_API fx_iterator_value fx_iterator_get_value(fx_iterator *it); FX_API fx_value fx_iterator_get_value(const fx_iterator *it);
FX_API const fx_iterator_value fx_iterator_get_cvalue(const fx_iterator *it);
FX_API fx_status fx_iterator_erase(fx_iterator *it); FX_API fx_status fx_iterator_erase(fx_iterator *it);
static inline bool fx_iterator_is_valid(const fx_iterator *it) static inline bool fx_iterator_is_valid(const fx_iterator *it)
{ {
+11
View File
@@ -67,6 +67,16 @@
fx_type_add_function(ty, func); \ fx_type_add_function(ty, func); \
} \ } \
} while (0) } while (0)
#define FX_TYPE_PROPERTY(name, get, set) \
do { \
if (ty) { \
fx_property *prop = fx_property_create( \
name, \
get, \
set); \
fx_type_add_property(ty, prop); \
} \
} while (0)
#define FX_TYPE_VTABLE_INTERFACE_BEGIN(interface_name, interface_id) \ #define FX_TYPE_VTABLE_INTERFACE_BEGIN(interface_name, interface_id) \
interface_name##_class *__FX_IFACE_I1(iface, __LINE__) \ interface_name##_class *__FX_IFACE_I1(iface, __LINE__) \
@@ -256,6 +266,7 @@
} \ } \
self = fx_assembly_create(); self = fx_assembly_create();
#define FX_ASSEMBLY_END(id) \ #define FX_ASSEMBLY_END(id) \
fx_assembly_register(self); \
return self; \ return self; \
} \ } \
const fx_assembly *__fx_assembly_get(void) \ const fx_assembly *__fx_assembly_get(void) \
+9 -3
View File
@@ -14,9 +14,14 @@
#define FX_RV(p) (fx_object_make_rvalue(p)) #define FX_RV(p) (fx_object_make_rvalue(p))
typedef FX_TYPE_FWDREF(fx_object) fx_object; typedef FX_TYPE_FWDREF(fx_object) fx_object;
struct fx_value;
typedef struct _fx_object_class { typedef struct _fx_object_class {
void (*to_string)(const fx_object *, FX_TYPE_FWDREF(fx_stream) *); fx_status (*to_string)(
const struct fx_value *,
FX_TYPE_FWDREF(fx_stream) *,
const char *);
fx_status (*hash)(const struct fx_value *, uint64_t *);
} fx_object_class; } fx_object_class;
FX_API fx_type_id fx_object_get_type(void); FX_API fx_type_id fx_object_get_type(void);
@@ -37,9 +42,10 @@ FX_API void fx_object_unref(fx_object *p);
FX_API fx_object *fx_object_make_rvalue(fx_object *p); FX_API fx_object *fx_object_make_rvalue(fx_object *p);
FX_API fx_object *fx_object_create(fx_type_id type); FX_API fx_object *fx_object_create(fx_type_id type);
FX_API void fx_object_to_string( FX_API fx_status fx_object_to_string(
const fx_object *p, const fx_object *p,
FX_TYPE_FWDREF(fx_stream) * out); FX_TYPE_FWDREF(fx_stream) * out,
const char *format);
FX_API bool fx_object_is_type(const fx_object *p, fx_type_id type); FX_API bool fx_object_is_type(const fx_object *p, fx_type_id type);
#endif #endif
+3
View File
@@ -64,6 +64,7 @@ typedef struct fx_type_info {
struct fx_bst ty_components; struct fx_bst ty_components;
struct fx_queue ty_class_hierarchy; struct fx_queue ty_class_hierarchy;
struct fx_namemap ty_functions; struct fx_namemap ty_functions;
struct fx_namemap ty_properties;
size_t ty_instance_size, ty_class_size; size_t ty_instance_size, ty_class_size;
} fx_type_info; } fx_type_info;
@@ -93,5 +94,7 @@ static inline int fx_type_id_compare(fx_type_id a, fx_type_id b)
FX_API fx_result fx_type_register(fx_type_info *info); FX_API fx_result fx_type_register(fx_type_info *info);
FX_API fx_result FX_API fx_result
fx_type_add_function(fx_type_info *info, struct _fx_object *func); fx_type_add_function(fx_type_info *info, struct _fx_object *func);
FX_API fx_result
fx_type_add_property(fx_type_info *info, struct _fx_object *prop);
#endif #endif
+2 -12
View File
@@ -74,26 +74,16 @@ enum fx_status fx_iterator_move_next(const fx_iterator *it)
return status; return status;
} }
fx_iterator_value fx_iterator_get_value(fx_iterator *it) fx_value fx_iterator_get_value(const fx_iterator *it)
{ {
FX_CLASS_DISPATCH_VIRTUAL_0( FX_CLASS_DISPATCH_VIRTUAL_0(
fx_iterator, fx_iterator,
FX_TYPE_ITERATOR, FX_TYPE_ITERATOR,
FX_ITERATOR_VALUE_NULL, FX_VALUE_EMPTY,
it_get_value, it_get_value,
it); it);
} }
const fx_iterator_value fx_iterator_get_cvalue(const fx_iterator *it)
{
FX_CLASS_DISPATCH_VIRTUAL_0(
fx_iterator,
FX_TYPE_ITERATOR,
FX_ITERATOR_VALUE_NULL,
it_get_cvalue,
it);
}
fx_status fx_iterator_erase(fx_iterator *it) fx_status fx_iterator_erase(fx_iterator *it)
{ {
enum fx_status status = FX_ERR_NOT_SUPPORTED; enum fx_status status = FX_ERR_NOT_SUPPORTED;
+12 -8
View File
@@ -85,15 +85,19 @@ struct _fx_object *fx_object_create(fx_type_id type)
return out; return out;
} }
void fx_object_to_string(const struct _fx_object *p, fx_stream *out) fx_status fx_object_to_string(
const struct _fx_object *p,
fx_stream *out,
const char *format)
{ {
FX_CLASS_DISPATCH_VIRTUAL_V( fx_value value = FX_VALUE_OBJECT(p);
fx_object, fx_object_class *iface = fx_object_get_interface(p, FX_TYPE_OBJECT);
FX_TYPE_OBJECT, if (iface && iface->to_string) {
to_string, return iface->to_string(&value, out, format);
p, }
out);
fx_stream_write_fmt(out, NULL, "<%s@%p>", p->obj_type->ty_name, p); fx_stream_write_fmt(out, NULL, "%s", p->obj_type->ty_name);
return FX_SUCCESS;
} }
bool fx_object_is_type(const struct _fx_object *p, fx_type_id type) bool fx_object_is_type(const struct _fx_object *p, fx_type_id type)
+426 -193
View File
File diff suppressed because it is too large Load Diff
+142 -45
View File
@@ -1,9 +1,18 @@
#include "cstr.h"
#include <ctype.h> #include <ctype.h>
#include <fx/comparable.h>
#include <fx/convertible.h>
#include <fx/cstr.h> #include <fx/cstr.h>
#include <fx/hash.h>
#include <fx/operable.h>
#include <fx/reflection/function.h> #include <fx/reflection/function.h>
#include <fx/reflection/property.h>
#include <fx/stream.h> #include <fx/stream.h>
#include <fx/string.h> #include <fx/string.h>
#include <fx/stringstream.h> #include <fx/stringstream.h>
#include <fx/value.h>
#include <fx/wstr.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
@@ -1157,23 +1166,6 @@ static fx_string *string_substr(
return newstr; return newstr;
} }
static uint64_t string_hash(const struct fx_string_p *str)
{
#define FNV1_OFFSET_BASIS 0xcbf29ce484222325
#define FNV1_PRIME 0x100000001b3
uint64_t hash = FNV1_OFFSET_BASIS;
size_t i = 0;
const char *s = string_ptr(str);
for (i = 0; i < str->s_len; i++) {
hash ^= s[i];
hash *= FNV1_PRIME;
}
return hash;
}
/*** PUBLIC FUNCTIONS *********************************************************/ /*** PUBLIC FUNCTIONS *********************************************************/
fx_string *fx_string_create_from_cstr(const char *s) fx_string *fx_string_create_from_cstr(const char *s)
@@ -1471,11 +1463,6 @@ fx_string *fx_string_get_substr(const fx_string *str, size_t start, size_t len)
len); len);
} }
uint64_t fx_string_hash(const fx_string *str)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRING, string_hash, str);
}
/*** PUBLIC ALIAS FUNCTIONS ***************************************************/ /*** PUBLIC ALIAS FUNCTIONS ***************************************************/
enum fx_status fx_string_append_c(fx_string *dest, char c) enum fx_status fx_string_append_c(fx_string *dest, char c)
@@ -1547,13 +1534,54 @@ static void string_fini(fx_object *obj, void *priv)
} }
} }
static void string_to_string(const fx_object *obj, fx_stream *out) static fx_status string_to_string(
const fx_value *obj,
fx_stream *out,
const char *format)
{ {
struct fx_string_p *str = fx_object_get_private(obj, FX_TYPE_STRING); struct fx_string_p *str = fx_object_get_private(
obj->v_object,
FX_TYPE_STRING);
const char *s = string_ptr(str); const char *s = string_ptr(str);
for (size_t i = 0; i < str->s_len; i++) { for (size_t i = 0; i < str->s_len; i++) {
fx_stream_write_char(out, s[i]); fx_stream_write_char(out, s[i]);
} }
return FX_SUCCESS;
}
static fx_status string_hash(const fx_value *v, uint64_t *out_hash)
{
struct fx_string_p *str = fx_object_get_private(
v->v_object,
FX_TYPE_STRING);
const char *s = string_ptr(str);
*out_hash = fx_hash_cstr(s);
return FX_SUCCESS;
}
static i32 compare(const fx_value *left, const fx_value *right)
{
const char *left_cstr, *right_cstr;
if (!FX_OK(fx_value_get_cstr(left, &left_cstr))) {
return -1;
}
if (!FX_OK(fx_value_get_cstr(right, &right_cstr))) {
return -1;
}
return strcmp(left_cstr, right_cstr);
}
static fx_status get_length(
const fx_value *str_v,
const fx_property *prop,
fx_value *out)
{
fx_string *str = NULL;
fx_value_get_object(str_v, &str);
*out = FX_SIZE(fx_string_get_size(str, FX_STRLEN_CODEPOINTS));
return FX_SUCCESS;
} }
/*** ITERATOR FUNCTIONS *******************************************************/ /*** ITERATOR FUNCTIONS *******************************************************/
@@ -1662,19 +1690,17 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
} }
} }
static fx_iterator_value chars_iterator_get_value( static fx_value chars_iterator_get_value(struct fx_string_iterator_p *it)
struct fx_string_iterator_p *it)
{ {
return FX_ITERATOR_VALUE_INT(it->char_value); return FX_U32(it->char_value);
} }
static fx_iterator_value tokens_iterator_get_value( static fx_value tokens_iterator_get_value(struct fx_string_iterator_p *it)
struct fx_string_iterator_p *it)
{ {
return FX_ITERATOR_VALUE_CPTR(it->string_value); return FX_CSTR(it->string_value);
} }
static fx_iterator_value iterator_get_value(fx_iterator *obj) static fx_value iterator_get_value(const fx_iterator *obj)
{ {
struct fx_string_iterator_p *it = fx_object_get_private( struct fx_string_iterator_p *it = fx_object_get_private(
obj, obj,
@@ -1686,32 +1712,65 @@ static fx_iterator_value iterator_get_value(fx_iterator *obj)
case ITERATOR_MODE_TOKENS: case ITERATOR_MODE_TOKENS:
return tokens_iterator_get_value(it); return tokens_iterator_get_value(it);
default: default:
return FX_ITERATOR_VALUE_NULL; return FX_VALUE_EMPTY;
} }
} }
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj) static fx_status add(const fx_value *l, const fx_value *r, fx_value *out)
{ {
struct fx_string_iterator_p *it = fx_object_get_private( fx_string *left = l->v_object;
obj, fx_string *right = r->v_object;
FX_TYPE_STRING_ITERATOR); fx_string *result = fx_string_create_from_cstr(
fx_string_get_cstr(left));
switch (it->_m) { if (!result) {
case ITERATOR_MODE_CHARS: return FX_ERR_NO_MEMORY;
return chars_iterator_get_value(it);
case ITERATOR_MODE_TOKENS:
return tokens_iterator_get_value(it);
default:
return FX_ITERATOR_VALUE_NULL;
} }
fx_string_append_cstr(result, fx_string_get_cstr(right));
*out = FX_VALUE_OBJECT(result);
return FX_SUCCESS;
} }
#define STRING_CONVERTER(fx_type, c_type) \
static fx_status to_##fx_type(const fx_value *in, c_type *out) \
{ \
const char *s = NULL; \
fx_value_get_cstr(in, &s); \
return cstr_to_##fx_type(s, out); \
}
STRING_CONVERTER(bool, bool)
STRING_CONVERTER(i16, i16)
STRING_CONVERTER(u16, u16)
STRING_CONVERTER(i32, i32)
STRING_CONVERTER(u32, u32)
STRING_CONVERTER(i64, i64)
STRING_CONVERTER(u64, u64)
STRING_CONVERTER(iptr, iptr)
STRING_CONVERTER(uptr, uptr)
STRING_CONVERTER(sbyte, sbyte)
STRING_CONVERTER(byte, byte)
STRING_CONVERTER(short, short)
STRING_CONVERTER(ushort, unsigned short)
STRING_CONVERTER(int, int)
STRING_CONVERTER(uint, unsigned int)
STRING_CONVERTER(long, long)
STRING_CONVERTER(ulong, unsigned long)
STRING_CONVERTER(longlong, long long)
STRING_CONVERTER(ulonglong, unsigned long long)
STRING_CONVERTER(size, size_t)
STRING_CONVERTER(float, float)
STRING_CONVERTER(double, double)
STRING_CONVERTER(cstr, const char *)
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
// ---- fx_string DEFINITION // ---- fx_string DEFINITION
FX_TYPE_CLASS_BEGIN(fx_string) FX_TYPE_CLASS_BEGIN(fx_string)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = string_to_string; FX_INTERFACE_ENTRY(to_string) = string_to_string;
FX_INTERFACE_ENTRY(hash) = string_hash;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
@@ -1719,6 +1778,40 @@ FX_TYPE_CLASS_BEGIN(fx_string)
FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin; FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
FX_INTERFACE_ENTRY(op_add) = add;
FX_TYPE_VTABLE_INTERFACE_END(fx_operable, FX_TYPE_OPERABLE)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_convertible, FX_TYPE_CONVERTIBLE)
FX_INTERFACE_ENTRY(c_to_bool) = to_bool;
FX_INTERFACE_ENTRY(c_to_bool) = to_bool;
FX_INTERFACE_ENTRY(c_to_i16) = to_i16;
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
FX_INTERFACE_ENTRY(c_to_u32) = to_u32;
FX_INTERFACE_ENTRY(c_to_i64) = to_i64;
FX_INTERFACE_ENTRY(c_to_u64) = to_u64;
FX_INTERFACE_ENTRY(c_to_iptr) = to_iptr;
FX_INTERFACE_ENTRY(c_to_uptr) = to_uptr;
FX_INTERFACE_ENTRY(c_to_sbyte) = to_sbyte;
FX_INTERFACE_ENTRY(c_to_byte) = to_byte;
FX_INTERFACE_ENTRY(c_to_short) = to_short;
FX_INTERFACE_ENTRY(c_to_ushort) = to_ushort;
FX_INTERFACE_ENTRY(c_to_int) = to_int;
FX_INTERFACE_ENTRY(c_to_uint) = to_uint;
FX_INTERFACE_ENTRY(c_to_long) = to_long;
FX_INTERFACE_ENTRY(c_to_ulong) = to_ulong;
FX_INTERFACE_ENTRY(c_to_longlong) = to_longlong;
FX_INTERFACE_ENTRY(c_to_ulonglong) = to_ulonglong;
FX_INTERFACE_ENTRY(c_to_size) = to_size;
FX_INTERFACE_ENTRY(c_to_float) = to_float;
FX_INTERFACE_ENTRY(c_to_double) = to_double;
FX_INTERFACE_ENTRY(c_to_cstr) = to_cstr;
FX_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_comparable, FX_TYPE_COMPARABLE)
FX_INTERFACE_ENTRY(c_compare) = compare;
FX_TYPE_VTABLE_INTERFACE_END(fx_comparable, FX_TYPE_COMPARABLE)
FX_TYPE_CONSTRUCTOR("create", fx_string_create, 0, FX_TYPE_VOID); FX_TYPE_CONSTRUCTOR("create", fx_string_create, 0, FX_TYPE_VOID);
FX_TYPE_CONSTRUCTOR( FX_TYPE_CONSTRUCTOR(
"create_from_cstr", "create_from_cstr",
@@ -1731,12 +1824,17 @@ FX_TYPE_CLASS_BEGIN(fx_string)
fx_string_get_cstr, fx_string_get_cstr,
0, 0,
FX_TYPE_STRING); FX_TYPE_STRING);
FX_TYPE_PROPERTY("length", get_length, NULL);
FX_TYPE_CLASS_END(fx_string) FX_TYPE_CLASS_END(fx_string)
FX_TYPE_DEFINITION_BEGIN(fx_string) FX_TYPE_DEFINITION_BEGIN(fx_string)
FX_TYPE_ID(0x200194f6, 0x0327, 0x4a82, 0xb9c9, 0xb62ddd038c33); FX_TYPE_ID(0x200194f6, 0x0327, 0x4a82, 0xb9c9, 0xb62ddd038c33);
FX_TYPE_NAME("fx.string"); FX_TYPE_NAME("fx.string");
FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE); FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE);
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
FX_TYPE_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
FX_TYPE_CLASS(fx_string_class); FX_TYPE_CLASS(fx_string_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_string_p); FX_TYPE_INSTANCE_PRIVATE(struct fx_string_p);
FX_TYPE_INSTANCE_INIT(string_init); FX_TYPE_INSTANCE_INIT(string_init);
@@ -1753,7 +1851,6 @@ FX_TYPE_CLASS_BEGIN(fx_string_iterator)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next; FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = NULL; FX_INTERFACE_ENTRY(it_erase) = NULL;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value; FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_string_iterator) FX_TYPE_CLASS_END(fx_string_iterator)
+3 -1
View File
@@ -17,8 +17,10 @@ int main(void)
size_t nr_delims = sizeof delims / sizeof delims[0]; size_t nr_delims = sizeof delims / sizeof delims[0];
fx_iterator *it = fx_string_tokenise(str, delims, nr_delims, 0); fx_iterator *it = fx_string_tokenise(str, delims, nr_delims, 0);
fx_foreach(const char *, tok, it) fx_foreach(val, it)
{ {
const char *tok = NULL;
fx_value_get_cstr(&val, &tok);
printf("%s\n", tok); printf("%s\n", tok);
} }
fx_iterator_unref(it); fx_iterator_unref(it);
+19 -2
View File
@@ -1,10 +1,27 @@
#include <fx/value-type.h> #include <fx/value-type.h>
#include <fx/value.h> #include <fx/value.h>
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
int main(void) int main(void)
{ {
fx_value v = FX_I32(1024); fx_value x = FX_I32(1024);
printf("%u\n", __fx_type_get_value_type(FX_TYPE_I64)); fx_value y = FX_I16(512);
fx_value z = FX_VALUE_EMPTY;
fx_type_id common_type = fx_value_get_common_type(&x, &y);
fx_value_change_type(&x, &x, common_type);
fx_value_change_type(&y, &y, common_type);
fx_status status = fx_value_add(&x, &y, &z);
if (!FX_OK(status)) {
printf("add failed\n");
return -1;
}
u64 result = 0;
fx_value_get_u64(&z, &result);
printf("result: %" PRIu64 "\n", result);
return 0; return 0;
} }
+40
View File
@@ -8,6 +8,7 @@
#include <fx/int.h> #include <fx/int.h>
#include <fx/object.h> #include <fx/object.h>
#include <fx/reflection/function.h> #include <fx/reflection/function.h>
#include <fx/reflection/property.h>
#include <fx/type.h> #include <fx/type.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -362,6 +363,29 @@ fx_result fx_type_add_function(fx_type_info *info, struct _fx_object *func)
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
fx_result fx_type_add_property(fx_type_info *info, struct _fx_object *prop)
{
const char *name = fx_property_get_name(prop);
struct fx_namemap_entry *entry = fx_namemap_get(
&info->ty_functions,
name);
if (entry) {
return FX_RESULT_ERR(NAME_EXISTS);
}
struct fx_type_property *ty_prop = malloc(sizeof *ty_prop);
if (!ty_prop) {
return FX_RESULT_ERR(NO_MEMORY);
}
memset(ty_prop, 0x0, sizeof *ty_prop);
ty_prop->p_prop = prop;
fx_namemap_put(&info->ty_properties, name, &ty_prop->p_entry);
return FX_RESULT_SUCCESS;
}
struct fx_type_info *fx_type_info_get_by_id(const union fx_type_id *key) struct fx_type_info *fx_type_info_get_by_id(const union fx_type_id *key)
{ {
return get_type(&type_idmap, key); return get_type(&type_idmap, key);
@@ -392,3 +416,19 @@ fx_function *fx_type_info_get_function_by_name(
f_entry); f_entry);
return func->f_func; return func->f_func;
} }
fx_function *fx_type_info_get_property_by_name(
const struct fx_type_info *ty,
const char *name)
{
fx_namemap_entry *entry = fx_namemap_get(&ty->ty_properties, name);
if (!entry) {
return NULL;
}
struct fx_type_property *prop = fx_unbox(
struct fx_type_property,
entry,
p_entry);
return prop->p_prop;
}
+9
View File
@@ -5,6 +5,7 @@
#include <fx/namemap.h> #include <fx/namemap.h>
#include <fx/queue.h> #include <fx/queue.h>
#include <fx/reflection/function.h> #include <fx/reflection/function.h>
#include <fx/reflection/property.h>
#include <fx/reflection/type.h> #include <fx/reflection/type.h>
#include <fx/type.h> #include <fx/type.h>
#include <stddef.h> #include <stddef.h>
@@ -24,6 +25,11 @@ struct fx_type_function {
fx_function *f_func; fx_function *f_func;
}; };
struct fx_type_property {
struct fx_namemap_entry p_entry;
fx_property *p_prop;
};
extern struct fx_type_registration *fx_type_get_registration(fx_type_id id); extern struct fx_type_registration *fx_type_get_registration(fx_type_id id);
extern struct fx_type_component *fx_type_get_component( extern struct fx_type_component *fx_type_get_component(
const fx_bst *tree, const fx_bst *tree,
@@ -33,5 +39,8 @@ extern struct fx_type_info *fx_type_info_get_by_name(const char *name);
extern fx_function *fx_type_info_get_function_by_name( extern fx_function *fx_type_info_get_function_by_name(
const struct fx_type_info *ty, const struct fx_type_info *ty,
const char *name); const char *name);
extern fx_property *fx_type_info_get_property_by_name(
const struct fx_type_info *ty,
const char *name);
#endif #endif
+7 -2
View File
@@ -2,6 +2,7 @@
#include <fx/string.h> #include <fx/string.h>
#include <fx/stringstream.h> #include <fx/stringstream.h>
#include <fx/uuid.h> #include <fx/uuid.h>
#include <fx/value.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -230,11 +231,15 @@ static void uuid_fini(fx_object *obj, void *priv)
struct fx_uuid_p *uuid = priv; struct fx_uuid_p *uuid = priv;
} }
static void uuid_to_string(const fx_object *uuid, fx_stream *out) static fx_status uuid_to_string(
const fx_value *uuid,
fx_stream *out,
const char *format)
{ {
char str[FX_UUID_STRING_MAX]; char str[FX_UUID_STRING_MAX];
fx_uuid_to_cstr(uuid, str); fx_uuid_to_cstr(uuid->v_object, str);
fx_stream_write_cstr(out, str, NULL); fx_stream_write_cstr(out, str, NULL);
return FX_SUCCESS;
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/