fx: update fx_iterator to return pointers to values

This commit is contained in:
2026-05-28 20:50:00 +01:00
parent 01cbcf712a
commit f79650698a
5 changed files with 84 additions and 129 deletions
+6 -13
View File
@@ -10,15 +10,10 @@
FX_DECLS_BEGIN;
#define fx_foreach(var, iterator) \
for (fx_value var = fx_iterator_get_value(iterator); \
for (const fx_value *var = fx_iterator_get_value(iterator); \
FX_OK(fx_iterator_get_status(iterator)); \
fx_iterator_move_next(iterator), \
var = fx_iterator_get_value(iterator))
#define fx_foreach_ptr(type, var, iterator) \
for (type *var = (type *)fx_iterator_get_value(iterator).v_ptr; \
FX_OK(fx_iterator_get_status(iterator)); \
fx_iterator_move_next(iterator), \
var = (type *)fx_iterator_get_value(iterator).v_ptr)
var = fx_iterator_get_value(iterator))
#define FX_ITERATOR_VALUE_INT(v) ((fx_iterator_value) {.v_int = (v)})
#define FX_ITERATOR_VALUE_PTR(v) ((fx_iterator_value) {.v_ptr = (v)})
@@ -36,12 +31,11 @@ FX_DECLARE_TYPE(fx_iterable);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterator)
fx_status (*it_move_next)(const fx_iterator *);
fx_status (*it_erase)(fx_iterator *);
fx_value (*it_get_value)(const fx_iterator *);
const fx_value *(*it_get_value)(const fx_iterator *);
FX_TYPE_CLASS_DECLARATION_END(fx_iterator)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterable)
fx_iterator *(*it_begin)(fx_iterable *);
const fx_iterator *(*it_cbegin)(const fx_iterable *);
const fx_iterator *(*it_begin)(const fx_iterable *);
FX_TYPE_CLASS_DECLARATION_END(fx_iterable)
FX_API fx_type_id fx_iterator_get_type(void);
@@ -56,15 +50,14 @@ static inline void fx_iterator_unref(const fx_iterator *p)
fx_object_unref((fx_object *)p);
}
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_begin(const fx_iterable *it);
FX_API fx_status fx_iterator_get_status(const fx_iterator *it);
FX_API fx_status
fx_iterator_set_status(const fx_iterator *it, fx_status status);
FX_API fx_status fx_iterator_move_next(const fx_iterator *it);
FX_API fx_value fx_iterator_get_value(const fx_iterator *it);
FX_API const fx_value *fx_iterator_get_value(const fx_iterator *it);
FX_API fx_status fx_iterator_erase(fx_iterator *it);
static inline bool fx_iterator_is_valid(const fx_iterator *it)
{
+3 -13
View File
@@ -23,7 +23,7 @@ static enum fx_status iterator_set_status(
/*** PUBLIC FUNCTIONS *********************************************************/
fx_iterator *fx_iterator_begin(fx_iterable *it)
const fx_iterator *fx_iterator_begin(const fx_iterable *it)
{
FX_CLASS_DISPATCH_VIRTUAL_0(
fx_iterable,
@@ -33,16 +33,6 @@ fx_iterator *fx_iterator_begin(fx_iterable *it)
it);
}
const fx_iterator *fx_iterator_cbegin(const fx_iterable *it)
{
FX_CLASS_DISPATCH_VIRTUAL_0(
fx_iterable,
FX_TYPE_ITERABLE,
NULL,
it_cbegin,
it);
}
enum fx_status fx_iterator_get_status(const fx_iterator *it)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ITERATOR, iterator_get_status, it);
@@ -74,12 +64,12 @@ enum fx_status fx_iterator_move_next(const fx_iterator *it)
return status;
}
fx_value fx_iterator_get_value(const fx_iterator *it)
const fx_value *fx_iterator_get_value(const fx_iterator *it)
{
FX_CLASS_DISPATCH_VIRTUAL_0(
fx_iterator,
FX_TYPE_ITERATOR,
FX_VALUE_EMPTY,
NULL,
it_get_value,
it);
}
+19 -27
View File
@@ -41,16 +41,14 @@ fx_result fx_object_instantiate(
out->obj_type = type;
out->obj_ref = 1;
struct fx_queue_entry *entry = fx_queue_first(
&type->ty_class_hierarchy);
struct fx_queue_entry *entry
= fx_queue_first(&type->ty_class_hierarchy);
while (entry) {
struct fx_type_component *comp = fx_unbox(
struct fx_type_component,
entry,
c_entry);
struct fx_type_component *comp
= fx_unbox(struct fx_type_component, entry, c_entry);
const struct fx_type_info *class_info = comp->c_type;
void *private_data = (char *)out
+ comp->c_instance_private_data_offset;
void *private_data
= (char *)out + comp->c_instance_private_data_offset;
if (class_info->ty_instance_init) {
class_info->ty_instance_init(out, private_data);
@@ -90,7 +88,7 @@ fx_status fx_object_to_string(
fx_stream *out,
const char *format)
{
fx_value value = FX_VALUE_OBJECT(p);
fx_value value = FX_VALUE_OBJECT_REF(p);
fx_object_class *iface = fx_object_get_interface(p, FX_TYPE_OBJECT);
if (iface && iface->to_string) {
return iface->to_string(&value, out, format);
@@ -106,9 +104,8 @@ bool fx_object_is_type(const struct _fx_object *p, fx_type_id type)
return true;
}
struct fx_type_component *comp = fx_type_get_component(
&p->obj_type->ty_components,
type);
struct fx_type_component *comp
= fx_type_get_component(&p->obj_type->ty_components, type);
return comp != NULL;
}
@@ -125,9 +122,8 @@ void *fx_object_get_private(const struct _fx_object *object, fx_type_id type)
return (char *)object + object->obj_main_priv_offset;
}
struct fx_type_component *comp = fx_type_get_component(
&object->obj_type->ty_components,
type);
struct fx_type_component *comp
= fx_type_get_component(&object->obj_type->ty_components, type);
if (!comp) {
return NULL;
}
@@ -143,9 +139,8 @@ void *fx_object_get_protected(const struct _fx_object *object, fx_type_id type)
assert(object->obj_magic == FX_OBJECT_MAGIC);
struct fx_type_component *comp = fx_type_get_component(
&object->obj_type->ty_components,
type);
struct fx_type_component *comp
= fx_type_get_component(&object->obj_type->ty_components, type);
if (!comp) {
return NULL;
}
@@ -177,9 +172,8 @@ enum fx_status fx_object_get_data(
assert(object->obj_magic == FX_OBJECT_MAGIC);
struct fx_type_component *comp = fx_type_get_component(
&object->obj_type->ty_components,
type);
struct fx_type_component *comp
= fx_type_get_component(&object->obj_type->ty_components, type);
if (!comp) {
return FX_ERR_INVALID_ARGUMENT;
}
@@ -218,14 +212,12 @@ void fx_object_unref(struct _fx_object *p)
struct fx_queue_entry *cur = fx_queue_last(&type->ty_class_hierarchy);
while (cur) {
struct fx_type_component *comp = fx_unbox(
struct fx_type_component,
cur,
c_entry);
struct fx_type_component *comp
= fx_unbox(struct fx_type_component, cur, c_entry);
const struct fx_type_info *class_info = comp->c_type;
void *private_data = (char *)p
+ comp->c_instance_private_data_offset;
void *private_data
= (char *)p + comp->c_instance_private_data_offset;
if (class_info->ty_instance_fini) {
class_info->ty_instance_fini(p, private_data);
+55 -75
View File
@@ -68,6 +68,7 @@ struct fx_string_iterator_p {
const char *string_value;
size_t string_length;
size_t string_codepoints;
fx_value value;
};
/*** PRIVATE FUNCTIONS ********************************************************/
@@ -293,9 +294,8 @@ static fx_string *string_duplicate(const struct fx_string_p *str)
return NULL;
}
struct fx_string_p *new_str_p = fx_object_get_private(
new_str,
FX_TYPE_STRING);
struct fx_string_p *new_str_p
= fx_object_get_private(new_str, FX_TYPE_STRING);
string_change_capacity(new_str_p, str->s_len);
const char *src = string_ptr(str);
@@ -397,9 +397,8 @@ static enum fx_status replace_utf8(
}
size_t new_data_nr_bytes = strlen(new_data);
size_t new_data_nr_codepoints = fx_wchar_utf8_codepoint_count(
new_data,
new_data_nr_bytes);
size_t new_data_nr_codepoints
= fx_wchar_utf8_codepoint_count(new_data, new_data_nr_bytes);
if (new_data_nr_codepoints == 0) {
/* new_data is not a valid utf-8 string */
return FX_ERR_INVALID_ARGUMENT;
@@ -417,8 +416,8 @@ static enum fx_status replace_utf8(
return status;
}
size_t new_total_bytes = str->s_len - old_data_nr_bytes
+ new_data_nr_bytes;
size_t new_total_bytes
= str->s_len - old_data_nr_bytes + new_data_nr_bytes;
if (new_total_bytes > str->s_max) {
status = string_reserve(str, new_total_bytes);
}
@@ -747,9 +746,8 @@ static enum fx_status string_insert_wstr_ansi(
at = dest->s_len;
}
size_t utf8_encoded_size = fx_wchar_utf8_string_encoded_size(
src,
nr_codepoints);
size_t utf8_encoded_size
= fx_wchar_utf8_string_encoded_size(src, nr_codepoints);
if (utf8_encoded_size == 0) {
return FX_ERR_INVALID_ARGUMENT;
}
@@ -796,9 +794,8 @@ static enum fx_status string_insert_wstr_utf8(
codepoint_offset = dest->s_codepoints;
}
size_t utf8_encoded_size = fx_wchar_utf8_string_encoded_size(
src,
nr_codepoints);
size_t utf8_encoded_size
= fx_wchar_utf8_string_encoded_size(src, nr_codepoints);
if (utf8_encoded_size == 0) {
return FX_ERR_INVALID_ARGUMENT;
}
@@ -1041,9 +1038,8 @@ static fx_iterator *string_tokenise(
}
fx_string_iterator *it_obj = fx_object_create(FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it = fx_object_get_private(
it_obj,
FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_STRING_ITERATOR);
it->_m = ITERATOR_MODE_TOKENS;
it->_d = delims;
@@ -1152,9 +1148,8 @@ static fx_string *string_substr(
}
fx_string *newstr = fx_string_create();
struct fx_string_p *newstr_p = fx_object_get_private(
newstr,
FX_TYPE_STRING);
struct fx_string_p *newstr_p
= fx_object_get_private(newstr, FX_TYPE_STRING);
string_reserve(newstr_p, len);
const char *src = string_ptr(str) + start;
@@ -1304,12 +1299,10 @@ enum fx_status fx_string_insert_s(
const fx_string *src,
size_t at)
{
struct fx_string_p *dest_p = fx_object_get_private(
dest,
FX_TYPE_STRING);
const struct fx_string_p *src_p = fx_object_get_private(
src,
FX_TYPE_STRING);
struct fx_string_p *dest_p
= fx_object_get_private(dest, FX_TYPE_STRING);
const struct fx_string_p *src_p
= fx_object_get_private(src, FX_TYPE_STRING);
return string_insert_s(dest_p, src_p, at);
}
@@ -1318,9 +1311,8 @@ enum fx_status fx_string_insert_cstr(
const char *src,
size_t at)
{
struct fx_string_p *dest_p = fx_object_get_private(
dest,
FX_TYPE_STRING);
struct fx_string_p *dest_p
= fx_object_get_private(dest, FX_TYPE_STRING);
return string_insert_cstr(dest_p, src, strlen(src), at);
}
@@ -1329,9 +1321,8 @@ enum fx_status fx_string_insert_wstr(
const fx_wchar *src,
size_t at)
{
struct fx_string_p *dest_p = fx_object_get_private(
dest,
FX_TYPE_STRING);
struct fx_string_p *dest_p
= fx_object_get_private(dest, FX_TYPE_STRING);
return string_insert_wstr(dest_p, src, fx_wstrlen(src), at);
}
@@ -1341,9 +1332,8 @@ enum fx_status fx_string_insert_cstrf(
const char *format,
...)
{
struct fx_string_p *dest_p = fx_object_get_private(
dest,
FX_TYPE_STRING);
struct fx_string_p *dest_p
= fx_object_get_private(dest, FX_TYPE_STRING);
va_list arg;
va_start(arg, format);
@@ -1370,9 +1360,8 @@ enum fx_status fx_string_insert_cstrn(
enum fx_status fx_string_append_cstrf(fx_string *dest, const char *format, ...)
{
struct fx_string_p *dest_p = fx_object_get_private(
dest,
FX_TYPE_STRING);
struct fx_string_p *dest_p
= fx_object_get_private(dest, FX_TYPE_STRING);
va_list arg;
va_start(arg, format);
@@ -1384,9 +1373,8 @@ enum fx_status fx_string_append_cstrf(fx_string *dest, const char *format, ...)
enum fx_status fx_string_prepend_cstrf(fx_string *dest, const char *format, ...)
{
struct fx_string_p *dest_p = fx_object_get_private(
dest,
FX_TYPE_STRING);
struct fx_string_p *dest_p
= fx_object_get_private(dest, FX_TYPE_STRING);
va_list arg;
va_start(arg, format);
@@ -1539,9 +1527,8 @@ static fx_status string_to_string(
fx_stream *out,
const char *format)
{
struct fx_string_p *str = fx_object_get_private(
obj->v_object,
FX_TYPE_STRING);
struct fx_string_p *str
= fx_object_get_private(obj->v_object, FX_TYPE_STRING);
const char *s = string_ptr(str);
for (size_t i = 0; i < str->s_len; i++) {
fx_stream_write_char(out, s[i]);
@@ -1552,9 +1539,8 @@ static fx_status string_to_string(
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);
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;
@@ -1570,7 +1556,9 @@ static i32 compare(const fx_value *left, const fx_value *right)
return -1;
}
return strcmp(left_cstr, right_cstr);
int result = strcmp(left_cstr, right_cstr);
return result;
}
static fx_status get_length(
@@ -1588,9 +1576,8 @@ static fx_status get_length(
static void iterator_fini(fx_iterator *obj)
{
struct fx_string_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_STRING_ITERATOR);
if (it->_tmp) {
fx_string_unref(it->_tmp);
}
@@ -1598,12 +1585,11 @@ static void iterator_fini(fx_iterator *obj)
memset(it, 0x0, sizeof *it);
}
static fx_iterator *iterator_begin(fx_object *obj)
static const fx_iterator *iterator_begin(const fx_object *obj)
{
fx_string_iterator *it_obj = fx_object_create(FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it = fx_object_get_private(
it_obj,
FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_STRING_ITERATOR);
struct fx_string_p *p = fx_object_get_private(obj, FX_TYPE_STRING);
if (!p->s_len) {
@@ -1623,11 +1609,6 @@ static fx_iterator *iterator_begin(fx_object *obj)
return it_obj;
}
static const fx_iterator *iterator_cbegin(const fx_object *obj)
{
return iterator_begin((fx_object *)obj);
}
static enum fx_status chars_iterator_move_next(struct fx_string_iterator_p *it)
{
if (!it->_s_p) {
@@ -1676,9 +1657,8 @@ static enum fx_status tokens_iterator_move_next(struct fx_string_iterator_p *it)
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
struct fx_string_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_STRING_ITERATOR);
switch (it->_m) {
case ITERATOR_MODE_CHARS:
@@ -1690,21 +1670,22 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
}
}
static fx_value chars_iterator_get_value(struct fx_string_iterator_p *it)
static fx_value *chars_iterator_get_value(struct fx_string_iterator_p *it)
{
return FX_U32(it->char_value);
it->value = FX_WCHAR(it->char_value);
return &it->value;
}
static fx_value tokens_iterator_get_value(struct fx_string_iterator_p *it)
static fx_value *tokens_iterator_get_value(struct fx_string_iterator_p *it)
{
return FX_CSTR(it->string_value);
it->value = FX_CSTR(it->string_value);
return &it->value;
}
static fx_value iterator_get_value(const fx_iterator *obj)
static const fx_value *iterator_get_value(const fx_iterator *obj)
{
struct fx_string_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_STRING_ITERATOR);
switch (it->_m) {
case ITERATOR_MODE_CHARS:
@@ -1712,7 +1693,7 @@ static fx_value iterator_get_value(const fx_iterator *obj)
case ITERATOR_MODE_TOKENS:
return tokens_iterator_get_value(it);
default:
return FX_VALUE_EMPTY;
return NULL;
}
}
@@ -1720,8 +1701,8 @@ static fx_status add(const fx_value *l, const fx_value *r, fx_value *out)
{
fx_string *left = l->v_object;
fx_string *right = r->v_object;
fx_string *result = fx_string_create_from_cstr(
fx_string_get_cstr(left));
fx_string *result
= fx_string_create_from_cstr(fx_string_get_cstr(left));
if (!result) {
return FX_ERR_NO_MEMORY;
}
@@ -1775,7 +1756,6 @@ FX_TYPE_CLASS_BEGIN(fx_string)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
+1 -1
View File
@@ -20,7 +20,7 @@ int main(void)
fx_foreach(val, it)
{
const char *tok = NULL;
fx_value_get_cstr(&val, &tok);
fx_value_get_cstr(val, &tok);
printf("%s\n", tok);
}
fx_iterator_unref(it);