Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 01cbcf712a | |||
| 5e3439bc4a | |||
| 56e1fae273 | |||
| c5eac75a0b | |||
| aea3f99859 | |||
| cca52c788a | |||
| 74fbd94f95 | |||
| 97fe00d4a1 | |||
| 0be82c63fa | |||
| 2df76a76c4 | |||
| 9d4aab49ac | |||
| d84d56681f | |||
| 6efe473c11 | |||
| 5d0aa577d6 | |||
| f9b456a99f | |||
| 673bf07cf6 | |||
| 82bb20c6af | |||
| 12b04eea8d | |||
| 0f1dfc0b07 | |||
| 13332145c2 | |||
| 7cdad180da | |||
| 554a30fa1a | |||
| 5058bb164a | |||
| 79a8041aac | |||
| bc902047d3 | |||
| 61182e0153 | |||
| f1258489d1 | |||
| 65a7a025c5 |
@@ -6,6 +6,16 @@ include (TestBigEndian)
|
||||
include(Templates)
|
||||
include(Platform)
|
||||
|
||||
test_big_endian(is_big_endian)
|
||||
if (is_big_endian)
|
||||
message(STATUS "Target is Big Endian")
|
||||
add_compile_definitions(FX_ENDIAN=1)
|
||||
else ()
|
||||
message(STATUS "Target is Little Endian")
|
||||
add_compile_definitions(FX_ENDIAN=0)
|
||||
endif ()
|
||||
|
||||
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_C_EXTENSIONS OFF)
|
||||
|
||||
|
||||
@@ -7,8 +7,7 @@ FX_ASSEMBLY_BEGIN()
|
||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "array", fx_array);
|
||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "bitbuffer", fx_bitbuffer);
|
||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "bitmap", fx_bitmap);
|
||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "dict", fx_dict);
|
||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "hashtable", fx_hashtable);
|
||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "hashmap", fx_hashmap);
|
||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "list", fx_list);
|
||||
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "tree", fx_tree);
|
||||
FX_ASSEMBLY_END()
|
||||
|
||||
+4
-10
@@ -18,10 +18,12 @@ function(add_fx_assembly)
|
||||
|
||||
set(assembly_include_paths "")
|
||||
set(assembly_sources ${CMAKE_CURRENT_SOURCE_DIR}/assembly.c)
|
||||
set(assembly_headers)
|
||||
|
||||
foreach (dir ${arg_NAMESPACES})
|
||||
add_subdirectory(${fx_source_root}/${dir} ${fx_build_root}/namespaces/${dir})
|
||||
set(assembly_sources ${assembly_sources} ${namespace_sources})
|
||||
set(assembly_headers ${assembly_headers} ${namespace_headers})
|
||||
set(assembly_include_paths ${assembly_include_paths} ${namespace_include_paths})
|
||||
endforeach (dir)
|
||||
|
||||
@@ -59,17 +61,7 @@ function(add_fx_assembly)
|
||||
set_target_properties(${assembly_target_name} PROPERTIES
|
||||
FOLDER "${assembly_name}")
|
||||
|
||||
TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
|
||||
if(IS_BIG_ENDIAN)
|
||||
target_compile_definitions(${assembly_target_name} PRIVATE
|
||||
BIG_ENDIAN)
|
||||
else()
|
||||
target_compile_definitions(${assembly_target_name} PRIVATE
|
||||
LITTLE_ENDIAN)
|
||||
endif()
|
||||
|
||||
install(TARGETS ${assembly_target_name})
|
||||
install(FILES ${headers} DESTINATION include/${assembly_path})
|
||||
endfunction(add_fx_assembly)
|
||||
|
||||
macro(export_fx_namespace_details ns_name)
|
||||
@@ -96,6 +88,7 @@ macro(export_fx_namespace_details ns_name)
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/sys/${fx_system_name}/${fx_system_arch}/*.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/sys/${fx_system_name}/${fx_system_arch}/*.S)
|
||||
file(GLOB headers include/${namespace_path}/*.h)
|
||||
set(namespace_headers ${headers} PARENT_SCOPE)
|
||||
set(namespace_sources
|
||||
${namespace_sources}
|
||||
${base_namespace_sources}
|
||||
@@ -110,5 +103,6 @@ macro(export_fx_namespace_details ns_name)
|
||||
PARENT_SCOPE)
|
||||
set(internal_libs ${internal_libs} PARENT_SCOPE)
|
||||
set(internal_defines ${internal_defines} PARENT_SCOPE)
|
||||
install(FILES ${headers} DESTINATION include/${namespace_path})
|
||||
endmacro(export_fx_namespace_details)
|
||||
|
||||
|
||||
+89
-109
@@ -2,6 +2,7 @@
|
||||
#include <fx/iterator.h>
|
||||
#include <fx/stream.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/value.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -12,7 +13,7 @@ struct fx_array_p {
|
||||
size_t ar_len;
|
||||
/* maximum number of items that can currently be stored in array */
|
||||
size_t ar_cap;
|
||||
fx_object **ar_data;
|
||||
fx_value *ar_data;
|
||||
};
|
||||
|
||||
struct fx_array_iterator_p {
|
||||
@@ -22,7 +23,7 @@ struct fx_array_iterator_p {
|
||||
/** The index of the current value */
|
||||
size_t i;
|
||||
/** The current value */
|
||||
fx_object *value;
|
||||
fx_value *value;
|
||||
};
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
@@ -32,7 +33,7 @@ static fx_status resize_array(struct fx_array_p *array, size_t new_capacity)
|
||||
if (array->ar_cap < new_capacity) {
|
||||
void *new_data = realloc(
|
||||
array->ar_data,
|
||||
new_capacity * sizeof(struct fx_dsref *));
|
||||
new_capacity * sizeof(fx_value));
|
||||
if (!new_data) {
|
||||
return FX_ERR_NO_MEMORY;
|
||||
}
|
||||
@@ -40,12 +41,12 @@ static fx_status resize_array(struct fx_array_p *array, size_t new_capacity)
|
||||
array->ar_data = new_data;
|
||||
} else {
|
||||
for (size_t i = new_capacity; i < array->ar_len; i++) {
|
||||
fx_object_unref(array->ar_data[i]);
|
||||
fx_value_unset(&array->ar_data[i]);
|
||||
}
|
||||
|
||||
void *new_data = realloc(
|
||||
array->ar_data,
|
||||
new_capacity * sizeof(struct fx_dsref *));
|
||||
new_capacity * sizeof(fx_value));
|
||||
if (!new_data) {
|
||||
return FX_ERR_NO_MEMORY;
|
||||
}
|
||||
@@ -63,7 +64,7 @@ static fx_status resize_array(struct fx_array_p *array, size_t new_capacity)
|
||||
|
||||
static fx_status array_insert(
|
||||
struct fx_array_p *array,
|
||||
fx_object *value,
|
||||
fx_value value,
|
||||
size_t at)
|
||||
{
|
||||
if (at == FX_NPOS) {
|
||||
@@ -84,13 +85,13 @@ static fx_status array_insert(
|
||||
}
|
||||
}
|
||||
|
||||
fx_object **src = array->ar_data + at;
|
||||
fx_object **dest = src + 1;
|
||||
size_t move_len = (array->ar_len - at) * sizeof(struct fx_dsref *);
|
||||
fx_value *src = array->ar_data + at;
|
||||
fx_value *dest = src + 1;
|
||||
size_t move_len = (array->ar_len - at) * sizeof(fx_value);
|
||||
|
||||
memmove(dest, src, move_len);
|
||||
|
||||
array->ar_data[at] = fx_object_ref(value);
|
||||
fx_value_copy(&array->ar_data[at], &value);
|
||||
array->ar_len++;
|
||||
|
||||
return FX_SUCCESS;
|
||||
@@ -102,11 +103,11 @@ static fx_status array_remove(struct fx_array_p *array, size_t at)
|
||||
return FX_ERR_OUT_OF_BOUNDS;
|
||||
}
|
||||
|
||||
fx_object **src = array->ar_data + at;
|
||||
fx_object **dest = src + 1;
|
||||
size_t move_len = array->ar_len * sizeof(struct fx_dsref *);
|
||||
fx_value *src = array->ar_data + at;
|
||||
fx_value *dest = src + 1;
|
||||
size_t move_len = array->ar_len * sizeof(fx_value);
|
||||
|
||||
fx_object_unref(array->ar_data[at]);
|
||||
fx_value_unset(&array->ar_data[at]);
|
||||
|
||||
memmove(dest, src, move_len);
|
||||
|
||||
@@ -125,17 +126,17 @@ static fx_status array_remove_back(struct fx_array_p *array)
|
||||
return array_remove(array, array->ar_len - 1);
|
||||
}
|
||||
|
||||
static fx_object *array_pop(struct fx_array_p *array, size_t at)
|
||||
static fx_value array_pop(struct fx_array_p *array, size_t at)
|
||||
{
|
||||
if (at >= array->ar_len) {
|
||||
return NULL;
|
||||
return FX_VALUE_EMPTY;
|
||||
}
|
||||
|
||||
fx_object **src = array->ar_data + at;
|
||||
fx_object **dest = src + 1;
|
||||
size_t move_len = array->ar_len * sizeof(struct fx_dsref *);
|
||||
fx_value *dest = array->ar_data + at;
|
||||
fx_value *src = dest + 1;
|
||||
size_t move_len = array->ar_len * sizeof(fx_value);
|
||||
|
||||
fx_object *out = array->ar_data[at];
|
||||
fx_value out = array->ar_data[at];
|
||||
|
||||
memmove(dest, src, move_len);
|
||||
|
||||
@@ -144,40 +145,42 @@ static fx_object *array_pop(struct fx_array_p *array, size_t at)
|
||||
return out;
|
||||
}
|
||||
|
||||
static fx_object *array_pop_front(struct fx_array_p *array)
|
||||
static fx_value array_pop_front(struct fx_array_p *array)
|
||||
{
|
||||
return array_pop(array, 0);
|
||||
}
|
||||
|
||||
static fx_object *array_pop_back(struct fx_array_p *array)
|
||||
static fx_value array_pop_back(struct fx_array_p *array)
|
||||
{
|
||||
return array_pop(array, array->ar_len - 1);
|
||||
}
|
||||
|
||||
static fx_object *array_at(const struct fx_array_p *array, size_t at)
|
||||
static fx_value *array_get_ref(const struct fx_array_p *array, size_t at)
|
||||
{
|
||||
if (at >= array->ar_len) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return array->ar_data[at];
|
||||
return &array->ar_data[at];
|
||||
}
|
||||
|
||||
static fx_object *array_get(struct fx_array_p *array, size_t at)
|
||||
static fx_value array_get(struct fx_array_p *array, size_t at)
|
||||
{
|
||||
if (at >= array->ar_len) {
|
||||
return NULL;
|
||||
return FX_VALUE_EMPTY;
|
||||
}
|
||||
|
||||
return fx_object_ref(array->ar_data[at]);
|
||||
fx_value result;
|
||||
fx_value_copy(&result, &array->ar_data[at]);
|
||||
return result;
|
||||
}
|
||||
|
||||
static size_t array_size(const struct fx_array_p *array)
|
||||
static size_t array_get_size(const struct fx_array_p *array)
|
||||
{
|
||||
return array->ar_len;
|
||||
}
|
||||
|
||||
static size_t array_capacity(const struct fx_array_p *array)
|
||||
static size_t array_get_capacity(const struct fx_array_p *array)
|
||||
{
|
||||
return array->ar_cap;
|
||||
}
|
||||
@@ -189,18 +192,16 @@ static void array_clear(struct fx_array_p *array)
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < array->ar_len; i++) {
|
||||
fx_object_unref(array->ar_data[i]);
|
||||
fx_value_unset(&array->ar_data[i]);
|
||||
}
|
||||
|
||||
memset(array->ar_data, 0x0, array->ar_cap * sizeof(fx_object *));
|
||||
memset(array->ar_data, 0x0, array->ar_cap * sizeof(fx_value));
|
||||
array->ar_len = 0;
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
fx_array *fx_array_create_with_values(
|
||||
fx_object *const *values,
|
||||
size_t nr_values)
|
||||
fx_array *fx_array_create_with_values(const fx_value *values, size_t nr_values)
|
||||
{
|
||||
fx_array *array = fx_array_create();
|
||||
if (!array) {
|
||||
@@ -211,14 +212,14 @@ fx_array *fx_array_create_with_values(
|
||||
|
||||
size_t real_nr_values = 0;
|
||||
for (size_t i = 0; i < nr_values; i++) {
|
||||
if (values[i]) {
|
||||
if (fx_value_is_set(&values[i])) {
|
||||
real_nr_values++;
|
||||
}
|
||||
}
|
||||
|
||||
p->ar_len = real_nr_values;
|
||||
p->ar_cap = real_nr_values;
|
||||
p->ar_data = calloc(real_nr_values, sizeof(struct fx_dsref *));
|
||||
p->ar_data = calloc(real_nr_values, sizeof(fx_value));
|
||||
if (!p->ar_data) {
|
||||
fx_array_unref(array);
|
||||
return NULL;
|
||||
@@ -226,13 +227,13 @@ fx_array *fx_array_create_with_values(
|
||||
|
||||
size_t index = 0;
|
||||
for (size_t i = 0; i < nr_values; i++) {
|
||||
p->ar_data[index++] = fx_object_ref(values[i]);
|
||||
fx_value_copy(&p->ar_data[index++], &values[i]);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
fx_status fx_array_insert(fx_array *array, fx_object *value, size_t at)
|
||||
fx_status fx_array_insert(fx_array *array, fx_value value, size_t at)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_insert, array, value, at);
|
||||
}
|
||||
@@ -252,39 +253,39 @@ fx_status fx_array_remove_back(fx_array *array)
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_remove_back, array);
|
||||
}
|
||||
|
||||
fx_object *fx_array_pop(fx_array *array, size_t at)
|
||||
fx_value fx_array_pop(fx_array *array, size_t at)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_pop, array, at);
|
||||
}
|
||||
|
||||
fx_object *fx_array_pop_front(fx_array *array)
|
||||
fx_value fx_array_pop_front(fx_array *array)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_pop_front, array);
|
||||
}
|
||||
|
||||
fx_object *fx_array_pop_back(fx_array *array)
|
||||
fx_value fx_array_pop_back(fx_array *array)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_pop_back, array);
|
||||
}
|
||||
|
||||
fx_object *fx_array_at(const fx_array *array, size_t at)
|
||||
fx_value *fx_array_get_ref(const fx_array *array, size_t at)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_at, array, at);
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_get_ref, array, at);
|
||||
}
|
||||
|
||||
fx_object *fx_array_get(fx_array *array, size_t at)
|
||||
fx_value fx_array_get(const fx_array *array, size_t at)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_get, array, at);
|
||||
}
|
||||
|
||||
size_t fx_array_size(const fx_array *array)
|
||||
size_t fx_array_get_size(const fx_array *array)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_size, array);
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_get_size, array);
|
||||
}
|
||||
|
||||
size_t fx_array_capacity(const fx_array *array)
|
||||
size_t fx_array_get_capacity(const fx_array *array)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_capacity, array);
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_get_capacity, array);
|
||||
}
|
||||
|
||||
void fx_array_clear(fx_array *array)
|
||||
@@ -294,12 +295,12 @@ void fx_array_clear(fx_array *array)
|
||||
|
||||
/*** PUBLIC ALIAS FUNCTIONS ***************************************************/
|
||||
|
||||
fx_status fx_array_append(fx_array *array, fx_object *value)
|
||||
fx_status fx_array_push_back(fx_array *array, fx_value value)
|
||||
{
|
||||
return fx_array_insert(array, value, FX_NPOS);
|
||||
}
|
||||
|
||||
fx_status fx_array_prepend(fx_array *array, fx_object *value)
|
||||
fx_status fx_array_push_front(fx_array *array, fx_value value)
|
||||
{
|
||||
return fx_array_insert(array, value, 0);
|
||||
}
|
||||
@@ -317,7 +318,7 @@ static void array_fini(fx_object *obj, void *priv)
|
||||
|
||||
if (array->ar_data) {
|
||||
for (size_t i = 0; i < array->ar_len; i++) {
|
||||
fx_object_unref(array->ar_data[i]);
|
||||
fx_value_unset(&array->ar_data[i]);
|
||||
}
|
||||
|
||||
free(array->ar_data);
|
||||
@@ -325,29 +326,34 @@ static void array_fini(fx_object *obj, void *priv)
|
||||
}
|
||||
}
|
||||
|
||||
static void array_to_string(const fx_object *obj, fx_stream *out)
|
||||
static fx_status array_to_string(
|
||||
const fx_value *obj,
|
||||
fx_stream *out,
|
||||
const char *format)
|
||||
{
|
||||
struct fx_array_p *array = fx_object_get_private(obj, FX_TYPE_ARRAY);
|
||||
struct fx_array_p *array = fx_object_get_private(
|
||||
obj->v_object,
|
||||
FX_TYPE_ARRAY);
|
||||
|
||||
if (!array->ar_len) {
|
||||
fx_stream_write_cstr(out, "[]", NULL);
|
||||
return;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_stream_write_cstr(out, "[\n", NULL);
|
||||
|
||||
fx_stream_push_indent(out, 1);
|
||||
size_t len = array_size(array);
|
||||
size_t len = array_get_size(array);
|
||||
|
||||
for (size_t i = 0; i < array->ar_len; i++) {
|
||||
fx_object *value = array->ar_data[i];
|
||||
bool is_string = fx_object_is_type(value, FX_TYPE_STRING);
|
||||
fx_value *value = &array->ar_data[i];
|
||||
bool is_string = fx_value_is_type(value, FX_TYPE_STRING);
|
||||
|
||||
if (is_string) {
|
||||
fx_stream_write_char(out, '"');
|
||||
}
|
||||
|
||||
fx_object_to_string(value, out);
|
||||
fx_value_to_string(value, out, NULL);
|
||||
|
||||
if (is_string) {
|
||||
fx_stream_write_char(out, '"');
|
||||
@@ -362,6 +368,7 @@ static void array_to_string(const fx_object *obj, fx_stream *out)
|
||||
|
||||
fx_stream_pop_indent(out);
|
||||
fx_stream_write_char(out, ']');
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
/*** ITERATOR FUNCTIONS *******************************************************/
|
||||
@@ -369,33 +376,15 @@ static void array_to_string(const fx_object *obj, fx_stream *out)
|
||||
static fx_iterator *iterable_begin(fx_object *obj)
|
||||
{
|
||||
fx_array_iterator *it_obj = fx_object_create(FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_iterator_p *it = fx_object_get_private(
|
||||
it_obj,
|
||||
FX_TYPE_ARRAY_ITERATOR);
|
||||
it->_a = obj;
|
||||
it->_a_p = fx_object_get_private(obj, FX_TYPE_ARRAY);
|
||||
it->i = 0;
|
||||
|
||||
if (it->_a_p->ar_len > 0) {
|
||||
it->value = it->_a_p->ar_data[0];
|
||||
} else {
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
it->value = NULL;
|
||||
}
|
||||
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
static const fx_iterator *iterable_cbegin(const fx_object *obj)
|
||||
{
|
||||
fx_array_iterator *it_obj = fx_object_create(FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_ARRAY_ITERATOR);
|
||||
it->_a = (fx_array *)obj;
|
||||
it->_a_p = fx_object_get_private(obj, FX_TYPE_ARRAY);
|
||||
it->i = 0;
|
||||
|
||||
if (it->_a_p->ar_len > 0) {
|
||||
it->value = it->_a_p->ar_data[0];
|
||||
it->value = &it->_a_p->ar_data[0];
|
||||
} else {
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
it->value = NULL;
|
||||
@@ -406,12 +395,13 @@ static const fx_iterator *iterable_cbegin(const fx_object *obj)
|
||||
|
||||
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_array_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_p *array = it->_a_p;
|
||||
|
||||
if (it->value == NULL || it->i >= array->ar_len) {
|
||||
return false;
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
it->i++;
|
||||
@@ -419,7 +409,7 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
if (it->i >= array->ar_len) {
|
||||
it->value = NULL;
|
||||
} else {
|
||||
it->value = array->ar_data[it->i];
|
||||
it->value = &array->ar_data[it->i];
|
||||
}
|
||||
|
||||
return (it->value != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA;
|
||||
@@ -427,22 +417,19 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
|
||||
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
{
|
||||
struct fx_array_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_p *array = it->_a_p;
|
||||
|
||||
if (it->i >= array->ar_len) {
|
||||
return FX_ERR_OUT_OF_BOUNDS;
|
||||
}
|
||||
|
||||
if (array->ar_data[it->i] != it->value) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
array_remove(array, it->i);
|
||||
|
||||
if (it->i < array->ar_len) {
|
||||
it->value = array->ar_data[it->i];
|
||||
it->value = &array->ar_data[it->i];
|
||||
} else {
|
||||
it->value = NULL;
|
||||
}
|
||||
@@ -450,36 +437,31 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_iterator_value iterator_get_value(fx_iterator *obj)
|
||||
static fx_value iterator_get_value(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_array_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_p *array = it->_a_p;
|
||||
|
||||
return FX_ITERATOR_VALUE_PTR(it->value);
|
||||
if (it->i >= array->ar_len) {
|
||||
return FX_VALUE_EMPTY;
|
||||
}
|
||||
|
||||
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_array_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
|
||||
|
||||
return FX_ITERATOR_VALUE_CPTR(it->value);
|
||||
return *it->value;
|
||||
}
|
||||
|
||||
static enum fx_status iterator_is_valid(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_array_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_ARRAY_ITERATOR);
|
||||
struct fx_array_p *array = it->_a_p;
|
||||
|
||||
if (it->i >= array->ar_len) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (array->ar_data[it->i] != it->value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (it->value != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
@@ -493,7 +475,6 @@ FX_TYPE_CLASS_BEGIN(fx_array)
|
||||
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
||||
FX_INTERFACE_ENTRY(it_begin) = iterable_begin;
|
||||
FX_INTERFACE_ENTRY(it_cbegin) = iterable_cbegin;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
||||
FX_TYPE_CLASS_END(fx_array)
|
||||
|
||||
@@ -516,7 +497,6 @@ FX_TYPE_CLASS_BEGIN(fx_array_iterator)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||
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_CLASS_END(fx_array_iterator)
|
||||
|
||||
|
||||
+10
-3
@@ -1,6 +1,7 @@
|
||||
#include <fx/bitop.h>
|
||||
#include <fx/collections/bitmap.h>
|
||||
#include <fx/stream.h>
|
||||
#include <fx/value.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BITS_PER_WORD (8 * sizeof(bitmap_word_t))
|
||||
@@ -306,10 +307,14 @@ static void bitmap_fini(fx_object *obj, void *priv)
|
||||
struct fx_bitmap_p *map = priv;
|
||||
}
|
||||
|
||||
static void bitmap_to_string(const fx_object *obj, fx_stream *out)
|
||||
static fx_status bitmap_to_string(
|
||||
const fx_value *obj,
|
||||
fx_stream *out,
|
||||
const char *format)
|
||||
{
|
||||
const struct fx_bitmap_p *map
|
||||
= fx_object_get_private(obj, FX_TYPE_BITMAP);
|
||||
const struct fx_bitmap_p *map = fx_object_get_private(
|
||||
obj->v_object,
|
||||
FX_TYPE_BITMAP);
|
||||
|
||||
unsigned char *bytes = (unsigned char *)map->map_words;
|
||||
size_t nr_bytes = map->map_nr_words * sizeof(bitmap_word_t);
|
||||
@@ -339,6 +344,8 @@ static void bitmap_to_string(const fx_object *obj, fx_stream *out)
|
||||
|
||||
fx_stream_write_fmt(out, NULL, "%c", (c & mask) ? '1' : '0');
|
||||
}
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <fx/collections/datetime.h>
|
||||
#include <fx/stream.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
/*** PRIVATE DATA *************************************************************/
|
||||
|
||||
@@ -566,9 +567,14 @@ static void datetime_fini(fx_object *obj, void *priv)
|
||||
struct fx_datetime_p *dt = priv;
|
||||
}
|
||||
|
||||
static void _datetime_to_string(const fx_object *obj, fx_stream *out)
|
||||
static fx_status _datetime_to_string(
|
||||
const fx_value *obj,
|
||||
fx_stream *out,
|
||||
const char *format)
|
||||
{
|
||||
struct fx_datetime_p *dt = fx_object_get_private(obj, FX_TYPE_DATETIME);
|
||||
struct fx_datetime_p *dt = fx_object_get_private(
|
||||
obj->v_object,
|
||||
FX_TYPE_DATETIME);
|
||||
|
||||
if (dt->dt_has_date) {
|
||||
fx_stream_write_fmt(
|
||||
@@ -607,6 +613,8 @@ static void _datetime_to_string(const fx_object *obj, fx_stream *out)
|
||||
dt->dt_zone_offset_minute);
|
||||
}
|
||||
}
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
@@ -1,732 +0,0 @@
|
||||
#include <fx/collections/dict.h>
|
||||
#include <fx/hash.h>
|
||||
#include <fx/status.h>
|
||||
#include <fx/stream.h>
|
||||
#include <fx/string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/*** PRIVATE DATA *************************************************************/
|
||||
|
||||
struct fx_dict_bucket_item {
|
||||
fx_queue_entry bi_entry;
|
||||
fx_string *bi_str;
|
||||
fx_object *bi_value;
|
||||
};
|
||||
|
||||
struct fx_dict_bucket {
|
||||
fx_bst_node bk_node;
|
||||
uint64_t bk_hash;
|
||||
fx_queue bk_items;
|
||||
};
|
||||
|
||||
struct fx_dict_p {
|
||||
fx_bst d_buckets;
|
||||
};
|
||||
|
||||
struct fx_dict_iterator_p {
|
||||
size_t i;
|
||||
fx_dict_item item;
|
||||
|
||||
fx_dict *_d;
|
||||
struct fx_dict_p *_d_p;
|
||||
fx_bst_node *_cbn;
|
||||
fx_queue_entry *_cqe;
|
||||
};
|
||||
|
||||
/*** MISC FUNCTIONS ***********************************************************/
|
||||
|
||||
static FX_BST_DEFINE_SIMPLE_GET(
|
||||
struct fx_dict_bucket,
|
||||
uint64_t,
|
||||
bk_node,
|
||||
bk_hash,
|
||||
get_bucket);
|
||||
static FX_BST_DEFINE_SIMPLE_INSERT(
|
||||
struct fx_dict_bucket,
|
||||
bk_node,
|
||||
bk_hash,
|
||||
put_bucket);
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
static struct fx_dict_bucket *create_bucket(void)
|
||||
{
|
||||
struct fx_dict_bucket *bucket = malloc(sizeof *bucket);
|
||||
if (!bucket) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(bucket, 0x0, sizeof *bucket);
|
||||
return bucket;
|
||||
}
|
||||
|
||||
static struct fx_dict_bucket_item *create_bucket_item(void)
|
||||
{
|
||||
struct fx_dict_bucket_item *item = malloc(sizeof *item);
|
||||
if (!item) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(item, 0x0, sizeof *item);
|
||||
return item;
|
||||
}
|
||||
|
||||
static fx_status dict_put(
|
||||
struct fx_dict_p *dict,
|
||||
const char *key,
|
||||
fx_object *value)
|
||||
{
|
||||
uint64_t hash = fx_hash_cstr(key);
|
||||
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
|
||||
if (!bucket) {
|
||||
bucket = create_bucket();
|
||||
if (!bucket) {
|
||||
return FX_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
bucket->bk_hash = hash;
|
||||
put_bucket(&dict->d_buckets, bucket);
|
||||
}
|
||||
|
||||
struct fx_dict_bucket_item *item = create_bucket_item();
|
||||
if (!item) {
|
||||
return FX_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
item->bi_str = fx_string_create_from_cstr(key);
|
||||
item->bi_value = fx_object_ref(value);
|
||||
|
||||
fx_queue_push_back(&bucket->bk_items, &item->bi_entry);
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status dict_put_sk(
|
||||
struct fx_dict_p *dict,
|
||||
const fx_string *key,
|
||||
fx_object *value)
|
||||
{
|
||||
uint64_t hash = fx_string_hash(key);
|
||||
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
|
||||
if (!bucket) {
|
||||
bucket = create_bucket();
|
||||
if (!bucket) {
|
||||
return FX_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
bucket->bk_hash = hash;
|
||||
put_bucket(&dict->d_buckets, bucket);
|
||||
}
|
||||
|
||||
struct fx_dict_bucket_item *item = create_bucket_item();
|
||||
if (!item) {
|
||||
return FX_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
item->bi_str = fx_string_duplicate(key);
|
||||
item->bi_value = fx_object_ref(value);
|
||||
|
||||
fx_queue_push_back(&bucket->bk_items, &item->bi_entry);
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_object *dict_at(const struct fx_dict_p *dict, const char *key)
|
||||
{
|
||||
uint64_t hash = fx_hash_cstr(key);
|
||||
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
|
||||
if (!bucket) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items);
|
||||
while (entry) {
|
||||
struct fx_dict_bucket_item *item
|
||||
= fx_unbox(struct fx_dict_bucket_item, entry, bi_entry);
|
||||
|
||||
if (!strcmp(fx_string_get_cstr(item->bi_str), key)) {
|
||||
return item->bi_value;
|
||||
}
|
||||
|
||||
entry = fx_queue_next(entry);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static fx_object *dict_at_sk(const struct fx_dict_p *dict, const fx_string *key)
|
||||
{
|
||||
uint64_t hash = fx_string_hash(key);
|
||||
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
|
||||
if (!bucket) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items);
|
||||
while (entry) {
|
||||
struct fx_dict_bucket_item *item
|
||||
= fx_unbox(struct fx_dict_bucket_item, entry, bi_entry);
|
||||
|
||||
if (fx_string_compare(item->bi_str, key)) {
|
||||
return item->bi_value;
|
||||
}
|
||||
|
||||
entry = fx_queue_next(entry);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static fx_object *dict_get(struct fx_dict_p *dict, const char *key)
|
||||
{
|
||||
fx_object *value = dict_at(dict, key);
|
||||
if (value) {
|
||||
fx_object_ref(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
static fx_object *dict_get_sk(struct fx_dict_p *dict, const fx_string *key)
|
||||
{
|
||||
fx_object *value = dict_at_sk(dict, key);
|
||||
if (value) {
|
||||
fx_object_ref(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
static bool dict_has_key(const struct fx_dict_p *dict, const char *key)
|
||||
{
|
||||
return dict_at(dict, key) != NULL;
|
||||
}
|
||||
|
||||
static bool dict_has_skey(const struct fx_dict_p *dict, const fx_string *key)
|
||||
{
|
||||
return dict_at_sk(dict, key) != NULL;
|
||||
}
|
||||
|
||||
static size_t dict_get_size(const struct fx_dict_p *dict)
|
||||
{
|
||||
size_t count = 0;
|
||||
#if 0
|
||||
fx_bst_iterator it1;
|
||||
fx_queue_iterator it2;
|
||||
|
||||
fx_bst_foreach (&it1, &dict->d_buckets) {
|
||||
struct fx_dict_bucket *bucket
|
||||
= fx_unbox(struct fx_dict_bucket, it1.node, bk_node);
|
||||
|
||||
fx_queue_foreach (&it2, &bucket->bk_items) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
static bool dict_is_empty(const struct fx_dict_p *dict)
|
||||
{
|
||||
fx_bst_node *first_node = fx_bst_first(&dict->d_buckets);
|
||||
struct fx_dict_bucket *first_bucket
|
||||
= fx_unbox(struct fx_dict_bucket, first_node, bk_node);
|
||||
if (!first_bucket) {
|
||||
return true;
|
||||
}
|
||||
|
||||
fx_queue_entry *first_entry = fx_queue_first(&first_bucket->bk_items);
|
||||
struct fx_dict_bucket_item *first_item
|
||||
= fx_unbox(struct fx_dict_bucket_item, first_entry, bi_entry);
|
||||
if (!first_item) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool get_next_node(
|
||||
struct fx_bst_node *cur_node,
|
||||
struct fx_queue_entry *cur_entry,
|
||||
struct fx_bst_node **out_next_node,
|
||||
struct fx_queue_entry **out_next_entry)
|
||||
{
|
||||
struct fx_dict_bucket *cur_bucket
|
||||
= fx_unbox(struct fx_dict_bucket, cur_node, bk_node);
|
||||
if (!cur_bucket) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct fx_dict_bucket_item *cur_item
|
||||
= fx_unbox(struct fx_dict_bucket_item, cur_entry, bi_entry);
|
||||
if (!cur_item) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct fx_bst_node *next_node = cur_node;
|
||||
struct fx_queue_entry *next_entry = fx_queue_next(cur_entry);
|
||||
if (!next_entry) {
|
||||
next_node = fx_bst_next(cur_node);
|
||||
if (!next_node) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct fx_dict_bucket *next_bucket
|
||||
= fx_unbox(struct fx_dict_bucket, next_node, bk_node);
|
||||
if (!next_bucket) {
|
||||
return false;
|
||||
}
|
||||
|
||||
next_entry = fx_queue_first(&next_bucket->bk_items);
|
||||
if (!next_entry) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
struct fx_dict_bucket_item *next_item
|
||||
= fx_unbox(struct fx_dict_bucket_item, next_entry, bi_entry);
|
||||
if (!next_item) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*out_next_node = next_node;
|
||||
*out_next_entry = next_entry;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static fx_status delete_item(
|
||||
struct fx_dict_p *dict,
|
||||
struct fx_dict_bucket *bucket,
|
||||
struct fx_dict_bucket_item *item)
|
||||
{
|
||||
fx_queue_delete(&bucket->bk_items, &item->bi_entry);
|
||||
free(item);
|
||||
|
||||
if (fx_queue_empty(&bucket->bk_items)) {
|
||||
fx_bst_delete(&dict->d_buckets, &bucket->bk_node);
|
||||
free(bucket);
|
||||
}
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
#if 0
|
||||
fx_dict *fx_dict_create_with_items(const fx_dict_item *items)
|
||||
{
|
||||
fx_dict *dict = fx_dict_create();
|
||||
if (!dict) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_dict_p *p = fx_object_get_private(dict, FX_TYPE_DICT);
|
||||
|
||||
for (size_t i = 0; items[i].key; i++) {
|
||||
dict_put(p, items[i].key, items[i].value);
|
||||
}
|
||||
|
||||
return dict;
|
||||
}
|
||||
#endif
|
||||
|
||||
fx_status fx_dict_put(fx_dict *dict, const char *key, fx_object *value)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_put, dict, key, value);
|
||||
}
|
||||
|
||||
fx_status fx_dict_put_sk(fx_dict *dict, const fx_string *key, fx_object *value)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_put_sk, dict, key, value);
|
||||
}
|
||||
|
||||
fx_object *fx_dict_at(const fx_dict *dict, const char *key)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_at, dict, key);
|
||||
}
|
||||
|
||||
fx_object *fx_dict_at_sk(const fx_dict *dict, const fx_string *key)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_at_sk, dict, key);
|
||||
}
|
||||
|
||||
fx_object *fx_dict_get(fx_dict *dict, const char *key)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_get, dict, key);
|
||||
}
|
||||
|
||||
fx_object *fx_dict_get_sk(fx_dict *dict, const fx_string *key)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_get_sk, dict, key);
|
||||
}
|
||||
|
||||
bool fx_dict_has_key(const fx_dict *dict, const char *key)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_has_key, dict, key);
|
||||
}
|
||||
|
||||
bool fx_dict_has_skey(const fx_dict *dict, const fx_string *key)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_has_skey, dict, key);
|
||||
}
|
||||
|
||||
size_t fx_dict_get_size(const fx_dict *dict)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DICT, dict_get_size, dict);
|
||||
}
|
||||
|
||||
bool fx_dict_is_empty(const fx_dict *dict)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DICT, dict_is_empty, dict);
|
||||
}
|
||||
|
||||
/*** PUBLIC ALIAS FUNCTIONS ***************************************************/
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void dict_init(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_dict_p *dict = priv;
|
||||
}
|
||||
|
||||
static void dict_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_dict_p *dict = priv;
|
||||
|
||||
struct fx_bst_node *node = fx_bst_first(&dict->d_buckets);
|
||||
while (node) {
|
||||
struct fx_dict_bucket *bucket
|
||||
= fx_unbox(struct fx_dict_bucket, node, bk_node);
|
||||
struct fx_bst_node *next_node = fx_bst_next(node);
|
||||
fx_bst_delete(&dict->d_buckets, node);
|
||||
|
||||
struct fx_queue_entry *entry
|
||||
= fx_queue_first(&bucket->bk_items);
|
||||
while (entry) {
|
||||
struct fx_dict_bucket_item *item = fx_unbox(
|
||||
struct fx_dict_bucket_item,
|
||||
entry,
|
||||
bi_entry);
|
||||
struct fx_queue_entry *next_entry
|
||||
= fx_queue_next(entry);
|
||||
fx_queue_delete(&bucket->bk_items, entry);
|
||||
|
||||
free(item->bi_str);
|
||||
fx_object_unref(item->bi_value);
|
||||
free(item);
|
||||
|
||||
entry = next_entry;
|
||||
}
|
||||
|
||||
free(bucket);
|
||||
node = next_node;
|
||||
}
|
||||
}
|
||||
|
||||
static void dict_to_string(const fx_object *obj, fx_stream *out)
|
||||
{
|
||||
struct fx_dict_p *dict = fx_object_get_private(obj, FX_TYPE_DICT);
|
||||
|
||||
if (dict_is_empty(dict)) {
|
||||
fx_stream_write_cstr(out, "{}", NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
fx_stream_write_cstr(out, "{\n", NULL);
|
||||
|
||||
fx_stream_push_indent(out, 1);
|
||||
size_t len = dict_get_size(dict);
|
||||
size_t i = 0;
|
||||
|
||||
struct fx_bst_node *node = fx_bst_first(&dict->d_buckets);
|
||||
while (node) {
|
||||
struct fx_dict_bucket *bucket
|
||||
= fx_unbox(struct fx_dict_bucket, node, bk_node);
|
||||
|
||||
struct fx_queue_entry *entry
|
||||
= fx_queue_first(&bucket->bk_items);
|
||||
while (entry) {
|
||||
struct fx_dict_bucket_item *item = fx_unbox(
|
||||
struct fx_dict_bucket_item,
|
||||
entry,
|
||||
bi_entry);
|
||||
|
||||
fx_object_to_string(item->bi_str, out);
|
||||
fx_stream_write_cstr(out, ": ", NULL);
|
||||
|
||||
bool is_string = fx_object_is_type(
|
||||
item->bi_value,
|
||||
FX_TYPE_STRING);
|
||||
|
||||
if (is_string) {
|
||||
fx_stream_write_char(out, '"');
|
||||
}
|
||||
|
||||
fx_object_to_string(item->bi_value, out);
|
||||
|
||||
if (is_string) {
|
||||
fx_stream_write_char(out, '"');
|
||||
}
|
||||
|
||||
if (i < len - 1) {
|
||||
fx_stream_write_cstr(out, ",", NULL);
|
||||
}
|
||||
|
||||
fx_stream_write_char(out, '\n');
|
||||
entry = fx_queue_next(entry);
|
||||
i++;
|
||||
}
|
||||
node = fx_bst_next(node);
|
||||
}
|
||||
|
||||
fx_stream_pop_indent(out);
|
||||
fx_stream_write_char(out, '}');
|
||||
}
|
||||
|
||||
/*** ITERATOR FUNCTIONS *******************************************************/
|
||||
|
||||
static fx_iterator *iterable_begin(fx_dict *dict)
|
||||
{
|
||||
fx_iterator *it_obj = fx_object_create(FX_TYPE_DICT_ITERATOR);
|
||||
if (!it_obj) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_dict_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_DICT_ITERATOR);
|
||||
|
||||
it->i = 0;
|
||||
it->_d = dict;
|
||||
it->_d_p = fx_object_get_private(dict, FX_TYPE_DICT);
|
||||
|
||||
if (dict_is_empty(it->_d_p)) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
fx_bst_node *first_node = fx_bst_first(&it->_d_p->d_buckets);
|
||||
struct fx_dict_bucket *first_bucket
|
||||
= fx_unbox(struct fx_dict_bucket, first_node, bk_node);
|
||||
if (!first_bucket) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
fx_queue_entry *first_entry = fx_queue_first(&first_bucket->bk_items);
|
||||
struct fx_dict_bucket_item *first_item
|
||||
= fx_unbox(struct fx_dict_bucket_item, first_entry, bi_entry);
|
||||
if (!first_item) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
it->item.key = first_item->bi_str;
|
||||
it->item.value = first_item->bi_value;
|
||||
|
||||
it->_d = dict;
|
||||
it->_cbn = first_node;
|
||||
it->_cqe = first_entry;
|
||||
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
static const fx_iterator *iterable_cbegin(const fx_dict *dict)
|
||||
{
|
||||
fx_iterator *it_obj = fx_object_create(FX_TYPE_DICT_ITERATOR);
|
||||
if (!it_obj) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_dict_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_DICT_ITERATOR);
|
||||
|
||||
it->i = 0;
|
||||
it->_d = (fx_dict *)dict;
|
||||
it->_d_p = fx_object_get_private(dict, FX_TYPE_DICT);
|
||||
|
||||
if (dict_is_empty(it->_d_p)) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
fx_bst_node *first_node = fx_bst_first(&it->_d_p->d_buckets);
|
||||
struct fx_dict_bucket *first_bucket
|
||||
= fx_unbox(struct fx_dict_bucket, first_node, bk_node);
|
||||
if (!first_bucket) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
fx_queue_entry *first_entry = fx_queue_first(&first_bucket->bk_items);
|
||||
struct fx_dict_bucket_item *first_item
|
||||
= fx_unbox(struct fx_dict_bucket_item, first_entry, bi_entry);
|
||||
if (!first_item) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
it->item.key = first_item->bi_str;
|
||||
it->item.value = first_item->bi_value;
|
||||
|
||||
it->_cbn = first_node;
|
||||
it->_cqe = first_entry;
|
||||
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_dict_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR);
|
||||
|
||||
struct fx_bst_node *next_node;
|
||||
struct fx_queue_entry *next_entry;
|
||||
if (!get_next_node(it->_cbn, it->_cqe, &next_node, &next_entry)) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
struct fx_dict_bucket_item *next_item
|
||||
= fx_unbox(struct fx_dict_bucket_item, next_entry, bi_entry);
|
||||
|
||||
if (!next_item) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
it->i++;
|
||||
it->item.key = next_item->bi_str;
|
||||
it->item.value = next_item->bi_value;
|
||||
|
||||
it->_cbn = next_node;
|
||||
it->_cqe = next_entry;
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
{
|
||||
struct fx_dict_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR);
|
||||
|
||||
if ((it->item.key || it->item.value) && !(it->_cbn && it->_cqe)) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (!it->item.key || !it->_cqe) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
struct fx_bst_node *next_node;
|
||||
struct fx_queue_entry *next_entry;
|
||||
if (!get_next_node(it->_cbn, it->_cqe, &next_node, &next_entry)) {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
struct fx_dict_bucket *cur_bucket
|
||||
= fx_unbox(struct fx_dict_bucket, it->_cbn, bk_node);
|
||||
struct fx_dict_bucket_item *cur_item
|
||||
= fx_unbox(struct fx_dict_bucket_item, it->_cqe, bi_entry);
|
||||
|
||||
struct fx_dict_bucket_item *next_item
|
||||
= fx_unbox(struct fx_dict_bucket_item, next_entry, bi_entry);
|
||||
|
||||
fx_status status = delete_item(it->_d_p, cur_bucket, cur_item);
|
||||
if (FX_ERR(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
if (next_item) {
|
||||
it->item.key = next_item->bi_str;
|
||||
it->item.value = next_item->bi_value;
|
||||
|
||||
it->_cbn = next_node;
|
||||
it->_cqe = next_entry;
|
||||
} else {
|
||||
it->item.key = NULL;
|
||||
it->item.value = NULL;
|
||||
|
||||
it->_cbn = NULL;
|
||||
it->_cqe = NULL;
|
||||
}
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_iterator_value iterator_get_value(fx_iterator *obj)
|
||||
{
|
||||
struct fx_dict_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR);
|
||||
|
||||
return FX_ITERATOR_VALUE_PTR(&it->item);
|
||||
}
|
||||
|
||||
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_dict_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR);
|
||||
|
||||
return FX_ITERATOR_VALUE_CPTR(&it->item);
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
// ---- fx_dict DEFINITION
|
||||
FX_TYPE_CLASS_BEGIN(fx_dict)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = dict_to_string;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
||||
FX_INTERFACE_ENTRY(it_begin) = iterable_begin;
|
||||
FX_INTERFACE_ENTRY(it_cbegin) = iterable_cbegin;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
||||
FX_TYPE_CLASS_END(fx_dict)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_dict)
|
||||
FX_TYPE_ID(0xd2af61d9, 0xd0be, 0x4960, 0xbe3f, 0x509749814c10);
|
||||
FX_TYPE_CLASS(fx_dict_class);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_dict_p);
|
||||
FX_TYPE_INSTANCE_INIT(dict_init);
|
||||
FX_TYPE_INSTANCE_FINI(dict_fini);
|
||||
FX_TYPE_DEFINITION_END(fx_dict)
|
||||
|
||||
// ---- fx_dict_iterator DEFINITION
|
||||
FX_TYPE_CLASS_BEGIN(fx_dict_iterator)
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||
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_CLASS_END(fx_dict_iterator)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_dict_iterator)
|
||||
FX_TYPE_ID(0x9ea96701, 0x1713, 0x4a3e, 0xbf63, 0xdc856b456f3b);
|
||||
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
|
||||
FX_TYPE_CLASS(fx_dict_iterator_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_dict_iterator_p);
|
||||
FX_TYPE_DEFINITION_END(fx_dict_iterator)
|
||||
+72
-52
@@ -112,14 +112,18 @@ static bool get_next_node(
|
||||
struct fx_bst_node **out_next_node,
|
||||
struct fx_queue_entry **out_next_entry)
|
||||
{
|
||||
struct fx_hashmap_bucket *cur_bucket
|
||||
= fx_unbox(struct fx_hashmap_bucket, cur_node, bk_node);
|
||||
struct fx_hashmap_bucket *cur_bucket = fx_unbox(
|
||||
struct fx_hashmap_bucket,
|
||||
cur_node,
|
||||
bk_node);
|
||||
if (!cur_bucket) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct fx_hashmap_bucket_item *cur_item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, cur_entry, bi_entry);
|
||||
struct fx_hashmap_bucket_item *cur_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
cur_entry,
|
||||
bi_entry);
|
||||
if (!cur_item) {
|
||||
return false;
|
||||
}
|
||||
@@ -146,8 +150,10 @@ static bool get_next_node(
|
||||
}
|
||||
}
|
||||
|
||||
struct fx_hashmap_bucket_item *next_item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry);
|
||||
struct fx_hashmap_bucket_item *next_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
next_entry,
|
||||
bi_entry);
|
||||
if (!next_item) {
|
||||
return false;
|
||||
}
|
||||
@@ -186,8 +192,9 @@ static fx_status hashmap_put(
|
||||
const fx_hashmap_value *value)
|
||||
{
|
||||
uint64_t hash = hash_key(key);
|
||||
struct fx_hashmap_bucket *bucket
|
||||
= get_bucket(&hashmap->h_buckets, hash);
|
||||
struct fx_hashmap_bucket *bucket = get_bucket(
|
||||
&hashmap->h_buckets,
|
||||
hash);
|
||||
|
||||
if (!bucket) {
|
||||
bucket = create_bucket();
|
||||
@@ -234,8 +241,9 @@ static const struct fx_hashmap_value *hashmap_get(
|
||||
{
|
||||
uint64_t hash = hash_key(key);
|
||||
|
||||
struct fx_hashmap_bucket *bucket
|
||||
= get_bucket(&hashmap->h_buckets, hash);
|
||||
struct fx_hashmap_bucket *bucket = get_bucket(
|
||||
&hashmap->h_buckets,
|
||||
hash);
|
||||
if (!bucket) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -262,8 +270,9 @@ static bool hashmap_has_key(
|
||||
const fx_hashmap_key *key)
|
||||
{
|
||||
uint64_t hash = hash_key(key);
|
||||
struct fx_hashmap_bucket *bucket
|
||||
= get_bucket(&hashmap->h_buckets, hash);
|
||||
struct fx_hashmap_bucket *bucket = get_bucket(
|
||||
&hashmap->h_buckets,
|
||||
hash);
|
||||
if (!bucket) {
|
||||
return false;
|
||||
}
|
||||
@@ -293,8 +302,10 @@ static size_t hashmap_get_size(const struct fx_hashmap_p *hashmap)
|
||||
static bool hashmap_is_empty(const struct fx_hashmap_p *hashmap)
|
||||
{
|
||||
fx_bst_node *first_node = fx_bst_first(&hashmap->h_buckets);
|
||||
struct fx_hashmap_bucket *first_bucket
|
||||
= fx_unbox(struct fx_hashmap_bucket, first_node, bk_node);
|
||||
struct fx_hashmap_bucket *first_bucket = fx_unbox(
|
||||
struct fx_hashmap_bucket,
|
||||
first_node,
|
||||
bk_node);
|
||||
if (!first_bucket) {
|
||||
return true;
|
||||
}
|
||||
@@ -358,8 +369,9 @@ fx_hashmap *fx_hashmap_create_with_items(const fx_hashmap_item *items)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_hashmap_p *p
|
||||
= fx_object_get_private(hashmap, FX_TYPE_HASHMAP);
|
||||
struct fx_hashmap_p *p = fx_object_get_private(
|
||||
hashmap,
|
||||
FX_TYPE_HASHMAP);
|
||||
|
||||
for (size_t i = 0; items[i].key.key_data && items[i].key.key_size;
|
||||
i++) {
|
||||
@@ -410,10 +422,11 @@ bool fx_hashmap_is_empty(const fx_hashmap *hashmap)
|
||||
|
||||
fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap)
|
||||
{
|
||||
fx_hashmap_iterator *it_obj
|
||||
= fx_object_create(FX_TYPE_HASHMAP_ITERATOR);
|
||||
struct fx_hashmap_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_HASHMAP_ITERATOR);
|
||||
fx_hashmap_iterator *it_obj = fx_object_create(
|
||||
FX_TYPE_HASHMAP_ITERATOR);
|
||||
struct fx_hashmap_iterator_p *it = fx_object_get_private(
|
||||
it_obj,
|
||||
FX_TYPE_HASHMAP_ITERATOR);
|
||||
|
||||
it->_h = hashmap;
|
||||
it->_h_p = fx_object_get_private(hashmap, FX_TYPE_HASHMAP);
|
||||
@@ -426,16 +439,18 @@ fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap)
|
||||
}
|
||||
|
||||
struct fx_bst_node *first_node = fx_bst_first(&it->_h_p->h_buckets);
|
||||
struct fx_hashmap_bucket *first_bucket
|
||||
= fx_unbox(struct fx_hashmap_bucket, first_node, bk_node);
|
||||
struct fx_hashmap_bucket *first_bucket = fx_unbox(
|
||||
struct fx_hashmap_bucket,
|
||||
first_node,
|
||||
bk_node);
|
||||
if (!first_bucket) {
|
||||
memset(&it->item, 0x0, sizeof it->item);
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
struct fx_queue_entry *first_entry
|
||||
= fx_queue_first(&first_bucket->bk_items);
|
||||
struct fx_queue_entry *first_entry = fx_queue_first(
|
||||
&first_bucket->bk_items);
|
||||
struct fx_hashmap_bucket_item *first_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
first_entry,
|
||||
@@ -473,8 +488,10 @@ static void hashmap_fini(fx_object *obj, void *priv)
|
||||
|
||||
struct fx_bst_node *node = fx_bst_first(&map->h_buckets);
|
||||
while (node) {
|
||||
struct fx_hashmap_bucket *b
|
||||
= fx_unbox(struct fx_hashmap_bucket, node, bk_node);
|
||||
struct fx_hashmap_bucket *b = fx_unbox(
|
||||
struct fx_hashmap_bucket,
|
||||
node,
|
||||
bk_node);
|
||||
struct fx_bst_node *next_node = fx_bst_next(node);
|
||||
fx_bst_delete(&map->h_buckets, node);
|
||||
|
||||
@@ -484,8 +501,8 @@ static void hashmap_fini(fx_object *obj, void *priv)
|
||||
struct fx_hashmap_bucket_item,
|
||||
entry,
|
||||
bi_entry);
|
||||
struct fx_queue_entry *next_entry
|
||||
= fx_queue_next(entry);
|
||||
struct fx_queue_entry *next_entry = fx_queue_next(
|
||||
entry);
|
||||
fx_queue_delete(&b->bk_items, entry);
|
||||
|
||||
if (map->h_key_dtor) {
|
||||
@@ -510,8 +527,9 @@ static void hashmap_fini(fx_object *obj, void *priv)
|
||||
|
||||
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_hashmap_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
|
||||
struct fx_hashmap_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_HASHMAP_ITERATOR);
|
||||
|
||||
struct fx_bst_node *next_node;
|
||||
struct fx_queue_entry *next_entry;
|
||||
@@ -520,8 +538,10 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
struct fx_hashmap_bucket_item *next_item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry);
|
||||
struct fx_hashmap_bucket_item *next_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
next_entry,
|
||||
bi_entry);
|
||||
|
||||
if (!next_item) {
|
||||
memset(&it->item, 0x0, sizeof it->item);
|
||||
@@ -540,8 +560,9 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
|
||||
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
{
|
||||
struct fx_hashmap_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
|
||||
struct fx_hashmap_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_HASHMAP_ITERATOR);
|
||||
|
||||
if ((it->item.key.key_data || it->item.value.value_data)
|
||||
&& !(it->_cbn && it->_cqe)) {
|
||||
@@ -559,13 +580,19 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
struct fx_hashmap_bucket *cur_bucket
|
||||
= fx_unbox(struct fx_hashmap_bucket, it->_cbn, bk_node);
|
||||
struct fx_hashmap_bucket_item *cur_item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, it->_cqe, bi_entry);
|
||||
struct fx_hashmap_bucket *cur_bucket = fx_unbox(
|
||||
struct fx_hashmap_bucket,
|
||||
it->_cbn,
|
||||
bk_node);
|
||||
struct fx_hashmap_bucket_item *cur_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
it->_cqe,
|
||||
bi_entry);
|
||||
|
||||
struct fx_hashmap_bucket_item *next_item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry);
|
||||
struct fx_hashmap_bucket_item *next_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
next_entry,
|
||||
bi_entry);
|
||||
|
||||
fx_status status = delete_item(it->_h_p, cur_bucket, cur_item);
|
||||
if (FX_ERR(status)) {
|
||||
@@ -590,18 +617,12 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_iterator_value iterator_get_value(fx_iterator *obj)
|
||||
static fx_value iterator_get_value(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_hashmap_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
|
||||
return FX_ITERATOR_VALUE_PTR(&it->item);
|
||||
}
|
||||
|
||||
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
|
||||
{
|
||||
const struct fx_hashmap_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
|
||||
return FX_ITERATOR_VALUE_CPTR(&it->item);
|
||||
struct fx_hashmap_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_HASHMAP_ITERATOR);
|
||||
return FX_POINTER(&it->item);
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
@@ -637,7 +658,6 @@ FX_TYPE_CLASS_BEGIN(fx_hashmap_iterator)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||
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_CLASS_END(fx_hashmap_iterator)
|
||||
|
||||
|
||||
@@ -0,0 +1,640 @@
|
||||
#include <fx/collections/hashtable.h>
|
||||
#include <fx/misc.h>
|
||||
#include <fx/reflection/property.h>
|
||||
#include <fx/status.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/vector.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/*** PRIVATE DATA *************************************************************/
|
||||
|
||||
/* these primes are used as array capacities to avoid hash collisions. */
|
||||
static const size_t primes[] = {
|
||||
11,
|
||||
23,
|
||||
47,
|
||||
97,
|
||||
197,
|
||||
397,
|
||||
797,
|
||||
1597,
|
||||
3203,
|
||||
6421,
|
||||
12853,
|
||||
25717,
|
||||
51437,
|
||||
};
|
||||
|
||||
struct table_item {
|
||||
fx_hashtable_item *i_item;
|
||||
};
|
||||
|
||||
struct fx_hashtable_item_p {
|
||||
fx_hashtable_item *i_self;
|
||||
fx_value i_key, i_value;
|
||||
};
|
||||
|
||||
struct fx_hashtable_p {
|
||||
struct table_item *t_items;
|
||||
/* number of items currently stored in t_items */
|
||||
size_t t_count;
|
||||
/* index in primes[] that represents the current maximum size of
|
||||
* t_items */
|
||||
size_t t_max_index;
|
||||
};
|
||||
|
||||
struct fx_hashtable_iterator_p {
|
||||
size_t i;
|
||||
fx_hashtable_item *item;
|
||||
|
||||
fx_hashtable *_h;
|
||||
struct fx_hashtable_p *_h_p;
|
||||
};
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
static fx_status convert_key_to_index(
|
||||
const fx_value *v,
|
||||
struct table_item *table,
|
||||
size_t table_size,
|
||||
size_t *out_index)
|
||||
{
|
||||
uint64_t hash = 0;
|
||||
fx_status status = fx_value_hash(v, &hash);
|
||||
if (!FX_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
size_t index = hash % table_size, i = 1;
|
||||
while (table[index].i_item && i < table_size) {
|
||||
const struct fx_hashtable_item_p *item = fx_object_get_private(
|
||||
table[index].i_item,
|
||||
FX_TYPE_HASHTABLE_ITEM);
|
||||
int cmp = fx_value_compare(&item->i_key, v);
|
||||
if (cmp == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
index = (index + (i * i)) % table_size;
|
||||
i++;
|
||||
}
|
||||
|
||||
*out_index = index;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status find_key(
|
||||
const fx_value *v,
|
||||
struct table_item *table,
|
||||
size_t table_size,
|
||||
size_t *out_index)
|
||||
{
|
||||
uint64_t hash = 0;
|
||||
fx_status status = fx_value_hash(v, &hash);
|
||||
if (!FX_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
bool found = false;
|
||||
size_t index = hash % table_size, i = 1;
|
||||
while (table[index].i_item && i < table_size) {
|
||||
const struct fx_hashtable_item_p *item = fx_object_get_private(
|
||||
table[index].i_item,
|
||||
FX_TYPE_HASHTABLE_ITEM);
|
||||
int cmp = fx_value_compare(&item->i_key, v);
|
||||
if (cmp == 0) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
index = (index + (i * i)) % table_size;
|
||||
i++;
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
return FX_ERR_NO_ENTRY;
|
||||
}
|
||||
|
||||
*out_index = index;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status add_item_to_table(
|
||||
fx_hashtable_item *item,
|
||||
struct table_item *table,
|
||||
size_t table_capacity)
|
||||
{
|
||||
size_t index = 0;
|
||||
struct fx_hashtable_item_p *item_p = fx_object_get_private(
|
||||
item,
|
||||
FX_TYPE_HASHTABLE_ITEM);
|
||||
fx_status status = convert_key_to_index(
|
||||
&item_p->i_key,
|
||||
table,
|
||||
table_capacity,
|
||||
&index);
|
||||
if (!FX_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
if (index >= table_capacity) {
|
||||
return FX_ERR_NO_SPACE;
|
||||
}
|
||||
|
||||
struct table_item *bucket = &table[index];
|
||||
if (bucket->i_item) {
|
||||
fx_hashtable_item_unref(bucket->i_item);
|
||||
}
|
||||
|
||||
bucket->i_item = item;
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status destroy_table(
|
||||
struct table_item *table,
|
||||
size_t length,
|
||||
bool destroy_items)
|
||||
{
|
||||
if (!destroy_items) {
|
||||
free(table);
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
struct table_item *item = &table[i];
|
||||
if (!item->i_item) {
|
||||
continue;
|
||||
}
|
||||
|
||||
fx_hashtable_item_unref(item->i_item);
|
||||
}
|
||||
|
||||
free(table);
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status migrate_table(
|
||||
struct table_item *src_table,
|
||||
size_t src_length,
|
||||
struct table_item *dest_table,
|
||||
size_t dest_capacity)
|
||||
{
|
||||
for (size_t i = 0; i < src_length; i++) {
|
||||
struct table_item *item = &src_table[i];
|
||||
if (!item->i_item) {
|
||||
continue;
|
||||
}
|
||||
|
||||
add_item_to_table(item->i_item, dest_table, dest_capacity);
|
||||
}
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status resize_table(
|
||||
struct fx_hashtable_p *hashtable,
|
||||
size_t new_capacity_index)
|
||||
{
|
||||
size_t capacity = primes[new_capacity_index];
|
||||
struct table_item *new_table = calloc(capacity, sizeof *new_table);
|
||||
if (!new_table) {
|
||||
return FX_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
fx_status status = migrate_table(
|
||||
hashtable->t_items,
|
||||
primes[hashtable->t_max_index],
|
||||
new_table,
|
||||
capacity);
|
||||
if (!FX_OK(status)) {
|
||||
free(new_table);
|
||||
return status;
|
||||
}
|
||||
|
||||
free(hashtable->t_items);
|
||||
hashtable->t_items = new_table;
|
||||
hashtable->t_max_index = new_capacity_index;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status hashtable_put(
|
||||
struct fx_hashtable_p *hashtable,
|
||||
fx_value *key,
|
||||
fx_value *value)
|
||||
{
|
||||
fx_status status = FX_SUCCESS;
|
||||
size_t capacity = primes[hashtable->t_max_index];
|
||||
if (hashtable->t_count + 1 > capacity) {
|
||||
status = resize_table(hashtable, hashtable->t_max_index + 1);
|
||||
if (!FX_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
status = convert_key_to_index(
|
||||
key,
|
||||
hashtable->t_items,
|
||||
capacity,
|
||||
&index);
|
||||
if (!FX_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
fx_hashtable_item *item = fx_object_create(FX_TYPE_HASHTABLE_ITEM);
|
||||
if (!item) {
|
||||
return FX_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
struct fx_hashtable_item_p *item_p = fx_object_get_private(
|
||||
item,
|
||||
FX_TYPE_HASHTABLE_ITEM);
|
||||
fx_value_copy(&item_p->i_key, key);
|
||||
fx_value_copy(&item_p->i_value, value);
|
||||
|
||||
hashtable->t_items[index].i_item = item;
|
||||
hashtable->t_count++;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static const fx_value *hashtable_get(
|
||||
const struct fx_hashtable_p *hashtable,
|
||||
const fx_value *key)
|
||||
{
|
||||
size_t index = 0;
|
||||
size_t capacity = primes[hashtable->t_max_index];
|
||||
fx_status status = convert_key_to_index(
|
||||
key,
|
||||
hashtable->t_items,
|
||||
capacity,
|
||||
&index);
|
||||
if (!FX_OK(status)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const fx_hashtable_item *item = hashtable->t_items[index].i_item;
|
||||
if (!item) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const struct fx_hashtable_item_p *item_p = fx_object_get_private(
|
||||
item,
|
||||
FX_TYPE_HASHTABLE_ITEM);
|
||||
return &item_p->i_value;
|
||||
}
|
||||
|
||||
static size_t hashtable_get_count(const struct fx_hashtable_p *hashtable)
|
||||
{
|
||||
return hashtable->t_count;
|
||||
}
|
||||
|
||||
static bool hashtable_is_empty(const struct fx_hashtable_p *hashtable)
|
||||
{
|
||||
return hashtable->t_count == 0;
|
||||
}
|
||||
|
||||
static fx_value hashtable_item_get_key(const struct fx_hashtable_item_p *item)
|
||||
{
|
||||
return item->i_key;
|
||||
}
|
||||
|
||||
static fx_value hashtable_item_get_value(const struct fx_hashtable_item_p *item)
|
||||
{
|
||||
return item->i_value;
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
fx_status fx_hashtable_put(
|
||||
fx_hashtable *hashtable,
|
||||
fx_value *key,
|
||||
fx_value *value)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_HASHTABLE,
|
||||
hashtable_put,
|
||||
hashtable,
|
||||
key,
|
||||
value);
|
||||
}
|
||||
|
||||
const fx_value *fx_hashtable_get(
|
||||
const fx_hashtable *hashtable,
|
||||
const fx_value *key)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_HASHTABLE,
|
||||
hashtable_get,
|
||||
hashtable,
|
||||
key);
|
||||
}
|
||||
|
||||
size_t fx_hashtable_get_count(const fx_hashtable *hashtable)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_TYPE_HASHTABLE,
|
||||
hashtable_get_count,
|
||||
hashtable);
|
||||
}
|
||||
|
||||
bool fx_hashtable_is_empty(const fx_hashtable *hashtable)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_TYPE_HASHTABLE,
|
||||
hashtable_is_empty,
|
||||
hashtable);
|
||||
}
|
||||
|
||||
fx_value fx_hashtable_item_get_key(const fx_hashtable_item *item)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_TYPE_HASHTABLE_ITEM,
|
||||
hashtable_item_get_key,
|
||||
item);
|
||||
}
|
||||
|
||||
fx_value fx_hashtable_item_get_value(const fx_hashtable_item *item)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_TYPE_HASHTABLE_ITEM,
|
||||
hashtable_item_get_value,
|
||||
item);
|
||||
}
|
||||
|
||||
fx_iterator *fx_hashtable_begin(fx_hashtable *hashtable)
|
||||
{
|
||||
fx_hashtable_iterator *it_obj = fx_object_create(
|
||||
FX_TYPE_HASHTABLE_ITERATOR);
|
||||
struct fx_hashtable_iterator_p *it = fx_object_get_private(
|
||||
it_obj,
|
||||
FX_TYPE_HASHTABLE_ITERATOR);
|
||||
|
||||
it->_h = hashtable;
|
||||
it->_h_p = fx_object_get_private(hashtable, FX_TYPE_HASHTABLE);
|
||||
|
||||
it->i = 0;
|
||||
if (fx_hashtable_is_empty(hashtable)) {
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
struct table_item *table = it->_h_p->t_items;
|
||||
size_t capacity = primes[it->_h_p->t_max_index];
|
||||
|
||||
while (it->i < capacity && table[it->i].i_item == NULL) {
|
||||
it->i++;
|
||||
}
|
||||
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void hashtable_init(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_hashtable_p *map = priv;
|
||||
map->t_max_index = 0;
|
||||
map->t_count = 0;
|
||||
|
||||
size_t capacity = primes[map->t_max_index];
|
||||
map->t_items = calloc(capacity, sizeof *map->t_items);
|
||||
}
|
||||
|
||||
static void hashtable_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_hashtable_p *map = priv;
|
||||
}
|
||||
|
||||
static fx_status to_string(
|
||||
const fx_value *obj,
|
||||
fx_stream *out,
|
||||
const char *format)
|
||||
{
|
||||
struct fx_hashtable_p *hashtable = fx_object_get_private(
|
||||
obj->v_object,
|
||||
FX_TYPE_HASHTABLE);
|
||||
|
||||
if (!hashtable->t_count) {
|
||||
fx_stream_write_cstr(out, "{}", NULL);
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_stream_write_cstr(out, "{\n", NULL);
|
||||
|
||||
fx_stream_push_indent(out, 1);
|
||||
|
||||
size_t nr_written = 0;
|
||||
size_t capacity = primes[hashtable->t_max_index];
|
||||
for (size_t i = 0; i < capacity; i++) {
|
||||
fx_hashtable_item *item = hashtable->t_items[i].i_item;
|
||||
if (!item) {
|
||||
continue;
|
||||
}
|
||||
|
||||
nr_written++;
|
||||
struct fx_hashtable_item_p *item_p = fx_object_get_private(
|
||||
item,
|
||||
FX_TYPE_HASHTABLE_ITEM);
|
||||
|
||||
const char *cstr = NULL;
|
||||
fx_value_get_cstr(&item_p->i_key, &cstr);
|
||||
|
||||
if (cstr) {
|
||||
fx_stream_write_char(out, '"');
|
||||
}
|
||||
|
||||
fx_value_to_string(&item_p->i_key, out, NULL);
|
||||
|
||||
if (cstr) {
|
||||
fx_stream_write_char(out, '"');
|
||||
}
|
||||
|
||||
fx_stream_write_cstr(out, " = ", NULL);
|
||||
|
||||
fx_value_get_cstr(&item_p->i_value, &cstr);
|
||||
if (cstr) {
|
||||
fx_stream_write_char(out, '"');
|
||||
}
|
||||
|
||||
fx_value_to_string(&item_p->i_value, out, NULL);
|
||||
|
||||
if (cstr) {
|
||||
fx_stream_write_char(out, '"');
|
||||
}
|
||||
|
||||
if (nr_written != hashtable->t_count) {
|
||||
fx_stream_write_char(out, ',');
|
||||
}
|
||||
|
||||
fx_stream_write_char(out, '\n');
|
||||
}
|
||||
|
||||
fx_stream_pop_indent(out);
|
||||
fx_stream_write_char(out, '}');
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status get_key(
|
||||
const fx_value *item_value,
|
||||
const fx_property *prop,
|
||||
fx_value *out)
|
||||
{
|
||||
fx_hashtable_item *item = NULL;
|
||||
fx_value_get_object(item_value, &item);
|
||||
if (!item) {
|
||||
return FX_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
struct fx_hashtable_item_p *item_p = fx_object_get_private(
|
||||
item,
|
||||
FX_TYPE_HASHTABLE_ITEM);
|
||||
fx_value_copy(out, &item_p->i_key);
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status get_value(
|
||||
const fx_value *item_value,
|
||||
const fx_property *prop,
|
||||
fx_value *out)
|
||||
{
|
||||
fx_hashtable_item *item = NULL;
|
||||
fx_value_get_object(item_value, &item);
|
||||
if (!item) {
|
||||
return FX_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
struct fx_hashtable_item_p *item_p = fx_object_get_private(
|
||||
item,
|
||||
FX_TYPE_HASHTABLE_ITEM);
|
||||
fx_value_copy(out, &item_p->i_value);
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
/*** ITERATOR FUNCTIONS *******************************************************/
|
||||
|
||||
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_hashtable_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_HASHTABLE_ITERATOR);
|
||||
struct table_item *table = it->_h_p->t_items;
|
||||
size_t capacity = primes[it->_h_p->t_max_index];
|
||||
|
||||
it->i++;
|
||||
while (it->i < capacity && table[it->i].i_item == NULL) {
|
||||
it->i++;
|
||||
}
|
||||
|
||||
if (it->i >= capacity || !table[it->i].i_item) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
{
|
||||
struct fx_hashtable_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_HASHTABLE_ITERATOR);
|
||||
|
||||
struct table_item *table = it->_h_p->t_items;
|
||||
size_t capacity = primes[it->_h_p->t_max_index];
|
||||
|
||||
if (table[it->i].i_item) {
|
||||
fx_hashtable_item_unref(table[it->i].i_item);
|
||||
table[it->i].i_item = NULL;
|
||||
}
|
||||
|
||||
it->i++;
|
||||
while (it->i < capacity && table[it->i].i_item == NULL) {
|
||||
it->i++;
|
||||
}
|
||||
|
||||
if (it->i >= capacity || !table[it->i].i_item) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_value iterator_get_value(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_hashtable_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_HASHTABLE_ITERATOR);
|
||||
struct table_item *table = it->_h_p->t_items;
|
||||
size_t capacity = primes[it->_h_p->t_max_index];
|
||||
|
||||
if (table[it->i].i_item) {
|
||||
return FX_VALUE_OBJECT(table[it->i].i_item);
|
||||
}
|
||||
|
||||
return FX_VALUE_EMPTY;
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
// ---- fx_hashtable DEFINITION
|
||||
FX_TYPE_CLASS_BEGIN(fx_hashtable)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
||||
FX_INTERFACE_ENTRY(it_begin) = fx_hashtable_begin;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
||||
FX_TYPE_CLASS_END(fx_hashtable)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_hashtable)
|
||||
FX_TYPE_ID(0xc73b431c, 0x5bd5, 0x45ac, 0x888b, 0x35d352db1fe4);
|
||||
FX_TYPE_NAME("fx.collections.hashtable");
|
||||
FX_TYPE_CLASS(fx_hashtable_class);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_p);
|
||||
FX_TYPE_INSTANCE_INIT(hashtable_init);
|
||||
FX_TYPE_INSTANCE_FINI(hashtable_fini);
|
||||
FX_TYPE_DEFINITION_END(fx_hashtable)
|
||||
|
||||
// ---- fx_hashtable_iterator DEFINITION
|
||||
FX_TYPE_CLASS_BEGIN(fx_hashtable_iterator)
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_TYPE_CLASS_END(fx_hashtable_iterator)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_hashtable_iterator)
|
||||
FX_TYPE_ID(0x42c6836d, 0xf801, 0x49f9, 0xba58, 0xf9cd6f1cb22d);
|
||||
FX_TYPE_NAME("fx.collections.hashtable.iterator");
|
||||
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
|
||||
FX_TYPE_CLASS(fx_hashtable_iterator_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_iterator_p);
|
||||
FX_TYPE_DEFINITION_END(fx_hashtable_iterator)
|
||||
|
||||
// ---- fx_hashtable_item DEFINITION
|
||||
FX_TYPE_CLASS_BEGIN(fx_hashtable_item)
|
||||
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_PROPERTY("key", get_key, NULL);
|
||||
FX_TYPE_PROPERTY("value", get_value, NULL);
|
||||
FX_TYPE_CLASS_END(fx_hashtable_item)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_hashtable_item)
|
||||
FX_TYPE_ID(0x748f78f3, 0x53df, 0x406c, 0x8777, 0x2075bd11fb97);
|
||||
FX_TYPE_NAME("fx.collections.hashtable.item");
|
||||
FX_TYPE_EXTENDS(FX_TYPE_OBJECT);
|
||||
FX_TYPE_CLASS(fx_hashtable_item_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_item_p);
|
||||
FX_TYPE_DEFINITION_END(fx_hashtable_item)
|
||||
@@ -1,13 +1,5 @@
|
||||
/**
|
||||
* A heterogeneous array of objects. fx_array only stores references
|
||||
* to the objects that it contains, not the object data itself.
|
||||
*
|
||||
* fx_array stores pointers to objects in a single contiguous array,
|
||||
* but this is an implementation detail that may change in the future.
|
||||
* Users of fx_array should not rely on this being the case.
|
||||
*/
|
||||
#ifndef FX_DS_ARRAY_H_
|
||||
#define FX_DS_ARRAY_H_
|
||||
#ifndef FX_COLLECTIONS_ARRAY_H_
|
||||
#define FX_COLLECTIONS_ARRAY_H_
|
||||
|
||||
#include <fx/iterator.h>
|
||||
#include <fx/macros.h>
|
||||
@@ -35,187 +27,37 @@ FX_API fx_type_id fx_array_iterator_get_type(void);
|
||||
|
||||
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_array, FX_TYPE_ARRAY);
|
||||
|
||||
/**
|
||||
* Creates an fx_array initialised with the contents of the provided
|
||||
* fx_object pointer array. The fx_array will take a reference to each
|
||||
* object specified in `values`, and will increment the reference count.
|
||||
* The order of objects in the new fx_array will be the same as the order
|
||||
* of objects in `values`. Any NULL pointers in the `values` array will
|
||||
* be ignored, and will not result in gaps in the created fx_array.
|
||||
* However, `nr_values` should be large enough to cover the final
|
||||
* non-NULL pointer in `values`, including any NULL pointers in-between.
|
||||
*
|
||||
* @param values The list of object pointers which should make up the
|
||||
* contents of the new fx_array.
|
||||
* @param nr_values The size of the `values` array.
|
||||
* @return A pointer to the new fx_array, or NULL if an error occurred.
|
||||
*/
|
||||
FX_API fx_array *fx_array_create_with_values(
|
||||
fx_object *const *values,
|
||||
const fx_value *values,
|
||||
size_t nr_values);
|
||||
|
||||
/**
|
||||
* Remove all object references from an fx_array, resetting the size of the
|
||||
* array to zero. The reference counts of all objects in the array will be
|
||||
* decremented.
|
||||
*
|
||||
* @param array The fx_array to clear.
|
||||
*/
|
||||
FX_API void fx_array_clear(fx_array *array);
|
||||
|
||||
/**
|
||||
* Inserts an object at the end of an fx_array. The reference count of
|
||||
* the object will be incremented.
|
||||
*
|
||||
* @param array The fx_array to append the object to.
|
||||
* @param value The object to append.
|
||||
* @return FX_SUCCESS if the object was appended successfully, or an
|
||||
* error code if an error occurred.
|
||||
*/
|
||||
FX_API fx_status fx_array_append(fx_array *array, fx_object *value);
|
||||
FX_API fx_status fx_array_push_back(fx_array *array, fx_value value);
|
||||
|
||||
/**
|
||||
* Inserts an object at the beginning of an fx_array. The reference count
|
||||
* of the object will be incremented. All other objects in the array
|
||||
* will be moved to make space for the object being pre-pended.
|
||||
*
|
||||
* @param array The fx_array to prepend the object to.
|
||||
* @param value The object to prepend.
|
||||
* @return FX_SUCCESS if the object was prepended successfully, or an
|
||||
* error code if an error occurred.
|
||||
*/
|
||||
FX_API fx_status fx_array_prepend(fx_array *array, fx_object *value);
|
||||
FX_API fx_status fx_array_push_front(fx_array *array, fx_value value);
|
||||
|
||||
/**
|
||||
* Inserts an object into an fx_array at a given index. The reference
|
||||
* count of the object will be incremented. If the specified index is at
|
||||
* the beginning or mid-way through the array (i.e. not at the end),
|
||||
* some or all of the objects already in the array will be moved to make
|
||||
* space for the object being inserted.
|
||||
*
|
||||
* @param array The fx_array to insert the object into.
|
||||
* @param value The object to insert.
|
||||
* @param at The index to insert the object at. If the index is
|
||||
* `FX_NPOS`, the object will be inserted at the end of the fx_array.
|
||||
* @return FX_SUCCESS if the object was inserted, or a status code
|
||||
* describing any error that occurred.
|
||||
*/
|
||||
FX_API fx_status fx_array_insert(fx_array *array, fx_object *value, size_t at);
|
||||
FX_API fx_status fx_array_insert(fx_array *array, fx_value value, size_t at);
|
||||
|
||||
/**
|
||||
* Removes the object at the specified index from an fx_array. The
|
||||
* reference count of the removed object will be decremented. If the
|
||||
* specified index is at the beginning or mid-way through the array
|
||||
* (i.e. not at the end), the remaining objects will be moved to fill
|
||||
* the empty space created by the object's removal.
|
||||
*
|
||||
* @param array The fx_array to remove the object from.
|
||||
* @param at The index of the object to be removed.
|
||||
* @return FX_SUCCESS if the object was removed, or a status code
|
||||
* describing any error that occurred.
|
||||
*/
|
||||
FX_API fx_status fx_array_remove(fx_array *array, size_t at);
|
||||
|
||||
/**
|
||||
* Removes the object at the beginning of an fx_array. The reference count
|
||||
* of the removed object will be decremented. The remaining objects will be
|
||||
* moved to fill the empty space created by the object's removal.
|
||||
*
|
||||
* @param array The fx_array to remove the object from.
|
||||
* @return FX_SUCCESS if the object was removed, or a status code describing any
|
||||
* error that occurred.
|
||||
*/
|
||||
FX_API fx_status fx_array_remove_front(fx_array *array);
|
||||
|
||||
/**
|
||||
* Removes the object at the end of an fx_array. The reference count
|
||||
* of the removed object will be decremented.
|
||||
*
|
||||
* @param array The fx_array to remove the object from.
|
||||
* @return FX_SUCCESS if the object was removed, or a status code describing any
|
||||
* error that occurred.
|
||||
*/
|
||||
FX_API fx_status fx_array_remove_back(fx_array *array);
|
||||
|
||||
/**
|
||||
* Removes the object at the specified index of an fx_array, and returns
|
||||
* a pointer to it. The reference count of the removed object will NOT
|
||||
* be decremented. The caller becomes the owner of the array's reference
|
||||
* to the object. If the specified index is at the beginning or mid-way
|
||||
* through the array (i.e. not at the end), the remaining objects will
|
||||
* be moved to fill the empty space created by the object's removal.
|
||||
*
|
||||
* @param array The fx_array to remove the object from.
|
||||
* @param at The index of the object to be removed.
|
||||
* @return An pointer to the removed object. This pointer is owned by
|
||||
* the caller. Returns NULL if an error occurred.
|
||||
*/
|
||||
FX_API fx_object *fx_array_pop(fx_array *array, size_t at);
|
||||
FX_API fx_value fx_array_pop(fx_array *array, size_t at);
|
||||
|
||||
/**
|
||||
* Removes the object at the beginning of an fx_array, and returns a
|
||||
* pointer to it. The reference count of the removed object will NOT be
|
||||
* decremented. The caller becomes the owner of the array's reference to
|
||||
* the object. The remaining objects in the fx_array will be moved to
|
||||
* fill the empty space left by the removed object.
|
||||
*
|
||||
* @param array The fx_array to remove the object from.
|
||||
* @return An pointer to the removed object. This pointer is owned by
|
||||
* the caller. Returns NULL if an error occurred.
|
||||
*/
|
||||
FX_API fx_object *fx_array_pop_front(fx_array *array);
|
||||
FX_API fx_value fx_array_pop_front(fx_array *array);
|
||||
|
||||
/**
|
||||
* Removes the object at the end of an fx_array, and returns a pointer to it.
|
||||
* The reference count of the removed object will NOT be decremented. The caller
|
||||
* becomes the owner of the array's reference to the object.
|
||||
*
|
||||
* @param array The fx_array to remove the object from.
|
||||
* @return An pointer to the removed object. This pointer is owned by the
|
||||
* caller. Returns NULL if an error occurred.
|
||||
*/
|
||||
FX_API fx_object *fx_array_pop_back(fx_array *array);
|
||||
FX_API fx_value fx_array_pop_back(fx_array *array);
|
||||
|
||||
/**
|
||||
* Returns an unowned pointer to the object at the given index of an fx_array.
|
||||
* The caller does not own the returned pointer, and MUST NOT release it.
|
||||
*
|
||||
* @param array The fx_array.
|
||||
* @param at The index of the object to return.
|
||||
* @return A pointer to the object at the given index. This pointer is NOT owned
|
||||
* by the caller. Returns NULL if an error occurred.
|
||||
*/
|
||||
FX_API fx_object *fx_array_at(const fx_array *array, size_t at);
|
||||
FX_API fx_value *fx_array_get_ref(const fx_array *array, size_t at);
|
||||
|
||||
/**
|
||||
* Returns an owned pointer to the object at the given index of an
|
||||
* fx_array. The caller owns the returned pointer, and must release it
|
||||
* when they are finished with it.
|
||||
*
|
||||
* @param array The fx_array.
|
||||
* @param at The index of the object to return.
|
||||
* @return A pointer to the object at the given index. This pointer is
|
||||
* owned by the caller. Returns NULL if an error occurred.
|
||||
*/
|
||||
FX_API fx_object *fx_array_get(fx_array *array, size_t at);
|
||||
FX_API fx_value fx_array_get(const fx_array *array, size_t at);
|
||||
|
||||
/**
|
||||
* Returns the number of objects contained in an fx_array.
|
||||
*
|
||||
* @param array The fx_array.
|
||||
* @return The number of objects contained in the fx_array.
|
||||
*/
|
||||
FX_API size_t fx_array_size(const fx_array *array);
|
||||
FX_API size_t fx_array_get_size(const fx_array *array);
|
||||
|
||||
/**
|
||||
* Returns the current maximum capacity of an fx_array. This represents
|
||||
* the number of objects that can be stored in an fx_array before its
|
||||
* internal buffer would need to be re-sized.
|
||||
*
|
||||
* @param array The fx_array.
|
||||
* @return The maximum capacity of the fx_array.
|
||||
*/
|
||||
FX_API size_t fx_array_capacity(const fx_array *array);
|
||||
FX_API size_t fx_array_get_capacity(const fx_array *array);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
#ifndef FX_DS_DICT_H_
|
||||
#define FX_DS_DICT_H_
|
||||
|
||||
#include <fx/bst.h>
|
||||
#include <fx/macros.h>
|
||||
#include <fx/misc.h>
|
||||
#include <fx/queue.h>
|
||||
#include <fx/status.h>
|
||||
#include <fx/string.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
#define FX_TYPE_DICT (fx_dict_get_type())
|
||||
#define FX_TYPE_DICT_ITERATOR (fx_dict_iterator_get_type())
|
||||
|
||||
struct fx_dict_p;
|
||||
|
||||
FX_DECLARE_TYPE(fx_dict);
|
||||
FX_DECLARE_TYPE(fx_dict_iterator);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_dict)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_dict)
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_dict_iterator)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_dict_iterator)
|
||||
|
||||
#define FX_DICT_ITEM(k, v) {.key = (k), .value = (v)}
|
||||
#define FX_DICT_ITEM_END {.key = NULL, .value = NULL}
|
||||
|
||||
#define fx_dict_foreach(it, dict) \
|
||||
for (int z__fx_unique_name() = fx_dict_iterator_begin(dict, it); \
|
||||
(it)->key != NULL; \
|
||||
fx_dict_iterator_next(it))
|
||||
|
||||
typedef struct fx_dict_item {
|
||||
const fx_string *key;
|
||||
fx_object *value;
|
||||
} fx_dict_item;
|
||||
|
||||
FX_API fx_type_id fx_dict_get_type(void);
|
||||
FX_API fx_type_id fx_dict_iterator_get_type(void);
|
||||
|
||||
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_dict, FX_TYPE_DICT);
|
||||
|
||||
#if 0
|
||||
FX_API fx_dict *fx_dict_create_with_items(const fx_dict_item *items);
|
||||
#endif
|
||||
|
||||
FX_API fx_status fx_dict_put(fx_dict *dict, const char *key, fx_object *value);
|
||||
FX_API fx_status
|
||||
fx_dict_put_sk(fx_dict *dict, const fx_string *key, fx_object *value);
|
||||
FX_API fx_object *fx_dict_at(const fx_dict *dict, const char *key);
|
||||
FX_API fx_object *fx_dict_at_sk(const fx_dict *dict, const fx_string *key);
|
||||
FX_API fx_object *fx_dict_get(fx_dict *dict, const char *key);
|
||||
FX_API fx_object *fx_dict_get_sk(fx_dict *dict, const fx_string *key);
|
||||
|
||||
FX_API bool fx_dict_has_key(const fx_dict *dict, const char *key);
|
||||
FX_API bool fx_dict_has_skey(const fx_dict *dict, const fx_string *key);
|
||||
FX_API size_t fx_dict_get_size(const fx_dict *dict);
|
||||
FX_API bool fx_dict_is_empty(const fx_dict *dict);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,55 @@
|
||||
#ifndef FX_COLLECTIONS_HASHTABLE_H_
|
||||
#define FX_COLLECTIONS_HASHTABLE_H_
|
||||
|
||||
#include <fx/bst.h>
|
||||
#include <fx/iterator.h>
|
||||
#include <fx/macros.h>
|
||||
#include <fx/misc.h>
|
||||
#include <fx/queue.h>
|
||||
#include <fx/status.h>
|
||||
#include <fx/value.h>
|
||||
#include <stddef.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
#define FX_TYPE_HASHTABLE (fx_hashtable_get_type())
|
||||
#define FX_TYPE_HASHTABLE_ITERATOR (fx_hashtable_iterator_get_type())
|
||||
#define FX_TYPE_HASHTABLE_ITEM (fx_hashtable_item_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(fx_hashtable);
|
||||
FX_DECLARE_TYPE(fx_hashtable_iterator);
|
||||
FX_DECLARE_TYPE(fx_hashtable_item);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashtable)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_hashtable)
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashtable_iterator)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_hashtable_iterator)
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashtable_item)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_hashtable_item)
|
||||
|
||||
FX_API fx_type_id fx_hashtable_get_type(void);
|
||||
FX_API fx_type_id fx_hashtable_iterator_get_type(void);
|
||||
FX_API fx_type_id fx_hashtable_item_get_type(void);
|
||||
|
||||
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_hashtable, FX_TYPE_HASHTABLE);
|
||||
FX_API fx_hashtable *fx_hashtable_create(void);
|
||||
|
||||
FX_API fx_status
|
||||
fx_hashtable_put(fx_hashtable *hashtable, fx_value *key, fx_value *value);
|
||||
FX_API const fx_value *fx_hashtable_get(
|
||||
const fx_hashtable *hashtable,
|
||||
const fx_value *key);
|
||||
|
||||
FX_API size_t fx_hashtable_get_count(const fx_hashtable *hashtable);
|
||||
FX_API bool fx_hashtable_is_empty(const fx_hashtable *hashtable);
|
||||
|
||||
FX_API fx_iterator *fx_hashtable_begin(fx_hashtable *hashtable);
|
||||
|
||||
FX_API fx_value fx_hashtable_item_get_key(const fx_hashtable_item *item);
|
||||
FX_API fx_value fx_hashtable_item_get_value(const fx_hashtable_item *item);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
@@ -1,58 +0,0 @@
|
||||
#ifndef FX_DS_TREE_H_
|
||||
#define FX_DS_TREE_H_
|
||||
|
||||
#include <fx/macros.h>
|
||||
#include <fx/misc.h>
|
||||
#include <fx/queue.h>
|
||||
#include <fx/string.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
#define FX_TYPE_TREE (fx_tree_get_type())
|
||||
#define FX_TYPE_TREE_ITERATOR (fx_tree_iterator_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(fx_tree);
|
||||
FX_DECLARE_TYPE(fx_tree_iterator);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_tree)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_tree)
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_tree_iterator)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_tree_iterator)
|
||||
|
||||
#define FX_TREE_NODE_INIT ((fx_tree_node) {0})
|
||||
|
||||
#define FX_TREE_CONTAINER(t, m, v) \
|
||||
((void *)((v) ? (uintptr_t)(v) - (offsetof(t, m)) : 0))
|
||||
|
||||
typedef struct fx_tree_node {
|
||||
struct fx_tree_node *__p01, *__p02, *__p03;
|
||||
struct fx_queue_entry __q01;
|
||||
} fx_tree_node;
|
||||
|
||||
FX_API fx_type_id fx_tree_get_type(void);
|
||||
FX_API fx_type_id fx_tree_iterator_get_type(void);
|
||||
|
||||
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_tree, FX_TYPE_TREE);
|
||||
|
||||
FX_API void fx_tree_set_root(fx_tree *tree, struct fx_tree_node *node);
|
||||
|
||||
FX_API void fx_tree_node_add_child(fx_tree_node *parent, fx_tree_node *child);
|
||||
FX_API void fx_tree_node_add_sibling(fx_tree_node *node, fx_tree_node *to_add);
|
||||
|
||||
FX_API fx_tree_node *fx_tree_node_get_child(fx_tree_node *node, size_t at);
|
||||
FX_API fx_tree_node *fx_tree_node_get_parent(fx_tree_node *node);
|
||||
|
||||
FX_API fx_iterator *fx_tree_begin(fx_tree *tree);
|
||||
FX_API const fx_iterator *fx_tree_cbegin(const fx_tree *tree);
|
||||
|
||||
FX_API fx_iterator *fx_tree_node_begin(fx_tree_node *node);
|
||||
FX_API const fx_iterator *fx_tree_node_cbegin(const fx_tree_node *node);
|
||||
|
||||
FX_API fx_iterator *fx_tree_node_begin_recursive(fx_tree_node *node);
|
||||
FX_API const fx_iterator *fx_tree_node_cbegin_recursive(
|
||||
const fx_tree_node *node);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
+49
-37
@@ -41,8 +41,10 @@ static void *list_first_item(const struct fx_list_p *q)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_list_entry *list_entry
|
||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
||||
struct fx_list_entry *list_entry = fx_unbox(
|
||||
struct fx_list_entry,
|
||||
entry,
|
||||
e_entry);
|
||||
|
||||
return list_entry->e_data;
|
||||
}
|
||||
@@ -55,8 +57,10 @@ static void *list_last_item(const struct fx_list_p *q)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_list_entry *list_entry
|
||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
||||
struct fx_list_entry *list_entry = fx_unbox(
|
||||
struct fx_list_entry,
|
||||
entry,
|
||||
e_entry);
|
||||
|
||||
return list_entry->e_data;
|
||||
}
|
||||
@@ -69,8 +73,10 @@ static struct fx_list_entry *list_first_entry(const struct fx_list_p *q)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_list_entry *list_entry
|
||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
||||
struct fx_list_entry *list_entry = fx_unbox(
|
||||
struct fx_list_entry,
|
||||
entry,
|
||||
e_entry);
|
||||
|
||||
return list_entry;
|
||||
}
|
||||
@@ -83,8 +89,10 @@ static struct fx_list_entry *list_last_entry(const struct fx_list_p *q)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_list_entry *list_entry
|
||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
||||
struct fx_list_entry *list_entry = fx_unbox(
|
||||
struct fx_list_entry,
|
||||
entry,
|
||||
e_entry);
|
||||
|
||||
return list_entry;
|
||||
}
|
||||
@@ -169,8 +177,10 @@ static void *list_pop_front(struct fx_list_p *q)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_list_entry *list_entry
|
||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
||||
struct fx_list_entry *list_entry = fx_unbox(
|
||||
struct fx_list_entry,
|
||||
entry,
|
||||
e_entry);
|
||||
|
||||
void *item = list_entry->e_data;
|
||||
free(list_entry);
|
||||
@@ -187,8 +197,10 @@ static void *list_pop_back(struct fx_list_p *q)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_list_entry *list_entry
|
||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
||||
struct fx_list_entry *list_entry = fx_unbox(
|
||||
struct fx_list_entry,
|
||||
entry,
|
||||
e_entry);
|
||||
|
||||
void *item = list_entry->e_data;
|
||||
free(list_entry);
|
||||
@@ -202,8 +214,10 @@ static struct fx_list_entry *find_item(struct fx_list_p *list, void *item)
|
||||
{
|
||||
struct fx_queue_entry *entry = fx_queue_first(&list->l_queue);
|
||||
while (entry) {
|
||||
struct fx_list_entry *list_entry
|
||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
||||
struct fx_list_entry *list_entry = fx_unbox(
|
||||
struct fx_list_entry,
|
||||
entry,
|
||||
e_entry);
|
||||
|
||||
if (list_entry->e_data == item) {
|
||||
return list_entry;
|
||||
@@ -246,8 +260,10 @@ static void list_delete_all(struct fx_list_p *q)
|
||||
{
|
||||
struct fx_queue_entry *entry = fx_queue_first(&q->l_queue);
|
||||
while (entry) {
|
||||
struct fx_list_entry *list_entry
|
||||
= fx_unbox(struct fx_list_entry, entry, e_entry);
|
||||
struct fx_list_entry *list_entry = fx_unbox(
|
||||
struct fx_list_entry,
|
||||
entry,
|
||||
e_entry);
|
||||
struct fx_queue_entry *next = fx_queue_next(entry);
|
||||
|
||||
free(list_entry);
|
||||
@@ -388,8 +404,9 @@ void *fx_list_entry_value(const struct fx_list_entry *entry)
|
||||
fx_iterator *fx_list_begin(fx_list *q)
|
||||
{
|
||||
fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR);
|
||||
struct fx_list_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_LIST_ITERATOR);
|
||||
struct fx_list_iterator_p *it = fx_object_get_private(
|
||||
it_obj,
|
||||
FX_TYPE_LIST_ITERATOR);
|
||||
|
||||
it->_q = q;
|
||||
it->_q_p = fx_object_get_private(q, FX_TYPE_LIST);
|
||||
@@ -407,8 +424,9 @@ fx_iterator *fx_list_begin(fx_list *q)
|
||||
const fx_iterator *fx_list_cbegin(const fx_list *q)
|
||||
{
|
||||
fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR);
|
||||
struct fx_list_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_LIST_ITERATOR);
|
||||
struct fx_list_iterator_p *it = fx_object_get_private(
|
||||
it_obj,
|
||||
FX_TYPE_LIST_ITERATOR);
|
||||
|
||||
it->_q = (fx_list *)q;
|
||||
it->_q_p = fx_object_get_private(q, FX_TYPE_LIST);
|
||||
@@ -440,8 +458,9 @@ static void list_fini(fx_object *obj, void *priv)
|
||||
|
||||
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_list_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
|
||||
struct fx_list_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_LIST_ITERATOR);
|
||||
|
||||
if (!it->_q_entry) {
|
||||
it->entry = NULL;
|
||||
@@ -467,8 +486,9 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
|
||||
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
{
|
||||
struct fx_list_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
|
||||
struct fx_list_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_LIST_ITERATOR);
|
||||
|
||||
if (!it->entry || !it->_q_entry) {
|
||||
return FX_ERR_OUT_OF_BOUNDS;
|
||||
@@ -488,20 +508,13 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_iterator_value iterator_get_value(fx_iterator *obj)
|
||||
static fx_value iterator_get_value(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_list_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
|
||||
struct fx_list_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_LIST_ITERATOR);
|
||||
|
||||
return FX_ITERATOR_VALUE_PTR(it->item);
|
||||
}
|
||||
|
||||
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_list_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
|
||||
|
||||
return FX_ITERATOR_VALUE_CPTR(it->item);
|
||||
return FX_POINTER(it->item);
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
@@ -537,7 +550,6 @@ FX_TYPE_CLASS_BEGIN(fx_list_iterator)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||
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_CLASS_END(fx_list_iterator)
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
int main(void)
|
||||
{
|
||||
#if 0
|
||||
fx_array *array = fx_array_create();
|
||||
fx_array_append(array, fx_int_create(32));
|
||||
fx_array_append(array, fx_int_create(64));
|
||||
@@ -17,5 +18,6 @@ int main(void)
|
||||
fx_iterator_unref(it);
|
||||
|
||||
fx_array_unref(array);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <fx/collections/hashtable.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/reflection/type.h>
|
||||
#include <fx/stream.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
fx_hashtable *ht = fx_hashtable_create();
|
||||
fx_hashtable_put(ht, &FX_INT(32), &FX_INT(64));
|
||||
fx_hashtable_put(ht, &FX_CSTR("hello"), &FX_INT(128));
|
||||
|
||||
const fx_value *v = fx_hashtable_get(ht, &FX_INT(32));
|
||||
if (v) {
|
||||
printf("32= ");
|
||||
fx_value_to_string(v, fx_stdout, NULL);
|
||||
printf("\n");
|
||||
} else {
|
||||
printf("no value\n");
|
||||
}
|
||||
|
||||
fx_iterator *it = fx_hashtable_begin(ht);
|
||||
fx_foreach(val, it)
|
||||
{
|
||||
fx_hashtable_item *item = NULL;
|
||||
fx_value_get_object(&val, &item);
|
||||
printf("item %p\n", item);
|
||||
fx_value key, value;
|
||||
key = fx_hashtable_item_get_key(item);
|
||||
value = fx_hashtable_item_get_value(item);
|
||||
|
||||
fx_iterator *prop_it = fx_type_get_properties(
|
||||
fx_type_get_by_id(FX_TYPE_HASHTABLE_ITEM));
|
||||
fx_foreach(prop_val, prop_it)
|
||||
{
|
||||
fx_property *prop;
|
||||
fx_value_get_object(&prop_val, &prop);
|
||||
printf("%s = ", fx_property_get_name(prop));
|
||||
fx_value value = FX_VALUE_EMPTY;
|
||||
fx_property_get_value(prop, &val, &value);
|
||||
fx_value_to_string(&value, fx_stdout, NULL);
|
||||
printf("\n");
|
||||
}
|
||||
fx_iterator_unref(prop_it);
|
||||
}
|
||||
fx_iterator_unref(it);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,12 +1,7 @@
|
||||
#include <fx/double.h>
|
||||
#include <fx/float.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
fx_double *d = fx_double_create(6.8);
|
||||
|
||||
printf("double=%lf\n", fx_double_get_value(d));
|
||||
fx_double_unref(d);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
int main(void)
|
||||
{
|
||||
fx_string *string = FX_CSTR("Hello, world!");
|
||||
fx_string *string = fx_string_create_from_cstr("Hello, world!");
|
||||
printf("string object = ");
|
||||
fx_object_to_string(string, fx_stdout);
|
||||
fx_object_to_string(string, fx_stdout, NULL);
|
||||
printf("\n");
|
||||
fx_string_unref(string);
|
||||
return 0;
|
||||
|
||||
@@ -1,128 +0,0 @@
|
||||
#include <fx/bst.h>
|
||||
#include <fx/collections/dict.h>
|
||||
#include <fx/collections/tree.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/iterator.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define NITEMS 16
|
||||
|
||||
struct tree_item {
|
||||
int value;
|
||||
fx_tree_node node;
|
||||
};
|
||||
|
||||
struct bst_item {
|
||||
int value;
|
||||
fx_bst_node node;
|
||||
};
|
||||
|
||||
FX_BST_DEFINE_SIMPLE_GET(struct bst_item, int, node, value, get_node)
|
||||
FX_BST_DEFINE_SIMPLE_INSERT(struct bst_item, node, value, put_node)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
fx_dict *dict = fx_dict_create();
|
||||
fx_dict_put(dict, "hello", fx_int_create(32));
|
||||
fx_dict_put(dict, "world", fx_int_create(64));
|
||||
fx_dict_put(dict, "more", fx_int_create(128));
|
||||
fx_dict_put(dict, "other", fx_int_create(256));
|
||||
|
||||
fx_iterator *it = fx_iterator_begin(dict);
|
||||
|
||||
size_t i = 0;
|
||||
fx_foreach(fx_dict_item *, item, it)
|
||||
{
|
||||
printf("item %zu: %s=%" PRIdPTR "\n",
|
||||
i++,
|
||||
fx_string_get_cstr(item->key),
|
||||
fx_int_get_value(item->value));
|
||||
}
|
||||
|
||||
fx_iterator_unref(it);
|
||||
|
||||
fx_tree *tree = fx_tree_create();
|
||||
struct tree_item items2[NITEMS];
|
||||
|
||||
for (int i = 0; i < NITEMS; i++) {
|
||||
items2[i].value = i;
|
||||
items2[i].node = FX_TREE_NODE_INIT;
|
||||
}
|
||||
|
||||
fx_tree_set_root(tree, &items2[0].node);
|
||||
|
||||
fx_tree_node_add_child(&items2[0].node, &items2[1].node);
|
||||
fx_tree_node_add_child(&items2[0].node, &items2[2].node);
|
||||
fx_tree_node_add_child(&items2[0].node, &items2[3].node);
|
||||
fx_tree_node_add_child(&items2[0].node, &items2[7].node);
|
||||
fx_tree_node_add_child(&items2[1].node, &items2[4].node);
|
||||
fx_tree_node_add_child(&items2[1].node, &items2[5].node);
|
||||
fx_tree_node_add_child(&items2[4].node, &items2[6].node);
|
||||
|
||||
#if 0
|
||||
it = fx_iterator_begin(tree);
|
||||
fx_tree_iterator it2;
|
||||
fx_tree_foreach(&it2, tree)
|
||||
{
|
||||
struct tree_item *item = fx_unbox(struct tree_item, it2.node, node);
|
||||
|
||||
for (size_t i = 0; i < it2.depth; i++) {
|
||||
fputs(" ", stdout);
|
||||
}
|
||||
|
||||
printf("%u\n", item->value);
|
||||
}
|
||||
|
||||
fx_bst bst = {0};
|
||||
struct bst_item items3[NITEMS] = {0};
|
||||
for (int i = 0; i < NITEMS; i++) {
|
||||
items3[i].value = i;
|
||||
put_node(&bst, &items3[i]);
|
||||
}
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
fx_bst_iterator it3;
|
||||
fx_bst_foreach (&it3, &bst) {
|
||||
struct bst_item *item
|
||||
= fx_unbox(struct bst_item, it3.node, node);
|
||||
|
||||
for (size_t i = 0; i < it3.depth; i++) {
|
||||
fputs(" ", stdout);
|
||||
}
|
||||
|
||||
printf("%d\n", item->value);
|
||||
}
|
||||
|
||||
fx_bst_iterator_begin(&bst, &it3);
|
||||
while (fx_bst_iterator_is_valid(&it3)) {
|
||||
struct bst_item *item
|
||||
= fx_unbox(struct bst_item, it3.node, node);
|
||||
|
||||
if (item->value == 9) {
|
||||
fx_bst_iterator_erase(&it3);
|
||||
} else {
|
||||
fx_bst_iterator_next(&it3);
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
fx_bst_foreach (&it3, &bst) {
|
||||
struct bst_item *item
|
||||
= fx_unbox(struct bst_item, it3.node, node);
|
||||
|
||||
for (size_t i = 0; i < it3.depth; i++) {
|
||||
fputs(" ", stdout);
|
||||
}
|
||||
|
||||
printf("%d\n", item->value);
|
||||
}
|
||||
|
||||
fx_tree_unref(tree);
|
||||
#endif
|
||||
fx_dict_unref(dict);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,390 +0,0 @@
|
||||
#include <fx/collections/tree.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ITERATOR_RECURSIVE 0x01u
|
||||
|
||||
#define ITERATOR_IS_RECURSIVE(it) (((it)->_f01 & ITERATOR_RECURSIVE) != 0)
|
||||
#define ITERATOR_UNSET_RECURSIVE(it) ((it)->_f01 &= ~ITERATOR_RECURSIVE)
|
||||
#define ITERATOR_SET_RECURSIVE(it) ((it)->_f01 |= ITERATOR_RECURSIVE)
|
||||
|
||||
#define NODE_PARENT(n) ((n)->__p01)
|
||||
#define NODE_FIRST_CHILD(n) ((n)->__p02)
|
||||
#define NODE_NEXT_SIBLING(n) ((n)->__p03)
|
||||
|
||||
/*** PRIVATE DATA *************************************************************/
|
||||
|
||||
struct fx_tree_p {
|
||||
struct fx_tree_node *t_root;
|
||||
};
|
||||
|
||||
struct fx_tree_iterator_p {
|
||||
size_t i, depth;
|
||||
fx_tree_node *node;
|
||||
|
||||
unsigned char _f01;
|
||||
};
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
static void tree_set_root(struct fx_tree_p *tree, struct fx_tree_node *node)
|
||||
{
|
||||
tree->t_root = node;
|
||||
}
|
||||
|
||||
static const struct fx_tree_node *next_node(
|
||||
const struct fx_tree_node *node,
|
||||
bool recursive,
|
||||
int *depth_diff)
|
||||
{
|
||||
if (!node) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!recursive) {
|
||||
node = NODE_NEXT_SIBLING(node);
|
||||
return node;
|
||||
}
|
||||
|
||||
int d = 0;
|
||||
struct fx_tree_node *next = NODE_FIRST_CHILD(node);
|
||||
if (next) {
|
||||
d = 1;
|
||||
*depth_diff = d;
|
||||
return next;
|
||||
}
|
||||
|
||||
const struct fx_tree_node *n = node;
|
||||
next = NODE_NEXT_SIBLING(n);
|
||||
while (!next) {
|
||||
n = NODE_PARENT(n);
|
||||
if (!n) {
|
||||
break;
|
||||
}
|
||||
|
||||
d--;
|
||||
next = NODE_NEXT_SIBLING(n);
|
||||
}
|
||||
|
||||
*depth_diff = d;
|
||||
return next;
|
||||
}
|
||||
|
||||
static void remove_node(struct fx_tree_node *node)
|
||||
{
|
||||
struct fx_tree_node *parent = NODE_PARENT(node);
|
||||
if (!parent) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct fx_tree_node *n0 = NULL, *n1 = NULL;
|
||||
n0 = NODE_FIRST_CHILD(parent);
|
||||
|
||||
while (n0) {
|
||||
if (n0 == node) {
|
||||
break;
|
||||
}
|
||||
|
||||
n1 = n0;
|
||||
n0 = NODE_NEXT_SIBLING(n0);
|
||||
}
|
||||
|
||||
if (!n0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (n1) {
|
||||
NODE_NEXT_SIBLING(n1) = NODE_NEXT_SIBLING(n0);
|
||||
} else {
|
||||
NODE_FIRST_CHILD(parent) = NODE_NEXT_SIBLING(n0);
|
||||
}
|
||||
|
||||
NODE_PARENT(n0) = NODE_NEXT_SIBLING(n0) = NULL;
|
||||
}
|
||||
|
||||
static void reparent_children(
|
||||
struct fx_tree_node *old_parent,
|
||||
struct fx_tree_node *new_parent)
|
||||
{
|
||||
struct fx_tree_node *last = NODE_FIRST_CHILD(new_parent);
|
||||
while (last && NODE_NEXT_SIBLING(last)) {
|
||||
last = NODE_NEXT_SIBLING(last);
|
||||
}
|
||||
|
||||
struct fx_tree_node *cur = NODE_FIRST_CHILD(old_parent);
|
||||
while (cur) {
|
||||
struct fx_tree_node *next = NODE_NEXT_SIBLING(cur);
|
||||
NODE_PARENT(cur) = new_parent;
|
||||
NODE_NEXT_SIBLING(cur) = NULL;
|
||||
|
||||
if (last) {
|
||||
NODE_NEXT_SIBLING(last) = cur;
|
||||
} else {
|
||||
NODE_FIRST_CHILD(new_parent) = cur;
|
||||
}
|
||||
|
||||
last = cur;
|
||||
cur = next;
|
||||
}
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
void fx_tree_set_root(fx_tree *tree, struct fx_tree_node *node)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_TREE, tree_set_root, tree, node);
|
||||
}
|
||||
|
||||
void fx_tree_node_add_child(
|
||||
struct fx_tree_node *parent,
|
||||
struct fx_tree_node *child)
|
||||
{
|
||||
if (NODE_PARENT(child)) {
|
||||
return;
|
||||
}
|
||||
|
||||
NODE_PARENT(child) = parent;
|
||||
if (!NODE_FIRST_CHILD(parent)) {
|
||||
NODE_FIRST_CHILD(parent) = child;
|
||||
return;
|
||||
}
|
||||
|
||||
struct fx_tree_node *cur = NODE_FIRST_CHILD(parent);
|
||||
while (NODE_NEXT_SIBLING(cur)) {
|
||||
cur = NODE_NEXT_SIBLING(cur);
|
||||
}
|
||||
|
||||
NODE_NEXT_SIBLING(cur) = child;
|
||||
}
|
||||
|
||||
void fx_tree_node_add_sibling(
|
||||
struct fx_tree_node *node,
|
||||
struct fx_tree_node *to_add)
|
||||
{
|
||||
if (NODE_PARENT(to_add) || !NODE_PARENT(node)) {
|
||||
return;
|
||||
}
|
||||
|
||||
fx_tree_node_add_child(NODE_PARENT(node), to_add);
|
||||
}
|
||||
|
||||
struct fx_tree_node *fx_tree_node_get_child(
|
||||
struct fx_tree_node *node,
|
||||
size_t at)
|
||||
{
|
||||
size_t i = 0;
|
||||
struct fx_tree_node *cur = NODE_FIRST_CHILD(node);
|
||||
|
||||
while (i < at) {
|
||||
if (!cur) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cur = NODE_NEXT_SIBLING(cur);
|
||||
i++;
|
||||
}
|
||||
|
||||
return cur;
|
||||
}
|
||||
|
||||
fx_iterator *fx_tree_begin(fx_tree *tree)
|
||||
{
|
||||
struct fx_tree_p *p = fx_object_get_private(tree, FX_TYPE_TREE);
|
||||
return fx_tree_node_begin(p->t_root);
|
||||
}
|
||||
|
||||
const fx_iterator *fx_tree_cbegin(const fx_tree *tree)
|
||||
{
|
||||
struct fx_tree_p *p = fx_object_get_private(tree, FX_TYPE_TREE);
|
||||
return fx_tree_node_begin(p->t_root);
|
||||
}
|
||||
|
||||
fx_iterator *fx_tree_node_begin(struct fx_tree_node *node)
|
||||
{
|
||||
fx_tree_iterator *it_obj = fx_object_create(FX_TYPE_TREE_ITERATOR);
|
||||
struct fx_tree_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_TREE_ITERATOR);
|
||||
|
||||
it->node = NODE_FIRST_CHILD(node);
|
||||
it->i = 0;
|
||||
it->depth = 0;
|
||||
|
||||
ITERATOR_UNSET_RECURSIVE(it);
|
||||
|
||||
if (!it->node) {
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
}
|
||||
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
const fx_iterator *fx_tree_node_cbegin(const struct fx_tree_node *node)
|
||||
{
|
||||
return fx_tree_node_begin((struct fx_tree_node *)node);
|
||||
}
|
||||
|
||||
fx_iterator *fx_tree_node_begin_recursive(struct fx_tree_node *node)
|
||||
{
|
||||
fx_tree_iterator *it_obj = fx_object_create(FX_TYPE_TREE_ITERATOR);
|
||||
struct fx_tree_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_TREE_ITERATOR);
|
||||
|
||||
it->node = node;
|
||||
it->i = 0;
|
||||
it->depth = 0;
|
||||
|
||||
ITERATOR_SET_RECURSIVE(it);
|
||||
|
||||
if (!it->node) {
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
}
|
||||
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
const fx_iterator *fx_tree_node_cbegin_recursive(
|
||||
const struct fx_tree_node *node)
|
||||
{
|
||||
return fx_tree_node_begin_recursive((struct fx_tree_node *)node);
|
||||
}
|
||||
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void tree_init(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_tree_p *tree = priv;
|
||||
}
|
||||
|
||||
static void tree_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_tree_p *tree = priv;
|
||||
}
|
||||
|
||||
/*** ITERATOR FUNCTIONS *******************************************************/
|
||||
|
||||
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_tree_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR);
|
||||
|
||||
int depth_diff = 0;
|
||||
const struct fx_tree_node *next
|
||||
= next_node(it->node, ITERATOR_IS_RECURSIVE(it), &depth_diff);
|
||||
|
||||
if (next) {
|
||||
it->depth += depth_diff;
|
||||
it->i++;
|
||||
} else {
|
||||
it->depth = 0;
|
||||
it->i = 0;
|
||||
}
|
||||
|
||||
it->node = (struct fx_tree_node *)next;
|
||||
return (it->node != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
{
|
||||
struct fx_tree_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR);
|
||||
|
||||
if (!it->node) {
|
||||
return FX_ERR_OUT_OF_BOUNDS;
|
||||
}
|
||||
|
||||
struct fx_tree_node *parent = NODE_PARENT(it->node);
|
||||
if (!parent) {
|
||||
return FX_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
int d = 0;
|
||||
struct fx_tree_node *n = it->node;
|
||||
struct fx_tree_node *next = NODE_NEXT_SIBLING(n);
|
||||
|
||||
if (!next) {
|
||||
next = NODE_FIRST_CHILD(n);
|
||||
}
|
||||
|
||||
while (!next) {
|
||||
n = NODE_PARENT(n);
|
||||
if (!n) {
|
||||
break;
|
||||
}
|
||||
|
||||
d--;
|
||||
next = NODE_NEXT_SIBLING(n);
|
||||
}
|
||||
|
||||
remove_node(it->node);
|
||||
reparent_children(it->node, parent);
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_iterator_value iterator_get_value(fx_iterator *obj)
|
||||
{
|
||||
struct fx_tree_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR);
|
||||
|
||||
if (!it->node) {
|
||||
return FX_ITERATOR_VALUE_NULL;
|
||||
}
|
||||
|
||||
return FX_ITERATOR_VALUE_PTR(it->node);
|
||||
}
|
||||
|
||||
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_tree_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR);
|
||||
|
||||
if (!it->node) {
|
||||
return FX_ITERATOR_VALUE_NULL;
|
||||
}
|
||||
|
||||
return FX_ITERATOR_VALUE_CPTR(it->node);
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
// ---- fx_tree DEFINITION
|
||||
FX_TYPE_CLASS_BEGIN(fx_tree)
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
||||
FX_INTERFACE_ENTRY(it_begin) = fx_tree_begin;
|
||||
FX_INTERFACE_ENTRY(it_cbegin) = fx_tree_cbegin;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
||||
FX_TYPE_CLASS_END(fx_tree)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_tree)
|
||||
FX_TYPE_ID(0x8d8fa36b, 0xc515, 0x4803, 0x8124, 0xfd704f01b8ae);
|
||||
FX_TYPE_CLASS(fx_tree_class);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_tree_p);
|
||||
FX_TYPE_INSTANCE_INIT(tree_init);
|
||||
FX_TYPE_INSTANCE_FINI(tree_fini);
|
||||
FX_TYPE_DEFINITION_END(fx_tree)
|
||||
|
||||
// ---- fx_tree_iterator DEFINITION
|
||||
FX_TYPE_CLASS_BEGIN(fx_tree_iterator)
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||
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_CLASS_END(fx_tree_iterator)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_tree_iterator)
|
||||
FX_TYPE_ID(0xb896e671, 0x84b2, 0x4892, 0xaf09, 0x407f305f4bf8);
|
||||
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
|
||||
FX_TYPE_CLASS(fx_tree_iterator_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_tree_iterator_p);
|
||||
FX_TYPE_DEFINITION_END(fx_tree_iterator)
|
||||
+29
-29
@@ -64,8 +64,9 @@ static fx_result directory_delete(
|
||||
{
|
||||
enum fx_status status = FX_SUCCESS;
|
||||
|
||||
fx_iterator *it
|
||||
= fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_LAST);
|
||||
fx_iterator *it = fx_directory_begin(
|
||||
dir,
|
||||
FX_DIRECTORY_ITERATE_PARENT_LAST);
|
||||
while (FX_OK(fx_iterator_get_status(it))) {
|
||||
fx_iterator_erase(it);
|
||||
}
|
||||
@@ -291,8 +292,9 @@ static fx_result directory_open(
|
||||
|
||||
fx_directory *dir = fx_object_create(FX_TYPE_DIRECTORY);
|
||||
fx_path *cwd = NULL;
|
||||
struct fx_directory_p *p
|
||||
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
|
||||
struct fx_directory_p *p = fx_object_get_private(
|
||||
dir,
|
||||
FX_TYPE_DIRECTORY);
|
||||
|
||||
if (!root) {
|
||||
cwd = fx_path_create_cwd();
|
||||
@@ -358,8 +360,9 @@ fx_result fx_directory_open_temp(fx_directory **out)
|
||||
rpath,
|
||||
FX_DIRECTORY_OPEN_CREATE,
|
||||
&dir);
|
||||
struct fx_directory_p *p
|
||||
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
|
||||
struct fx_directory_p *p = fx_object_get_private(
|
||||
dir,
|
||||
FX_TYPE_DIRECTORY);
|
||||
|
||||
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
|
||||
fx_path_unref(rpath);
|
||||
@@ -408,8 +411,9 @@ const char *fx_directory_get_rel_path_cstr(const fx_directory *dir)
|
||||
|
||||
fx_result fx_directory_delete(fx_directory *dir)
|
||||
{
|
||||
struct fx_directory_p *p
|
||||
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
|
||||
struct fx_directory_p *p = fx_object_get_private(
|
||||
dir,
|
||||
FX_TYPE_DIRECTORY);
|
||||
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
|
||||
/* TODO allow object release functions to return a fx_result value */
|
||||
fx_directory_unref(dir);
|
||||
@@ -555,8 +559,9 @@ fx_iterator *fx_directory_begin(
|
||||
enum fx_directory_iterator_flags flags)
|
||||
{
|
||||
fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR);
|
||||
struct fx_directory_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_DIRECTORY_ITERATOR);
|
||||
struct fx_directory_iterator_p *it = fx_object_get_private(
|
||||
it_obj,
|
||||
FX_TYPE_DIRECTORY_ITERATOR);
|
||||
|
||||
it->flags = flags;
|
||||
it->root = directory;
|
||||
@@ -628,8 +633,9 @@ static const fx_iterator *iterator_cbegin(const fx_object *obj)
|
||||
|
||||
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_directory_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
||||
struct fx_directory_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_DIRECTORY_ITERATOR);
|
||||
if (!it || !it->fts) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
@@ -677,10 +683,12 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
|
||||
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
{
|
||||
struct fx_directory_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
||||
fx_result result
|
||||
= fx_directory_path_unlink(it->root, it->entry.filepath);
|
||||
struct fx_directory_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_DIRECTORY_ITERATOR);
|
||||
fx_result result = fx_directory_path_unlink(
|
||||
it->root,
|
||||
it->entry.filepath);
|
||||
if (fx_result_is_error(result)) {
|
||||
enum fx_status status = fx_error_get_status_code(result);
|
||||
fx_error_discard(result);
|
||||
@@ -690,20 +698,13 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
return iterator_move_next(obj);
|
||||
}
|
||||
|
||||
static fx_iterator_value iterator_get_value(fx_iterator *obj)
|
||||
static fx_value iterator_get_value(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_directory_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
||||
struct fx_directory_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_DIRECTORY_ITERATOR);
|
||||
|
||||
return FX_ITERATOR_VALUE_PTR(&it->entry);
|
||||
}
|
||||
|
||||
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_directory_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
||||
|
||||
return FX_ITERATOR_VALUE_CPTR(&it->entry);
|
||||
return FX_POINTER(&it->entry);
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
@@ -739,7 +740,6 @@ FX_TYPE_CLASS_BEGIN(fx_directory_iterator)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||
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_CLASS_END(fx_directory_iterator)
|
||||
|
||||
|
||||
+18
-9
@@ -6,6 +6,7 @@
|
||||
#include <fx/io/file.h>
|
||||
#include <fx/io/path.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/value.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
@@ -134,8 +135,8 @@ static enum fx_status path_get_directory(
|
||||
size_t dir_path_len = (size_t)(sep - path_cstr);
|
||||
fx_string *dir_path_s = fx_string_get_substr(path_str, 0, dir_path_len);
|
||||
|
||||
fx_path *dir_path
|
||||
= fx_path_create_from_cstr(fx_string_get_cstr(dir_path_s));
|
||||
fx_path *dir_path = fx_path_create_from_cstr(
|
||||
fx_string_get_cstr(dir_path_s));
|
||||
fx_string_unref(dir_path_s);
|
||||
|
||||
*out_dir_path = dir_path;
|
||||
@@ -284,13 +285,15 @@ fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_path_p *result_p
|
||||
= fx_object_get_private(result, FX_TYPE_PATH);
|
||||
struct fx_path_p *result_p = fx_object_get_private(
|
||||
result,
|
||||
FX_TYPE_PATH);
|
||||
|
||||
for (size_t i = 0; i < nr_paths; i++) {
|
||||
if (paths[i]) {
|
||||
struct fx_path_p *path_p
|
||||
= fx_object_get_private(paths[i], FX_TYPE_PATH);
|
||||
struct fx_path_p *path_p = fx_object_get_private(
|
||||
paths[i],
|
||||
FX_TYPE_PATH);
|
||||
append_path(result_p, path_p);
|
||||
}
|
||||
}
|
||||
@@ -385,18 +388,24 @@ static void path_init(fx_object *obj, void *priv)
|
||||
}
|
||||
}
|
||||
|
||||
void path_fini(fx_object *obj, void *priv)
|
||||
static void path_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_path_p *path = priv;
|
||||
|
||||
fx_string_unref(path->p_pathstr);
|
||||
}
|
||||
|
||||
void path_to_string(const fx_object *obj, fx_stream *out)
|
||||
static fx_status path_to_string(
|
||||
const fx_value *obj,
|
||||
fx_stream *out,
|
||||
const char *format)
|
||||
{
|
||||
struct fx_path_p *path = fx_object_get_private(obj, FX_TYPE_PATH);
|
||||
struct fx_path_p *path = fx_object_get_private(
|
||||
obj->v_object,
|
||||
FX_TYPE_PATH);
|
||||
|
||||
fx_stream_write_cstr(out, fx_string_get_cstr(path->p_pathstr), NULL);
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
+6
-2
@@ -20,9 +20,13 @@ int main(int argc, const char **argv)
|
||||
return -1;
|
||||
}
|
||||
|
||||
fx_iterator *it = fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_FIRST);
|
||||
fx_foreach(fx_directory_entry *, entry, it)
|
||||
fx_iterator *it = fx_directory_begin(
|
||||
dir,
|
||||
FX_DIRECTORY_ITERATE_PARENT_FIRST);
|
||||
fx_foreach(val, it)
|
||||
{
|
||||
fx_directory_entry *entry = NULL;
|
||||
fx_value_get_pointer(&val, (void **)&entry);
|
||||
printf("%s\n", fx_path_ptr(entry->filepath));
|
||||
}
|
||||
|
||||
|
||||
+351
-29
@@ -1,8 +1,24 @@
|
||||
#include "fx/namemap.h"
|
||||
|
||||
#include <fx/bst.h>
|
||||
#include <fx/hash.h>
|
||||
#include <fx/iterator.h>
|
||||
#include <fx/macros.h>
|
||||
#include <fx/queue.h>
|
||||
#include <fx/reflection/assembly.h>
|
||||
#include <fx/reflection/type.h>
|
||||
|
||||
FX_API fx_type_id fx_assembly_iterator_get_type();
|
||||
FX_API fx_type_id fx_assembly_type_iterator_get_type();
|
||||
|
||||
FX_DECLARE_TYPE(fx_assembly_iterator);
|
||||
FX_DECLARE_TYPE(fx_assembly_type_iterator);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_assembly_iterator)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_assembly_iterator)
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_assembly_type_iterator)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_assembly_type_iterator)
|
||||
|
||||
enum map_entry_type {
|
||||
MAP_ENTRY_NONE = 0,
|
||||
@@ -12,6 +28,7 @@ enum map_entry_type {
|
||||
|
||||
struct map_entry {
|
||||
enum map_entry_type e_type;
|
||||
struct map_bucket *e_bucket;
|
||||
uint64_t e_hash;
|
||||
union {
|
||||
fx_queue_entry e_entry;
|
||||
@@ -39,8 +56,10 @@ struct type {
|
||||
};
|
||||
|
||||
struct fx_assembly_p {
|
||||
fx_assembly *a_self;
|
||||
const char *a_name;
|
||||
struct map a_types;
|
||||
fx_namemap_entry a_entry;
|
||||
|
||||
struct {
|
||||
long v_major;
|
||||
@@ -50,6 +69,14 @@ struct fx_assembly_p {
|
||||
} a_version;
|
||||
};
|
||||
|
||||
struct fx_assembly_iterator_p {
|
||||
fx_namemap_entry *it_cur;
|
||||
};
|
||||
|
||||
struct fx_assembly_type_iterator_p {
|
||||
struct type *it_item;
|
||||
};
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
FX_BST_DEFINE_SIMPLE_GET(
|
||||
@@ -60,6 +87,8 @@ FX_BST_DEFINE_SIMPLE_GET(
|
||||
map_get_entry);
|
||||
FX_BST_DEFINE_SIMPLE_INSERT(struct map_entry, e_node, e_hash, map_put_entry);
|
||||
|
||||
static fx_namemap assembly_map = FX_NAMEMAP_INIT;
|
||||
|
||||
static struct map_bucket *map_item_convert_to_bucket(
|
||||
struct map *map,
|
||||
struct map_item *item)
|
||||
@@ -78,6 +107,28 @@ static struct map_bucket *map_item_convert_to_bucket(
|
||||
return bucket;
|
||||
}
|
||||
|
||||
static struct map_item *map_first_item(const struct map *map)
|
||||
{
|
||||
fx_bst_node *first = fx_bst_first(&map->m_entries);
|
||||
if (!first) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct map_entry *entry = fx_unbox(struct map_entry, first, e_node);
|
||||
if (entry->e_type == MAP_ENTRY_ITEM) {
|
||||
return (struct map_item *)entry;
|
||||
}
|
||||
|
||||
struct map_bucket *bucket = (struct map_bucket *)entry;
|
||||
fx_queue_entry *first_entry = fx_queue_first(&bucket->b_items);
|
||||
if (!first_entry) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
entry = fx_unbox(struct map_entry, first_entry, e_entry);
|
||||
return (struct map_item *)entry;
|
||||
}
|
||||
|
||||
static void map_put(struct map *map, const char *name, struct map_item *item)
|
||||
{
|
||||
uint64_t hash = fx_hash_cstr(name);
|
||||
@@ -109,34 +160,81 @@ static void map_put(struct map *map, const char *name, struct map_item *item)
|
||||
}
|
||||
}
|
||||
|
||||
static void assembly_set_name(struct fx_assembly_p *asm, const char *name)
|
||||
static struct map_item *map_item_next(struct map_item *item)
|
||||
{
|
||||
asm->a_name = name;
|
||||
struct map_bucket *bucket = item->i_entry.e_bucket;
|
||||
struct map_entry *next_item = NULL;
|
||||
if (bucket) {
|
||||
struct fx_queue_entry *q_item = fx_queue_next(
|
||||
&item->i_entry.e_entry);
|
||||
if (!q_item) {
|
||||
struct fx_bst_node *node = fx_bst_next(
|
||||
&bucket->b_entry.e_node);
|
||||
next_item = fx_unbox(struct map_entry, node, e_node);
|
||||
} else {
|
||||
next_item = fx_unbox(struct map_entry, q_item, e_entry);
|
||||
}
|
||||
} else {
|
||||
struct fx_bst_node *node = fx_bst_next(&item->i_entry.e_node);
|
||||
next_item = fx_unbox(struct map_entry, node, e_node);
|
||||
}
|
||||
|
||||
if (!next_item) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (next_item->e_type == MAP_ENTRY_BUCKET) {
|
||||
bucket = (struct map_bucket *)next_item;
|
||||
struct fx_queue_entry *q_item = fx_queue_first(
|
||||
&bucket->b_items);
|
||||
next_item = fx_unbox(struct map_entry, q_item, e_entry);
|
||||
}
|
||||
|
||||
return (struct map_item *)next_item;
|
||||
}
|
||||
|
||||
static fx_status assembly_register(struct fx_assembly_p *assembly)
|
||||
{
|
||||
if (fx_namemap_get(&assembly_map, assembly->a_name)) {
|
||||
return FX_ERR_NAME_EXISTS;
|
||||
}
|
||||
|
||||
fx_namemap_put(&assembly_map, assembly->a_name, &assembly->a_entry);
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static void assembly_set_name(struct fx_assembly_p *assembly, const char *name)
|
||||
{
|
||||
assembly->a_name = name;
|
||||
}
|
||||
|
||||
static void assembly_set_version(
|
||||
struct fx_assembly_p *asm,
|
||||
struct fx_assembly_p *assembly,
|
||||
long major,
|
||||
long minor,
|
||||
long build,
|
||||
long revision)
|
||||
{
|
||||
asm->a_version.v_major = major;
|
||||
asm->a_version.v_minor = minor;
|
||||
asm->a_version.v_build = build;
|
||||
asm->a_version.v_revision = revision;
|
||||
assembly->a_version.v_major = major;
|
||||
assembly->a_version.v_minor = minor;
|
||||
assembly->a_version.v_build = build;
|
||||
assembly->a_version.v_revision = revision;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
static void assembly_add_type(
|
||||
struct fx_assembly_p *asm,
|
||||
struct fx_assembly_p *assembly,
|
||||
const char *full_name,
|
||||
fx_type_id type_id)
|
||||
{
|
||||
struct type *type = malloc(sizeof *type);
|
||||
memset(type, 0x0, sizeof *type);
|
||||
|
||||
map_put(&asm->a_types, full_name, &type->e_map_item);
|
||||
type->e_type = type_id;
|
||||
fx_type *ty = (fx_type *)fx_type_get_by_id(type_id);
|
||||
fx_type_set_assembly(ty, assembly->a_self);
|
||||
|
||||
map_put(&assembly->a_types, full_name, &type->e_map_item);
|
||||
}
|
||||
|
||||
static void map_item_dump(struct map_item *item)
|
||||
@@ -145,20 +243,22 @@ static void map_item_dump(struct map_item *item)
|
||||
printf(" Type: %s\n", type->e_map_item.i_name);
|
||||
}
|
||||
|
||||
static void assembly_dump(struct fx_assembly_p *asm)
|
||||
static void assembly_dump(struct fx_assembly_p *assembly)
|
||||
{
|
||||
printf("Loaded assembly:\n");
|
||||
printf(" %s, Version=%ld.%ld.%ld.%ld\n",
|
||||
asm->a_name,
|
||||
asm->a_version.v_major,
|
||||
asm->a_version.v_minor,
|
||||
asm->a_version.v_build,
|
||||
asm->a_version.v_revision);
|
||||
assembly->a_name,
|
||||
assembly->a_version.v_major,
|
||||
assembly->a_version.v_minor,
|
||||
assembly->a_version.v_build,
|
||||
assembly->a_version.v_revision);
|
||||
|
||||
fx_bst_node *cur_node = fx_bst_first(&asm->a_types.m_entries);
|
||||
fx_bst_node *cur_node = fx_bst_first(&assembly->a_types.m_entries);
|
||||
while (cur_node) {
|
||||
struct map_entry *entry
|
||||
= fx_unbox(struct map_entry, cur_node, e_node);
|
||||
struct map_entry *entry = fx_unbox(
|
||||
struct map_entry,
|
||||
cur_node,
|
||||
e_node);
|
||||
switch (entry->e_type) {
|
||||
case MAP_ENTRY_ITEM: {
|
||||
struct map_item *item = (struct map_item *)entry;
|
||||
@@ -168,8 +268,8 @@ static void assembly_dump(struct fx_assembly_p *asm)
|
||||
|
||||
case MAP_ENTRY_BUCKET: {
|
||||
struct map_bucket *bucket = (struct map_bucket *)entry;
|
||||
fx_queue_entry *cur_qentry
|
||||
= fx_queue_first(&bucket->b_items);
|
||||
fx_queue_entry *cur_qentry = fx_queue_first(
|
||||
&bucket->b_items);
|
||||
while (cur_qentry) {
|
||||
struct map_item *item = fx_unbox(
|
||||
struct map_item,
|
||||
@@ -187,19 +287,110 @@ static void assembly_dump(struct fx_assembly_p *asm)
|
||||
}
|
||||
}
|
||||
|
||||
static fx_iterator *assembly_get_types(const struct fx_assembly_p *assembly)
|
||||
{
|
||||
fx_assembly_type_iterator *it = fx_object_create(
|
||||
fx_assembly_type_iterator_get_type());
|
||||
if (!it) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_assembly_type_iterator_p *it_p = fx_object_get_private(
|
||||
it,
|
||||
fx_assembly_type_iterator_get_type());
|
||||
struct map_item *item = map_first_item(&assembly->a_types);
|
||||
if (item) {
|
||||
it_p->it_item = fx_unbox(struct type, item, e_map_item);
|
||||
} else {
|
||||
fx_iterator_set_status(it, FX_ERR_NO_DATA);
|
||||
}
|
||||
|
||||
return it;
|
||||
}
|
||||
|
||||
static const char *assembly_get_name(const struct fx_assembly_p *assembly)
|
||||
{
|
||||
return assembly->a_name;
|
||||
}
|
||||
|
||||
static void assembly_get_version(
|
||||
const struct fx_assembly_p *assembly,
|
||||
long *out_major,
|
||||
long *out_minor,
|
||||
long *out_build,
|
||||
long *out_revision)
|
||||
{
|
||||
if (out_major) {
|
||||
*out_major = assembly->a_version.v_major;
|
||||
}
|
||||
|
||||
if (out_minor) {
|
||||
*out_minor = assembly->a_version.v_minor;
|
||||
}
|
||||
|
||||
if (out_build) {
|
||||
*out_build = assembly->a_version.v_build;
|
||||
}
|
||||
|
||||
if (out_revision) {
|
||||
*out_revision = assembly->a_version.v_revision;
|
||||
}
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
void fx_assembly_set_name(fx_assembly *asm, const char *name)
|
||||
fx_iterator *fx_assembly_get_all(void)
|
||||
{
|
||||
fx_assembly_iterator *it = fx_object_create(
|
||||
fx_assembly_iterator_get_type());
|
||||
if (!it) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_assembly_iterator_p *it_p = fx_object_get_private(
|
||||
it,
|
||||
fx_assembly_iterator_get_type());
|
||||
it_p->it_cur = fx_namemap_first(&assembly_map);
|
||||
if (!it_p->it_cur) {
|
||||
fx_iterator_set_status(it, FX_ERR_NO_DATA);
|
||||
}
|
||||
|
||||
return it;
|
||||
}
|
||||
|
||||
const fx_assembly *fx_assembly_get_by_name(const char *name)
|
||||
{
|
||||
const fx_namemap_entry *entry = fx_namemap_get(&assembly_map, name);
|
||||
if (!entry) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_assembly_p *assembly = fx_unbox(
|
||||
struct fx_assembly_p,
|
||||
entry,
|
||||
a_entry);
|
||||
return assembly->a_self;
|
||||
}
|
||||
|
||||
fx_status fx_assembly_register(fx_assembly *assembly)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_register,
|
||||
assembly);
|
||||
}
|
||||
|
||||
void fx_assembly_set_name(fx_assembly *assembly, const char *name)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_V(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_set_name,
|
||||
asm,
|
||||
assembly,
|
||||
name);
|
||||
}
|
||||
|
||||
void fx_assembly_set_version(
|
||||
fx_assembly *asm,
|
||||
fx_assembly *assembly,
|
||||
long major,
|
||||
long minor,
|
||||
long build,
|
||||
@@ -208,7 +399,7 @@ void fx_assembly_set_version(
|
||||
FX_CLASS_DISPATCH_STATIC_V(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_set_version,
|
||||
asm,
|
||||
assembly,
|
||||
major,
|
||||
minor,
|
||||
build,
|
||||
@@ -216,39 +407,131 @@ void fx_assembly_set_version(
|
||||
}
|
||||
|
||||
void fx_assembly_add_type(
|
||||
fx_assembly *asm,
|
||||
fx_assembly *assembly,
|
||||
const char *full_name,
|
||||
fx_type_id type_id)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_V(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_add_type,
|
||||
asm,
|
||||
assembly,
|
||||
full_name,
|
||||
type_id);
|
||||
}
|
||||
|
||||
void fx_assembly_dump(const fx_assembly *asm)
|
||||
fx_iterator *fx_assembly_get_types(const fx_assembly *assembly)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_get_types,
|
||||
assembly);
|
||||
}
|
||||
|
||||
const char *fx_assembly_get_name(const fx_assembly *assembly)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_get_name,
|
||||
assembly);
|
||||
}
|
||||
|
||||
void fx_assembly_get_version(
|
||||
const fx_assembly *assembly,
|
||||
long *out_major,
|
||||
long *out_minor,
|
||||
long *out_build,
|
||||
long *out_revision)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_V(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_get_version,
|
||||
assembly,
|
||||
out_major,
|
||||
out_minor,
|
||||
out_build,
|
||||
out_revision);
|
||||
}
|
||||
|
||||
void fx_assembly_dump(const fx_assembly *assembly)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_dump,
|
||||
asm);
|
||||
assembly);
|
||||
}
|
||||
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void assembly_init(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_assembly_p *assembly = priv;
|
||||
assembly->a_self = obj;
|
||||
}
|
||||
|
||||
static void assembly_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
}
|
||||
|
||||
/*** ITERATOR DEFINITION ******************************************************/
|
||||
|
||||
static enum fx_status type_iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_assembly_type_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
fx_assembly_type_iterator_get_type());
|
||||
if (!it->it_item) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
struct map_item *next = map_item_next(&it->it_item->e_map_item);
|
||||
it->it_item = fx_unbox(struct type, next, e_map_item);
|
||||
return it->it_item ? FX_SUCCESS : FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
static fx_value type_iterator_get_value(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_assembly_type_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
fx_assembly_type_iterator_get_type());
|
||||
if (!it->it_item) {
|
||||
return FX_VALUE_EMPTY;
|
||||
}
|
||||
|
||||
const fx_type *ty = fx_type_get_by_id(it->it_item->e_type);
|
||||
return FX_VALUE_OBJECT(ty);
|
||||
}
|
||||
|
||||
static enum fx_status assembly_iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_assembly_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
fx_assembly_iterator_get_type());
|
||||
if (!it->it_cur) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
it->it_cur = fx_namemap_next(&assembly_map, it->it_cur);
|
||||
return it->it_cur ? FX_SUCCESS : FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
static fx_value assembly_iterator_get_value(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_assembly_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
fx_assembly_iterator_get_type());
|
||||
if (!it->it_cur) {
|
||||
return FX_VALUE_EMPTY;
|
||||
}
|
||||
|
||||
struct fx_assembly_p *assembly = fx_unbox(
|
||||
struct fx_assembly_p,
|
||||
it->it_cur,
|
||||
a_entry);
|
||||
return FX_VALUE_OBJECT(assembly->a_self);
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
// ---- fx_string DEFINITION
|
||||
FX_TYPE_CLASS_BEGIN(fx_assembly)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = NULL;
|
||||
@@ -257,8 +540,47 @@ FX_TYPE_CLASS_END(fx_assembly)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_assembly)
|
||||
FX_TYPE_ID(0xf6690c30, 0x6642, 0x42f0, 0xb79f, 0xe2baf3684b1b);
|
||||
FX_TYPE_NAME("fx.reflection.assembly");
|
||||
FX_TYPE_CLASS(fx_assembly_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_assembly_p);
|
||||
FX_TYPE_INSTANCE_INIT(assembly_init);
|
||||
FX_TYPE_INSTANCE_FINI(assembly_fini);
|
||||
FX_TYPE_DEFINITION_END(fx_assembly)
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_assembly_type_iterator)
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = type_iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_get_value) = type_iterator_get_value;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_TYPE_CLASS_END(fx_assembly_type_iterator)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_assembly_type_iterator)
|
||||
FX_TYPE_ID(0x74ecb8df, 0x155d, 0x4e45, 0x96a6, 0x0f71e3ea0a1e);
|
||||
FX_TYPE_NAME("fx.reflection.assembly.type_iterator");
|
||||
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
|
||||
FX_TYPE_CLASS(fx_assembly_type_iterator_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_assembly_type_iterator_p);
|
||||
FX_TYPE_DEFINITION_END(fx_assembly_type_iterator)
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_assembly_iterator)
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = assembly_iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_get_value) = assembly_iterator_get_value;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_TYPE_CLASS_END(fx_assembly_iterator)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_assembly_iterator)
|
||||
FX_TYPE_ID(0x391f8d65, 0x9baf, 0x4941, 0xbdbe, 0xe739226f8947);
|
||||
FX_TYPE_NAME("fx.reflection.assembly.iterator");
|
||||
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
|
||||
FX_TYPE_CLASS(fx_assembly_iterator_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_assembly_iterator_p);
|
||||
FX_TYPE_DEFINITION_END(fx_assembly_iterator)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <fx/macros.h>
|
||||
#include <fx/reflection/function.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/value-type.h>
|
||||
#include <fx/value.h>
|
||||
#include <platform/callvm.h>
|
||||
|
||||
@@ -8,8 +9,8 @@ struct fx_function_p {
|
||||
char *func_name;
|
||||
fx_function_flags func_flags;
|
||||
fx_function_impl func_impl;
|
||||
fx_value_type func_return_type;
|
||||
fx_value_type *func_arg_types;
|
||||
fx_type_id func_return_type;
|
||||
fx_type_id *func_arg_types;
|
||||
/* number of explicit arguments that the function takes.
|
||||
* if the FX_FUNCTION_F_VARARG flag is set, the function supports a
|
||||
* variable number of arguments, which may be handled differently by the
|
||||
@@ -61,6 +62,29 @@ static fx_status function_bind(
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static void push_arg(
|
||||
const fx_value *arg,
|
||||
fx_type_id param_type,
|
||||
struct callvm *vm)
|
||||
{
|
||||
if (!param_type) {
|
||||
callvm_push(vm, arg);
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned int param_value_type = __fx_type_get_value_type(param_type);
|
||||
if (param_value_type == __FX_VALUE_TYPE_CSTR
|
||||
&& fx_value_is_type(arg, FX_TYPE_STRING)) {
|
||||
fx_object *obj;
|
||||
fx_value_get_object(arg, &obj);
|
||||
fx_value cstr = FX_CSTR(fx_string_get_cstr(obj));
|
||||
callvm_push(vm, &cstr);
|
||||
return;
|
||||
}
|
||||
|
||||
callvm_push(vm, arg);
|
||||
}
|
||||
|
||||
static fx_status function_invoke(
|
||||
const struct fx_function_p *func,
|
||||
const fx_value *args,
|
||||
@@ -84,14 +108,27 @@ static fx_status function_invoke(
|
||||
struct callvm vm = {0};
|
||||
callvm_reset(&vm, nr_fixed_args);
|
||||
|
||||
size_t param_index = 0;
|
||||
for (size_t i = 0; i < func->func_nr_bound_args; i++) {
|
||||
const fx_value *arg = &func->func_bound_args[i];
|
||||
callvm_push(&vm, arg);
|
||||
fx_type_id param_type = NULL;
|
||||
if (param_index < func->func_nr_args) {
|
||||
param_type = func->func_arg_types[param_index];
|
||||
}
|
||||
|
||||
push_arg(arg, param_type, &vm);
|
||||
param_index++;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < nr_args; i++) {
|
||||
const fx_value *arg = &args[i];
|
||||
callvm_push(&vm, arg);
|
||||
fx_type_id param_type = NULL;
|
||||
if (param_index < func->func_nr_args) {
|
||||
param_type = func->func_arg_types[param_index];
|
||||
}
|
||||
|
||||
push_arg(arg, param_type, &vm);
|
||||
param_index++;
|
||||
}
|
||||
|
||||
*return_value = callvm_invoke(
|
||||
@@ -109,9 +146,9 @@ fx_function *fx_function_create(
|
||||
const char *name,
|
||||
fx_function_flags flags,
|
||||
fx_function_impl impl,
|
||||
const fx_value_type *args,
|
||||
const fx_type_id *args,
|
||||
size_t nr_args,
|
||||
fx_value_type return_type)
|
||||
fx_type_id return_type)
|
||||
{
|
||||
fx_function *func = fx_object_create(FX_REFLECTION_TYPE_FUNCTION);
|
||||
if (!func) {
|
||||
@@ -122,7 +159,7 @@ fx_function *fx_function_create(
|
||||
func,
|
||||
FX_REFLECTION_TYPE_FUNCTION);
|
||||
|
||||
if (nr_args == 1 && args[0] == FX_VALUE_TYPE_NONE) {
|
||||
if (nr_args == 1 && args[0] == NULL) {
|
||||
nr_args = 0;
|
||||
}
|
||||
|
||||
@@ -191,8 +228,7 @@ static void function_fini(fx_object *obj, void *priv)
|
||||
}
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION
|
||||
* *********************************************************/
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_function)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef FX_REFLECTION_ASSEMBLY_H_
|
||||
#define FX_REFLECTION_ASSEMBLY_H_
|
||||
|
||||
#include <fx/iterator.h>
|
||||
#include <fx/macros.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
@@ -16,20 +17,33 @@ FX_API fx_type_id fx_assembly_get_type();
|
||||
|
||||
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_assembly, FX_REFLECTION_TYPE_ASSEMBLY);
|
||||
|
||||
FX_API void fx_assembly_set_name(fx_assembly *asm, const char *name);
|
||||
FX_API fx_iterator *fx_assembly_get_all(void);
|
||||
FX_API const fx_assembly *fx_assembly_get_by_name(const char *name);
|
||||
|
||||
FX_API fx_status fx_assembly_register(fx_assembly *assembly);
|
||||
FX_API void fx_assembly_set_name(fx_assembly *assembly, const char *name);
|
||||
FX_API void fx_assembly_set_version(
|
||||
fx_assembly *asm,
|
||||
fx_assembly *assembly,
|
||||
long major,
|
||||
long minor,
|
||||
long build,
|
||||
long revision);
|
||||
|
||||
FX_API void fx_assembly_add_type(
|
||||
fx_assembly *asm,
|
||||
fx_assembly *assembly,
|
||||
const char *full_name,
|
||||
fx_type_id type_id);
|
||||
FX_API fx_iterator *fx_assembly_get_types(const fx_assembly *assembly);
|
||||
|
||||
FX_API void fx_assembly_dump(const fx_assembly *asm);
|
||||
FX_API const char *fx_assembly_get_name(const fx_assembly *assembly);
|
||||
FX_API void fx_assembly_get_version(
|
||||
const fx_assembly *assembly,
|
||||
long *out_major,
|
||||
long *out_minor,
|
||||
long *out_build,
|
||||
long *out_revision);
|
||||
|
||||
FX_API void fx_assembly_dump(const fx_assembly *assembly);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
|
||||
@@ -32,9 +32,9 @@ FX_API fx_function *fx_function_create(
|
||||
const char *name,
|
||||
fx_function_flags flags,
|
||||
fx_function_impl impl,
|
||||
const fx_value_type *args,
|
||||
const fx_type_id *args,
|
||||
size_t nr_args,
|
||||
fx_value_type return_type);
|
||||
fx_type_id return_type);
|
||||
FX_API fx_status
|
||||
fx_function_bind(fx_function *func, fx_value *args, size_t nr_args);
|
||||
FX_API fx_status fx_function_invoke(
|
||||
|
||||
@@ -1,4 +1,38 @@
|
||||
#ifndef FX_REFLECTION_PROPERTY_H_
|
||||
#define FX_REFLECTION_PROPERTY_H_
|
||||
|
||||
#include <fx/macros.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
#define FX_REFLECTION_TYPE_PROPERTY (fx_property_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(fx_property)
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_property)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_property)
|
||||
|
||||
typedef fx_status (
|
||||
*fx_property_getter)(const fx_value *, const fx_property *, fx_value *);
|
||||
typedef fx_status (
|
||||
*fx_property_setter)(fx_value *, const fx_property *, fx_value *);
|
||||
|
||||
FX_API fx_type_id fx_property_get_type(void);
|
||||
|
||||
FX_API fx_property *fx_property_create(
|
||||
const char *name,
|
||||
fx_property_getter get,
|
||||
fx_property_setter set);
|
||||
|
||||
FX_API const char *fx_property_get_name(const fx_property *prop);
|
||||
FX_API fx_status fx_property_get_value(
|
||||
const fx_property *prop,
|
||||
const fx_value *container,
|
||||
fx_value *out);
|
||||
FX_API fx_status fx_property_set_value(
|
||||
const fx_property *prop,
|
||||
fx_value *container,
|
||||
fx_value *new_value);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
#ifndef FX_REFLECTION_TYPE_H_
|
||||
#define FX_REFLECTION_TYPE_H_
|
||||
|
||||
#include <fx/iterator.h>
|
||||
#include <fx/macros.h>
|
||||
#include <fx/reflection/assembly.h>
|
||||
#include <fx/reflection/function.h>
|
||||
#include <fx/reflection/property.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
@@ -17,14 +20,27 @@ FX_TYPE_CLASS_DECLARATION_END(fx_type)
|
||||
|
||||
FX_API fx_type_id fx_type_get_type();
|
||||
|
||||
FX_API const fx_assembly *fx_type_get_assembly(const fx_type *ty);
|
||||
FX_API fx_status fx_type_set_assembly(fx_type *ty, fx_assembly *assembly);
|
||||
|
||||
FX_API const char *fx_type_get_name(const fx_type *ty);
|
||||
FX_API fx_type_flags fx_type_get_flags(const fx_type *ty);
|
||||
FX_API const fx_function *fx_type_get_function(
|
||||
const fx_type *ty,
|
||||
const char *name);
|
||||
FX_API fx_iterator *fx_type_get_functions(const fx_type *ty);
|
||||
FX_API const fx_property *fx_type_get_property(
|
||||
const fx_type *ty,
|
||||
const char *name);
|
||||
FX_API fx_iterator *fx_type_get_properties(const fx_type *ty);
|
||||
|
||||
FX_API const fx_type *fx_type_get_by_id(fx_type_id id);
|
||||
FX_API const fx_type *fx_type_get_by_name(const char *name);
|
||||
|
||||
FX_API fx_type_id fx_type_get_id(const fx_type *ty);
|
||||
FX_API const fx_type *fx_type_get_parent(const fx_type *ty);
|
||||
FX_API void *fx_type_get_interface(const fx_type *ty, fx_type_id interface_id);
|
||||
|
||||
FX_API fx_type *__fx_type_create(struct fx_type_info *opaque);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
#include <fx/macros.h>
|
||||
#include <fx/reflection/property.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/value-type.h>
|
||||
#include <fx/value.h>
|
||||
#include <platform/callvm.h>
|
||||
|
||||
struct fx_property_p {
|
||||
fx_property *prop_self;
|
||||
char *prop_name;
|
||||
fx_property_getter prop_getter;
|
||||
fx_property_setter prop_setter;
|
||||
};
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
static const char *property_get_name(const struct fx_property_p *prop)
|
||||
{
|
||||
return prop->prop_name;
|
||||
}
|
||||
|
||||
static fx_status property_get_value(
|
||||
const struct fx_property_p *prop,
|
||||
const fx_value *container,
|
||||
fx_value *out)
|
||||
{
|
||||
if (!prop->prop_getter) {
|
||||
return FX_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return prop->prop_getter(container, prop->prop_self, out);
|
||||
}
|
||||
|
||||
static fx_status property_set_value(
|
||||
const struct fx_property_p *prop,
|
||||
fx_value *container,
|
||||
fx_value *new_value)
|
||||
{
|
||||
if (!prop->prop_setter) {
|
||||
return FX_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return prop->prop_setter(container, prop->prop_self, new_value);
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
fx_property *fx_property_create(
|
||||
const char *name,
|
||||
fx_property_getter getter,
|
||||
fx_property_setter setter)
|
||||
{
|
||||
fx_property *prop = fx_object_create(FX_REFLECTION_TYPE_PROPERTY);
|
||||
if (!prop) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_property_p *p = fx_object_get_private(
|
||||
prop,
|
||||
FX_REFLECTION_TYPE_PROPERTY);
|
||||
|
||||
p->prop_name = fx_strdup(name);
|
||||
if (!p->prop_name) {
|
||||
fx_property_unref(prop);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
p->prop_self = prop;
|
||||
p->prop_getter = getter;
|
||||
p->prop_setter = setter;
|
||||
|
||||
return prop;
|
||||
}
|
||||
|
||||
const char *fx_property_get_name(const fx_property *prop)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_PROPERTY,
|
||||
property_get_name,
|
||||
prop);
|
||||
}
|
||||
|
||||
fx_status fx_property_get_value(
|
||||
const fx_property *prop,
|
||||
const fx_value *container,
|
||||
fx_value *out)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_REFLECTION_TYPE_PROPERTY,
|
||||
property_get_value,
|
||||
prop,
|
||||
container,
|
||||
out);
|
||||
}
|
||||
|
||||
fx_status fx_property_set_value(
|
||||
const fx_property *prop,
|
||||
fx_value *container,
|
||||
fx_value *new_value)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_REFLECTION_TYPE_PROPERTY,
|
||||
property_set_value,
|
||||
prop,
|
||||
container,
|
||||
new_value);
|
||||
}
|
||||
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void property_init(fx_object *obj, void *priv)
|
||||
{
|
||||
}
|
||||
|
||||
static void property_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
struct fx_property_p *p = priv;
|
||||
if (p->prop_name) {
|
||||
free(p->prop_name);
|
||||
p->prop_name = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION
|
||||
* *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_property)
|
||||
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_property)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_property)
|
||||
FX_TYPE_ID(0x7007a5ef, 0x3caf, 0x4215, 0x8c0b, 0xd54bf8195624);
|
||||
FX_TYPE_NAME("fx.reflection.property");
|
||||
FX_TYPE_CLASS(fx_property_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_property_p);
|
||||
FX_TYPE_INSTANCE_INIT(property_init);
|
||||
FX_TYPE_INSTANCE_FINI(property_fini);
|
||||
FX_TYPE_DEFINITION_END(fx_property)
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#include <fx/type.h>
|
||||
#include <fx/value-type.h>
|
||||
#include <fx/value.h>
|
||||
#include <platform/callvm.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -77,13 +80,88 @@ void callvm_push(struct callvm *vm, const fx_value *value)
|
||||
return;
|
||||
}
|
||||
|
||||
switch (value->v_type.t_primitive) {
|
||||
case FX_VALUE_TYPE_DOUBLE:
|
||||
if (!fx_type_is_value_type(value->v_type)) {
|
||||
push_int(vm, (uintptr_t)value->v_object);
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned int value_type = __fx_type_get_value_type(value->v_type);
|
||||
|
||||
switch (value_type) {
|
||||
case __FX_VALUE_TYPE_BOOL:
|
||||
push_int(vm, value->v_bool);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_I16:
|
||||
push_int(vm, value->v_i16);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_U16:
|
||||
push_int(vm, value->v_u16);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_I32:
|
||||
push_int(vm, value->v_i32);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_U32:
|
||||
push_int(vm, value->v_u32);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_I64:
|
||||
push_int(vm, value->v_i64);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_U64:
|
||||
push_int(vm, value->v_u64);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_IPTR:
|
||||
push_int(vm, value->v_iptr);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_UPTR:
|
||||
push_int(vm, value->v_uptr);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_SBYTE:
|
||||
push_int(vm, value->v_sbyte);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_BYTE:
|
||||
push_int(vm, value->v_byte);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_SHORT:
|
||||
push_int(vm, value->v_short);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_USHORT:
|
||||
push_int(vm, value->v_ushort);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_INT:
|
||||
push_int(vm, value->v_int);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_UINT:
|
||||
push_int(vm, value->v_uint);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_LONG:
|
||||
push_int(vm, value->v_long);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_ULONG:
|
||||
push_int(vm, value->v_ulong);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_LONGLONG:
|
||||
push_int(vm, value->v_longlong);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_ULONGLONG:
|
||||
push_int(vm, value->v_ulonglong);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_SIZE:
|
||||
push_int(vm, value->v_size);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_FLOAT:
|
||||
push_double(vm, value->v_float);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_DOUBLE:
|
||||
push_double(vm, value->v_double);
|
||||
break;
|
||||
default:
|
||||
case __FX_VALUE_TYPE_CSTR:
|
||||
push_int(vm, (uintptr_t)value->v_cstr);
|
||||
break;
|
||||
case __FX_VALUE_TYPE_POINTER:
|
||||
push_int(vm, (uintptr_t)value->v_pointer);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,17 +172,69 @@ extern void callvm_invoke_v(fx_function_impl impl, struct callvm *vm);
|
||||
fx_value callvm_invoke(
|
||||
struct callvm *vm,
|
||||
fx_function_impl impl,
|
||||
fx_value_type return_type)
|
||||
fx_type_id return_type)
|
||||
{
|
||||
switch (return_type) {
|
||||
case FX_VALUE_TYPE_NONE:
|
||||
callvm_invoke_v(impl, vm);
|
||||
break;
|
||||
case FX_VALUE_TYPE_DOUBLE:
|
||||
return FX_VALUE_DOUBLE(callvm_invoke_d(impl, vm));
|
||||
default:
|
||||
return FX_VALUE_INT(callvm_invoke_i(impl, vm));
|
||||
if (!fx_type_is_value_type(return_type)) {
|
||||
uintptr_t v = callvm_invoke_i(impl, vm);
|
||||
fx_value result = {
|
||||
.v_type = return_type,
|
||||
.v_object = (fx_object *)v,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
unsigned int value_type = __fx_type_get_value_type(return_type);
|
||||
|
||||
switch (value_type) {
|
||||
case __FX_VALUE_TYPE_BOOL:
|
||||
return FX_BOOL(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_I16:
|
||||
return FX_I16(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_U16:
|
||||
return FX_U16(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_I32:
|
||||
return FX_I32(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_U32:
|
||||
return FX_U32(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_I64:
|
||||
return FX_I64(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_U64:
|
||||
return FX_U64(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_IPTR:
|
||||
return FX_IPTR(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_UPTR:
|
||||
return FX_UPTR(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_SBYTE:
|
||||
return FX_SBYTE(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_BYTE:
|
||||
return FX_BYTE(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_SHORT:
|
||||
return FX_SHORT(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_USHORT:
|
||||
return FX_USHORT(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_INT:
|
||||
return FX_INT(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_UINT:
|
||||
return FX_UINT(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_LONG:
|
||||
return FX_LONG(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_ULONG:
|
||||
return FX_ULONG(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_LONGLONG:
|
||||
return FX_LONGLONG(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_ULONGLONG:
|
||||
return FX_ULONGLONG(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_SIZE:
|
||||
return FX_SIZE(callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_FLOAT:
|
||||
return FX_FLOAT(callvm_invoke_d(impl, vm));
|
||||
case __FX_VALUE_TYPE_DOUBLE:
|
||||
return FX_DOUBLE(callvm_invoke_d(impl, vm));
|
||||
case __FX_VALUE_TYPE_CSTR:
|
||||
return FX_CSTR((const char *)callvm_invoke_i(impl, vm));
|
||||
case __FX_VALUE_TYPE_POINTER:
|
||||
return FX_POINTER((void *)callvm_invoke_i(impl, vm));
|
||||
default:
|
||||
return FX_VALUE_EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,11 @@
|
||||
#include <fx/value.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define CALLVM_INVOKE_PROTOTYPE(suffix) \
|
||||
fx_value callvm_invoke_##suffix( \
|
||||
struct callvm *vm, \
|
||||
fx_function_impl impl)
|
||||
|
||||
#define MAX_FIXED_ARGS ((unsigned int)-1)
|
||||
#define MAX_DOUBLE_ARGS 8
|
||||
#define MAX_INT_ARGS 8
|
||||
@@ -26,11 +31,13 @@ struct callvm {
|
||||
uintptr_t *vm_arg_excess;
|
||||
};
|
||||
|
||||
typedef fx_value (*callvm_invoke_impl)(struct callvm *, fx_function_impl);
|
||||
|
||||
extern void callvm_reset(struct callvm *vm, unsigned int max_fixed_args);
|
||||
extern void callvm_push(struct callvm *vm, const fx_value *value);
|
||||
extern fx_value callvm_invoke(
|
||||
struct callvm *vm,
|
||||
fx_function_impl impl,
|
||||
fx_value_type return_type);
|
||||
fx_type_id return_type);
|
||||
|
||||
#endif
|
||||
|
||||
+318
-3
@@ -1,11 +1,42 @@
|
||||
#include "../fx/type.h"
|
||||
|
||||
#include <fx/macros.h>
|
||||
#include <fx/reflection/type.h>
|
||||
#include <fx/type.h>
|
||||
|
||||
struct fx_type_registration;
|
||||
|
||||
#define FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR \
|
||||
(fx_type_function_iterator_get_type())
|
||||
#define FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR \
|
||||
(fx_type_property_iterator_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(fx_type_function_iterator);
|
||||
FX_DECLARE_TYPE(fx_type_property_iterator);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_type_function_iterator)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_type_function_iterator)
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_type_property_iterator)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_type_property_iterator)
|
||||
|
||||
FX_API fx_type_id fx_type_function_iterator_get_type();
|
||||
FX_API fx_type_id fx_type_property_iterator_get_type();
|
||||
|
||||
struct fx_type_p {
|
||||
struct fx_type_info *ty_info;
|
||||
fx_assembly *ty_assembly;
|
||||
};
|
||||
|
||||
struct fx_type_function_iterator_p {
|
||||
struct fx_namemap *it_src;
|
||||
struct fx_namemap_entry *it_cur;
|
||||
};
|
||||
|
||||
struct fx_type_property_iterator_p {
|
||||
const fx_type_info *it_cur_type;
|
||||
struct fx_namemap *it_src;
|
||||
struct fx_namemap_entry *it_cur;
|
||||
};
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
@@ -16,11 +47,27 @@ extern fx_function *fx_type_info_get_function_by_name(
|
||||
const struct fx_type_info *ty,
|
||||
const char *name);
|
||||
|
||||
static const fx_assembly *type_get_assembly(const struct fx_type_p *ty)
|
||||
{
|
||||
return ty->ty_assembly;
|
||||
}
|
||||
|
||||
static fx_status type_set_assembly(struct fx_type_p *ty, fx_assembly *assembly)
|
||||
{
|
||||
ty->ty_assembly = assembly;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static const char *type_get_name(const struct fx_type_p *ty)
|
||||
{
|
||||
return ty->ty_info->ty_name;
|
||||
}
|
||||
|
||||
static fx_type_flags type_get_flags(const struct fx_type_p *ty)
|
||||
{
|
||||
return ty->ty_info->ty_flags;
|
||||
}
|
||||
|
||||
const fx_function *type_get_function(
|
||||
const struct fx_type_p *ty,
|
||||
const char *name)
|
||||
@@ -28,6 +75,98 @@ const fx_function *type_get_function(
|
||||
return fx_type_info_get_function_by_name(ty->ty_info, name);
|
||||
}
|
||||
|
||||
fx_iterator *type_get_functions(const struct fx_type_p *ty)
|
||||
{
|
||||
fx_type_function_iterator *it_obj = fx_object_create(
|
||||
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
|
||||
struct fx_type_function_iterator_p *it = fx_object_get_private(
|
||||
it_obj,
|
||||
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
|
||||
|
||||
it->it_src = &ty->ty_info->ty_functions;
|
||||
it->it_cur = fx_namemap_first(it->it_src);
|
||||
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
const fx_property *type_get_property(
|
||||
const struct fx_type_p *ty,
|
||||
const char *name)
|
||||
{
|
||||
struct fx_type_info *cur = ty->ty_info;
|
||||
while (cur) {
|
||||
const fx_property *prop = fx_type_info_get_property_by_name(
|
||||
cur,
|
||||
name);
|
||||
if (prop) {
|
||||
return prop;
|
||||
}
|
||||
|
||||
if (fx_type_id_compare(&cur->ty_id, FX_TYPE_OBJECT) == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
cur = fx_type_info_get_by_id(&cur->ty_parent_id);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static fx_iterator *type_get_properties(const struct fx_type_p *ty)
|
||||
{
|
||||
fx_type_property_iterator *it_obj = fx_object_create(
|
||||
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
|
||||
struct fx_type_property_iterator_p *it = fx_object_get_private(
|
||||
it_obj,
|
||||
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
|
||||
|
||||
it->it_cur_type = ty->ty_info;
|
||||
it->it_src = &ty->ty_info->ty_properties;
|
||||
it->it_cur = fx_namemap_first(it->it_src);
|
||||
while (!it->it_cur) {
|
||||
if (fx_type_id_compare(&it->it_cur_type->ty_id, FX_TYPE_OBJECT)
|
||||
== 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
const struct fx_type_info *next_type = fx_type_info_get_by_id(
|
||||
&it->it_cur_type->ty_parent_id);
|
||||
if (!next_type) {
|
||||
break;
|
||||
}
|
||||
|
||||
it->it_cur_type = next_type;
|
||||
it->it_cur = fx_namemap_first(&it->it_cur_type->ty_properties);
|
||||
}
|
||||
|
||||
if (!it->it_cur) {
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
}
|
||||
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
static fx_type_id type_get_id(const struct fx_type_p *ty)
|
||||
{
|
||||
return &ty->ty_info->ty_id;
|
||||
}
|
||||
|
||||
static const fx_type *type_get_parent(const struct fx_type_p *ty)
|
||||
{
|
||||
if (fx_type_id_compare(&ty->ty_info->ty_id, FX_TYPE_OBJECT) == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return fx_type_get_by_id(&ty->ty_info->ty_parent_id);
|
||||
}
|
||||
|
||||
static void *type_get_interface(
|
||||
const struct fx_type_p *ty,
|
||||
fx_type_id interface_id)
|
||||
{
|
||||
return fx_class_get_interface(ty->ty_info->ty_class, interface_id);
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
fx_type *__fx_type_create(struct fx_type_info *type_info)
|
||||
@@ -45,11 +184,33 @@ fx_type *__fx_type_create(struct fx_type_info *type_info)
|
||||
return out;
|
||||
}
|
||||
|
||||
const fx_assembly *fx_type_get_assembly(const fx_type *ty)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_TYPE,
|
||||
type_get_assembly,
|
||||
ty);
|
||||
}
|
||||
|
||||
fx_status fx_type_set_assembly(fx_type *ty, fx_assembly *assembly)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_REFLECTION_TYPE_TYPE,
|
||||
type_set_assembly,
|
||||
ty,
|
||||
assembly);
|
||||
}
|
||||
|
||||
const char *fx_type_get_name(const fx_type *ty)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_REFLECTION_TYPE_TYPE, type_get_name, ty);
|
||||
}
|
||||
|
||||
fx_type_flags fx_type_get_flags(const fx_type *ty)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_REFLECTION_TYPE_TYPE, type_get_flags, ty);
|
||||
}
|
||||
|
||||
const fx_function *fx_type_get_function(const fx_type *ty, const char *name)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
@@ -59,6 +220,31 @@ const fx_function *fx_type_get_function(const fx_type *ty, const char *name)
|
||||
name);
|
||||
}
|
||||
|
||||
fx_iterator *fx_type_get_functions(const fx_type *ty)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_TYPE,
|
||||
type_get_functions,
|
||||
ty);
|
||||
}
|
||||
|
||||
const fx_property *fx_type_get_property(const fx_type *ty, const char *name)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_REFLECTION_TYPE_TYPE,
|
||||
type_get_property,
|
||||
ty,
|
||||
name);
|
||||
}
|
||||
|
||||
fx_iterator *fx_type_get_properties(const fx_type *ty)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_TYPE,
|
||||
type_get_properties,
|
||||
ty);
|
||||
}
|
||||
|
||||
const fx_type *fx_type_get_by_id(fx_type_id id)
|
||||
{
|
||||
struct fx_type_info *ty = fx_type_info_get_by_id(id);
|
||||
@@ -71,6 +257,28 @@ const fx_type *fx_type_get_by_name(const char *name)
|
||||
return ty ? ty->ty_metatype : NULL;
|
||||
}
|
||||
|
||||
fx_type_id fx_type_get_id(const fx_type *ty)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_REFLECTION_TYPE_TYPE, type_get_id, ty);
|
||||
}
|
||||
|
||||
const fx_type *fx_type_get_parent(const fx_type *ty)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_TYPE,
|
||||
type_get_parent,
|
||||
ty);
|
||||
}
|
||||
|
||||
void *fx_type_get_interface(const fx_type *ty, fx_type_id interface_id)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_REFLECTION_TYPE_TYPE,
|
||||
type_get_interface,
|
||||
ty,
|
||||
interface_id);
|
||||
}
|
||||
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void type_init(fx_object *obj, void *priv)
|
||||
@@ -81,12 +289,87 @@ static void type_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
}
|
||||
|
||||
/*** ITERATOR FUNCTIONS *******************************************************/
|
||||
|
||||
static enum fx_status function_iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_type_function_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
|
||||
if (!it->it_cur) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
it->it_cur = fx_namemap_next(it->it_src, it->it_cur);
|
||||
return it->it_cur ? FX_SUCCESS : FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
static fx_value function_iterator_get_value(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_type_function_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
|
||||
if (!it->it_cur) {
|
||||
return FX_VALUE_EMPTY;
|
||||
}
|
||||
|
||||
struct fx_type_function *func = fx_unbox(
|
||||
struct fx_type_function,
|
||||
it->it_cur,
|
||||
f_entry);
|
||||
|
||||
return FX_VALUE_OBJECT(func->f_func);
|
||||
}
|
||||
|
||||
static enum fx_status property_iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_type_property_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
|
||||
if (!it->it_cur) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
it->it_cur = fx_namemap_next(it->it_src, it->it_cur);
|
||||
if (!it->it_cur) {
|
||||
if (fx_type_id_compare(&it->it_cur_type->ty_id, FX_TYPE_OBJECT)
|
||||
== 0) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
const struct fx_type_info *next_type = fx_type_info_get_by_id(
|
||||
&it->it_cur_type->ty_parent_id);
|
||||
if (!next_type) {
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
it->it_cur_type = next_type;
|
||||
it->it_cur = fx_namemap_first(&it->it_cur_type->ty_properties);
|
||||
}
|
||||
|
||||
return it->it_cur ? FX_SUCCESS : FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
static fx_value property_iterator_get_value(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_type_property_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
|
||||
if (!it->it_cur) {
|
||||
return FX_VALUE_EMPTY;
|
||||
}
|
||||
|
||||
struct fx_type_property *prop = fx_unbox(
|
||||
struct fx_type_property,
|
||||
it->it_cur,
|
||||
p_entry);
|
||||
|
||||
return FX_VALUE_OBJECT(prop->p_prop);
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_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_type)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_type)
|
||||
@@ -97,3 +380,35 @@ FX_TYPE_DEFINITION_BEGIN(fx_type)
|
||||
FX_TYPE_INSTANCE_INIT(type_init);
|
||||
FX_TYPE_INSTANCE_FINI(type_fini);
|
||||
FX_TYPE_DEFINITION_END(fx_type)
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_type_function_iterator)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = function_iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_erase) = NULL;
|
||||
FX_INTERFACE_ENTRY(it_get_value) = function_iterator_get_value;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_TYPE_CLASS_END(fx_type_function_iterator)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_type_function_iterator)
|
||||
FX_TYPE_ID(0xb29012d5, 0x4d6c, 0x4e3c, 0x9fe5, 0x7d1a8a9e1075);
|
||||
FX_TYPE_NAME("fx.reflection.type.function_iterator");
|
||||
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
|
||||
FX_TYPE_CLASS(fx_type_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_type_function_iterator_p);
|
||||
FX_TYPE_DEFINITION_END(fx_type_function_iterator)
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_type_property_iterator)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = property_iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_erase) = NULL;
|
||||
FX_INTERFACE_ENTRY(it_get_value) = property_iterator_get_value;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_TYPE_CLASS_END(fx_type_property_iterator)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_type_property_iterator)
|
||||
FX_TYPE_ID(0x91fcbdd4, 0x7c54, 0x48c2, 0xa5a4, 0x186f76efd84c);
|
||||
FX_TYPE_NAME("fx.reflection.type.property_iterator");
|
||||
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
|
||||
FX_TYPE_CLASS(fx_type_property_iterator_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_type_property_iterator_p);
|
||||
FX_TYPE_DEFINITION_END(fx_type_property_iterator)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include <fx/collections/array.h>
|
||||
#include <fx/collections/dict.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/serial/ctx.h>
|
||||
#include <fx/serial/toml.h>
|
||||
@@ -9,6 +8,7 @@
|
||||
|
||||
int main(void)
|
||||
{
|
||||
#if 0
|
||||
fx_serial_ctx *ctx = fx_toml_serial_ctx_create();
|
||||
|
||||
fx_dict *dict = fx_dict_create();
|
||||
@@ -33,6 +33,7 @@ int main(void)
|
||||
|
||||
fx_dict_unref(dict);
|
||||
fx_serial_ctx_unref(ctx);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#include <fx/bool.h>
|
||||
#include <fx/collections/array.h>
|
||||
#include <fx/collections/datetime.h>
|
||||
#include <fx/collections/dict.h>
|
||||
#include <fx/double.h>
|
||||
#include <fx/float.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/serial/ctx.h>
|
||||
#include <fx/serial/toml.h>
|
||||
@@ -16,8 +15,10 @@ void write_raw_string(const fx_string *data)
|
||||
fx_stream_write_cstr(fx_stdout, "\"", NULL);
|
||||
|
||||
const fx_iterator *it = fx_iterator_cbegin(data);
|
||||
fx_foreach_c(fx_wchar, c, it)
|
||||
fx_foreach(val, it)
|
||||
{
|
||||
fx_wchar c;
|
||||
fx_value_get_wchar(&val, &c);
|
||||
if (c >= 0x10000) {
|
||||
c -= 0x10000;
|
||||
long hi = 0xD800 | ((c >> 10) & 0x3FF);
|
||||
@@ -55,6 +56,7 @@ void write_tagged_string(fx_string *data)
|
||||
|
||||
void write_tagged_integer(fx_int *data)
|
||||
{
|
||||
#if 0
|
||||
fx_stream_write_cstr(
|
||||
fx_stdout,
|
||||
"{ \"type\": \"integer\", \"value\": \"",
|
||||
@@ -78,10 +80,12 @@ void write_tagged_integer(fx_int *data)
|
||||
}
|
||||
|
||||
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
void write_tagged_float(fx_double *data)
|
||||
{
|
||||
#if 0
|
||||
fx_stream_write_cstr(
|
||||
fx_stdout,
|
||||
"{ \"type\": \"float\", \"value\": \"",
|
||||
@@ -106,16 +110,19 @@ void write_tagged_float(fx_double *data)
|
||||
}
|
||||
|
||||
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
void write_tagged_bool(fx_bool *data)
|
||||
{
|
||||
#if 0
|
||||
fx_stream_write_fmt(
|
||||
fx_stdout,
|
||||
NULL,
|
||||
"{ \"type\": \"bool\", \"value\": \"%s\" }",
|
||||
fx_bool_get_value(data) ? "true" : "false",
|
||||
NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
void write_tagged_datetime(fx_datetime *data)
|
||||
@@ -154,6 +161,7 @@ void write_tagged_datetime(fx_datetime *data)
|
||||
fx_string_unref(new_data);
|
||||
}
|
||||
|
||||
#if 0
|
||||
void write_tagged_dict(fx_dict *data)
|
||||
{
|
||||
fx_stream_write_cstr(fx_stdout, "{ ", NULL);
|
||||
@@ -175,6 +183,7 @@ void write_tagged_dict(fx_dict *data)
|
||||
|
||||
fx_stream_write_cstr(fx_stdout, " }", NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
void write_tagged_array(fx_array *data)
|
||||
{
|
||||
@@ -182,8 +191,10 @@ void write_tagged_array(fx_array *data)
|
||||
|
||||
int i = 0;
|
||||
fx_iterator *it = fx_iterator_begin(data);
|
||||
fx_foreach(fx_object *, obj, it)
|
||||
fx_foreach(val, it)
|
||||
{
|
||||
fx_object *obj;
|
||||
fx_value_get_object(&val, &obj);
|
||||
if (i++ > 0) {
|
||||
fx_stream_write_cstr(fx_stdout, ", ", NULL);
|
||||
}
|
||||
@@ -197,20 +208,19 @@ void write_tagged_array(fx_array *data)
|
||||
|
||||
void write_tagged_value(fx_object *data)
|
||||
{
|
||||
#if 0
|
||||
if (fx_object_is_type(data, FX_TYPE_DICT)) {
|
||||
write_tagged_dict(data);
|
||||
} else if (fx_object_is_type(data, FX_TYPE_ARRAY)) {
|
||||
} else
|
||||
#endif
|
||||
if (fx_object_is_type(data, FX_TYPE_ARRAY)) {
|
||||
write_tagged_array(data);
|
||||
} else if (fx_object_is_type(data, FX_TYPE_STRING)) {
|
||||
write_tagged_string(data);
|
||||
} else if (fx_object_is_type(data, FX_TYPE_DATETIME)) {
|
||||
write_tagged_datetime(data);
|
||||
} else if (fx_object_is_type(data, FX_TYPE_INT)) {
|
||||
write_tagged_integer(data);
|
||||
} else if (fx_object_is_type(data, FX_TYPE_BOOL)) {
|
||||
write_tagged_bool(data);
|
||||
} else if (fx_object_is_type(data, FX_TYPE_DOUBLE)) {
|
||||
write_tagged_float(data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+136
-70
@@ -1,17 +1,16 @@
|
||||
#include <fx/bool.h>
|
||||
#include <fx/collections/array.h>
|
||||
#include <fx/collections/datetime.h>
|
||||
#include <fx/collections/dict.h>
|
||||
#include <fx/collections/hashmap.h>
|
||||
#include <fx/double.h>
|
||||
#include <fx/collections/hashtable.h>
|
||||
#include <fx/error.h>
|
||||
#include <fx/float.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/serial/ctx.h>
|
||||
#include <fx/serial/toml.h>
|
||||
#include <fx/status.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/stringstream.h>
|
||||
#include <fx/uint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -139,8 +138,9 @@ static void ctx_set_object_flags(
|
||||
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
|
||||
};
|
||||
|
||||
const fx_hashmap_value *old_value
|
||||
= fx_hashmap_get(ctx->ctx_objects_flags, &key);
|
||||
const fx_hashmap_value *old_value = fx_hashmap_get(
|
||||
ctx->ctx_objects_flags,
|
||||
&key);
|
||||
|
||||
enum object_flags new_flags = 0;
|
||||
if (old_value) {
|
||||
@@ -172,8 +172,9 @@ static void ctx_clear_object_flags(
|
||||
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
|
||||
};
|
||||
|
||||
const fx_hashmap_value *old_value
|
||||
= fx_hashmap_get(ctx->ctx_objects_flags, &key);
|
||||
const fx_hashmap_value *old_value = fx_hashmap_get(
|
||||
ctx->ctx_objects_flags,
|
||||
&key);
|
||||
|
||||
enum object_flags new_flags = 0;
|
||||
if (old_value) {
|
||||
@@ -202,8 +203,9 @@ static enum object_flags ctx_get_object_flags(struct ctx *ctx, fx_object *obj)
|
||||
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
|
||||
};
|
||||
|
||||
const fx_hashmap_value *value
|
||||
= fx_hashmap_get(ctx->ctx_objects_flags, &key);
|
||||
const fx_hashmap_value *value = fx_hashmap_get(
|
||||
ctx->ctx_objects_flags,
|
||||
&key);
|
||||
if (value) {
|
||||
return (enum object_flags)(uintptr_t)value->value_data;
|
||||
}
|
||||
@@ -924,8 +926,11 @@ static void split_word(struct ctx *ctx, fx_string *wordbuf)
|
||||
FX_STRING_TOK_F_INCLUDE_EMPTY_TOKENS);
|
||||
|
||||
size_t i = 0;
|
||||
fx_foreach_c(const char *, tok, it)
|
||||
fx_foreach(tokv, it)
|
||||
{
|
||||
const char *tok = NULL;
|
||||
fx_value_get_cstr(&tokv, &tok);
|
||||
|
||||
if (i > 0) {
|
||||
enqueue_token(ctx, TOK_DOT);
|
||||
}
|
||||
@@ -1028,8 +1033,10 @@ static void read_word(struct ctx *ctx)
|
||||
}
|
||||
|
||||
fx_iterator *it = fx_iterator_begin(wordbuf);
|
||||
fx_foreach(fx_wchar, c, it)
|
||||
fx_foreach(cv, it)
|
||||
{
|
||||
fx_wchar c = FX_WCHAR_INVALID;
|
||||
fx_value_get_wchar(&cv, &c);
|
||||
/* only allow ASCII numbers/letters here */
|
||||
bool ok = isalnum(c) || c == '_' || c == '-' || c == '.';
|
||||
if (!ok) {
|
||||
@@ -1177,8 +1184,8 @@ static void read_string(struct ctx *ctx, bool squote)
|
||||
}
|
||||
|
||||
if (c != '\n') {
|
||||
ctx->ctx_result
|
||||
= FX_RESULT_ERR(BAD_FORMAT);
|
||||
ctx->ctx_result = FX_RESULT_ERR(
|
||||
BAD_FORMAT);
|
||||
fail = true;
|
||||
break;
|
||||
}
|
||||
@@ -1223,16 +1230,18 @@ static void read_string(struct ctx *ctx, bool squote)
|
||||
case 'U':
|
||||
c = read_unicode_sequence(ctx);
|
||||
if (c == FX_WCHAR_INVALID) {
|
||||
ctx->ctx_result
|
||||
= FX_RESULT_ERR(BAD_FORMAT);
|
||||
ctx->ctx_result = FX_RESULT_ERR(
|
||||
BAD_FORMAT);
|
||||
fail = true;
|
||||
break;
|
||||
}
|
||||
|
||||
ctx->ctx_result
|
||||
= FX_OK(fx_string_append_wc(str, c))
|
||||
ctx->ctx_result = FX_OK(fx_string_append_wc(
|
||||
str,
|
||||
c))
|
||||
? FX_SUCCESS
|
||||
: FX_RESULT_ERR(BAD_FORMAT);
|
||||
: FX_RESULT_ERR(
|
||||
BAD_FORMAT);
|
||||
fail = !FX_OK(ctx->ctx_result);
|
||||
break;
|
||||
default:
|
||||
@@ -1664,7 +1673,7 @@ static void print_token(struct token *tok)
|
||||
}
|
||||
|
||||
static fx_result parse_value(struct ctx *ctx, fx_object **result);
|
||||
static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container);
|
||||
static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container);
|
||||
|
||||
static fx_result parse_timestamp(struct ctx *ctx, fx_object **result)
|
||||
{
|
||||
@@ -1690,6 +1699,7 @@ static fx_result parse_string(struct ctx *ctx, fx_object **result)
|
||||
|
||||
static fx_result parse_int(struct ctx *ctx, fx_object **result)
|
||||
{
|
||||
#if 0
|
||||
struct token *tok = peek_token(ctx);
|
||||
fx_int *val = fx_int_create(tok->tok_value.i.v);
|
||||
if (!val) {
|
||||
@@ -1711,11 +1721,13 @@ static fx_result parse_int(struct ctx *ctx, fx_object **result)
|
||||
}
|
||||
|
||||
*result = (val);
|
||||
#endif
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_result parse_float(struct ctx *ctx, fx_object **result)
|
||||
{
|
||||
#if 0
|
||||
struct token *tok = peek_token(ctx);
|
||||
fx_double *val = fx_double_create(tok->tok_value.f.v);
|
||||
if (!val) {
|
||||
@@ -1737,11 +1749,13 @@ static fx_result parse_float(struct ctx *ctx, fx_object **result)
|
||||
}
|
||||
|
||||
*result = (val);
|
||||
#endif
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_result parse_bool(struct ctx *ctx, fx_object **result)
|
||||
{
|
||||
#if 0
|
||||
struct token *tok = peek_token(ctx);
|
||||
fx_bool *val = fx_bool_create(tok->tok_value.b);
|
||||
if (!val) {
|
||||
@@ -1749,6 +1763,7 @@ static fx_result parse_bool(struct ctx *ctx, fx_object **result)
|
||||
}
|
||||
|
||||
*result = (val);
|
||||
#endif
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -1758,7 +1773,7 @@ static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
|
||||
|
||||
advance_token(ctx);
|
||||
|
||||
fx_dict *table = fx_dict_create();
|
||||
fx_hashtable *table = fx_hashtable_create();
|
||||
if (!table) {
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
@@ -1775,14 +1790,14 @@ static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
|
||||
fx_object *value;
|
||||
fx_result status = parse_key_value_pair(ctx, table);
|
||||
if (!FX_OK(status)) {
|
||||
fx_dict_unref(table);
|
||||
fx_hashtable_unref(table);
|
||||
return status;
|
||||
}
|
||||
|
||||
tok = peek_token(ctx);
|
||||
|
||||
if (!tok) {
|
||||
fx_dict_unref(table);
|
||||
fx_hashtable_unref(table);
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -1794,7 +1809,7 @@ static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
|
||||
advance_token(ctx);
|
||||
break;
|
||||
default:
|
||||
fx_dict_unref(table);
|
||||
fx_hashtable_unref(table);
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
}
|
||||
@@ -1857,7 +1872,9 @@ static fx_result parse_array_inline(struct ctx *ctx, fx_object **result)
|
||||
return status;
|
||||
}
|
||||
|
||||
#if 0
|
||||
fx_array_append(array, FX_RV(value));
|
||||
#endif
|
||||
ENABLE_EXTENDED_LEXING(ctx);
|
||||
|
||||
advance_token(ctx);
|
||||
@@ -1912,7 +1929,7 @@ static fx_result parse_value(struct ctx *ctx, fx_object **result)
|
||||
}
|
||||
}
|
||||
|
||||
static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
|
||||
static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
|
||||
{
|
||||
struct token *tok = peek_token(ctx);
|
||||
if (!IS_VALID_KEY_COMPONENT(tok)) {
|
||||
@@ -1931,19 +1948,26 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
|
||||
}
|
||||
|
||||
while (tok && tok->tok_type == TOK_DOT) {
|
||||
fx_object *sub_dict = fx_dict_at_sk(container, key);
|
||||
if (!sub_dict) {
|
||||
sub_dict = (fx_dict_create());
|
||||
fx_dict_put_sk(container, key, FX_RV(sub_dict));
|
||||
const fx_value *child = fx_hashtable_get(
|
||||
container,
|
||||
&FX_VALUE_OBJECT(key));
|
||||
fx_hashmap *subtable = NULL;
|
||||
fx_value_get_object(child, &subtable);
|
||||
if (!subtable) {
|
||||
subtable = (fx_hashtable_create());
|
||||
fx_hashtable_put(
|
||||
container,
|
||||
&FX_VALUE_OBJECT(key),
|
||||
&FX_VALUE_OBJECT(subtable));
|
||||
} else if (
|
||||
sub_dict
|
||||
&& !fx_object_is_type(sub_dict, FX_TYPE_DICT)) {
|
||||
subtable
|
||||
&& !fx_object_is_type(subtable, FX_TYPE_HASHTABLE)) {
|
||||
free(key);
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
#if 1
|
||||
enum object_flags flags = ctx_get_object_flags(ctx, sub_dict);
|
||||
enum object_flags flags = ctx_get_object_flags(ctx, subtable);
|
||||
if (flags
|
||||
& (OBJECT_KV_END_DEFINED | OBJECT_HEADER_END_DEFINED)) {
|
||||
free(key);
|
||||
@@ -1951,7 +1975,7 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
|
||||
}
|
||||
#endif
|
||||
|
||||
ctx_set_object_flags(ctx, sub_dict, OBJECT_KV_MID_DEFINED);
|
||||
ctx_set_object_flags(ctx, subtable, OBJECT_KV_MID_DEFINED);
|
||||
|
||||
advance_token(ctx);
|
||||
tok = peek_token(ctx);
|
||||
@@ -1960,7 +1984,7 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
container = sub_dict;
|
||||
container = subtable;
|
||||
fx_string_unref(key);
|
||||
key = fx_string_duplicate(tok->tok_str);
|
||||
if (!key) {
|
||||
@@ -1971,7 +1995,7 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
|
||||
tok = peek_token(ctx);
|
||||
}
|
||||
|
||||
if (fx_dict_has_skey(container, key)) {
|
||||
if (fx_hashtable_get(container, &FX_VALUE_OBJECT(key))) {
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
@@ -2001,9 +2025,13 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
|
||||
return fx_result_propagate(result);
|
||||
}
|
||||
|
||||
fx_dict_put_sk(container, key, FX_RV(value));
|
||||
fx_hashtable_put(
|
||||
container,
|
||||
&FX_VALUE_OBJECT(key),
|
||||
&FX_VALUE_OBJECT(value));
|
||||
fx_object_unref(value);
|
||||
|
||||
if (fx_object_is_type(value, FX_TYPE_DICT)
|
||||
if (fx_object_is_type(value, FX_TYPE_HASHTABLE)
|
||||
|| fx_object_is_type(value, FX_TYPE_ARRAY)) {
|
||||
ctx_set_object_flags(ctx, value, OBJECT_KV_END_DEFINED);
|
||||
}
|
||||
@@ -2013,8 +2041,8 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
|
||||
|
||||
static fx_result parse_table_header(
|
||||
struct ctx *ctx,
|
||||
fx_dict *container,
|
||||
fx_dict **new_container)
|
||||
fx_hashtable *container,
|
||||
fx_hashtable **new_container)
|
||||
{
|
||||
advance_token(ctx);
|
||||
struct token *tok = peek_token(ctx);
|
||||
@@ -2034,17 +2062,26 @@ static fx_result parse_table_header(
|
||||
}
|
||||
|
||||
while (tok && tok->tok_type == TOK_DOT) {
|
||||
fx_object *sub_dict = fx_dict_at_sk(container, key);
|
||||
enum object_flags flags = ctx_get_object_flags(ctx, sub_dict);
|
||||
if (!sub_dict) {
|
||||
sub_dict = (fx_dict_create());
|
||||
fx_dict_put_sk(container, key, FX_RV(sub_dict));
|
||||
} else if (fx_object_is_type(sub_dict, FX_TYPE_ARRAY)) {
|
||||
|
||||
sub_dict = fx_array_at(
|
||||
sub_dict,
|
||||
fx_array_size(sub_dict) - 1);
|
||||
} else if (!fx_object_is_type(sub_dict, FX_TYPE_DICT)) {
|
||||
const fx_value *value = fx_hashtable_get(
|
||||
container,
|
||||
&FX_VALUE_OBJECT(key));
|
||||
fx_object *subtable = NULL;
|
||||
fx_value_get_object(value, &subtable);
|
||||
enum object_flags flags = ctx_get_object_flags(ctx, subtable);
|
||||
if (!subtable) {
|
||||
subtable = (fx_hashtable_create());
|
||||
fx_hashtable_put(
|
||||
container,
|
||||
&FX_VALUE_OBJECT(key),
|
||||
&FX_VALUE_OBJECT(subtable));
|
||||
fx_object_unref(subtable);
|
||||
} else if (fx_object_is_type(subtable, FX_TYPE_ARRAY)) {
|
||||
#if 0
|
||||
subtable = fx_array_at(
|
||||
subtable,
|
||||
fx_array_size(subtable) - 1);
|
||||
#endif
|
||||
} else if (!fx_object_is_type(subtable, FX_TYPE_HASHTABLE)) {
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
@@ -2058,9 +2095,9 @@ static fx_result parse_table_header(
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
ctx_set_object_flags(ctx, sub_dict, OBJECT_HEADER_MID_DEFINED);
|
||||
ctx_set_object_flags(ctx, subtable, OBJECT_HEADER_MID_DEFINED);
|
||||
|
||||
container = sub_dict;
|
||||
container = subtable;
|
||||
fx_string_unref(key);
|
||||
key = fx_string_duplicate(tok->tok_str);
|
||||
if (!key) {
|
||||
@@ -2075,20 +2112,28 @@ static fx_result parse_table_header(
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
fx_dict *new_table = fx_dict_at_sk(container, key);
|
||||
const fx_value *child = fx_hashtable_get(
|
||||
container,
|
||||
&FX_VALUE_OBJECT(key));
|
||||
fx_hashtable *new_table = NULL;
|
||||
fx_value_get_object(child, &new_table);
|
||||
|
||||
if (!new_table) {
|
||||
new_table = fx_dict_create();
|
||||
new_table = fx_hashtable_create();
|
||||
|
||||
if (!new_table) {
|
||||
free(key);
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
fx_dict_put_sk(container, key, FX_RV(new_table));
|
||||
fx_hashtable_put(
|
||||
container,
|
||||
&FX_VALUE_OBJECT(key),
|
||||
&FX_VALUE_OBJECT(new_table));
|
||||
fx_hashtable_unref(new_table);
|
||||
}
|
||||
|
||||
if (!fx_object_is_type((new_table), FX_TYPE_DICT)) {
|
||||
if (!fx_object_is_type((new_table), FX_TYPE_HASHTABLE)) {
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
@@ -2109,8 +2154,8 @@ static fx_result parse_table_header(
|
||||
|
||||
static fx_result parse_array_header(
|
||||
struct ctx *ctx,
|
||||
fx_dict *container,
|
||||
fx_dict **new_container)
|
||||
fx_hashtable *container,
|
||||
fx_hashtable **new_container)
|
||||
{
|
||||
advance_token(ctx);
|
||||
struct token *tok = peek_token(ctx);
|
||||
@@ -2130,15 +2175,26 @@ static fx_result parse_array_header(
|
||||
}
|
||||
|
||||
while (tok && tok->tok_type == TOK_DOT) {
|
||||
fx_object *sub_dict = fx_dict_at_sk(container, key);
|
||||
const fx_value *child = fx_hashtable_get(
|
||||
container,
|
||||
&FX_VALUE_OBJECT(key));
|
||||
fx_object *sub_dict = NULL;
|
||||
fx_value_get_object(child, &sub_dict);
|
||||
|
||||
if (!sub_dict) {
|
||||
sub_dict = (fx_dict_create());
|
||||
fx_dict_put_sk(container, key, FX_RV(sub_dict));
|
||||
sub_dict = (fx_hashtable_create());
|
||||
fx_hashtable_put(
|
||||
container,
|
||||
&FX_VALUE_OBJECT(key),
|
||||
&FX_VALUE_OBJECT(sub_dict));
|
||||
fx_hashtable_unref(sub_dict);
|
||||
} else if (fx_object_is_type(sub_dict, FX_TYPE_ARRAY)) {
|
||||
#if 0
|
||||
sub_dict = fx_array_at(
|
||||
sub_dict,
|
||||
fx_array_size(sub_dict) - 1);
|
||||
} else if (!fx_object_is_type(sub_dict, FX_TYPE_DICT)) {
|
||||
#endif
|
||||
} else if (!fx_object_is_type(sub_dict, FX_TYPE_HASHTABLE)) {
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
@@ -2163,10 +2219,18 @@ static fx_result parse_array_header(
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
|
||||
fx_array *array = fx_dict_get_sk(container, key);
|
||||
const fx_value *child = fx_hashtable_get(
|
||||
container,
|
||||
&FX_VALUE_OBJECT(key));
|
||||
fx_array *array = NULL;
|
||||
fx_value_get_object(child, &array);
|
||||
|
||||
if (!array) {
|
||||
array = fx_array_create();
|
||||
fx_dict_put_sk(container, key, FX_RV(array));
|
||||
fx_hashtable_put(
|
||||
container,
|
||||
&FX_VALUE_OBJECT(key),
|
||||
&FX_VALUE_OBJECT(array));
|
||||
} else if (!fx_object_is_type(array, FX_TYPE_ARRAY)) {
|
||||
return FX_RESULT_ERR(BAD_FORMAT);
|
||||
}
|
||||
@@ -2177,23 +2241,25 @@ static fx_result parse_array_header(
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
fx_dict *new_table = fx_dict_create();
|
||||
fx_hashtable *new_table = fx_hashtable_create();
|
||||
if (!new_table) {
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
#if 0
|
||||
fx_array_append(array, FX_RV(new_table));
|
||||
#endif
|
||||
|
||||
advance_token(ctx);
|
||||
*new_container = new_table;
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_result parse_root(struct ctx *ctx, fx_dict **out)
|
||||
static fx_result parse_root(struct ctx *ctx, fx_hashtable **out)
|
||||
{
|
||||
fx_result result = FX_RESULT_SUCCESS;
|
||||
fx_dict *root = fx_dict_create();
|
||||
fx_dict *current = root;
|
||||
fx_hashtable *root = fx_hashtable_create();
|
||||
fx_hashtable *current = root;
|
||||
|
||||
while (!(ctx->ctx_flags & CTX_EOF) && fx_result_is_success(result)) {
|
||||
struct token *tok = peek_token(ctx);
|
||||
@@ -2253,7 +2319,7 @@ static fx_result parse_root(struct ctx *ctx, fx_dict **out)
|
||||
}
|
||||
|
||||
if (fx_result_is_error(result)) {
|
||||
fx_dict_unref(root);
|
||||
fx_hashtable_unref(root);
|
||||
root = NULL;
|
||||
}
|
||||
|
||||
@@ -2285,11 +2351,11 @@ static fx_result toml_deserialise(
|
||||
}
|
||||
|
||||
if (ctx.ctx_flags & CTX_EOF) {
|
||||
*dest = (fx_dict_create());
|
||||
*dest = (fx_hashtable_create());
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
fx_dict *data = NULL;
|
||||
fx_hashtable *data = NULL;
|
||||
ctx.ctx_result = parse_root(&ctx, &data);
|
||||
if (fx_result_is_error(ctx.ctx_result)) {
|
||||
return fx_result_propagate(ctx.ctx_result);
|
||||
|
||||
+3
-7
@@ -102,8 +102,8 @@ static print_function format_printers[] = {
|
||||
[FX_PRINT_WARN] = print_warn,
|
||||
[FX_PRINT_ERR] = print_err,
|
||||
};
|
||||
static size_t nr_format_printers
|
||||
= sizeof format_printers / sizeof format_printers[FX_PRINT_NORMAL];
|
||||
static size_t nr_format_printers = sizeof format_printers
|
||||
/ sizeof format_printers[FX_PRINT_NORMAL];
|
||||
|
||||
fx_status fx_print(fx_print_format format, const char *str, ...)
|
||||
{
|
||||
@@ -140,11 +140,7 @@ int fx_printf(const char *format, ...)
|
||||
{
|
||||
va_list arg;
|
||||
va_start(arg, format);
|
||||
int x = fx_tty_vprintf(
|
||||
fx_stdtty,
|
||||
FX_TTY_DISABLE_INTERPOLATED_FORMATTING,
|
||||
format,
|
||||
arg);
|
||||
int x = fx_tty_vprintf(fx_stdtty, 0, format, arg);
|
||||
va_end(arg);
|
||||
return x;
|
||||
}
|
||||
|
||||
+14
-947
@@ -1,975 +1,42 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// \author (c) Marco Paland (info@paland.com)
|
||||
// 2014-2019, PALANDesign Hannover, Germany
|
||||
//
|
||||
// \license The MIT License (MIT)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
// \brief Tiny printf, sprintf and (v)snprintf implementation, optimized for speed on
|
||||
// embedded systems with a very limited resources. These routines are thread
|
||||
// safe and reentrant!
|
||||
// Use this instead of the bloated standard/newlib printf cause these use
|
||||
// malloc for printf (and may not be thread safe).
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "printf.h"
|
||||
|
||||
#include <fx/term/tty.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H ...) to include the
|
||||
// printf_config.h header file
|
||||
// default: undefined
|
||||
#ifdef PRINTF_INCLUDE_CONFIG_H
|
||||
#include "printf_config.h"
|
||||
#endif
|
||||
|
||||
// 'ntoa' conversion buffer size, this must be big enough to hold one converted
|
||||
// numeric number including padded zeros (dynamically created on stack)
|
||||
// default: 32 byte
|
||||
#ifndef PRINTF_NTOA_BUFFER_SIZE
|
||||
#define PRINTF_NTOA_BUFFER_SIZE 32U
|
||||
#endif
|
||||
|
||||
// 'ftoa' conversion buffer size, this must be big enough to hold one converted
|
||||
// float number including padded zeros (dynamically created on stack)
|
||||
// default: 32 byte
|
||||
#ifndef PRINTF_FTOA_BUFFER_SIZE
|
||||
#define PRINTF_FTOA_BUFFER_SIZE 32U
|
||||
#endif
|
||||
|
||||
// support for the floating point type (%f)
|
||||
// default: activated
|
||||
#ifndef PRINTF_DISABLE_SUPPORT_FLOAT
|
||||
#define PRINTF_SUPPORT_FLOAT
|
||||
#endif
|
||||
|
||||
// support for exponential floating point notation (%e/%g)
|
||||
// default: activated
|
||||
#ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
|
||||
#define PRINTF_SUPPORT_EXPONENTIAL
|
||||
#endif
|
||||
|
||||
// define the default floating point precision
|
||||
// default: 6 digits
|
||||
#ifndef PRINTF_DEFAULT_FLOAT_PRECISION
|
||||
#define PRINTF_DEFAULT_FLOAT_PRECISION 6U
|
||||
#endif
|
||||
|
||||
// define the largest float suitable to print with %f
|
||||
// default: 1e9
|
||||
#ifndef PRINTF_MAX_FLOAT
|
||||
#define PRINTF_MAX_FLOAT 1e9
|
||||
#endif
|
||||
|
||||
// support for the long long types (%llu or %p)
|
||||
// default: activated
|
||||
#ifndef PRINTF_DISABLE_SUPPORT_LONG_LONG
|
||||
#define PRINTF_SUPPORT_LONG_LONG
|
||||
#endif
|
||||
|
||||
// support for the ptrdiff_t type (%t)
|
||||
// ptrdiff_t is normally defined in <stddef.h> as long or long long type
|
||||
// default: activated
|
||||
#ifndef PRINTF_DISABLE_SUPPORT_PTRDIFF_T
|
||||
#define PRINTF_SUPPORT_PTRDIFF_T
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// internal flag definitions
|
||||
#define FLAGS_ZEROPAD (1U << 0U)
|
||||
#define FLAGS_LEFT (1U << 1U)
|
||||
#define FLAGS_PLUS (1U << 2U)
|
||||
#define FLAGS_SPACE (1U << 3U)
|
||||
#define FLAGS_HASH (1U << 4U)
|
||||
#define FLAGS_UPPERCASE (1U << 5U)
|
||||
#define FLAGS_CHAR (1U << 6U)
|
||||
#define FLAGS_SHORT (1U << 7U)
|
||||
#define FLAGS_LONG (1U << 8U)
|
||||
#define FLAGS_LONG_LONG (1U << 9U)
|
||||
#define FLAGS_PRECISION (1U << 10U)
|
||||
#define FLAGS_ADAPT_EXP (1U << 11U)
|
||||
|
||||
// import float.h for DBL_MAX
|
||||
#if defined(PRINTF_SUPPORT_FLOAT)
|
||||
#include <float.h>
|
||||
#endif
|
||||
|
||||
struct out_tty_args {
|
||||
struct fx_tty *tty;
|
||||
enum fx_tty_print_flags flags;
|
||||
bool format_ch;
|
||||
};
|
||||
|
||||
// output function type
|
||||
typedef void (*out_fct_type)(char character, struct out_tty_args *args);
|
||||
|
||||
// wrapper (used as buffer) for output function type
|
||||
typedef struct {
|
||||
void (*fct)(char character, void *arg);
|
||||
void *arg;
|
||||
} out_fct_wrap_type;
|
||||
|
||||
// internal buffer output
|
||||
static inline void _out_buffer(
|
||||
char character, void *buffer, size_t idx, size_t maxlen)
|
||||
{
|
||||
if (idx < maxlen) {
|
||||
((char *)buffer)[idx] = character;
|
||||
}
|
||||
}
|
||||
|
||||
// internal null output
|
||||
static inline void _out_null(char character, void *buffer, size_t idx, size_t maxlen)
|
||||
{
|
||||
(void)character;
|
||||
(void)buffer;
|
||||
(void)idx;
|
||||
(void)maxlen;
|
||||
}
|
||||
|
||||
// internal output function wrapper
|
||||
static inline void _out_fct(char character, void *buffer, size_t idx, size_t maxlen)
|
||||
{
|
||||
(void)idx;
|
||||
(void)maxlen;
|
||||
if (character) {
|
||||
// buffer is the output fct pointer
|
||||
((out_fct_wrap_type *)buffer)
|
||||
->fct(character, ((out_fct_wrap_type *)buffer)->arg);
|
||||
}
|
||||
}
|
||||
|
||||
// internal secure strlen
|
||||
// \return The length of the string (excluding the terminating 0) limited by 'maxsize'
|
||||
static inline unsigned int _strnlen_s(const char *str, size_t maxsize)
|
||||
{
|
||||
const char *s;
|
||||
for (s = str; *s && maxsize--; ++s)
|
||||
;
|
||||
return (unsigned int)(s - str);
|
||||
}
|
||||
|
||||
// internal test if char is a digit (0-9)
|
||||
// \return true if char is a digit
|
||||
static inline bool _is_digit(char ch)
|
||||
{
|
||||
return (ch >= '0') && (ch <= '9');
|
||||
}
|
||||
|
||||
// internal ASCII string to unsigned int conversion
|
||||
static unsigned int _atoi(const char **str)
|
||||
{
|
||||
unsigned int i = 0U;
|
||||
while (_is_digit(**str)) {
|
||||
i = i * 10U + (unsigned int)(*((*str)++) - '0');
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
// output the specified string in reverse, taking care of any zero-padding
|
||||
static size_t _out_rev(
|
||||
out_fct_type out, struct out_tty_args *args, const char *buf,
|
||||
size_t len, unsigned int width, unsigned int flags)
|
||||
{
|
||||
size_t idx = 0;
|
||||
|
||||
// pad spaces up to given width
|
||||
if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) {
|
||||
for (size_t i = len; i < width; i++) {
|
||||
out(' ', args);
|
||||
}
|
||||
}
|
||||
|
||||
// reverse string
|
||||
while (len) {
|
||||
out(buf[--len], args);
|
||||
}
|
||||
|
||||
// append pad spaces up to given width
|
||||
if (flags & FLAGS_LEFT) {
|
||||
while (idx < width) {
|
||||
out(' ', args);
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
|
||||
return idx;
|
||||
}
|
||||
|
||||
// internal itoa format
|
||||
static size_t _ntoa_format(
|
||||
out_fct_type out, struct out_tty_args *args, char *buf, size_t len,
|
||||
bool negative, unsigned int base, unsigned int prec, unsigned int width,
|
||||
unsigned int flags)
|
||||
{
|
||||
// pad leading zeros
|
||||
if (!(flags & FLAGS_LEFT)) {
|
||||
if (width && (flags & FLAGS_ZEROPAD)
|
||||
&& (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
|
||||
width--;
|
||||
}
|
||||
while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
while ((flags & FLAGS_ZEROPAD) && (len < width)
|
||||
&& (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
}
|
||||
|
||||
// handle hash
|
||||
if (flags & FLAGS_HASH) {
|
||||
if (!(flags & FLAGS_PRECISION) && len
|
||||
&& ((len == prec) || (len == width))) {
|
||||
len--;
|
||||
if (len && (base == 16U)) {
|
||||
len--;
|
||||
}
|
||||
}
|
||||
if ((base == 16U) && !(flags & FLAGS_UPPERCASE)
|
||||
&& (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = 'x';
|
||||
} else if (
|
||||
(base == 16U) && (flags & FLAGS_UPPERCASE)
|
||||
&& (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = 'X';
|
||||
} else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = 'b';
|
||||
}
|
||||
if (len < PRINTF_NTOA_BUFFER_SIZE) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
}
|
||||
|
||||
if (len < PRINTF_NTOA_BUFFER_SIZE) {
|
||||
if (negative) {
|
||||
buf[len++] = '-';
|
||||
} else if (flags & FLAGS_PLUS) {
|
||||
buf[len++] = '+'; // ignore the space if the '+' exists
|
||||
} else if (flags & FLAGS_SPACE) {
|
||||
buf[len++] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
return _out_rev(out, args, buf, len, width, flags);
|
||||
}
|
||||
|
||||
// internal itoa for 'long' type
|
||||
static size_t _ntoa_long(
|
||||
out_fct_type out, struct out_tty_args *args, unsigned long value,
|
||||
bool negative, unsigned long base, unsigned int prec,
|
||||
unsigned int width, unsigned int flags)
|
||||
{
|
||||
char buf[PRINTF_NTOA_BUFFER_SIZE];
|
||||
size_t len = 0U;
|
||||
|
||||
// no hash for 0 values
|
||||
if (!value) {
|
||||
flags &= ~FLAGS_HASH;
|
||||
}
|
||||
|
||||
// write if precision != 0 and value is != 0
|
||||
if (!(flags & FLAGS_PRECISION) || value) {
|
||||
do {
|
||||
const char digit = (char)(value % base);
|
||||
buf[len++] = digit < 10
|
||||
? '0' + digit
|
||||
: (flags & FLAGS_UPPERCASE ? 'A' : 'a')
|
||||
+ digit - 10;
|
||||
value /= base;
|
||||
} while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
|
||||
}
|
||||
|
||||
return _ntoa_format(
|
||||
out, args, buf, len, negative, (unsigned int)base, prec, width,
|
||||
flags);
|
||||
}
|
||||
|
||||
// internal itoa for 'long long' type
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
static size_t _ntoa_long_long(
|
||||
out_fct_type out, struct out_tty_args *args, unsigned long long value,
|
||||
bool negative, unsigned long long base, unsigned int prec,
|
||||
unsigned int width, unsigned int flags)
|
||||
{
|
||||
char buf[PRINTF_NTOA_BUFFER_SIZE];
|
||||
size_t len = 0U;
|
||||
|
||||
// no hash for 0 values
|
||||
if (!value) {
|
||||
flags &= ~FLAGS_HASH;
|
||||
}
|
||||
|
||||
// write if precision != 0 and value is != 0
|
||||
if (!(flags & FLAGS_PRECISION) || value) {
|
||||
do {
|
||||
const char digit = (char)(value % base);
|
||||
buf[len++] = digit < 10
|
||||
? '0' + digit
|
||||
: (flags & FLAGS_UPPERCASE ? 'A' : 'a')
|
||||
+ digit - 10;
|
||||
value /= base;
|
||||
} while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
|
||||
}
|
||||
|
||||
return _ntoa_format(
|
||||
out, args, buf, len, negative, (unsigned int)base, prec, width,
|
||||
flags);
|
||||
}
|
||||
#endif // PRINTF_SUPPORT_LONG_LONG
|
||||
|
||||
#if defined(PRINTF_SUPPORT_FLOAT)
|
||||
|
||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||
// forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT
|
||||
static size_t _etoa(
|
||||
out_fct_type out, struct out_tty_args *args, double value,
|
||||
unsigned int prec, unsigned int width, unsigned int flags);
|
||||
#endif
|
||||
|
||||
// internal ftoa for fixed decimal floating point
|
||||
static size_t _ftoa(
|
||||
out_fct_type out, struct out_tty_args *args, double value,
|
||||
unsigned int prec, unsigned int width, unsigned int flags)
|
||||
{
|
||||
char buf[PRINTF_FTOA_BUFFER_SIZE];
|
||||
size_t len = 0U;
|
||||
double diff = 0.0;
|
||||
|
||||
// powers of 10
|
||||
static const double pow10[]
|
||||
= {1, 10, 100, 1000, 10000,
|
||||
100000, 1000000, 10000000, 100000000, 1000000000};
|
||||
|
||||
// test for special values
|
||||
if (value != value)
|
||||
return _out_rev(out, args, "nan", 3, width, flags);
|
||||
if (value < -DBL_MAX)
|
||||
return _out_rev(out, args, "fni-", 4, width, flags);
|
||||
if (value > DBL_MAX)
|
||||
return _out_rev(
|
||||
out, args, (flags & FLAGS_PLUS) ? "fni+" : "fni",
|
||||
(flags & FLAGS_PLUS) ? 4U : 3U, width, flags);
|
||||
|
||||
// test for very large values
|
||||
// standard printf behavior is to print EVERY whole number digit --
|
||||
// which could be 100s of characters overflowing your buffers == bad
|
||||
if ((value > PRINTF_MAX_FLOAT) || (value < -PRINTF_MAX_FLOAT)) {
|
||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||
return _etoa(out, args, value, prec, width, flags);
|
||||
#else
|
||||
return 0U;
|
||||
#endif
|
||||
}
|
||||
|
||||
// test for negative
|
||||
bool negative = false;
|
||||
if (value < 0) {
|
||||
negative = true;
|
||||
value = 0 - value;
|
||||
}
|
||||
|
||||
// set default precision, if not set explicitly
|
||||
if (!(flags & FLAGS_PRECISION)) {
|
||||
prec = PRINTF_DEFAULT_FLOAT_PRECISION;
|
||||
}
|
||||
// limit precision to 9, cause a prec >= 10 can lead to overflow errors
|
||||
while ((len < PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) {
|
||||
buf[len++] = '0';
|
||||
prec--;
|
||||
}
|
||||
|
||||
int whole = (int)value;
|
||||
double tmp = (value - whole) * pow10[prec];
|
||||
unsigned long frac = (unsigned long)tmp;
|
||||
diff = tmp - frac;
|
||||
|
||||
if (diff > 0.5) {
|
||||
++frac;
|
||||
// handle rollover, e.g. case 0.99 with prec 1 is 1.0
|
||||
if (frac >= pow10[prec]) {
|
||||
frac = 0;
|
||||
++whole;
|
||||
}
|
||||
} else if (diff < 0.5) {
|
||||
} else if ((frac == 0U) || (frac & 1U)) {
|
||||
// if halfway, round up if odd OR if last digit is 0
|
||||
++frac;
|
||||
}
|
||||
|
||||
if (prec == 0U) {
|
||||
diff = value - (double)whole;
|
||||
if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) {
|
||||
// exactly 0.5 and ODD, then round up
|
||||
// 1.5 -> 2, but 2.5 -> 2
|
||||
++whole;
|
||||
}
|
||||
} else {
|
||||
unsigned int count = prec;
|
||||
// now do fractional part, as an unsigned number
|
||||
while (len < PRINTF_FTOA_BUFFER_SIZE) {
|
||||
--count;
|
||||
buf[len++] = (char)(48U + (frac % 10U));
|
||||
if (!(frac /= 10U)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// add extra 0s
|
||||
while ((len < PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
if (len < PRINTF_FTOA_BUFFER_SIZE) {
|
||||
// add decimal
|
||||
buf[len++] = '.';
|
||||
}
|
||||
}
|
||||
|
||||
// do whole part, number is reversed
|
||||
while (len < PRINTF_FTOA_BUFFER_SIZE) {
|
||||
buf[len++] = (char)(48 + (whole % 10));
|
||||
if (!(whole /= 10)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// pad leading zeros
|
||||
if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) {
|
||||
if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
|
||||
width--;
|
||||
}
|
||||
while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) {
|
||||
buf[len++] = '0';
|
||||
}
|
||||
}
|
||||
|
||||
if (len < PRINTF_FTOA_BUFFER_SIZE) {
|
||||
if (negative) {
|
||||
buf[len++] = '-';
|
||||
} else if (flags & FLAGS_PLUS) {
|
||||
buf[len++] = '+'; // ignore the space if the '+' exists
|
||||
} else if (flags & FLAGS_SPACE) {
|
||||
buf[len++] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
return _out_rev(out, args, buf, len, width, flags);
|
||||
}
|
||||
|
||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||
// internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse <m.jasperse@gmail.com>
|
||||
static size_t _etoa(
|
||||
out_fct_type out, struct out_tty_args *args, double value,
|
||||
unsigned int prec, unsigned int width, unsigned int flags)
|
||||
{
|
||||
// check for NaN and special values
|
||||
if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) {
|
||||
return _ftoa(out, args, value, prec, width, flags);
|
||||
}
|
||||
|
||||
// determine the sign
|
||||
const bool negative = value < 0;
|
||||
if (negative) {
|
||||
value = -value;
|
||||
}
|
||||
|
||||
// default precision
|
||||
if (!(flags & FLAGS_PRECISION)) {
|
||||
prec = PRINTF_DEFAULT_FLOAT_PRECISION;
|
||||
}
|
||||
|
||||
// determine the decimal exponent
|
||||
// based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c)
|
||||
union {
|
||||
uint64_t U;
|
||||
double F;
|
||||
} conv;
|
||||
|
||||
conv.F = value;
|
||||
int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023; // effectively log2
|
||||
conv.U = (conv.U & ((1ULL << 52U) - 1U))
|
||||
| (1023ULL << 52U); // drop the exponent so conv.F is now in [1,2)
|
||||
// now approximate log10 from the log2 integer part and an expansion of ln around 1.5
|
||||
int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981
|
||||
+ (conv.F - 1.5) * 0.289529654602168);
|
||||
// now we want to compute 10^expval but we want to be sure it won't overflow
|
||||
exp2 = (int)(expval * 3.321928094887362 + 0.5);
|
||||
const double z = expval * 2.302585092994046 - exp2 * 0.6931471805599453;
|
||||
const double z2 = z * z;
|
||||
conv.U = (uint64_t)(exp2 + 1023) << 52U;
|
||||
// compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex
|
||||
conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14)))));
|
||||
// correct for rounding errors
|
||||
if (value < conv.F) {
|
||||
expval--;
|
||||
conv.F /= 10;
|
||||
}
|
||||
|
||||
// the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters
|
||||
unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U;
|
||||
|
||||
// in "%g" mode, "prec" is the number of *significant figures* not decimals
|
||||
if (flags & FLAGS_ADAPT_EXP) {
|
||||
// do we want to fall-back to "%f" mode?
|
||||
if ((value >= 1e-4) && (value < 1e6)) {
|
||||
if ((int)prec > expval) {
|
||||
prec = (unsigned)((int)prec - expval - 1);
|
||||
} else {
|
||||
prec = 0;
|
||||
}
|
||||
flags |= FLAGS_PRECISION; // make sure _ftoa respects precision
|
||||
// no characters in exponent
|
||||
minwidth = 0U;
|
||||
expval = 0;
|
||||
} else {
|
||||
// we use one sigfig for the whole part
|
||||
if ((prec > 0) && (flags & FLAGS_PRECISION)) {
|
||||
--prec;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// will everything fit?
|
||||
unsigned int fwidth = width;
|
||||
if (width > minwidth) {
|
||||
// we didn't fall-back so subtract the characters required for the exponent
|
||||
fwidth -= minwidth;
|
||||
} else {
|
||||
// not enough characters, so go back to default sizing
|
||||
fwidth = 0U;
|
||||
}
|
||||
if ((flags & FLAGS_LEFT) && minwidth) {
|
||||
// if we're padding on the right, DON'T pad the floating part
|
||||
fwidth = 0U;
|
||||
}
|
||||
|
||||
// rescale the float value
|
||||
if (expval) {
|
||||
value /= conv.F;
|
||||
}
|
||||
|
||||
// output the floating part
|
||||
size_t idx
|
||||
= _ftoa(out, args, negative ? -value : value, prec, fwidth,
|
||||
flags & ~FLAGS_ADAPT_EXP);
|
||||
|
||||
// output the exponent part
|
||||
if (minwidth) {
|
||||
// output the exponential symbol
|
||||
out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', args);
|
||||
// output the exponent value
|
||||
idx = _ntoa_long(
|
||||
out, args, (expval < 0) ? -expval : expval, expval < 0,
|
||||
10, 0, minwidth - 1, FLAGS_ZEROPAD | FLAGS_PLUS);
|
||||
// might need to right-pad spaces
|
||||
if (flags & FLAGS_LEFT) {
|
||||
while (idx < width) {
|
||||
out(' ', args);
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return idx;
|
||||
}
|
||||
#endif // PRINTF_SUPPORT_EXPONENTIAL
|
||||
#endif // PRINTF_SUPPORT_FLOAT
|
||||
|
||||
#define set_format_ch(args) ((args)->format_ch = true)
|
||||
#define unset_format_ch(args) ((args)->format_ch = false)
|
||||
|
||||
// internal vsnprintf
|
||||
static int _vsnprintf(
|
||||
out_fct_type out, struct out_tty_args *args, const char *format, va_list va)
|
||||
{
|
||||
unsigned int flags, width, precision, n;
|
||||
size_t idx = 0U;
|
||||
|
||||
while (*format) {
|
||||
// format specifier? %[flags][width][.precision][length]
|
||||
if (*format != '%') {
|
||||
// no
|
||||
set_format_ch(args);
|
||||
out(*format, args);
|
||||
unset_format_ch(args);
|
||||
format++;
|
||||
continue;
|
||||
} else {
|
||||
// yes, evaluate it
|
||||
format++;
|
||||
}
|
||||
|
||||
// evaluate flags
|
||||
flags = 0U;
|
||||
do {
|
||||
switch (*format) {
|
||||
case '0':
|
||||
flags |= FLAGS_ZEROPAD;
|
||||
format++;
|
||||
n = 1U;
|
||||
break;
|
||||
case '-':
|
||||
flags |= FLAGS_LEFT;
|
||||
format++;
|
||||
n = 1U;
|
||||
break;
|
||||
case '+':
|
||||
flags |= FLAGS_PLUS;
|
||||
format++;
|
||||
n = 1U;
|
||||
break;
|
||||
case ' ':
|
||||
flags |= FLAGS_SPACE;
|
||||
format++;
|
||||
n = 1U;
|
||||
break;
|
||||
case '#':
|
||||
flags |= FLAGS_HASH;
|
||||
format++;
|
||||
n = 1U;
|
||||
break;
|
||||
default:
|
||||
n = 0U;
|
||||
break;
|
||||
}
|
||||
} while (n);
|
||||
|
||||
// evaluate width field
|
||||
width = 0U;
|
||||
if (_is_digit(*format)) {
|
||||
width = _atoi(&format);
|
||||
} else if (*format == '*') {
|
||||
const int w = va_arg(va, int);
|
||||
if (w < 0) {
|
||||
flags |= FLAGS_LEFT; // reverse padding
|
||||
width = (unsigned int)-w;
|
||||
} else {
|
||||
width = (unsigned int)w;
|
||||
}
|
||||
format++;
|
||||
}
|
||||
|
||||
// evaluate precision field
|
||||
precision = 0U;
|
||||
if (*format == '.') {
|
||||
flags |= FLAGS_PRECISION;
|
||||
format++;
|
||||
if (_is_digit(*format)) {
|
||||
precision = _atoi(&format);
|
||||
} else if (*format == '*') {
|
||||
const int prec = (int)va_arg(va, int);
|
||||
precision = prec > 0 ? (unsigned int)prec : 0U;
|
||||
format++;
|
||||
}
|
||||
}
|
||||
|
||||
// evaluate length field
|
||||
switch (*format) {
|
||||
case 'l':
|
||||
flags |= FLAGS_LONG;
|
||||
format++;
|
||||
if (*format == 'l') {
|
||||
flags |= FLAGS_LONG_LONG;
|
||||
format++;
|
||||
}
|
||||
break;
|
||||
case 'h':
|
||||
flags |= FLAGS_SHORT;
|
||||
format++;
|
||||
if (*format == 'h') {
|
||||
flags |= FLAGS_CHAR;
|
||||
format++;
|
||||
}
|
||||
break;
|
||||
#if defined(PRINTF_SUPPORT_PTRDIFF_T)
|
||||
case 't':
|
||||
flags
|
||||
|= (sizeof(ptrdiff_t) == sizeof(long)
|
||||
? FLAGS_LONG
|
||||
: FLAGS_LONG_LONG);
|
||||
format++;
|
||||
break;
|
||||
#endif
|
||||
case 'j':
|
||||
flags
|
||||
|= (sizeof(intmax_t) == sizeof(long)
|
||||
? FLAGS_LONG
|
||||
: FLAGS_LONG_LONG);
|
||||
format++;
|
||||
break;
|
||||
case 'z':
|
||||
flags
|
||||
|= (sizeof(size_t) == sizeof(long)
|
||||
? FLAGS_LONG
|
||||
: FLAGS_LONG_LONG);
|
||||
format++;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// evaluate specifier
|
||||
switch (*format) {
|
||||
case 'd':
|
||||
case 'i':
|
||||
case 'u':
|
||||
case 'x':
|
||||
case 'X':
|
||||
case 'o':
|
||||
case 'b': {
|
||||
// set the base
|
||||
unsigned int base;
|
||||
if (*format == 'x' || *format == 'X') {
|
||||
base = 16U;
|
||||
} else if (*format == 'o') {
|
||||
base = 8U;
|
||||
} else if (*format == 'b') {
|
||||
base = 2U;
|
||||
} else {
|
||||
base = 10U;
|
||||
flags &= ~FLAGS_HASH; // no hash for dec format
|
||||
}
|
||||
// uppercase
|
||||
if (*format == 'X') {
|
||||
flags |= FLAGS_UPPERCASE;
|
||||
}
|
||||
|
||||
// no plus or space flag for u, x, X, o, b
|
||||
if ((*format != 'i') && (*format != 'd')) {
|
||||
flags &= ~(FLAGS_PLUS | FLAGS_SPACE);
|
||||
}
|
||||
|
||||
// ignore '0' flag when precision is given
|
||||
if (flags & FLAGS_PRECISION) {
|
||||
flags &= ~FLAGS_ZEROPAD;
|
||||
}
|
||||
|
||||
// convert the integer
|
||||
if ((*format == 'i') || (*format == 'd')) {
|
||||
// signed
|
||||
if (flags & FLAGS_LONG_LONG) {
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
const long long value
|
||||
= va_arg(va, long long);
|
||||
idx = _ntoa_long_long(
|
||||
out, args,
|
||||
(unsigned long long)(value > 0 ? value
|
||||
: 0 - value),
|
||||
value < 0, base, precision,
|
||||
width, flags);
|
||||
#endif
|
||||
} else if (flags & FLAGS_LONG) {
|
||||
const long value = va_arg(va, long);
|
||||
idx = _ntoa_long(
|
||||
out, args,
|
||||
(unsigned long)(value > 0 ? value
|
||||
: 0 - value),
|
||||
value < 0, base, precision,
|
||||
width, flags);
|
||||
} else {
|
||||
const int value
|
||||
= (flags & FLAGS_CHAR)
|
||||
? (char)va_arg(va, int)
|
||||
: (flags & FLAGS_SHORT)
|
||||
? (short int)va_arg(va, int)
|
||||
: va_arg(va, int);
|
||||
idx = _ntoa_long(
|
||||
out, args,
|
||||
(unsigned int)(value > 0 ? value
|
||||
: 0 - value),
|
||||
value < 0, base, precision,
|
||||
width, flags);
|
||||
}
|
||||
} else {
|
||||
// unsigned
|
||||
if (flags & FLAGS_LONG_LONG) {
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
idx = _ntoa_long_long(
|
||||
out, args,
|
||||
va_arg(va, unsigned long long),
|
||||
false, base, precision, width,
|
||||
flags);
|
||||
#endif
|
||||
} else if (flags & FLAGS_LONG) {
|
||||
idx = _ntoa_long(
|
||||
out, args,
|
||||
va_arg(va, unsigned long), false,
|
||||
base, precision, width, flags);
|
||||
} else {
|
||||
const unsigned int value
|
||||
= (flags & FLAGS_CHAR)
|
||||
? (unsigned char)va_arg(
|
||||
va, unsigned int)
|
||||
: (flags & FLAGS_SHORT)
|
||||
? (unsigned short int)va_arg(
|
||||
va, unsigned int)
|
||||
: va_arg(va, unsigned int);
|
||||
idx = _ntoa_long(
|
||||
out, args, value, false, base,
|
||||
precision, width, flags);
|
||||
}
|
||||
}
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
#if defined(PRINTF_SUPPORT_FLOAT)
|
||||
case 'f':
|
||||
case 'F':
|
||||
if (*format == 'F')
|
||||
flags |= FLAGS_UPPERCASE;
|
||||
idx
|
||||
= _ftoa(out, args, va_arg(va, double),
|
||||
precision, width, flags);
|
||||
format++;
|
||||
break;
|
||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||
case 'e':
|
||||
case 'E':
|
||||
case 'g':
|
||||
case 'G':
|
||||
if ((*format == 'g') || (*format == 'G'))
|
||||
flags |= FLAGS_ADAPT_EXP;
|
||||
if ((*format == 'E') || (*format == 'G'))
|
||||
flags |= FLAGS_UPPERCASE;
|
||||
idx
|
||||
= _etoa(out, args, va_arg(va, double),
|
||||
precision, width, flags);
|
||||
format++;
|
||||
break;
|
||||
#endif // PRINTF_SUPPORT_EXPONENTIAL
|
||||
#endif // PRINTF_SUPPORT_FLOAT
|
||||
case 'c': {
|
||||
unsigned int l = 1U;
|
||||
// pre padding
|
||||
if (!(flags & FLAGS_LEFT)) {
|
||||
while (l++ < width) {
|
||||
out(' ', args);
|
||||
}
|
||||
}
|
||||
// char output
|
||||
out((char)va_arg(va, int), args);
|
||||
// post padding
|
||||
if (flags & FLAGS_LEFT) {
|
||||
while (l++ < width) {
|
||||
out(' ', args);
|
||||
}
|
||||
}
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
|
||||
case 's': {
|
||||
const char *p = va_arg(va, char *);
|
||||
unsigned int l = _strnlen_s(
|
||||
p, precision ? precision : (size_t)-1);
|
||||
// pre padding
|
||||
if (flags & FLAGS_PRECISION) {
|
||||
l = (l < precision ? l : precision);
|
||||
}
|
||||
if (!(flags & FLAGS_LEFT)) {
|
||||
while (l++ < width) {
|
||||
out(' ', args);
|
||||
}
|
||||
}
|
||||
// string output
|
||||
while ((*p != 0)
|
||||
&& (!(flags & FLAGS_PRECISION) || precision--)) {
|
||||
out(*(p++), args);
|
||||
}
|
||||
// post padding
|
||||
if (flags & FLAGS_LEFT) {
|
||||
while (l++ < width) {
|
||||
out(' ', args);
|
||||
}
|
||||
}
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
|
||||
case 'p': {
|
||||
width = sizeof(void *) * 2U;
|
||||
flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE;
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
|
||||
if (is_ll) {
|
||||
idx = _ntoa_long_long(
|
||||
out, args, (uintptr_t)va_arg(va, void *),
|
||||
false, 16U, precision, width, flags);
|
||||
} else {
|
||||
#endif
|
||||
idx = _ntoa_long(
|
||||
out, args,
|
||||
(unsigned long)((uintptr_t)va_arg(
|
||||
va, void *)),
|
||||
false, 16U, precision, width, flags);
|
||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||
}
|
||||
#endif
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
|
||||
case '%':
|
||||
set_format_ch(args);
|
||||
out('%', args);
|
||||
unset_format_ch(args);
|
||||
format++;
|
||||
break;
|
||||
|
||||
default:
|
||||
set_format_ch(args);
|
||||
out(*format, args);
|
||||
unset_format_ch(args);
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// termination
|
||||
out((char)0, args);
|
||||
|
||||
// return written chars without terminating \0
|
||||
return (int)idx;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static void out_tty(char c, struct out_tty_args *args)
|
||||
static void out_tty(char c, void *argp)
|
||||
{
|
||||
if (c == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct out_tty_args *args = argp;
|
||||
enum fx_tty_print_flags flags = args->flags;
|
||||
|
||||
if (!args->format_ch && (flags & FX_TTY_DISABLE_INTERPOLATED_FORMATTING)) {
|
||||
if (!args->format_ch
|
||||
&& (flags & FX_TTY_DISABLE_INTERPOLATED_FORMATTING)) {
|
||||
flags |= FX_TTY_DISABLE_FORMATTING;
|
||||
}
|
||||
|
||||
fx_tty_putc(args->tty, flags, c);
|
||||
}
|
||||
|
||||
extern int z__fx_fctprintf(
|
||||
void (*out)(char c, void *extra_arg),
|
||||
void *extra_arg,
|
||||
const char *format,
|
||||
va_list arg);
|
||||
|
||||
int fx_tty_vprintf(
|
||||
struct fx_tty *tty, enum fx_tty_print_flags flags, const char *format,
|
||||
struct fx_tty *tty,
|
||||
enum fx_tty_print_flags flags,
|
||||
const char *format,
|
||||
va_list args)
|
||||
{
|
||||
struct out_tty_args tty_args = {
|
||||
@@ -977,6 +44,6 @@ int fx_tty_vprintf(
|
||||
.tty = tty,
|
||||
};
|
||||
|
||||
const int ret = _vsnprintf(out_tty, &tty_args, format, args);
|
||||
const int ret = z__fx_fctprintf(out_tty, &tty_args, format, args);
|
||||
return ret;
|
||||
}
|
||||
|
||||
+50
-10
@@ -1,5 +1,6 @@
|
||||
#include "../../tty.h"
|
||||
|
||||
#include <fx/encoding.h>
|
||||
#include <fx/term/tty.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -102,9 +103,10 @@ static void init_tty(struct fx_tty *tty, FILE *in, FILE *out)
|
||||
tcgetattr(fd, &tty->t_ios_default);
|
||||
memcpy(&tty->t_ios_raw, &tty->t_ios_default, sizeof tty->t_ios_raw);
|
||||
|
||||
tty->t_ios_raw.c_iflag &= ~(ICRNL | IXON);
|
||||
tty->t_ios_raw.c_iflag &= ~(BRKINT | INPCK | ISTRIP | ICRNL | IXON);
|
||||
tty->t_ios_raw.c_oflag &= ~(OPOST);
|
||||
tty->t_ios_raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
|
||||
tty->t_ios_raw.c_cflag |= (CS8);
|
||||
tty->t_ios_raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
|
||||
|
||||
tty->t_flags |= FX_TTY_INIT;
|
||||
}
|
||||
@@ -206,7 +208,8 @@ void fx_tty_reset_vmode(struct fx_tty *tty)
|
||||
}
|
||||
|
||||
static int compare_colour(
|
||||
const struct fx_tty_colour *a, const struct fx_tty_colour *b)
|
||||
const struct fx_tty_colour *a,
|
||||
const struct fx_tty_colour *b)
|
||||
{
|
||||
if (a->c_mode != b->c_mode) {
|
||||
return -1;
|
||||
@@ -243,7 +246,9 @@ static int compare_colour(
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int compare_vmode(const struct fx_tty_vmode *a, const struct fx_tty_vmode *b)
|
||||
static int compare_vmode(
|
||||
const struct fx_tty_vmode *a,
|
||||
const struct fx_tty_vmode *b)
|
||||
{
|
||||
if (a->v_attrib != b->v_attrib) {
|
||||
return -1;
|
||||
@@ -271,7 +276,9 @@ static void put_ansi_attrib(struct fx_tty *tty, const char *s)
|
||||
}
|
||||
|
||||
static void set_attrib(
|
||||
struct fx_tty *tty, enum fx_tty_attrib old, enum fx_tty_attrib new)
|
||||
struct fx_tty *tty,
|
||||
enum fx_tty_attrib old,
|
||||
enum fx_tty_attrib new)
|
||||
{
|
||||
if (old == new) {
|
||||
return;
|
||||
@@ -309,7 +316,8 @@ static void set_attrib(
|
||||
}
|
||||
|
||||
static void set_fg(
|
||||
struct fx_tty *tty, const struct fx_tty_colour *old,
|
||||
struct fx_tty *tty,
|
||||
const struct fx_tty_colour *old,
|
||||
const struct fx_tty_colour *new)
|
||||
{
|
||||
if (compare_colour(old, new) == 0) {
|
||||
@@ -345,7 +353,8 @@ static void set_fg(
|
||||
}
|
||||
|
||||
static void set_bg(
|
||||
struct fx_tty *tty, const struct fx_tty_colour *old,
|
||||
struct fx_tty *tty,
|
||||
const struct fx_tty_colour *old,
|
||||
const struct fx_tty_colour *new)
|
||||
{
|
||||
if (compare_colour(old, new) == 0) {
|
||||
@@ -399,6 +408,24 @@ void fx_tty_set_vmode(struct fx_tty *tty, const struct fx_tty_vmode *vmode)
|
||||
memcpy(&tty->t_vmode, vmode, sizeof *vmode);
|
||||
}
|
||||
|
||||
static fx_keycode read_utf8(int fd, char header, int len)
|
||||
{
|
||||
char s[4] = {header, 0, 0, 0};
|
||||
|
||||
for (int i = 1; i < len; i++) {
|
||||
char c;
|
||||
int v = read(fd, &c, 1);
|
||||
|
||||
if (v != 1) {
|
||||
return FX_KEY_EOF;
|
||||
}
|
||||
|
||||
s[i] = c;
|
||||
}
|
||||
|
||||
return fx_wchar_utf8_codepoint_decode(s);
|
||||
}
|
||||
|
||||
fx_keycode fx_tty_read_key(struct fx_tty *tty)
|
||||
{
|
||||
char c;
|
||||
@@ -423,6 +450,11 @@ fx_keycode fx_tty_read_key(struct fx_tty *tty)
|
||||
return FX_TTY_CTRL_KEY(c + 'a' - 1);
|
||||
}
|
||||
|
||||
int utf8_len = fx_wchar_utf8_header_decode(c);
|
||||
if (utf8_len > 1) {
|
||||
return read_utf8(fd, c, utf8_len);
|
||||
}
|
||||
|
||||
if (c != 0x1b) {
|
||||
return c;
|
||||
}
|
||||
@@ -458,7 +490,10 @@ fx_keycode fx_tty_read_key(struct fx_tty *tty)
|
||||
return c;
|
||||
}
|
||||
|
||||
void fx_tty_move_cursor_x(struct fx_tty *tty, enum fx_tty_position_base base, int pos)
|
||||
void fx_tty_move_cursor_x(
|
||||
struct fx_tty *tty,
|
||||
enum fx_tty_position_base base,
|
||||
int pos)
|
||||
{
|
||||
if (base == FX_TTY_POS_CURSOR && pos < 0 && pos >= -4) {
|
||||
for (int i = 0; i > pos; i--) {
|
||||
@@ -487,7 +522,10 @@ void fx_tty_move_cursor_x(struct fx_tty *tty, enum fx_tty_position_base base, in
|
||||
}
|
||||
}
|
||||
|
||||
void fx_tty_move_cursor_y(struct fx_tty *tty, enum fx_tty_position_base base, int pos)
|
||||
void fx_tty_move_cursor_y(
|
||||
struct fx_tty *tty,
|
||||
enum fx_tty_position_base base,
|
||||
int pos)
|
||||
{
|
||||
if (base == FX_TTY_POS_START) {
|
||||
/* we don't need this functionality right now */
|
||||
@@ -528,7 +566,9 @@ void fx_tty_clear(struct fx_tty *tty, enum fx_tty_clear_mode mode)
|
||||
}
|
||||
|
||||
enum fx_status fx_tty_get_dimensions(
|
||||
struct fx_tty *tty, unsigned int *w, unsigned int *h)
|
||||
struct fx_tty *tty,
|
||||
unsigned int *w,
|
||||
unsigned int *h)
|
||||
{
|
||||
if (!(tty->t_flags & FX_TTY_INTERACTIVE)) {
|
||||
return -1;
|
||||
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
set(source_dirs hash)
|
||||
set(source_dirs hash int float)
|
||||
export_fx_namespace_details(fx)
|
||||
|
||||
@@ -1,84 +1,86 @@
|
||||
#include "int/convert.h"
|
||||
|
||||
#include <fx/bool.h>
|
||||
#include <fx/stream.h>
|
||||
#include <fx/macros.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
/*** PRIVATE DATA *************************************************************/
|
||||
HASH(bool)
|
||||
|
||||
struct fx_bool_p {
|
||||
bool b_value;
|
||||
};
|
||||
GENERIC_CONVERT(bool, i16, i16)
|
||||
GENERIC_CONVERT(bool, u16, u16)
|
||||
GENERIC_CONVERT(bool, i32, i32)
|
||||
GENERIC_CONVERT(bool, u32, u32)
|
||||
GENERIC_CONVERT(bool, i64, i64)
|
||||
GENERIC_CONVERT(bool, u64, u64)
|
||||
GENERIC_CONVERT(bool, iptr, iptr)
|
||||
GENERIC_CONVERT(bool, uptr, uptr)
|
||||
GENERIC_CONVERT(bool, sbyte, sbyte)
|
||||
GENERIC_CONVERT(bool, byte, byte)
|
||||
GENERIC_CONVERT(bool, short, short)
|
||||
GENERIC_CONVERT(bool, ushort, unsigned short)
|
||||
GENERIC_CONVERT(bool, int, int)
|
||||
GENERIC_CONVERT(bool, uint, unsigned int)
|
||||
GENERIC_CONVERT(bool, long, long)
|
||||
GENERIC_CONVERT(bool, ulong, unsigned long)
|
||||
GENERIC_CONVERT(bool, longlong, long long)
|
||||
GENERIC_CONVERT(bool, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(bool, size, size_t)
|
||||
GENERIC_CONVERT(bool, float, float)
|
||||
GENERIC_CONVERT(bool, double, double)
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
COMPARE(bool, bool)
|
||||
|
||||
static void bool_set_value(struct fx_bool_p *i, bool v)
|
||||
static fx_status to_string(
|
||||
const fx_value *v,
|
||||
fx_stream *out,
|
||||
const char *format)
|
||||
{
|
||||
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);
|
||||
}
|
||||
return fx_stream_write_cstr(out, v->v_bool ? "true" : "false", NULL);
|
||||
}
|
||||
|
||||
/*** 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) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
FX_INTERFACE_ENTRY(c_to_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <fx/comparable.h>
|
||||
#include <fx/macros.h>
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_comparable)
|
||||
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_comparable)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_comparable)
|
||||
FX_TYPE_FLAGS(FX_TYPE_F_ABSTRACT);
|
||||
FX_TYPE_ID(0xb25ab0c6, 0xfddb, 0x447f, 0xa54d, 0x44c4a0ef74a1);
|
||||
FX_TYPE_NAME("fx.comparable");
|
||||
FX_TYPE_CLASS(fx_comparable_class);
|
||||
FX_TYPE_DEFINITION_END(fx_comparable)
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <fx/convertible.h>
|
||||
#include <fx/macros.h>
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_convertible)
|
||||
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_convertible)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_convertible)
|
||||
FX_TYPE_FLAGS(FX_TYPE_F_ABSTRACT);
|
||||
FX_TYPE_ID(0x8443b648, 0x83ea, 0x4a65, 0x85ef, 0x5570b0157d0e);
|
||||
FX_TYPE_NAME("fx.convertible");
|
||||
FX_TYPE_CLASS(fx_convertible_class);
|
||||
FX_TYPE_DEFINITION_END(fx_convertible)
|
||||
@@ -0,0 +1,572 @@
|
||||
#include <ctype.h>
|
||||
#include <fx/comparable.h>
|
||||
#include <fx/convertible.h>
|
||||
#include <fx/cstr.h>
|
||||
#include <fx/hash.h>
|
||||
#include <fx/operable.h>
|
||||
#include <fx/stream.h>
|
||||
#include <fx/value-type.h>
|
||||
#include <fx/value.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
|
||||
static fx_status to_string(
|
||||
const fx_value *v,
|
||||
fx_stream *out,
|
||||
const char *format)
|
||||
{
|
||||
if (!v->v_cstr) {
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
return fx_stream_write_fmt(out, NULL, "%s", v->v_cstr);
|
||||
}
|
||||
|
||||
static i32 compare(const fx_value *left, const fx_value *right)
|
||||
{
|
||||
const char *s;
|
||||
if (!FX_OK(fx_value_get_cstr(right, &s))) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return strcmp(left->v_cstr, s);
|
||||
}
|
||||
|
||||
static fx_status hash(const fx_value *v, uint64_t *out_hash)
|
||||
{
|
||||
*out_hash = fx_hash_cstr(v->v_cstr);
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_char(const char *in, fx_wchar *out)
|
||||
{
|
||||
if (in[0] == '\0' || in[1] == '\0') {
|
||||
*out = in[0];
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
fx_status cstr_to_bool(const char *in, bool *out)
|
||||
{
|
||||
if (!strcmp(in, "1") || !strcmp(in, "true") || strcmp(in, "True")) {
|
||||
*out = true;
|
||||
return FX_SUCCESS;
|
||||
} else if (
|
||||
!strcmp(in, "0") || !strcmp(in, "false")
|
||||
|| strcmp(in, "False")) {
|
||||
*out = false;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
fx_status cstr_to_i16(const char *in, i16 *out)
|
||||
{
|
||||
char *ep;
|
||||
long v = strtol(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v < INT16_MIN || v > INT16_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (i16)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_u16(const char *in, u16 *out)
|
||||
{
|
||||
char *ep;
|
||||
unsigned long v = strtoul(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v > UINT16_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (u16)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_i32(const char *in, i32 *out)
|
||||
{
|
||||
char *ep;
|
||||
long long v = strtoll(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v < INT32_MIN || v > INT32_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (i32)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_u32(const char *in, u32 *out)
|
||||
{
|
||||
char *ep;
|
||||
unsigned long long v = strtoull(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v > UINT32_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (u32)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_i64(const char *in, i64 *out)
|
||||
{
|
||||
char *ep;
|
||||
long long v = strtoll(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v < INT64_MIN || v > INT64_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (i64)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_u64(const char *in, u64 *out)
|
||||
{
|
||||
char *ep;
|
||||
unsigned long long v = strtoull(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v > UINT64_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (u16)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_iptr(const char *in, iptr *out)
|
||||
{
|
||||
char *ep;
|
||||
intptr_t v = strtoimax(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v < INTPTR_MIN || v > INTPTR_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (iptr)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_uptr(const char *in, uptr *out)
|
||||
{
|
||||
char *ep;
|
||||
uintptr_t v = strtoumax(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v > UINTPTR_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (uptr)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_sbyte(const char *in, sbyte *out)
|
||||
{
|
||||
char *ep;
|
||||
long v = strtol(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v > INT8_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (sbyte)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_byte(const char *in, byte *out)
|
||||
{
|
||||
char *ep;
|
||||
unsigned long v = strtoul(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v > UINT8_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (byte)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_short(const char *in, short *out)
|
||||
{
|
||||
char *ep;
|
||||
long v = strtol(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v < SHRT_MIN || v > SHRT_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (short)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_ushort(const char *in, unsigned short *out)
|
||||
{
|
||||
char *ep;
|
||||
unsigned long v = strtoul(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v > USHRT_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (unsigned short)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_int(const char *in, int *out)
|
||||
{
|
||||
char *ep;
|
||||
long v = strtol(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v < INT_MIN || v > INT_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (int)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_uint(const char *in, unsigned int *out)
|
||||
{
|
||||
char *ep;
|
||||
unsigned long v = strtoul(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v > UINT_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (unsigned int)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_long(const char *in, long *out)
|
||||
{
|
||||
char *ep;
|
||||
long long v = strtoll(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v < LONG_MIN || v > LONG_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (long)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_ulong(const char *in, unsigned long *out)
|
||||
{
|
||||
char *ep;
|
||||
unsigned long long v = strtoull(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v > ULONG_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (unsigned long)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_longlong(const char *in, long long *out)
|
||||
{
|
||||
char *ep;
|
||||
long long v = strtoll(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v < LLONG_MIN || v > LLONG_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (long long)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_ulonglong(const char *in, unsigned long long *out)
|
||||
{
|
||||
char *ep;
|
||||
unsigned long long v = strtoull(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v > ULLONG_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (unsigned long long)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_size(const char *in, size_t *out)
|
||||
{
|
||||
char *ep;
|
||||
unsigned long long v = strtoull(in, &ep, 0);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (v > SIZE_T_MAX) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = (size_t)v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_float(const char *in, float *out)
|
||||
{
|
||||
char *ep;
|
||||
float v = strtof(in, &ep);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_double(const char *in, double *out)
|
||||
{
|
||||
char *ep;
|
||||
double v = strtod(in, &ep);
|
||||
if (*ep) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
*out = v;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
fx_status cstr_to_cstr(const char *in, const char **out)
|
||||
{
|
||||
*out = in;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
#define CSTR_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); \
|
||||
}
|
||||
|
||||
CSTR_CONVERTER(bool, bool)
|
||||
CSTR_CONVERTER(i16, i16)
|
||||
CSTR_CONVERTER(u16, u16)
|
||||
CSTR_CONVERTER(i32, i32)
|
||||
CSTR_CONVERTER(u32, u32)
|
||||
CSTR_CONVERTER(i64, i64)
|
||||
CSTR_CONVERTER(u64, u64)
|
||||
CSTR_CONVERTER(iptr, iptr)
|
||||
CSTR_CONVERTER(uptr, uptr)
|
||||
CSTR_CONVERTER(sbyte, sbyte)
|
||||
CSTR_CONVERTER(byte, byte)
|
||||
CSTR_CONVERTER(short, short)
|
||||
CSTR_CONVERTER(ushort, unsigned short)
|
||||
CSTR_CONVERTER(int, int)
|
||||
CSTR_CONVERTER(uint, unsigned int)
|
||||
CSTR_CONVERTER(long, long)
|
||||
CSTR_CONVERTER(ulong, unsigned long)
|
||||
CSTR_CONVERTER(longlong, long long)
|
||||
CSTR_CONVERTER(ulonglong, unsigned long long)
|
||||
CSTR_CONVERTER(size, size_t)
|
||||
CSTR_CONVERTER(float, float)
|
||||
CSTR_CONVERTER(double, double)
|
||||
CSTR_CONVERTER(cstr, const char *)
|
||||
|
||||
static fx_status add(const fx_value *l, const fx_value *r, fx_value *out)
|
||||
{
|
||||
const char *left = l->v_cstr;
|
||||
const char *right = r->v_cstr;
|
||||
fx_string *result = fx_string_create_from_cstr(left);
|
||||
if (!result) {
|
||||
return FX_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
fx_string_append_cstr(result, right);
|
||||
*out = FX_VALUE_OBJECT(result);
|
||||
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_cstr)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_comparable, FX_TYPE_COMPARABLE)
|
||||
FX_INTERFACE_ENTRY(c_compare) = compare;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_comparable, FX_TYPE_COMPARABLE)
|
||||
|
||||
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_CLASS_END(fx_cstr)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_cstr)
|
||||
__FX_VALUE_TYPE_ID(CSTR);
|
||||
FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
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;
|
||||
}
|
||||
|
||||
size_t fx_strcmp_nocase(const char *a, const char *b)
|
||||
{
|
||||
while (1) {
|
||||
char ch_a = tolower(*a);
|
||||
char ch_b = tolower(*b);
|
||||
|
||||
if (!ch_a && !ch_b) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (ch_a != ch_b) {
|
||||
return ch_a - ch_b;
|
||||
}
|
||||
|
||||
a++;
|
||||
b++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef CSTR_H_
|
||||
#define CSTR_H_
|
||||
|
||||
#include <fx/status.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
extern fx_status cstr_to_char(const char *in, fx_wchar *out);
|
||||
|
||||
extern fx_status cstr_to_bool(const char *in, bool *out);
|
||||
extern fx_status cstr_to_i16(const char *in, i16 *out);
|
||||
extern fx_status cstr_to_u16(const char *in, u16 *out);
|
||||
extern fx_status cstr_to_i32(const char *in, i32 *out);
|
||||
extern fx_status cstr_to_u32(const char *in, u32 *out);
|
||||
extern fx_status cstr_to_i64(const char *in, i64 *out);
|
||||
extern fx_status cstr_to_u64(const char *in, u64 *out);
|
||||
extern fx_status cstr_to_iptr(const char *in, iptr *out);
|
||||
extern fx_status cstr_to_uptr(const char *in, uptr *out);
|
||||
|
||||
extern fx_status cstr_to_sbyte(const char *in, sbyte *out);
|
||||
extern fx_status cstr_to_byte(const char *in, byte *out);
|
||||
extern fx_status cstr_to_short(const char *in, short *out);
|
||||
extern fx_status cstr_to_ushort(const char *in, unsigned short *out);
|
||||
extern fx_status cstr_to_int(const char *in, int *out);
|
||||
extern fx_status cstr_to_uint(const char *in, unsigned int *out);
|
||||
extern fx_status cstr_to_long(const char *in, long *out);
|
||||
extern fx_status cstr_to_ulong(const char *in, unsigned long *out);
|
||||
extern fx_status cstr_to_longlong(const char *in, long long *out);
|
||||
extern fx_status cstr_to_ulonglong(const char *in, unsigned long long *out);
|
||||
extern fx_status cstr_to_size(const char *in, size_t *out);
|
||||
|
||||
extern fx_status cstr_to_float(const char *in, float *out);
|
||||
extern fx_status cstr_to_double(const char *in, double *out);
|
||||
extern fx_status cstr_to_cstr(const char *in, const char **out);
|
||||
|
||||
#endif
|
||||
-280
@@ -1,280 +0,0 @@
|
||||
#include <fx/double.h>
|
||||
#include <fx/stream.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/*** 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)
|
||||
@@ -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);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <fx/endian.h>
|
||||
#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
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
#include "../int/convert.h"
|
||||
|
||||
#include <fx/float.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
BOOL_CONVERT(double)
|
||||
GENERIC_CONVERT(double, i16, i16)
|
||||
GENERIC_CONVERT(double, u16, u16)
|
||||
GENERIC_CONVERT(double, i32, i32)
|
||||
GENERIC_CONVERT(double, u32, u32)
|
||||
GENERIC_CONVERT(double, i64, i64)
|
||||
GENERIC_CONVERT(double, u64, u64)
|
||||
GENERIC_CONVERT(double, iptr, iptr)
|
||||
GENERIC_CONVERT(double, uptr, uptr)
|
||||
GENERIC_CONVERT(double, sbyte, sbyte)
|
||||
GENERIC_CONVERT(double, byte, byte)
|
||||
GENERIC_CONVERT(double, short, short)
|
||||
GENERIC_CONVERT(double, ushort, unsigned short)
|
||||
GENERIC_CONVERT(double, int, int)
|
||||
GENERIC_CONVERT(double, uint, unsigned int)
|
||||
GENERIC_CONVERT(double, long, long)
|
||||
GENERIC_CONVERT(double, ulong, unsigned long)
|
||||
GENERIC_CONVERT(double, longlong, long long)
|
||||
GENERIC_CONVERT(double, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(double, size, size_t)
|
||||
GENERIC_CONVERT(double, float, float)
|
||||
|
||||
TO_STRING(double, "%g")
|
||||
|
||||
BINARY_OP(add, +, double)
|
||||
BINARY_OP(subtract, -, double)
|
||||
BINARY_OP(multiply, *, double)
|
||||
BINARY_OP(divide, /, double)
|
||||
PREFIX_OP(increment, ++, double)
|
||||
PREFIX_OP(decrement, --, double)
|
||||
IN_PLACE_OP(add_in_place, +=, double)
|
||||
IN_PLACE_OP(subtract_in_place, -=, double)
|
||||
IN_PLACE_OP(multiply_in_place, *=, double)
|
||||
IN_PLACE_OP(divide_in_place, /=, double)
|
||||
|
||||
COMPARE(double, double)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_double)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_NAME("fx.double");
|
||||
FX_TYPE_CLASS(fx_double_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(double);
|
||||
FX_TYPE_DEFINITION_END(fx_double)
|
||||
@@ -0,0 +1,100 @@
|
||||
#include "../int/convert.h"
|
||||
|
||||
#include <fx/float.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
TO_STRING(float, "%g")
|
||||
|
||||
BOOL_CONVERT(float)
|
||||
GENERIC_CONVERT(float, i16, i16)
|
||||
GENERIC_CONVERT(float, u16, u16)
|
||||
GENERIC_CONVERT(float, i32, i32)
|
||||
GENERIC_CONVERT(float, u32, u32)
|
||||
GENERIC_CONVERT(float, i64, i64)
|
||||
GENERIC_CONVERT(float, u64, u64)
|
||||
GENERIC_CONVERT(float, iptr, iptr)
|
||||
GENERIC_CONVERT(float, uptr, uptr)
|
||||
GENERIC_CONVERT(float, sbyte, sbyte)
|
||||
GENERIC_CONVERT(float, byte, byte)
|
||||
GENERIC_CONVERT(float, short, short)
|
||||
GENERIC_CONVERT(float, ushort, unsigned short)
|
||||
GENERIC_CONVERT(float, int, int)
|
||||
GENERIC_CONVERT(float, uint, unsigned int)
|
||||
GENERIC_CONVERT(float, long, long)
|
||||
GENERIC_CONVERT(float, ulong, unsigned long)
|
||||
GENERIC_CONVERT(float, longlong, long long)
|
||||
GENERIC_CONVERT(float, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(float, size, size_t)
|
||||
GENERIC_CONVERT(float, double, double)
|
||||
|
||||
BINARY_OP(add, +, float)
|
||||
BINARY_OP(subtract, -, float)
|
||||
BINARY_OP(multiply, *, float)
|
||||
BINARY_OP(divide, /, float)
|
||||
PREFIX_OP(increment, ++, float)
|
||||
PREFIX_OP(decrement, --, float)
|
||||
IN_PLACE_OP(add_in_place, +=, float)
|
||||
IN_PLACE_OP(subtract_in_place, -=, float)
|
||||
IN_PLACE_OP(multiply_in_place, *=, float)
|
||||
IN_PLACE_OP(divide_in_place, /=, float)
|
||||
|
||||
COMPARE(float, float)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_float)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_double) = to_double;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_NAME("fx.float");
|
||||
FX_TYPE_CLASS(fx_float_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(float);
|
||||
FX_TYPE_DEFINITION_END(fx_float)
|
||||
+29
-2
@@ -40,8 +40,8 @@ static const struct fx_hash_function_ops *hash_functions[] = {
|
||||
[FX_HASH_SHAKE128] = &z__fx_shake128_ops,
|
||||
[FX_HASH_SHAKE256] = &z__fx_shake256_ops,
|
||||
};
|
||||
static const size_t nr_hash_functions
|
||||
= sizeof hash_functions / sizeof hash_functions[0];
|
||||
static const size_t nr_hash_functions = sizeof hash_functions
|
||||
/ sizeof hash_functions[0];
|
||||
|
||||
uint64_t fx_hash_cstr(const char *s)
|
||||
{
|
||||
@@ -66,6 +66,33 @@ uint64_t fx_hash_cstr_ex(const char *s, size_t *len)
|
||||
return hash;
|
||||
}
|
||||
|
||||
uint64_t fx_hash_wstr(const fx_wchar *s)
|
||||
{
|
||||
uint64_t hash = FNV1_OFFSET_BASIS;
|
||||
size_t i = 0;
|
||||
|
||||
for (i = 0; s[i]; i++) {
|
||||
hash ^= s[i];
|
||||
hash *= FNV1_PRIME;
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
uint64_t fx_hash_buffer(const void *p, size_t len)
|
||||
{
|
||||
const char *s = p;
|
||||
uint64_t hash = FNV1_OFFSET_BASIS;
|
||||
size_t i = 0;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
hash ^= s[i];
|
||||
hash *= FNV1_PRIME;
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
enum fx_status fx_hash_ctx_init(
|
||||
struct fx_hash_ctx *ctx,
|
||||
enum fx_hash_function func)
|
||||
|
||||
+8
-9
@@ -30,18 +30,18 @@ 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 & 15] = rol( \
|
||||
block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] \
|
||||
^ block->l[(i + 2) & 15] ^ block->l[i & 15], \
|
||||
1))
|
||||
|
||||
@@ -235,9 +235,8 @@ 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)]
|
||||
finalcount[i] = (unsigned char)((context->ctx_state.sha1.count[(
|
||||
i >= 4 ? 0 : 1)]
|
||||
>> ((3 - (i & 3)) * 8))
|
||||
& 255); /* Endian independent */
|
||||
}
|
||||
@@ -251,8 +250,8 @@ 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]
|
||||
digest[i] = (unsigned char)((context->ctx_state.sha1
|
||||
.state[i >> 2]
|
||||
>> ((3 - (i & 3)) * 8))
|
||||
& 255);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef FX_CORE_BSTR_H_
|
||||
#define FX_CORE_BSTR_H_
|
||||
|
||||
#include <fx/encoding.h>
|
||||
#include <fx/misc.h>
|
||||
#include <fx/status.h>
|
||||
#include <stdarg.h>
|
||||
@@ -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);
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef FX_COMPARABLE_H_
|
||||
#define FX_COMPARABLE_H_
|
||||
|
||||
#include <fx/int-primitives.h>
|
||||
#include <fx/macros.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
#define FX_TYPE_COMPARABLE (fx_comparable_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(fx_comparable);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_comparable)
|
||||
i32 (*c_compare)(const fx_value *a, const fx_value *b);
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_comparable)
|
||||
|
||||
FX_API fx_type_id fx_comparable_get_type(void);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,59 @@
|
||||
#ifndef FX_CONVERTIBLE_H_
|
||||
#define FX_CONVERTIBLE_H_
|
||||
|
||||
#include <fx/encoding.h>
|
||||
#include <fx/int-primitives.h>
|
||||
#include <fx/macros.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
#define FX_TYPE_CONVERTIBLE (fx_convertible_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(fx_convertible);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_convertible)
|
||||
fx_status (*c_to_char)(const fx_value *in, fx_wchar *out);
|
||||
|
||||
fx_status (*c_to_bool)(const fx_value *in, bool *out);
|
||||
fx_status (*c_to_i16)(const fx_value *in, i16 *out);
|
||||
fx_status (*c_to_u16)(const fx_value *in, u16 *out);
|
||||
fx_status (*c_to_i32)(const fx_value *in, i32 *out);
|
||||
fx_status (*c_to_u32)(const fx_value *in, u32 *out);
|
||||
fx_status (*c_to_i64)(const fx_value *in, i64 *out);
|
||||
fx_status (*c_to_u64)(const fx_value *in, u64 *out);
|
||||
fx_status (*c_to_iptr)(const fx_value *in, iptr *out);
|
||||
fx_status (*c_to_uptr)(const fx_value *in, uptr *out);
|
||||
|
||||
fx_status (*c_to_sbyte)(const fx_value *in, sbyte *out);
|
||||
fx_status (*c_to_byte)(const fx_value *in, byte *out);
|
||||
fx_status (*c_to_short)(const fx_value *in, short *out);
|
||||
fx_status (*c_to_ushort)(const fx_value *in, unsigned short *out);
|
||||
fx_status (*c_to_int)(const fx_value *in, int *out);
|
||||
fx_status (*c_to_uint)(const fx_value *in, unsigned int *out);
|
||||
fx_status (*c_to_long)(const fx_value *in, long *out);
|
||||
fx_status (*c_to_ulong)(const fx_value *in, unsigned long *out);
|
||||
fx_status (*c_to_longlong)(const fx_value *in, long long *out);
|
||||
fx_status (
|
||||
*c_to_ulonglong)(const fx_value *in, unsigned long long *out);
|
||||
fx_status (*c_to_size)(const fx_value *in, size_t *out);
|
||||
|
||||
fx_status (*c_to_float)(const fx_value *in, float *out);
|
||||
fx_status (*c_to_double)(const fx_value *in, double *out);
|
||||
|
||||
fx_status (*c_to_cstr)(const fx_value *in, const char **out);
|
||||
fx_status (*c_to_wchar)(const fx_value *in, fx_wchar *out);
|
||||
fx_status (*c_to_wstr)(const fx_value *in, const fx_wchar **out);
|
||||
|
||||
fx_status (*c_to_object)(
|
||||
const fx_value *in,
|
||||
fx_value *out,
|
||||
fx_type_id convert_to);
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_convertible)
|
||||
|
||||
FX_API fx_type_id fx_convertible_get_type(void);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef FX_CSTR_H_
|
||||
#define FX_CSTR_H_
|
||||
|
||||
#include <fx/encoding.h>
|
||||
#include <fx/macros.h>
|
||||
|
||||
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 size_t fx_strcmp_nocase(const char *a, const char *b);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
@@ -1,40 +0,0 @@
|
||||
#ifndef FX_DOUBLE_H_
|
||||
#define FX_DOUBLE_H_
|
||||
|
||||
#include <fx/macros.h>
|
||||
|
||||
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
|
||||
@@ -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
|
||||
|
||||
+2
-18
@@ -4,6 +4,7 @@
|
||||
#include <fx/misc.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef FX_DOUBLE_H_
|
||||
#define FX_DOUBLE_H_
|
||||
|
||||
#include <fx/macros.h>
|
||||
|
||||
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
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef FX_CORE_HASH_H_
|
||||
#define FX_CORE_HASH_H_
|
||||
|
||||
#include <fx/encoding.h>
|
||||
#include <fx/misc.h>
|
||||
#include <fx/status.h>
|
||||
#include <stddef.h>
|
||||
@@ -99,15 +100,16 @@ typedef struct fx_hash_ctx {
|
||||
} fx_hash_ctx;
|
||||
|
||||
FX_API uint64_t fx_hash_cstr(const char *s);
|
||||
FX_API uint64_t fx_hash_wstr(const fx_wchar *s);
|
||||
FX_API uint64_t fx_hash_cstr_ex(const char *s, size_t *len);
|
||||
FX_API uint64_t fx_hash_buffer(const void *s, size_t len);
|
||||
|
||||
FX_API fx_status fx_hash_ctx_init(fx_hash_ctx *ctx, fx_hash_function func);
|
||||
FX_API fx_status fx_hash_ctx_reset(fx_hash_ctx *ctx);
|
||||
FX_API fx_status
|
||||
fx_hash_ctx_update(fx_hash_ctx *ctx, const void *p, size_t len);
|
||||
FX_API fx_status fx_hash_ctx_update_rope(
|
||||
fx_hash_ctx *ctx,
|
||||
const struct fx_rope *rope);
|
||||
FX_API fx_status
|
||||
fx_hash_ctx_update_rope(fx_hash_ctx *ctx, const struct fx_rope *rope);
|
||||
FX_API fx_status
|
||||
fx_hash_ctx_finish(fx_hash_ctx *ctx, void *out_digest, size_t out_max);
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef FX_INT_PRIMITIVES_H_
|
||||
#define FX_INT_PRIMITIVES_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
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
|
||||
+141
-18
@@ -1,39 +1,162 @@
|
||||
#ifndef FX_INT_H_
|
||||
#define FX_INT_H_
|
||||
|
||||
#include <fx/int-primitives.h>
|
||||
#include <fx/macros.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
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;
|
||||
|
||||
|
||||
@@ -4,31 +4,21 @@
|
||||
#include <fx/macros.h>
|
||||
#include <fx/misc.h>
|
||||
#include <fx/status.h>
|
||||
#include <fx/value.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
#define fx_foreach(type, var, iterator) \
|
||||
for (type var = (type)fx_iterator_get_value(iterator).v_int; \
|
||||
#define fx_foreach(var, iterator) \
|
||||
for (fx_value var = fx_iterator_get_value(iterator); \
|
||||
FX_OK(fx_iterator_get_status(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) \
|
||||
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)
|
||||
#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_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_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_iterable);
|
||||
@@ -52,8 +36,7 @@ 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_iterator_value (*it_get_value)(fx_iterator *);
|
||||
const fx_iterator_value (*it_get_cvalue)(const fx_iterator *);
|
||||
fx_value (*it_get_value)(const fx_iterator *);
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_iterator)
|
||||
|
||||
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 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_set_status(const fx_iterator *it, fx_status status);
|
||||
|
||||
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 const fx_iterator_value fx_iterator_get_cvalue(const fx_iterator *it);
|
||||
FX_API 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)
|
||||
{
|
||||
|
||||
+17
-4
@@ -5,7 +5,6 @@
|
||||
#include <fx/object.h>
|
||||
#include <fx/thread.h>
|
||||
#include <fx/type.h>
|
||||
#include <fx/value.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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), \
|
||||
@@ -68,6 +67,16 @@
|
||||
fx_type_add_function(ty, func); \
|
||||
} \
|
||||
} 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) \
|
||||
interface_name##_class *__FX_IFACE_I1(iface, __LINE__) \
|
||||
@@ -117,6 +126,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)
|
||||
@@ -254,6 +266,7 @@
|
||||
} \
|
||||
self = fx_assembly_create();
|
||||
#define FX_ASSEMBLY_END(id) \
|
||||
fx_assembly_register(self); \
|
||||
return self; \
|
||||
} \
|
||||
const fx_assembly *__fx_assembly_get(void) \
|
||||
|
||||
+10
-3
@@ -14,12 +14,18 @@
|
||||
#define FX_RV(p) (fx_object_make_rvalue(p))
|
||||
|
||||
typedef FX_TYPE_FWDREF(fx_object) fx_object;
|
||||
struct fx_value;
|
||||
|
||||
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_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);
|
||||
@@ -36,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_create(fx_type_id type);
|
||||
FX_API void fx_object_to_string(
|
||||
FX_API fx_status fx_object_to_string(
|
||||
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);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#ifndef FX_OPERABLE_H_
|
||||
#define FX_OPERABLE_H_
|
||||
|
||||
#include <fx/int-primitives.h>
|
||||
#include <fx/macros.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
#define FX_TYPE_OPERABLE (fx_operable_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(fx_operable);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_operable)
|
||||
fx_status (*op_add)(
|
||||
const fx_value *left,
|
||||
const fx_value *right,
|
||||
fx_value *out);
|
||||
fx_status (*op_subtract)(
|
||||
const fx_value *left,
|
||||
const fx_value *right,
|
||||
fx_value *out);
|
||||
fx_status (*op_multiply)(
|
||||
const fx_value *left,
|
||||
const fx_value *right,
|
||||
fx_value *out);
|
||||
fx_status (*op_divide)(
|
||||
const fx_value *left,
|
||||
const fx_value *right,
|
||||
fx_value *out);
|
||||
fx_status (*op_increment)(fx_value *value);
|
||||
fx_status (*op_decrement)(fx_value *value);
|
||||
fx_status (*op_add_in_place)(fx_value *left, const fx_value *right);
|
||||
fx_status (
|
||||
*op_subtract_in_place)(fx_value *left, const fx_value *right);
|
||||
fx_status (
|
||||
*op_multiply_in_place)(fx_value *left, const fx_value *right);
|
||||
fx_status (*op_divide_in_place)(fx_value *left, const fx_value *right);
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_operable)
|
||||
|
||||
FX_API fx_type_id fx_operable_get_type(void);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef FX_POINTER_H_
|
||||
#define FX_POINTER_H_
|
||||
|
||||
#include <fx/macros.h>
|
||||
|
||||
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
|
||||
@@ -97,6 +97,8 @@ FX_API fx_status fx_stream_read_all_bytes_s(
|
||||
FX_API fx_status fx_stream_write_char(fx_stream *stream, fx_wchar c);
|
||||
FX_API fx_status
|
||||
fx_stream_write_cstr(fx_stream *stream, const char *s, size_t *nr_written);
|
||||
FX_API fx_status
|
||||
fx_stream_write_wstr(fx_stream *stream, const fx_wchar *s, size_t *nr_written);
|
||||
|
||||
FX_API fx_status fx_stream_write_bytes(
|
||||
fx_stream *stream,
|
||||
|
||||
+1
-15
@@ -2,6 +2,7 @@
|
||||
#define FX_STRING_H_
|
||||
|
||||
#include <ctype.h>
|
||||
#include <fx/cstr.h>
|
||||
#include <fx/encoding.h>
|
||||
#include <fx/iterator.h>
|
||||
#include <fx/macros.h>
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#define FX_TYPE_VOID ((fx_type_id)NULL)
|
||||
#define FX_TYPE_MAX_INTERFACES 64
|
||||
|
||||
struct _fx_class;
|
||||
@@ -63,6 +64,7 @@ typedef struct fx_type_info {
|
||||
struct fx_bst ty_components;
|
||||
struct fx_queue ty_class_hierarchy;
|
||||
struct fx_namemap ty_functions;
|
||||
struct fx_namemap ty_properties;
|
||||
|
||||
size_t ty_instance_size, ty_class_size;
|
||||
} fx_type_info;
|
||||
@@ -92,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_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
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
#ifndef FX_UINT_H_
|
||||
#define FX_UINT_H_
|
||||
|
||||
#include <fx/macros.h>
|
||||
|
||||
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
|
||||
@@ -0,0 +1,56 @@
|
||||
#ifndef FX_VALUE_TYPE_H_
|
||||
#define FX_VALUE_TYPE_H_
|
||||
|
||||
#include <fx/macros.h>
|
||||
|
||||
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_WCHAR,
|
||||
__FX_VALUE_TYPE_WSTR,
|
||||
__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);
|
||||
FX_API size_t __fx_value_type_get_size(unsigned int ty);
|
||||
|
||||
#endif
|
||||
+161
-71
@@ -1,66 +1,88 @@
|
||||
#ifndef FX_VALUE_H_
|
||||
#define FX_VALUE_H_
|
||||
|
||||
#include <fx/bool.h>
|
||||
#include <fx/cstr.h>
|
||||
#include <fx/float.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/object.h>
|
||||
#include <fx/pointer.h>
|
||||
#include <fx/type.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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_longlong, v)
|
||||
#define FX_ULONGLONG(v) __FX_VALUE_CREATE(ULONGLONG, v_ulonglong, 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_WCHAR(v) __FX_VALUE_CREATE(WCHAR, v_wchar, v)
|
||||
#define FX_WSTR(v) __FX_VALUE_CREATE(WSTR, v_wstr, 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;
|
||||
|
||||
fx_wchar v_wchar;
|
||||
const fx_wchar *v_wstr;
|
||||
const char *v_cstr;
|
||||
void *v_pointer;
|
||||
fx_object *v_object;
|
||||
@@ -68,38 +90,106 @@ 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);
|
||||
FX_API void fx_value_copy(fx_value *dst, const fx_value *src);
|
||||
FX_API fx_value fx_value_copy_return(const fx_value src);
|
||||
FX_API fx_value fx_value_ref_copy_return(const fx_value *src);
|
||||
FX_API void fx_value_copy_array(
|
||||
fx_value *dst,
|
||||
const fx_value *src,
|
||||
size_t count);
|
||||
|
||||
FX_API void fx_value_unset(fx_value *v);
|
||||
FX_API void fx_value_unset_array(fx_value *v, size_t length);
|
||||
|
||||
FX_API void fx_value_set_bool(fx_value *v, bool b);
|
||||
FX_API void fx_value_set_int(fx_value *v, intptr_t i);
|
||||
FX_API void fx_value_set_uint(fx_value *v, uintptr_t u);
|
||||
FX_API void fx_value_set_double(fx_value *v, double d);
|
||||
FX_API void fx_value_set_static_cstr(fx_value *v, const char *cstr);
|
||||
FX_API void fx_value_set_pointer(fx_value *v, void *p);
|
||||
FX_API void fx_value_set_object(fx_value *v, fx_object *obj);
|
||||
FX_API fx_status fx_value_hash(const fx_value *v, uint64_t *out_hash);
|
||||
FX_API int fx_value_compare(const fx_value *left, const fx_value *right);
|
||||
|
||||
FX_API bool fx_value_is_bool(const fx_value *v);
|
||||
FX_API bool fx_value_is_int(const fx_value *v);
|
||||
FX_API bool fx_value_is_uint(const fx_value *v);
|
||||
FX_API bool fx_value_is_double(const fx_value *v);
|
||||
FX_API bool fx_value_is_string(const fx_value *v);
|
||||
FX_API bool fx_value_is_pointer(const fx_value *v);
|
||||
FX_API bool fx_value_is_object(const fx_value *v, fx_type_id type);
|
||||
FX_API fx_status fx_value_to_string(
|
||||
const fx_value *v,
|
||||
FX_TYPE_FWDREF(fx_stream) * out,
|
||||
const char *format);
|
||||
|
||||
FX_API bool fx_value_get_bool(const fx_value *v);
|
||||
FX_API intptr_t fx_value_get_int(const fx_value *v);
|
||||
FX_API uintptr_t fx_value_get_uint(const fx_value *v);
|
||||
FX_API double fx_value_get_double(const fx_value *v);
|
||||
FX_API const char *fx_value_get_cstr(const fx_value *v);
|
||||
FX_API void *fx_value_get_pointer(fx_value *v);
|
||||
FX_API const void *fx_value_get_pointer_c(const fx_value *v);
|
||||
FX_API fx_object *fx_value_get_object(fx_value *v);
|
||||
FX_API const fx_object *fx_value_get_object_c(const fx_value *v);
|
||||
FX_API bool fx_value_is_set(const fx_value *v);
|
||||
FX_API bool fx_value_is_type(const fx_value *v, fx_type_id type);
|
||||
FX_API fx_status
|
||||
fx_value_change_type(fx_value *in, fx_value *out, fx_type_id convert_to);
|
||||
FX_API fx_type_id
|
||||
fx_value_get_common_type(const fx_value *a, const fx_value *b);
|
||||
|
||||
FX_API fx_status
|
||||
fx_value_add(const fx_value *left, const fx_value *right, fx_value *out);
|
||||
FX_API fx_status
|
||||
fx_value_subtract(const fx_value *left, const fx_value *right, fx_value *out);
|
||||
FX_API fx_status
|
||||
fx_value_multiply(const fx_value *left, const fx_value *right, fx_value *out);
|
||||
FX_API fx_status
|
||||
fx_value_divide(const fx_value *left, const fx_value *right, fx_value *out);
|
||||
FX_API fx_status fx_value_increment(fx_value *value);
|
||||
FX_API fx_status fx_value_decrement(fx_value *value);
|
||||
FX_API fx_status fx_value_add_in_place(fx_value *left, const fx_value *right);
|
||||
FX_API fx_status
|
||||
fx_value_subtract_in_place(fx_value *left, const fx_value *right);
|
||||
FX_API fx_status
|
||||
fx_value_multiply_in_place(fx_value *left, const fx_value *right);
|
||||
FX_API fx_status
|
||||
fx_value_divide_in_place(fx_value *left, const fx_value *right);
|
||||
|
||||
FX_API fx_status fx_value_set_bool(fx_value *v, bool value);
|
||||
FX_API fx_status fx_value_set_i16(fx_value *v, i16 value);
|
||||
FX_API fx_status fx_value_set_u16(fx_value *v, u16 value);
|
||||
FX_API fx_status fx_value_set_i32(fx_value *v, i32 value);
|
||||
FX_API fx_status fx_value_set_u32(fx_value *v, u32 value);
|
||||
FX_API fx_status fx_value_set_i64(fx_value *v, i64 value);
|
||||
FX_API fx_status fx_value_set_u64(fx_value *v, u64 value);
|
||||
FX_API fx_status fx_value_set_iptr(fx_value *v, iptr value);
|
||||
FX_API fx_status fx_value_set_uptr(fx_value *v, uptr value);
|
||||
FX_API fx_status fx_value_set_sbyte(fx_value *v, sbyte value);
|
||||
FX_API fx_status fx_value_set_byte(fx_value *v, byte value);
|
||||
FX_API fx_status fx_value_set_short(fx_value *v, short value);
|
||||
FX_API fx_status fx_value_set_ushort(fx_value *v, unsigned short value);
|
||||
FX_API fx_status fx_value_set_int(fx_value *v, int value);
|
||||
FX_API fx_status fx_value_set_uint(fx_value *v, unsigned int value);
|
||||
FX_API fx_status fx_value_set_long(fx_value *v, long value);
|
||||
FX_API fx_status fx_value_set_ulong(fx_value *v, unsigned long value);
|
||||
FX_API fx_status fx_value_set_longlong(fx_value *v, long long value);
|
||||
FX_API fx_status fx_value_set_ulonglong(fx_value *v, unsigned long long value);
|
||||
FX_API fx_status fx_value_set_size(fx_value *v, size_t value);
|
||||
FX_API fx_status fx_value_set_float(fx_value *v, float value);
|
||||
FX_API fx_status fx_value_set_double(fx_value *v, double value);
|
||||
FX_API fx_status fx_value_set_cstr(fx_value *v, const char *value);
|
||||
FX_API fx_status fx_value_set_wchar(fx_value *v, fx_wchar value);
|
||||
FX_API fx_status fx_value_set_wstr(fx_value *v, const fx_wchar *value);
|
||||
FX_API fx_status fx_value_set_pointer(fx_value *v, void *value);
|
||||
FX_API fx_status fx_value_set_object(fx_value *v, fx_object *value);
|
||||
|
||||
FX_API fx_status fx_value_get_bool(const fx_value *v, bool *out);
|
||||
FX_API fx_status fx_value_get_i16(const fx_value *v, i16 *out);
|
||||
FX_API fx_status fx_value_get_u16(const fx_value *v, u16 *out);
|
||||
FX_API fx_status fx_value_get_i32(const fx_value *v, i32 *out);
|
||||
FX_API fx_status fx_value_get_u32(const fx_value *v, u32 *out);
|
||||
FX_API fx_status fx_value_get_i64(const fx_value *v, i64 *out);
|
||||
FX_API fx_status fx_value_get_u64(const fx_value *v, u64 *out);
|
||||
FX_API fx_status fx_value_get_iptr(const fx_value *v, iptr *out);
|
||||
FX_API fx_status fx_value_get_uptr(const fx_value *v, uptr *out);
|
||||
FX_API fx_status fx_value_get_sbyte(const fx_value *v, sbyte *out);
|
||||
FX_API fx_status fx_value_get_byte(const fx_value *v, byte *out);
|
||||
FX_API fx_status fx_value_get_short(const fx_value *v, short *out);
|
||||
FX_API fx_status fx_value_get_ushort(const fx_value *v, unsigned short *out);
|
||||
FX_API fx_status fx_value_get_int(const fx_value *v, int *out);
|
||||
FX_API fx_status fx_value_get_uint(const fx_value *v, unsigned int *out);
|
||||
FX_API fx_status fx_value_get_long(const fx_value *v, long *out);
|
||||
FX_API fx_status fx_value_get_ulong(const fx_value *v, unsigned long *out);
|
||||
FX_API fx_status fx_value_get_longlong(const fx_value *v, long long *out);
|
||||
FX_API fx_status
|
||||
fx_value_get_ulonglong(const fx_value *v, unsigned long long *out);
|
||||
FX_API fx_status fx_value_get_size(const fx_value *v, size_t *out);
|
||||
FX_API fx_status fx_value_get_float(const fx_value *v, float *out);
|
||||
FX_API fx_status fx_value_get_double(const fx_value *v, double *out);
|
||||
FX_API fx_status fx_value_get_cstr(const fx_value *v, const char **out);
|
||||
FX_API fx_status fx_value_get_wchar(const fx_value *v, fx_wchar *out);
|
||||
FX_API fx_status fx_value_get_wstr(const fx_value *v, const fx_wchar **out);
|
||||
FX_API fx_status fx_value_get_pointer(const fx_value *v, void **out);
|
||||
FX_API fx_status fx_value_get_object(const fx_value *v, fx_object **out);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef FX_WSTR_H_
|
||||
#define FX_WSTR_H_
|
||||
|
||||
#include <fx/encoding.h>
|
||||
#include <fx/macros.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
#define FX_TYPE_WCHAR (fx_wchar_get_type())
|
||||
#define FX_TYPE_WSTR (fx_wstr_get_type())
|
||||
|
||||
typedef struct _fx_wchar_class fx_wchar_class;
|
||||
typedef struct _fx_wstr_class fx_wstr_class;
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_wchar)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_wchar)
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_wstr)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_wstr)
|
||||
|
||||
FX_API fx_type_id fx_wchar_get_type(void);
|
||||
FX_API fx_type_id fx_wstr_get_type(void);
|
||||
|
||||
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
|
||||
@@ -1,272 +0,0 @@
|
||||
#include <fx/int.h>
|
||||
#include <fx/stream.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/*** 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)
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
HASH(byte)
|
||||
TO_STRING(byte, "%u")
|
||||
|
||||
BOOL_CONVERT(byte)
|
||||
GENERIC_CONVERT(byte, i16, i16)
|
||||
GENERIC_CONVERT(byte, u16, u16)
|
||||
GENERIC_CONVERT(byte, i32, i32)
|
||||
GENERIC_CONVERT(byte, u32, u32)
|
||||
GENERIC_CONVERT(byte, i64, i64)
|
||||
GENERIC_CONVERT(byte, u64, u64)
|
||||
GENERIC_CONVERT(byte, iptr, iptr)
|
||||
GENERIC_CONVERT(byte, uptr, uptr)
|
||||
GENERIC_CONVERT(byte, sbyte, sbyte)
|
||||
GENERIC_CONVERT(byte, short, short)
|
||||
GENERIC_CONVERT(byte, ushort, unsigned short)
|
||||
GENERIC_CONVERT(byte, int, int)
|
||||
GENERIC_CONVERT(byte, uint, unsigned int)
|
||||
GENERIC_CONVERT(byte, long, long)
|
||||
GENERIC_CONVERT(byte, ulong, unsigned long)
|
||||
GENERIC_CONVERT(byte, longlong, long long)
|
||||
GENERIC_CONVERT(byte, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(byte, size, size_t)
|
||||
GENERIC_CONVERT(byte, float, float)
|
||||
GENERIC_CONVERT(byte, double, double)
|
||||
|
||||
BINARY_OP(add, +, byte)
|
||||
BINARY_OP(subtract, -, byte)
|
||||
BINARY_OP(multiply, *, byte)
|
||||
BINARY_OP(divide, /, byte)
|
||||
PREFIX_OP(increment, ++, byte)
|
||||
PREFIX_OP(decrement, --, byte)
|
||||
IN_PLACE_OP(add_in_place, +=, byte)
|
||||
IN_PLACE_OP(subtract_in_place, -=, byte)
|
||||
IN_PLACE_OP(multiply_in_place, *=, byte)
|
||||
IN_PLACE_OP(divide_in_place, /=, byte)
|
||||
|
||||
COMPARE(byte, byte)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_byte)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.byte");
|
||||
FX_TYPE_CLASS(fx_byte_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(byte);
|
||||
FX_TYPE_DEFINITION_END(fx_byte)
|
||||
@@ -0,0 +1,81 @@
|
||||
#ifndef INT_CONVERT_H_
|
||||
#define INT_CONVERT_H_
|
||||
|
||||
#include <fx/comparable.h>
|
||||
#include <fx/convertible.h>
|
||||
#include <fx/operable.h>
|
||||
#include <fx/value.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#define HASH(type_name) \
|
||||
static fx_status hash(const fx_value *v, uint64_t *out_hash) \
|
||||
{ \
|
||||
*out_hash = v->v_##type_name; \
|
||||
return FX_SUCCESS; \
|
||||
}
|
||||
|
||||
#define TO_STRING(type_name, fmt) \
|
||||
static fx_status to_string( \
|
||||
const fx_value *v, \
|
||||
fx_stream *out, \
|
||||
const char *format) \
|
||||
{ \
|
||||
return fx_stream_write_fmt(out, NULL, fmt, v->v_##type_name); \
|
||||
}
|
||||
|
||||
#define BINARY_OP(op_name, op_sym, type_name) \
|
||||
static fx_status op_name( \
|
||||
const fx_value *left, \
|
||||
const fx_value *right, \
|
||||
fx_value *out) \
|
||||
{ \
|
||||
out->v_type = left->v_type; \
|
||||
out->v_##type_name = left->v_##type_name op_sym \
|
||||
right->v_##type_name; \
|
||||
return FX_SUCCESS; \
|
||||
}
|
||||
|
||||
#define PREFIX_OP(op_name, op_sym, type_name) \
|
||||
static fx_status op_name(fx_value *value) \
|
||||
{ \
|
||||
value->v_##type_name op_sym; \
|
||||
return FX_SUCCESS; \
|
||||
}
|
||||
|
||||
#define IN_PLACE_OP(op_name, op_sym, type_name) \
|
||||
static fx_status op_name(fx_value *left, const fx_value *right) \
|
||||
{ \
|
||||
left->v_##type_name op_sym right->v_##type_name; \
|
||||
return FX_SUCCESS; \
|
||||
}
|
||||
|
||||
#define BOOL_CONVERT(fx_from) \
|
||||
static fx_status to_bool(const fx_value *in, bool *out) \
|
||||
{ \
|
||||
*out = in->v_##fx_from != 0 ? true : false; \
|
||||
return FX_SUCCESS; \
|
||||
}
|
||||
#define GENERIC_CONVERT(fx_from, fx_to, c_to) \
|
||||
static fx_status to_##fx_to(const fx_value *in, c_to *out) \
|
||||
{ \
|
||||
*out = (c_to)in->v_##fx_from; \
|
||||
return FX_SUCCESS; \
|
||||
}
|
||||
|
||||
#define COMPARE(fx_type, c_type) \
|
||||
static i32 compare(const fx_value *left, const fx_value *right) \
|
||||
{ \
|
||||
c_type right_v = 0; \
|
||||
if (!FX_OK(fx_value_get_##fx_type(right, &right_v))) { \
|
||||
return -1; \
|
||||
} \
|
||||
if (left->v_##fx_type < right_v) { \
|
||||
return -1; \
|
||||
} else if (left->v_##fx_type > right_v) { \
|
||||
return 1; \
|
||||
} else { \
|
||||
return 0; \
|
||||
} \
|
||||
}
|
||||
|
||||
#endif
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/convertible.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
#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
|
||||
}
|
||||
|
||||
HASH(i16)
|
||||
TO_STRING(i16, "%" PRId16)
|
||||
|
||||
BOOL_CONVERT(i16)
|
||||
GENERIC_CONVERT(i16, u16, u16)
|
||||
GENERIC_CONVERT(i16, i32, i32)
|
||||
GENERIC_CONVERT(i16, u32, u32)
|
||||
GENERIC_CONVERT(i16, i64, i64)
|
||||
GENERIC_CONVERT(i16, u64, u64)
|
||||
GENERIC_CONVERT(i16, iptr, iptr)
|
||||
GENERIC_CONVERT(i16, uptr, uptr)
|
||||
GENERIC_CONVERT(i16, sbyte, sbyte)
|
||||
GENERIC_CONVERT(i16, byte, byte)
|
||||
GENERIC_CONVERT(i16, short, short)
|
||||
GENERIC_CONVERT(i16, ushort, unsigned short)
|
||||
GENERIC_CONVERT(i16, int, int)
|
||||
GENERIC_CONVERT(i16, uint, unsigned int)
|
||||
GENERIC_CONVERT(i16, long, long)
|
||||
GENERIC_CONVERT(i16, ulong, unsigned long)
|
||||
GENERIC_CONVERT(i16, longlong, long long)
|
||||
GENERIC_CONVERT(i16, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(i16, size, size_t)
|
||||
GENERIC_CONVERT(i16, float, float)
|
||||
GENERIC_CONVERT(i16, double, double)
|
||||
|
||||
BINARY_OP(add, +, i16)
|
||||
BINARY_OP(subtract, -, i16)
|
||||
BINARY_OP(multiply, *, i16)
|
||||
BINARY_OP(divide, /, i16)
|
||||
PREFIX_OP(increment, ++, i16)
|
||||
PREFIX_OP(decrement, --, i16)
|
||||
IN_PLACE_OP(add_in_place, +=, i16)
|
||||
IN_PLACE_OP(subtract_in_place, -=, i16)
|
||||
IN_PLACE_OP(multiply_in_place, *=, i16)
|
||||
IN_PLACE_OP(divide_in_place, /=, i16)
|
||||
|
||||
COMPARE(i16, i16)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_i16)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.i16");
|
||||
FX_TYPE_CLASS(fx_i16_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(i16);
|
||||
FX_TYPE_DEFINITION_END(fx_i16)
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/convertible.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
#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
|
||||
}
|
||||
|
||||
HASH(i32)
|
||||
TO_STRING(i32, "%" PRId32)
|
||||
|
||||
BOOL_CONVERT(i32)
|
||||
GENERIC_CONVERT(i32, i16, i16)
|
||||
GENERIC_CONVERT(i32, u16, u16)
|
||||
GENERIC_CONVERT(i32, u32, u32)
|
||||
GENERIC_CONVERT(i32, i64, i64)
|
||||
GENERIC_CONVERT(i32, u64, u64)
|
||||
GENERIC_CONVERT(i32, iptr, iptr)
|
||||
GENERIC_CONVERT(i32, uptr, uptr)
|
||||
GENERIC_CONVERT(i32, sbyte, sbyte)
|
||||
GENERIC_CONVERT(i32, byte, byte)
|
||||
GENERIC_CONVERT(i32, short, short)
|
||||
GENERIC_CONVERT(i32, ushort, unsigned short)
|
||||
GENERIC_CONVERT(i32, int, int)
|
||||
GENERIC_CONVERT(i32, uint, unsigned int)
|
||||
GENERIC_CONVERT(i32, long, long)
|
||||
GENERIC_CONVERT(i32, ulong, unsigned long)
|
||||
GENERIC_CONVERT(i32, longlong, long long)
|
||||
GENERIC_CONVERT(i32, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(i32, size, size_t)
|
||||
GENERIC_CONVERT(i32, float, float)
|
||||
GENERIC_CONVERT(i32, double, double)
|
||||
|
||||
BINARY_OP(add, +, i32)
|
||||
BINARY_OP(subtract, -, i32)
|
||||
BINARY_OP(multiply, *, i32)
|
||||
BINARY_OP(divide, /, i32)
|
||||
PREFIX_OP(increment, ++, i32)
|
||||
PREFIX_OP(decrement, --, i32)
|
||||
IN_PLACE_OP(add_in_place, +=, i32)
|
||||
IN_PLACE_OP(subtract_in_place, -=, i32)
|
||||
IN_PLACE_OP(multiply_in_place, *=, i32)
|
||||
IN_PLACE_OP(divide_in_place, /=, i32)
|
||||
|
||||
COMPARE(i32, i32)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_i32)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.i32");
|
||||
FX_TYPE_CLASS(fx_i32_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(i32);
|
||||
FX_TYPE_DEFINITION_END(fx_i32)
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/convertible.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
/* 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
|
||||
}
|
||||
|
||||
HASH(i64)
|
||||
TO_STRING(i64, "%" PRId64)
|
||||
|
||||
BOOL_CONVERT(i64)
|
||||
GENERIC_CONVERT(i64, i16, i16)
|
||||
GENERIC_CONVERT(i64, u16, u16)
|
||||
GENERIC_CONVERT(i64, i32, i32)
|
||||
GENERIC_CONVERT(i64, u32, u32)
|
||||
GENERIC_CONVERT(i64, u64, u64)
|
||||
GENERIC_CONVERT(i64, iptr, iptr)
|
||||
GENERIC_CONVERT(i64, uptr, uptr)
|
||||
GENERIC_CONVERT(i64, sbyte, sbyte)
|
||||
GENERIC_CONVERT(i64, byte, byte)
|
||||
GENERIC_CONVERT(i64, short, short)
|
||||
GENERIC_CONVERT(i64, ushort, unsigned short)
|
||||
GENERIC_CONVERT(i64, int, int)
|
||||
GENERIC_CONVERT(i64, uint, unsigned int)
|
||||
GENERIC_CONVERT(i64, long, long)
|
||||
GENERIC_CONVERT(i64, ulong, unsigned long)
|
||||
GENERIC_CONVERT(i64, longlong, long long)
|
||||
GENERIC_CONVERT(i64, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(i64, size, size_t)
|
||||
GENERIC_CONVERT(i64, float, float)
|
||||
GENERIC_CONVERT(i64, double, double)
|
||||
|
||||
BINARY_OP(add, +, i64)
|
||||
BINARY_OP(subtract, -, i64)
|
||||
BINARY_OP(multiply, *, i64)
|
||||
BINARY_OP(divide, /, i64)
|
||||
PREFIX_OP(increment, ++, i64)
|
||||
PREFIX_OP(decrement, --, i64)
|
||||
IN_PLACE_OP(add_in_place, +=, i64)
|
||||
IN_PLACE_OP(subtract_in_place, -=, i64)
|
||||
IN_PLACE_OP(multiply_in_place, *=, i64)
|
||||
IN_PLACE_OP(divide_in_place, /=, i64)
|
||||
|
||||
COMPARE(i64, i64)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_i64)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
FX_INTERFACE_ENTRY(c_to_u32) = to_u32;
|
||||
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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.i64");
|
||||
FX_TYPE_CLASS(fx_i64_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(i64);
|
||||
FX_TYPE_DEFINITION_END(fx_i64)
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
HASH(int)
|
||||
TO_STRING(int, "%d")
|
||||
|
||||
BOOL_CONVERT(int)
|
||||
GENERIC_CONVERT(int, i16, i16)
|
||||
GENERIC_CONVERT(int, u16, u16)
|
||||
GENERIC_CONVERT(int, i32, i32)
|
||||
GENERIC_CONVERT(int, u32, u32)
|
||||
GENERIC_CONVERT(int, i64, i64)
|
||||
GENERIC_CONVERT(int, u64, u64)
|
||||
GENERIC_CONVERT(int, iptr, iptr)
|
||||
GENERIC_CONVERT(int, uptr, uptr)
|
||||
GENERIC_CONVERT(int, sbyte, sbyte)
|
||||
GENERIC_CONVERT(int, byte, byte)
|
||||
GENERIC_CONVERT(int, short, short)
|
||||
GENERIC_CONVERT(int, ushort, unsigned short)
|
||||
GENERIC_CONVERT(int, uint, unsigned int)
|
||||
GENERIC_CONVERT(int, long, long)
|
||||
GENERIC_CONVERT(int, ulong, unsigned long)
|
||||
GENERIC_CONVERT(int, longlong, long long)
|
||||
GENERIC_CONVERT(int, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(int, size, size_t)
|
||||
GENERIC_CONVERT(int, float, float)
|
||||
GENERIC_CONVERT(int, double, double)
|
||||
|
||||
BINARY_OP(add, +, int)
|
||||
BINARY_OP(subtract, -, int)
|
||||
BINARY_OP(multiply, *, int)
|
||||
BINARY_OP(divide, /, int)
|
||||
PREFIX_OP(increment, ++, int)
|
||||
PREFIX_OP(decrement, --, int)
|
||||
IN_PLACE_OP(add_in_place, +=, int)
|
||||
IN_PLACE_OP(subtract_in_place, -=, int)
|
||||
IN_PLACE_OP(multiply_in_place, *=, int)
|
||||
IN_PLACE_OP(divide_in_place, /=, int)
|
||||
|
||||
COMPARE(int, int)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_int)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.int");
|
||||
FX_TYPE_CLASS(fx_int_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(int);
|
||||
FX_TYPE_DEFINITION_END(fx_int)
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
HASH(iptr)
|
||||
TO_STRING(iptr, "%" PRIdPTR)
|
||||
|
||||
BOOL_CONVERT(iptr)
|
||||
GENERIC_CONVERT(iptr, i16, i16)
|
||||
GENERIC_CONVERT(iptr, u16, u16)
|
||||
GENERIC_CONVERT(iptr, i32, i32)
|
||||
GENERIC_CONVERT(iptr, u32, u32)
|
||||
GENERIC_CONVERT(iptr, i64, i64)
|
||||
GENERIC_CONVERT(iptr, u64, u64)
|
||||
GENERIC_CONVERT(iptr, uptr, uptr)
|
||||
GENERIC_CONVERT(iptr, sbyte, sbyte)
|
||||
GENERIC_CONVERT(iptr, byte, byte)
|
||||
GENERIC_CONVERT(iptr, short, short)
|
||||
GENERIC_CONVERT(iptr, ushort, unsigned short)
|
||||
GENERIC_CONVERT(iptr, int, int)
|
||||
GENERIC_CONVERT(iptr, uint, unsigned int)
|
||||
GENERIC_CONVERT(iptr, long, long)
|
||||
GENERIC_CONVERT(iptr, ulong, unsigned long)
|
||||
GENERIC_CONVERT(iptr, longlong, long long)
|
||||
GENERIC_CONVERT(iptr, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(iptr, size, size_t)
|
||||
GENERIC_CONVERT(iptr, float, float)
|
||||
GENERIC_CONVERT(iptr, double, double)
|
||||
|
||||
BINARY_OP(add, +, iptr)
|
||||
BINARY_OP(subtract, -, iptr)
|
||||
BINARY_OP(multiply, *, iptr)
|
||||
BINARY_OP(divide, /, iptr)
|
||||
PREFIX_OP(increment, ++, iptr)
|
||||
PREFIX_OP(decrement, --, iptr)
|
||||
IN_PLACE_OP(add_in_place, +=, iptr)
|
||||
IN_PLACE_OP(subtract_in_place, -=, iptr)
|
||||
IN_PLACE_OP(multiply_in_place, *=, iptr)
|
||||
IN_PLACE_OP(divide_in_place, /=, iptr)
|
||||
|
||||
COMPARE(iptr, iptr)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_iptr)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.iptr");
|
||||
FX_TYPE_CLASS(fx_iptr_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(iptr);
|
||||
FX_TYPE_DEFINITION_END(fx_iptr)
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
HASH(long)
|
||||
TO_STRING(long, "%ld")
|
||||
|
||||
BOOL_CONVERT(long)
|
||||
GENERIC_CONVERT(long, i16, i16)
|
||||
GENERIC_CONVERT(long, u16, u16)
|
||||
GENERIC_CONVERT(long, i32, i32)
|
||||
GENERIC_CONVERT(long, u32, u32)
|
||||
GENERIC_CONVERT(long, i64, i64)
|
||||
GENERIC_CONVERT(long, u64, u64)
|
||||
GENERIC_CONVERT(long, iptr, iptr)
|
||||
GENERIC_CONVERT(long, uptr, uptr)
|
||||
GENERIC_CONVERT(long, sbyte, sbyte)
|
||||
GENERIC_CONVERT(long, byte, byte)
|
||||
GENERIC_CONVERT(long, short, short)
|
||||
GENERIC_CONVERT(long, ushort, unsigned short)
|
||||
GENERIC_CONVERT(long, int, int)
|
||||
GENERIC_CONVERT(long, uint, unsigned int)
|
||||
GENERIC_CONVERT(long, ulong, unsigned long)
|
||||
GENERIC_CONVERT(long, longlong, long long)
|
||||
GENERIC_CONVERT(long, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(long, size, size_t)
|
||||
GENERIC_CONVERT(long, float, float)
|
||||
GENERIC_CONVERT(long, double, double)
|
||||
|
||||
BINARY_OP(add, +, long)
|
||||
BINARY_OP(subtract, -, long)
|
||||
BINARY_OP(multiply, *, long)
|
||||
BINARY_OP(divide, /, long)
|
||||
PREFIX_OP(increment, ++, long)
|
||||
PREFIX_OP(decrement, --, long)
|
||||
IN_PLACE_OP(add_in_place, +=, long)
|
||||
IN_PLACE_OP(subtract_in_place, -=, long)
|
||||
IN_PLACE_OP(multiply_in_place, *=, long)
|
||||
IN_PLACE_OP(divide_in_place, /=, long)
|
||||
|
||||
COMPARE(long, long)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_long)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.long");
|
||||
FX_TYPE_CLASS(fx_long_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(long);
|
||||
FX_TYPE_DEFINITION_END(fx_long)
|
||||
@@ -0,0 +1,103 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
HASH(longlong)
|
||||
TO_STRING(longlong, "%lld")
|
||||
|
||||
BOOL_CONVERT(longlong)
|
||||
GENERIC_CONVERT(longlong, i16, i16)
|
||||
GENERIC_CONVERT(longlong, u16, u16)
|
||||
GENERIC_CONVERT(longlong, i32, i32)
|
||||
GENERIC_CONVERT(longlong, u32, u32)
|
||||
GENERIC_CONVERT(longlong, i64, i64)
|
||||
GENERIC_CONVERT(longlong, u64, u64)
|
||||
GENERIC_CONVERT(longlong, iptr, iptr)
|
||||
GENERIC_CONVERT(longlong, uptr, uptr)
|
||||
GENERIC_CONVERT(longlong, sbyte, sbyte)
|
||||
GENERIC_CONVERT(longlong, byte, byte)
|
||||
GENERIC_CONVERT(longlong, short, short)
|
||||
GENERIC_CONVERT(longlong, ushort, unsigned short)
|
||||
GENERIC_CONVERT(longlong, int, int)
|
||||
GENERIC_CONVERT(longlong, uint, unsigned int)
|
||||
GENERIC_CONVERT(longlong, long, long)
|
||||
GENERIC_CONVERT(longlong, ulong, unsigned long)
|
||||
GENERIC_CONVERT(longlong, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(longlong, size, size_t)
|
||||
GENERIC_CONVERT(longlong, float, float)
|
||||
GENERIC_CONVERT(longlong, double, double)
|
||||
|
||||
BINARY_OP(add, +, longlong)
|
||||
BINARY_OP(subtract, -, longlong)
|
||||
BINARY_OP(multiply, *, longlong)
|
||||
BINARY_OP(divide, /, longlong)
|
||||
PREFIX_OP(increment, ++, longlong)
|
||||
PREFIX_OP(decrement, --, longlong)
|
||||
IN_PLACE_OP(add_in_place, +=, longlong)
|
||||
IN_PLACE_OP(subtract_in_place, -=, longlong)
|
||||
IN_PLACE_OP(multiply_in_place, *=, longlong)
|
||||
IN_PLACE_OP(divide_in_place, /=, longlong)
|
||||
|
||||
COMPARE(longlong, long long)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_longlong)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.longlong");
|
||||
FX_TYPE_CLASS(fx_longlong_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(long long);
|
||||
FX_TYPE_DEFINITION_END(fx_longlong)
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
HASH(sbyte)
|
||||
TO_STRING(sbyte, "%d")
|
||||
|
||||
BOOL_CONVERT(sbyte)
|
||||
GENERIC_CONVERT(sbyte, i16, i16)
|
||||
GENERIC_CONVERT(sbyte, u16, u16)
|
||||
GENERIC_CONVERT(sbyte, i32, i32)
|
||||
GENERIC_CONVERT(sbyte, u32, u32)
|
||||
GENERIC_CONVERT(sbyte, i64, i64)
|
||||
GENERIC_CONVERT(sbyte, u64, u64)
|
||||
GENERIC_CONVERT(sbyte, iptr, iptr)
|
||||
GENERIC_CONVERT(sbyte, uptr, uptr)
|
||||
GENERIC_CONVERT(sbyte, byte, byte)
|
||||
GENERIC_CONVERT(sbyte, short, short)
|
||||
GENERIC_CONVERT(sbyte, ushort, unsigned short)
|
||||
GENERIC_CONVERT(sbyte, int, int)
|
||||
GENERIC_CONVERT(sbyte, uint, unsigned int)
|
||||
GENERIC_CONVERT(sbyte, long, long)
|
||||
GENERIC_CONVERT(sbyte, ulong, unsigned long)
|
||||
GENERIC_CONVERT(sbyte, longlong, long long)
|
||||
GENERIC_CONVERT(sbyte, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(sbyte, size, size_t)
|
||||
GENERIC_CONVERT(sbyte, float, float)
|
||||
GENERIC_CONVERT(sbyte, double, double)
|
||||
|
||||
BINARY_OP(add, +, sbyte)
|
||||
BINARY_OP(subtract, -, sbyte)
|
||||
BINARY_OP(multiply, *, sbyte)
|
||||
BINARY_OP(divide, /, sbyte)
|
||||
PREFIX_OP(increment, ++, sbyte)
|
||||
PREFIX_OP(decrement, --, sbyte)
|
||||
IN_PLACE_OP(add_in_place, +=, sbyte)
|
||||
IN_PLACE_OP(subtract_in_place, -=, sbyte)
|
||||
IN_PLACE_OP(multiply_in_place, *=, sbyte)
|
||||
IN_PLACE_OP(divide_in_place, /=, sbyte)
|
||||
|
||||
COMPARE(sbyte, sbyte)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_sbyte)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.sbyte");
|
||||
FX_TYPE_CLASS(fx_sbyte_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(sbyte);
|
||||
FX_TYPE_DEFINITION_END(fx_sbyte)
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
HASH(short)
|
||||
TO_STRING(short, "%d")
|
||||
|
||||
BOOL_CONVERT(short)
|
||||
GENERIC_CONVERT(short, i16, i16)
|
||||
GENERIC_CONVERT(short, u16, u16)
|
||||
GENERIC_CONVERT(short, i32, i32)
|
||||
GENERIC_CONVERT(short, u32, u32)
|
||||
GENERIC_CONVERT(short, i64, i64)
|
||||
GENERIC_CONVERT(short, u64, u64)
|
||||
GENERIC_CONVERT(short, iptr, iptr)
|
||||
GENERIC_CONVERT(short, uptr, uptr)
|
||||
GENERIC_CONVERT(short, sbyte, sbyte)
|
||||
GENERIC_CONVERT(short, byte, byte)
|
||||
GENERIC_CONVERT(short, ushort, unsigned short)
|
||||
GENERIC_CONVERT(short, int, int)
|
||||
GENERIC_CONVERT(short, uint, unsigned int)
|
||||
GENERIC_CONVERT(short, long, long)
|
||||
GENERIC_CONVERT(short, ulong, unsigned long)
|
||||
GENERIC_CONVERT(short, longlong, long long)
|
||||
GENERIC_CONVERT(short, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(short, size, size_t)
|
||||
GENERIC_CONVERT(short, float, float)
|
||||
GENERIC_CONVERT(short, double, double)
|
||||
|
||||
BINARY_OP(add, +, short)
|
||||
BINARY_OP(subtract, -, short)
|
||||
BINARY_OP(multiply, *, short)
|
||||
BINARY_OP(divide, /, short)
|
||||
PREFIX_OP(increment, ++, short)
|
||||
PREFIX_OP(decrement, --, short)
|
||||
IN_PLACE_OP(add_in_place, +=, short)
|
||||
IN_PLACE_OP(subtract_in_place, -=, short)
|
||||
IN_PLACE_OP(multiply_in_place, *=, short)
|
||||
IN_PLACE_OP(divide_in_place, /=, short)
|
||||
|
||||
COMPARE(short, short)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_short)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.short");
|
||||
FX_TYPE_CLASS(fx_short_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(short);
|
||||
FX_TYPE_DEFINITION_END(fx_short)
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
HASH(size)
|
||||
TO_STRING(size, "%zu")
|
||||
|
||||
BOOL_CONVERT(size)
|
||||
GENERIC_CONVERT(size, i16, i16)
|
||||
GENERIC_CONVERT(size, u16, u16)
|
||||
GENERIC_CONVERT(size, i32, i32)
|
||||
GENERIC_CONVERT(size, u32, u32)
|
||||
GENERIC_CONVERT(size, i64, i64)
|
||||
GENERIC_CONVERT(size, u64, u64)
|
||||
GENERIC_CONVERT(size, iptr, iptr)
|
||||
GENERIC_CONVERT(size, uptr, uptr)
|
||||
GENERIC_CONVERT(size, sbyte, sbyte)
|
||||
GENERIC_CONVERT(size, byte, byte)
|
||||
GENERIC_CONVERT(size, short, short)
|
||||
GENERIC_CONVERT(size, ushort, unsigned short)
|
||||
GENERIC_CONVERT(size, int, int)
|
||||
GENERIC_CONVERT(size, uint, unsigned int)
|
||||
GENERIC_CONVERT(size, long, long)
|
||||
GENERIC_CONVERT(size, ulong, unsigned long)
|
||||
GENERIC_CONVERT(size, longlong, long long)
|
||||
GENERIC_CONVERT(size, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(size, float, float)
|
||||
GENERIC_CONVERT(size, double, double)
|
||||
|
||||
BINARY_OP(add, +, size)
|
||||
BINARY_OP(subtract, -, size)
|
||||
BINARY_OP(multiply, *, size)
|
||||
BINARY_OP(divide, /, size)
|
||||
PREFIX_OP(increment, ++, size)
|
||||
PREFIX_OP(decrement, --, size)
|
||||
IN_PLACE_OP(add_in_place, +=, size)
|
||||
IN_PLACE_OP(subtract_in_place, -=, size)
|
||||
IN_PLACE_OP(multiply_in_place, *=, size)
|
||||
IN_PLACE_OP(divide_in_place, /=, size)
|
||||
|
||||
COMPARE(size, size_t)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_size)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_float) = to_float;
|
||||
FX_INTERFACE_ENTRY(c_to_double) = to_double;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_NAME("fx.size");
|
||||
FX_TYPE_CLASS(fx_size_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(size_t);
|
||||
FX_TYPE_DEFINITION_END(fx_size)
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/convertible.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
#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
|
||||
}
|
||||
|
||||
HASH(u16)
|
||||
TO_STRING(u16, "%" PRIu16)
|
||||
|
||||
BOOL_CONVERT(u16)
|
||||
GENERIC_CONVERT(u16, i16, i16)
|
||||
GENERIC_CONVERT(u16, i32, i32)
|
||||
GENERIC_CONVERT(u16, u32, u32)
|
||||
GENERIC_CONVERT(u16, i64, i64)
|
||||
GENERIC_CONVERT(u16, u64, u64)
|
||||
GENERIC_CONVERT(u16, iptr, iptr)
|
||||
GENERIC_CONVERT(u16, uptr, uptr)
|
||||
GENERIC_CONVERT(u16, sbyte, sbyte)
|
||||
GENERIC_CONVERT(u16, byte, byte)
|
||||
GENERIC_CONVERT(u16, short, short)
|
||||
GENERIC_CONVERT(u16, ushort, unsigned short)
|
||||
GENERIC_CONVERT(u16, int, int)
|
||||
GENERIC_CONVERT(u16, uint, unsigned int)
|
||||
GENERIC_CONVERT(u16, long, long)
|
||||
GENERIC_CONVERT(u16, ulong, unsigned long)
|
||||
GENERIC_CONVERT(u16, longlong, long long)
|
||||
GENERIC_CONVERT(u16, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(u16, size, size_t)
|
||||
GENERIC_CONVERT(u16, float, float)
|
||||
GENERIC_CONVERT(u16, double, double)
|
||||
|
||||
BINARY_OP(add, +, u16)
|
||||
BINARY_OP(subtract, -, u16)
|
||||
BINARY_OP(multiply, *, u16)
|
||||
BINARY_OP(divide, /, u16)
|
||||
PREFIX_OP(increment, ++, u16)
|
||||
PREFIX_OP(decrement, --, u16)
|
||||
IN_PLACE_OP(add_in_place, +=, u16)
|
||||
IN_PLACE_OP(subtract_in_place, -=, u16)
|
||||
IN_PLACE_OP(multiply_in_place, *=, u16)
|
||||
IN_PLACE_OP(divide_in_place, /=, u16)
|
||||
|
||||
COMPARE(u16, u16)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_u16)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.u16");
|
||||
FX_TYPE_CLASS(fx_u16_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(u16);
|
||||
FX_TYPE_DEFINITION_END(fx_u16)
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/convertible.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
#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
|
||||
}
|
||||
|
||||
HASH(u32)
|
||||
TO_STRING(u32, "%" PRIu32)
|
||||
|
||||
BOOL_CONVERT(u32)
|
||||
GENERIC_CONVERT(u32, i16, i16)
|
||||
GENERIC_CONVERT(u32, u16, u16)
|
||||
GENERIC_CONVERT(u32, i32, i32)
|
||||
GENERIC_CONVERT(u32, i64, i64)
|
||||
GENERIC_CONVERT(u32, u64, u64)
|
||||
GENERIC_CONVERT(u32, iptr, iptr)
|
||||
GENERIC_CONVERT(u32, uptr, uptr)
|
||||
GENERIC_CONVERT(u32, sbyte, sbyte)
|
||||
GENERIC_CONVERT(u32, byte, byte)
|
||||
GENERIC_CONVERT(u32, short, short)
|
||||
GENERIC_CONVERT(u32, ushort, unsigned short)
|
||||
GENERIC_CONVERT(u32, int, int)
|
||||
GENERIC_CONVERT(u32, uint, unsigned int)
|
||||
GENERIC_CONVERT(u32, long, long)
|
||||
GENERIC_CONVERT(u32, ulong, unsigned long)
|
||||
GENERIC_CONVERT(u32, longlong, long long)
|
||||
GENERIC_CONVERT(u32, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(u32, size, size_t)
|
||||
GENERIC_CONVERT(u32, float, float)
|
||||
GENERIC_CONVERT(u32, double, double)
|
||||
|
||||
BINARY_OP(add, +, u32)
|
||||
BINARY_OP(subtract, -, u32)
|
||||
BINARY_OP(multiply, *, u32)
|
||||
BINARY_OP(divide, /, u32)
|
||||
PREFIX_OP(increment, ++, u32)
|
||||
PREFIX_OP(decrement, --, u32)
|
||||
IN_PLACE_OP(add_in_place, +=, u32)
|
||||
IN_PLACE_OP(subtract_in_place, -=, u32)
|
||||
IN_PLACE_OP(multiply_in_place, *=, u32)
|
||||
IN_PLACE_OP(divide_in_place, /=, u32)
|
||||
|
||||
COMPARE(u32, u32)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_u32)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.u32");
|
||||
FX_TYPE_CLASS(fx_u32_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(u32);
|
||||
FX_TYPE_DEFINITION_END(fx_u32)
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/convertible.h>
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
/* 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
|
||||
}
|
||||
|
||||
HASH(u64)
|
||||
TO_STRING(u64, "%" PRIu64)
|
||||
|
||||
BOOL_CONVERT(u64)
|
||||
GENERIC_CONVERT(u64, i16, i16)
|
||||
GENERIC_CONVERT(u64, u16, u16)
|
||||
GENERIC_CONVERT(u64, i32, i32)
|
||||
GENERIC_CONVERT(u64, u32, u32)
|
||||
GENERIC_CONVERT(u64, i64, i64)
|
||||
GENERIC_CONVERT(u64, iptr, iptr)
|
||||
GENERIC_CONVERT(u64, uptr, uptr)
|
||||
GENERIC_CONVERT(u64, sbyte, sbyte)
|
||||
GENERIC_CONVERT(u64, byte, byte)
|
||||
GENERIC_CONVERT(u64, short, short)
|
||||
GENERIC_CONVERT(u64, ushort, unsigned short)
|
||||
GENERIC_CONVERT(u64, int, int)
|
||||
GENERIC_CONVERT(u64, uint, unsigned int)
|
||||
GENERIC_CONVERT(u64, long, long)
|
||||
GENERIC_CONVERT(u64, ulong, unsigned long)
|
||||
GENERIC_CONVERT(u64, longlong, long long)
|
||||
GENERIC_CONVERT(u64, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(u64, size, size_t)
|
||||
GENERIC_CONVERT(u64, float, float)
|
||||
GENERIC_CONVERT(u64, double, double)
|
||||
|
||||
BINARY_OP(add, +, u64)
|
||||
BINARY_OP(subtract, -, u64)
|
||||
BINARY_OP(multiply, *, u64)
|
||||
BINARY_OP(divide, /, u64)
|
||||
PREFIX_OP(increment, ++, u64)
|
||||
PREFIX_OP(decrement, --, u64)
|
||||
IN_PLACE_OP(add_in_place, +=, u64)
|
||||
IN_PLACE_OP(subtract_in_place, -=, u64)
|
||||
IN_PLACE_OP(multiply_in_place, *=, u64)
|
||||
IN_PLACE_OP(divide_in_place, /=, u64)
|
||||
|
||||
COMPARE(u64, u64)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_u64)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
FX_INTERFACE_ENTRY(c_to_u32) = to_u32;
|
||||
FX_INTERFACE_ENTRY(c_to_i64) = to_i64;
|
||||
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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.u64");
|
||||
FX_TYPE_CLASS(fx_u64_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(u64);
|
||||
FX_TYPE_DEFINITION_END(fx_u64)
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
HASH(uint)
|
||||
TO_STRING(uint, "%u")
|
||||
|
||||
BOOL_CONVERT(uint)
|
||||
GENERIC_CONVERT(uint, i16, i16)
|
||||
GENERIC_CONVERT(uint, u16, u16)
|
||||
GENERIC_CONVERT(uint, i32, i32)
|
||||
GENERIC_CONVERT(uint, u32, u32)
|
||||
GENERIC_CONVERT(uint, i64, i64)
|
||||
GENERIC_CONVERT(uint, u64, u64)
|
||||
GENERIC_CONVERT(uint, iptr, iptr)
|
||||
GENERIC_CONVERT(uint, uptr, uptr)
|
||||
GENERIC_CONVERT(uint, sbyte, sbyte)
|
||||
GENERIC_CONVERT(uint, byte, byte)
|
||||
GENERIC_CONVERT(uint, short, short)
|
||||
GENERIC_CONVERT(uint, ushort, unsigned short)
|
||||
GENERIC_CONVERT(uint, int, int)
|
||||
GENERIC_CONVERT(uint, long, long)
|
||||
GENERIC_CONVERT(uint, ulong, unsigned long)
|
||||
GENERIC_CONVERT(uint, longlong, long long)
|
||||
GENERIC_CONVERT(uint, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(uint, size, size_t)
|
||||
GENERIC_CONVERT(uint, float, float)
|
||||
GENERIC_CONVERT(uint, double, double)
|
||||
|
||||
BINARY_OP(add, +, uint)
|
||||
BINARY_OP(subtract, -, uint)
|
||||
BINARY_OP(multiply, *, uint)
|
||||
BINARY_OP(divide, /, uint)
|
||||
PREFIX_OP(increment, ++, uint)
|
||||
PREFIX_OP(decrement, --, uint)
|
||||
IN_PLACE_OP(add_in_place, +=, uint)
|
||||
IN_PLACE_OP(subtract_in_place, -=, uint)
|
||||
IN_PLACE_OP(multiply_in_place, *=, uint)
|
||||
IN_PLACE_OP(divide_in_place, /=, uint)
|
||||
|
||||
COMPARE(uint, unsigned int)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_uint)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.uint");
|
||||
FX_TYPE_CLASS(fx_uint_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(unsigned int);
|
||||
FX_TYPE_DEFINITION_END(fx_uint)
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
HASH(ulong)
|
||||
TO_STRING(ulong, "%lu")
|
||||
|
||||
BOOL_CONVERT(ulong)
|
||||
GENERIC_CONVERT(ulong, i16, i16)
|
||||
GENERIC_CONVERT(ulong, u16, u16)
|
||||
GENERIC_CONVERT(ulong, i32, i32)
|
||||
GENERIC_CONVERT(ulong, u32, u32)
|
||||
GENERIC_CONVERT(ulong, i64, i64)
|
||||
GENERIC_CONVERT(ulong, u64, u64)
|
||||
GENERIC_CONVERT(ulong, iptr, iptr)
|
||||
GENERIC_CONVERT(ulong, uptr, uptr)
|
||||
GENERIC_CONVERT(ulong, sbyte, sbyte)
|
||||
GENERIC_CONVERT(ulong, byte, byte)
|
||||
GENERIC_CONVERT(ulong, short, short)
|
||||
GENERIC_CONVERT(ulong, ushort, unsigned short)
|
||||
GENERIC_CONVERT(ulong, int, int)
|
||||
GENERIC_CONVERT(ulong, uint, unsigned int)
|
||||
GENERIC_CONVERT(ulong, long, long)
|
||||
GENERIC_CONVERT(ulong, longlong, long long)
|
||||
GENERIC_CONVERT(ulong, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(ulong, size, size_t)
|
||||
GENERIC_CONVERT(ulong, float, float)
|
||||
GENERIC_CONVERT(ulong, double, double)
|
||||
|
||||
BINARY_OP(add, +, ulong)
|
||||
BINARY_OP(subtract, -, ulong)
|
||||
BINARY_OP(multiply, *, ulong)
|
||||
BINARY_OP(divide, /, ulong)
|
||||
PREFIX_OP(increment, ++, ulong)
|
||||
PREFIX_OP(decrement, --, ulong)
|
||||
IN_PLACE_OP(add_in_place, +=, ulong)
|
||||
IN_PLACE_OP(subtract_in_place, -=, ulong)
|
||||
IN_PLACE_OP(multiply_in_place, *=, ulong)
|
||||
IN_PLACE_OP(divide_in_place, /=, ulong)
|
||||
|
||||
COMPARE(ulong, unsigned long)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_ulong)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.ulong");
|
||||
FX_TYPE_CLASS(fx_ulong_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(unsigned long);
|
||||
FX_TYPE_DEFINITION_END(fx_ulong)
|
||||
@@ -0,0 +1,103 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
HASH(ulonglong)
|
||||
TO_STRING(ulonglong, "%llu")
|
||||
|
||||
BOOL_CONVERT(ulonglong)
|
||||
GENERIC_CONVERT(ulonglong, i16, i16)
|
||||
GENERIC_CONVERT(ulonglong, u16, u16)
|
||||
GENERIC_CONVERT(ulonglong, i32, i32)
|
||||
GENERIC_CONVERT(ulonglong, u32, u32)
|
||||
GENERIC_CONVERT(ulonglong, i64, i64)
|
||||
GENERIC_CONVERT(ulonglong, u64, u64)
|
||||
GENERIC_CONVERT(ulonglong, iptr, iptr)
|
||||
GENERIC_CONVERT(ulonglong, uptr, uptr)
|
||||
GENERIC_CONVERT(ulonglong, sbyte, sbyte)
|
||||
GENERIC_CONVERT(ulonglong, byte, byte)
|
||||
GENERIC_CONVERT(ulonglong, short, short)
|
||||
GENERIC_CONVERT(ulonglong, ushort, unsigned short)
|
||||
GENERIC_CONVERT(ulonglong, int, int)
|
||||
GENERIC_CONVERT(ulonglong, uint, unsigned int)
|
||||
GENERIC_CONVERT(ulonglong, long, long)
|
||||
GENERIC_CONVERT(ulonglong, ulong, unsigned long)
|
||||
GENERIC_CONVERT(ulonglong, longlong, long long)
|
||||
GENERIC_CONVERT(ulonglong, size, size_t)
|
||||
GENERIC_CONVERT(ulonglong, float, float)
|
||||
GENERIC_CONVERT(ulonglong, double, double)
|
||||
|
||||
BINARY_OP(add, +, ulonglong)
|
||||
BINARY_OP(subtract, -, ulonglong)
|
||||
BINARY_OP(multiply, *, ulonglong)
|
||||
BINARY_OP(divide, /, ulonglong)
|
||||
PREFIX_OP(increment, ++, ulonglong)
|
||||
PREFIX_OP(decrement, --, ulonglong)
|
||||
IN_PLACE_OP(add_in_place, +=, ulonglong)
|
||||
IN_PLACE_OP(subtract_in_place, -=, ulonglong)
|
||||
IN_PLACE_OP(multiply_in_place, *=, ulonglong)
|
||||
IN_PLACE_OP(divide_in_place, /=, ulonglong)
|
||||
|
||||
COMPARE(ulonglong, unsigned long long)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_ulonglong)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_size) = to_size;
|
||||
FX_INTERFACE_ENTRY(c_to_float) = to_float;
|
||||
FX_INTERFACE_ENTRY(c_to_double) = to_double;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.ulonglong");
|
||||
FX_TYPE_CLASS(fx_ulonglong_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(unsigned long long);
|
||||
FX_TYPE_DEFINITION_END(fx_ulonglong)
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
HASH(uptr)
|
||||
TO_STRING(uptr, "%" PRIuPTR)
|
||||
|
||||
BOOL_CONVERT(uptr)
|
||||
GENERIC_CONVERT(uptr, i16, i16)
|
||||
GENERIC_CONVERT(uptr, u16, u16)
|
||||
GENERIC_CONVERT(uptr, i32, i32)
|
||||
GENERIC_CONVERT(uptr, u32, u32)
|
||||
GENERIC_CONVERT(uptr, i64, i64)
|
||||
GENERIC_CONVERT(uptr, u64, u64)
|
||||
GENERIC_CONVERT(uptr, iptr, iptr)
|
||||
GENERIC_CONVERT(uptr, sbyte, sbyte)
|
||||
GENERIC_CONVERT(uptr, byte, byte)
|
||||
GENERIC_CONVERT(uptr, short, short)
|
||||
GENERIC_CONVERT(uptr, ushort, unsigned short)
|
||||
GENERIC_CONVERT(uptr, int, int)
|
||||
GENERIC_CONVERT(uptr, uint, unsigned int)
|
||||
GENERIC_CONVERT(uptr, long, long)
|
||||
GENERIC_CONVERT(uptr, ulong, unsigned long)
|
||||
GENERIC_CONVERT(uptr, longlong, long long)
|
||||
GENERIC_CONVERT(uptr, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(uptr, size, size_t)
|
||||
GENERIC_CONVERT(uptr, float, float)
|
||||
GENERIC_CONVERT(uptr, double, double)
|
||||
|
||||
BINARY_OP(add, +, uptr)
|
||||
BINARY_OP(subtract, -, uptr)
|
||||
BINARY_OP(multiply, *, uptr)
|
||||
BINARY_OP(divide, /, uptr)
|
||||
PREFIX_OP(increment, ++, uptr)
|
||||
PREFIX_OP(decrement, --, uptr)
|
||||
IN_PLACE_OP(add_in_place, +=, uptr)
|
||||
IN_PLACE_OP(subtract_in_place, -=, uptr)
|
||||
IN_PLACE_OP(multiply_in_place, *=, uptr)
|
||||
IN_PLACE_OP(divide_in_place, /=, uptr)
|
||||
|
||||
COMPARE(uptr, uptr)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_uptr)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.uptr");
|
||||
FX_TYPE_CLASS(fx_uptr_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(uptr);
|
||||
FX_TYPE_DEFINITION_END(fx_uptr)
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
#include "convert.h"
|
||||
|
||||
#include <fx/int.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
HASH(ushort)
|
||||
TO_STRING(ushort, "%u")
|
||||
|
||||
BOOL_CONVERT(ushort)
|
||||
GENERIC_CONVERT(ushort, i16, i16)
|
||||
GENERIC_CONVERT(ushort, u16, u16)
|
||||
GENERIC_CONVERT(ushort, i32, i32)
|
||||
GENERIC_CONVERT(ushort, u32, u32)
|
||||
GENERIC_CONVERT(ushort, i64, i64)
|
||||
GENERIC_CONVERT(ushort, u64, u64)
|
||||
GENERIC_CONVERT(ushort, iptr, iptr)
|
||||
GENERIC_CONVERT(ushort, uptr, uptr)
|
||||
GENERIC_CONVERT(ushort, sbyte, sbyte)
|
||||
GENERIC_CONVERT(ushort, byte, byte)
|
||||
GENERIC_CONVERT(ushort, short, short)
|
||||
GENERIC_CONVERT(ushort, int, int)
|
||||
GENERIC_CONVERT(ushort, uint, unsigned int)
|
||||
GENERIC_CONVERT(ushort, long, long)
|
||||
GENERIC_CONVERT(ushort, ulong, unsigned long)
|
||||
GENERIC_CONVERT(ushort, longlong, long long)
|
||||
GENERIC_CONVERT(ushort, ulonglong, unsigned long long)
|
||||
GENERIC_CONVERT(ushort, size, size_t)
|
||||
GENERIC_CONVERT(ushort, float, float)
|
||||
GENERIC_CONVERT(ushort, double, double)
|
||||
|
||||
BINARY_OP(add, +, ushort)
|
||||
BINARY_OP(subtract, -, ushort)
|
||||
BINARY_OP(multiply, *, ushort)
|
||||
BINARY_OP(divide, /, ushort)
|
||||
PREFIX_OP(increment, ++, ushort)
|
||||
PREFIX_OP(decrement, --, ushort)
|
||||
IN_PLACE_OP(add_in_place, +=, ushort)
|
||||
IN_PLACE_OP(subtract_in_place, -=, ushort)
|
||||
IN_PLACE_OP(multiply_in_place, *=, ushort)
|
||||
IN_PLACE_OP(divide_in_place, /=, ushort)
|
||||
|
||||
COMPARE(ushort, unsigned short)
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(fx_ushort)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = to_string;
|
||||
FX_INTERFACE_ENTRY(hash) = hash;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
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_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
||||
FX_INTERFACE_ENTRY(op_add) = add;
|
||||
FX_INTERFACE_ENTRY(op_subtract) = subtract;
|
||||
FX_INTERFACE_ENTRY(op_multiply) = multiply;
|
||||
FX_INTERFACE_ENTRY(op_divide) = divide;
|
||||
FX_INTERFACE_ENTRY(op_increment) = increment;
|
||||
FX_INTERFACE_ENTRY(op_decrement) = decrement;
|
||||
FX_INTERFACE_ENTRY(op_add_in_place) = add_in_place;
|
||||
FX_INTERFACE_ENTRY(op_subtract_in_place) = subtract_in_place;
|
||||
FX_INTERFACE_ENTRY(op_multiply_in_place) = multiply_in_place;
|
||||
FX_INTERFACE_ENTRY(op_divide_in_place) = divide_in_place;
|
||||
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_i16) = to_i16;
|
||||
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
||||
FX_INTERFACE_ENTRY(c_to_i32) = to_i32;
|
||||
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_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_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
||||
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_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||
FX_TYPE_NAME("fx.ushort");
|
||||
FX_TYPE_CLASS(fx_ushort_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(unsigned short);
|
||||
FX_TYPE_DEFINITION_END(fx_ushort)
|
||||
+2
-12
@@ -74,26 +74,16 @@ enum fx_status fx_iterator_move_next(const fx_iterator *it)
|
||||
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_iterator,
|
||||
FX_TYPE_ITERATOR,
|
||||
FX_ITERATOR_VALUE_NULL,
|
||||
FX_VALUE_EMPTY,
|
||||
it_get_value,
|
||||
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)
|
||||
{
|
||||
enum fx_status status = FX_ERR_NOT_SUPPORTED;
|
||||
|
||||
@@ -141,6 +141,10 @@ struct fx_namemap_entry *fx_namemap_first(const struct fx_namemap *map)
|
||||
struct __fx_namemap_entry,
|
||||
first,
|
||||
e_node);
|
||||
if (!entry) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (entry->e_flags == MAP_ENTRY_ITEM) {
|
||||
return (struct fx_namemap_entry *)entry;
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user