From 6353cbbb460b48d8e924e53c1c3eb8a82f9a16e8 Mon Sep 17 00:00:00 2001 From: Hugues Delorme Date: Sat, 2 Mar 2013 21:51:08 +0100 Subject: [PATCH] Merge manip structs in their respective 'main' struct --- src/c/libstl/stlb_read.c | 88 +++++++--------------- src/c/libstl/stlb_read.h | 34 +++------ src/c/libstl/stlb_write.c | 82 ++++++--------------- src/c/libstl/stlb_write.h | 33 +++------ src/c/stream.c | 70 +++++------------- src/c/stream.h | 24 ++---- src/c/task_control.c | 151 +++----------------------------------- src/c/task_control.h | 74 +++++-------------- 8 files changed, 125 insertions(+), 431 deletions(-) diff --git a/src/c/libstl/stlb_read.c b/src/c/libstl/stlb_read.c index 47eb2f7..2df7710 100644 --- a/src/c/libstl/stlb_read.c +++ b/src/c/libstl/stlb_read.c @@ -2,39 +2,6 @@ #include "../endian.h" -struct _internal_foug_stlb_geom_input -{ - void* cookie; - foug_stlb_geom_input_manip_t manip; -}; - -foug_stlb_geom_input_t* foug_stlb_geom_input_create(foug_malloc_func_t func, - void* data, - foug_stlb_geom_input_manip_t manip) -{ - foug_stlb_geom_input_t* geom; - - if (func == NULL) - return NULL; - geom = (*func)(sizeof(struct _internal_foug_stlb_geom_input)); - if (geom != NULL) { - geom->cookie = data; - geom->manip = manip; - } - return geom; -} - -void* foug_stlb_geom_input_get_cookie(const foug_stlb_geom_input_t* geom) -{ - return geom != NULL ? geom->cookie : NULL; -} - -void foug_stlb_geom_input_set_cookie(foug_stlb_geom_input_t* geom, void* data) -{ - if (geom != NULL) - geom->cookie = data; -} - static foug_bool_t foug_stlb_no_error(int code) { return code == FOUG_STLB_READ_NO_ERROR; @@ -48,7 +15,7 @@ static void foug_stlb_read_facets(foug_stlb_geom_input_t* geom_input, uint32_t buffer_offset; uint32_t i_facet; - if (geom_input->manip.process_next_triangle_func == NULL) + if (geom_input->process_next_triangle_func == NULL) return; buffer_offset = 0; @@ -58,12 +25,13 @@ static void foug_stlb_read_facets(foug_stlb_geom_input_t* geom_input, buffer_offset += FOUG_STLB_TRIANGLE_SIZE; /* Declare triangle */ - geom_input->manip.process_next_triangle_func(geom_input, &triangle); + geom_input->process_next_triangle_func(geom_input, &triangle); } } -int foug_stlb_read(foug_stlb_read_args_t args) +int foug_stlb_read(foug_stlb_read_args_t *args) { + foug_task_progress_t progress; uint8_t header_data[FOUG_STLB_HEADER_SIZE]; uint32_t total_facet_count; size_t buffer_facet_count; @@ -71,41 +39,39 @@ int foug_stlb_read(foug_stlb_read_args_t args) int error; size_t facet_count_read; - if (args.geom_input == NULL) - return FOUG_STLB_READ_NULL_GEOM_INPUT_ERROR; - if (args.stream == NULL) - return FOUG_STLB_READ_NULL_STREAM_ERROR; - if (args.buffer == NULL) + if (args->buffer == NULL) return FOUG_STLB_READ_NULL_BUFFER; - if (args.buffer_size < FOUG_STLB_MIN_CONTENTS_SIZE) + if (args->buffer_size < FOUG_STLB_MIN_CONTENTS_SIZE) return FOUG_STLB_READ_INVALID_BUFFER_SIZE_ERROR; /* Read header */ - if (foug_stream_read(args.stream, header_data, 1, FOUG_STLB_HEADER_SIZE) != FOUG_STLB_HEADER_SIZE) + if (foug_stream_read(&args->stream, header_data, 1, FOUG_STLB_HEADER_SIZE) != FOUG_STLB_HEADER_SIZE) return FOUG_STLB_READ_HEADER_WRONG_SIZE_ERROR; - if (args.geom_input->manip.process_header_func != NULL) - args.geom_input->manip.process_header_func(args.geom_input, header_data); + if (args->geom_input.process_header_func != NULL) + args->geom_input.process_header_func(&args->geom_input, header_data); /* Read facet count */ - if (foug_stream_read(args.stream, args.buffer, sizeof(uint32_t), 1) != 1) + if (foug_stream_read(&args->stream, args->buffer, sizeof(uint32_t), 1) != 1) return FOUG_STLB_READ_FACET_COUNT_ERROR; - total_facet_count = foug_decode_uint32_le(args.buffer); - if (args.geom_input->manip.begin_triangles_func != NULL) - args.geom_input->manip.begin_triangles_func(args.geom_input, total_facet_count); + total_facet_count = foug_decode_uint32_le(args->buffer); + if (args->geom_input.begin_triangles_func != NULL) + args->geom_input.begin_triangles_func(&args->geom_input, total_facet_count); - foug_task_control_reset(args.task_control); - foug_task_control_set_range(args.task_control, 0.f, (foug_real32_t)total_facet_count); + progress.range_min = 0.f; + progress.range_max = (foug_real32_t)total_facet_count; + progress.value = 0.f; + args->task_control.is_stop_requested = 0; /* Read triangles */ - buffer_facet_count = args.buffer_size / FOUG_STLB_TRIANGLE_SIZE; + buffer_facet_count = args->buffer_size / FOUG_STLB_TRIANGLE_SIZE; accum_facet_count_read = 0; error = FOUG_STLB_READ_NO_ERROR; while (foug_stlb_no_error(error) && accum_facet_count_read < total_facet_count) { - facet_count_read = foug_stream_read(args.stream, - args.buffer, FOUG_STLB_TRIANGLE_SIZE, buffer_facet_count); - if (foug_stream_error(args.stream) != 0) + facet_count_read = foug_stream_read(&args->stream, + args->buffer, FOUG_STLB_TRIANGLE_SIZE, buffer_facet_count); + if (foug_stream_error(&args->stream) != 0) error = FOUG_STLB_READ_STREAM_ERROR; else if (facet_count_read > 0) error = FOUG_STLB_READ_NO_ERROR; @@ -113,20 +79,20 @@ int foug_stlb_read(foug_stlb_read_args_t args) break; /* Exit if no facet to read */ if (foug_stlb_no_error(error)) { - foug_stlb_read_facets(args.geom_input, args.buffer, facet_count_read); + foug_stlb_read_facets(&args->geom_input, args->buffer, facet_count_read); accum_facet_count_read += facet_count_read; - if (foug_task_control_is_stop_requested(args.task_control)) { + if (args->task_control.is_stop_requested) { error = FOUG_STLB_READ_TASK_STOPPED_ERROR; - foug_task_control_handle_stop(args.task_control); + foug_task_control_handle_stop(&args->task_control); } else { - foug_task_control_set_progress(args.task_control, (foug_real32_t)accum_facet_count_read); + foug_task_control_set_progress(&args->task_control, &progress, accum_facet_count_read); } } } /* end while */ - if (foug_stlb_no_error(error) && args.geom_input->manip.end_triangles_func != NULL) - args.geom_input->manip.end_triangles_func(args.geom_input); + if (foug_stlb_no_error(error) && args->geom_input.end_triangles_func != NULL) + args->geom_input.end_triangles_func(&args->geom_input); if (foug_stlb_no_error(error) && accum_facet_count_read != total_facet_count) error = FOUG_STLB_READ_FACET_COUNT_ERROR; diff --git a/src/c/libstl/stlb_read.h b/src/c/libstl/stlb_read.h index 1c04c15..8221d5f 100644 --- a/src/c/libstl/stlb_read.h +++ b/src/c/libstl/stlb_read.h @@ -6,48 +6,32 @@ #include "../stream.h" #include "../task_control.h" -/* foug_stlb_geom_input : opaque structure */ -typedef struct _internal_foug_stlb_geom_input foug_stlb_geom_input_t; - -/* foug_stlb_geom_input_manip */ -typedef struct foug_stlb_geom_input_manip +/* foug_stlb_geom_input */ +typedef struct foug_stlb_geom_input foug_stlb_geom_input_t; +struct foug_stlb_geom_input { + void* cookie; void (*process_header_func)(foug_stlb_geom_input_t*, const uint8_t*); void (*begin_triangles_func)(foug_stlb_geom_input_t*, uint32_t); void (*process_next_triangle_func)(foug_stlb_geom_input_t*, const foug_stlb_triangle_t*); void (*end_triangles_func)(foug_stlb_geom_input_t*); -} foug_stlb_geom_input_manip_t; - -/* foug_stlb_geom_input : services */ - -FOUG_DATAEX_LIBSTL_EXPORT -foug_stlb_geom_input_t* foug_stlb_geom_input_create(foug_malloc_func_t func, - void* data, - foug_stlb_geom_input_manip_t manip); - -FOUG_DATAEX_LIBSTL_EXPORT -void* foug_stlb_geom_input_get_cookie(const foug_stlb_geom_input_t* geom); - -FOUG_DATAEX_LIBSTL_EXPORT -void foug_stlb_geom_input_set_cookie(foug_stlb_geom_input_t* geom, void* data); +}; /* foug_stlb_read_args */ typedef struct foug_stlb_read_args { - foug_stlb_geom_input_t* geom_input; - foug_stream_t* stream; - foug_task_control_t* task_control; + foug_stlb_geom_input_t geom_input; + foug_stream_t stream; + foug_task_control_t task_control; uint8_t* buffer; uint32_t buffer_size; } foug_stlb_read_args_t; FOUG_DATAEX_LIBSTL_EXPORT -int foug_stlb_read(foug_stlb_read_args_t args); +int foug_stlb_read(foug_stlb_read_args_t* args); /* Error codes returned by foug_stlb_read() */ #define FOUG_STLB_READ_NO_ERROR 0 -#define FOUG_STLB_READ_NULL_GEOM_INPUT_ERROR 1 -#define FOUG_STLB_READ_NULL_STREAM_ERROR 2 #define FOUG_STLB_READ_NULL_BUFFER 3 #define FOUG_STLB_READ_INVALID_BUFFER_SIZE_ERROR 4 #define FOUG_STLB_READ_HEADER_WRONG_SIZE_ERROR 5 diff --git a/src/c/libstl/stlb_write.c b/src/c/libstl/stlb_write.c index 1ef3628..559dae4 100644 --- a/src/c/libstl/stlb_write.c +++ b/src/c/libstl/stlb_write.c @@ -4,39 +4,6 @@ #include "../endian.h" #include -struct _internal_foug_stlb_geom_output -{ - void* cookie; - foug_stlb_geom_output_manip_t manip; -}; - -foug_stlb_geom_output_t* foug_stlb_geom_output_create(foug_malloc_func_t func, - void* data, - foug_stlb_geom_output_manip_t manip) -{ - foug_stlb_geom_output_t* geom; - - if (func == NULL) - return NULL; - geom = (*func)(sizeof(struct _internal_foug_stlb_geom_output)); - if (geom != NULL) { - geom->cookie = data; - geom->manip = manip; - } - return geom; -} - -void* foug_stlb_geom_output_get_cookie(const foug_stlb_geom_output_t* geom) -{ - return geom != NULL ? geom->cookie : NULL; -} - -void foug_stlb_geom_output_set_cookie(foug_stlb_geom_output_t* geom, void* data) -{ - if (geom != NULL) - geom->cookie = data; -} - static foug_bool_t foug_stlb_no_error(int code) { return code == FOUG_STLB_WRITE_NO_ERROR; @@ -51,20 +18,21 @@ static void foug_stlb_write_facets(const foug_stlb_geom_output_t* geom_output, uint32_t buffer_offset; uint32_t i_facet; - if (geom_output->manip.get_triangle_func == NULL) + if (geom_output->get_triangle_func == NULL) return; buffer_offset = 0; for (i_facet = ifacet_start; i_facet < (ifacet_start + facet_count); ++i_facet) { - geom_output->manip.get_triangle_func(geom_output, i_facet, &triangle); + geom_output->get_triangle_func(geom_output, i_facet, &triangle); memcpy(buffer + buffer_offset, &triangle, FOUG_STLB_TRIANGLE_SIZE); buffer_offset += FOUG_STLB_TRIANGLE_SIZE; } /* end for */ } -int foug_stlb_write(foug_stlb_write_args_t args) +int foug_stlb_write(foug_stlb_write_args_t *args) { + foug_task_progress_t progress; uint8_t header_data[FOUG_STLB_HEADER_SIZE]; uint32_t facet_count; uint32_t i_facet; @@ -72,41 +40,39 @@ int foug_stlb_write(foug_stlb_write_args_t args) uint32_t ifacet_start; int error; - if (args.geom_output == NULL) - return FOUG_STLB_WRITE_NULL_GEOM_OUTPUT_ERROR; - if (args.stream == NULL) - return FOUG_STLB_WRITE_NULL_STREAM_ERROR; - if (args.buffer == NULL) + if (args->buffer == NULL) return FOUG_STLB_WRITE_NULL_BUFFER_ERROR; - if (args.buffer_size < FOUG_STLB_MIN_CONTENTS_SIZE) + if (args->buffer_size < FOUG_STLB_MIN_CONTENTS_SIZE) return FOUG_STLB_WRITE_INVALID_BUFFER_SIZE_ERROR; - if (args.geom_output->manip.get_triangle_count_func == NULL) + if (args->geom_output.get_triangle_count_func == NULL) return FOUG_STLB_WRITE_NULL_GET_TRIANGLE_COUNT_FUNC; - if (args.geom_output->manip.get_triangle_func == NULL) + if (args->geom_output.get_triangle_func == NULL) return FOUG_STLB_WRITE_NULL_GET_TRIANGLE_FUNC; /* Write header */ - if (args.geom_output->manip.get_header_func != NULL) - args.geom_output->manip.get_header_func(args.geom_output, header_data); + if (args->geom_output.get_header_func != NULL) + args->geom_output.get_header_func(&args->geom_output, header_data); else memset(header_data, 0, FOUG_STLB_HEADER_SIZE); - if (foug_stream_write(args.stream, header_data, FOUG_STLB_HEADER_SIZE, 1) != 1) + if (foug_stream_write(&args->stream, header_data, FOUG_STLB_HEADER_SIZE, 1) != 1) return FOUG_STLB_WRITE_STREAM_ERROR; /* Write facet count */ - facet_count = args.geom_output->manip.get_triangle_count_func(args.geom_output); - foug_encode_uint32_le(facet_count, args.buffer); - if (foug_stream_write(args.stream, args.buffer, sizeof(uint32_t), 1) != 1) + facet_count = args->geom_output.get_triangle_count_func(&args->geom_output); + foug_encode_uint32_le(facet_count, args->buffer); + if (foug_stream_write(&args->stream, args->buffer, sizeof(uint32_t), 1) != 1) return FOUG_STLB_WRITE_STREAM_ERROR; - foug_task_control_reset(args.task_control); - foug_task_control_set_range(args.task_control, 0., (foug_real32_t)facet_count); + progress.range_min = 0.f; + progress.range_max = (foug_real32_t)facet_count; + progress.value = 0.f; + args->task_control.is_stop_requested = 0; /* Write triangles */ error = FOUG_STLB_WRITE_NO_ERROR; - buffer_facet_count = args.buffer_size / FOUG_STLB_TRIANGLE_SIZE; + buffer_facet_count = args->buffer_size / FOUG_STLB_TRIANGLE_SIZE; ifacet_start = 0; for (i_facet = 0; i_facet < facet_count && foug_stlb_no_error(error); @@ -115,11 +81,11 @@ int foug_stlb_write(foug_stlb_write_args_t args) /* Write to buffer */ if (buffer_facet_count > (facet_count - ifacet_start)) buffer_facet_count = facet_count - ifacet_start; - foug_stlb_write_facets(args.geom_output, args.buffer, ifacet_start, buffer_facet_count); + foug_stlb_write_facets(&args->geom_output, args->buffer, ifacet_start, buffer_facet_count); ifacet_start += buffer_facet_count; /* Write buffer to stream */ - if (foug_stream_write(args.stream, args.buffer, FOUG_STLB_TRIANGLE_SIZE, buffer_facet_count) + if (foug_stream_write(&args->stream, args->buffer, FOUG_STLB_TRIANGLE_SIZE, buffer_facet_count) != buffer_facet_count) { error = FOUG_STLB_WRITE_STREAM_ERROR; @@ -127,12 +93,12 @@ int foug_stlb_write(foug_stlb_write_args_t args) /* Task control */ if (foug_stlb_no_error(error)) { - if (foug_task_control_is_stop_requested(args.task_control)) { - foug_task_control_handle_stop(args.task_control); + if (args->task_control.is_stop_requested) { + foug_task_control_handle_stop(&args->task_control); error = FOUG_STLB_WRITE_TASK_STOPPED_ERROR; } else { - foug_task_control_set_progress(args.task_control, (foug_real32_t)(i_facet + 1)); + foug_task_control_set_progress(&args->task_control, &progress, i_facet + 1); } } } /* end for */ diff --git a/src/c/libstl/stlb_write.h b/src/c/libstl/stlb_write.h index 32a0e33..3619700 100644 --- a/src/c/libstl/stlb_write.h +++ b/src/c/libstl/stlb_write.h @@ -6,46 +6,31 @@ #include "../stream.h" #include "../task_control.h" -/* foug_stlb_geom_output : opaque structure */ -typedef struct _internal_foug_stlb_geom_output foug_stlb_geom_output_t; - -/* foug_stlb_geom_output_manip */ -typedef struct foug_stlb_geom_output_manip +/* foug_stlb_geom_output */ +typedef struct foug_stlb_geom_output foug_stlb_geom_output_t; +struct foug_stlb_geom_output { + void* cookie; void (*get_header_func)(const foug_stlb_geom_output_t*, uint8_t*); uint32_t (*get_triangle_count_func)(const foug_stlb_geom_output_t*); void (*get_triangle_func)(const foug_stlb_geom_output_t*, uint32_t, foug_stlb_triangle_t*); -} foug_stlb_geom_output_manip_t; - -/* foug_stlb_geom_output : services */ -FOUG_DATAEX_LIBSTL_EXPORT -foug_stlb_geom_output_t* foug_stlb_geom_output_create(foug_malloc_func_t func, - void* data, - foug_stlb_geom_output_manip_t manip); - -FOUG_DATAEX_LIBSTL_EXPORT -void* foug_stlb_geom_output_get_cookie(const foug_stlb_geom_output_t* geom); - -FOUG_DATAEX_LIBSTL_EXPORT -void foug_stlb_geom_output_set_cookie(foug_stlb_geom_output_t* geom, void* data); +}; /* foug_stlb_write_args */ typedef struct foug_stlb_write_args { - foug_stlb_geom_output_t* geom_output; - foug_stream_t* stream; - foug_task_control_t* task_control; + foug_stlb_geom_output_t geom_output; + foug_stream_t stream; + foug_task_control_t task_control; uint8_t* buffer; uint32_t buffer_size; } foug_stlb_write_args_t; FOUG_DATAEX_LIBSTL_EXPORT -int foug_stlb_write(foug_stlb_write_args_t args); +int foug_stlb_write(foug_stlb_write_args_t* args); /* Error codes returned by foug_stlb_write() */ #define FOUG_STLB_WRITE_NO_ERROR 0 -#define FOUG_STLB_WRITE_NULL_GEOM_OUTPUT_ERROR 1 -#define FOUG_STLB_WRITE_NULL_STREAM_ERROR 2 #define FOUG_STLB_WRITE_NULL_BUFFER_ERROR 3 #define FOUG_STLB_WRITE_NULL_GET_TRIANGLE_COUNT_FUNC 4 #define FOUG_STLB_WRITE_NULL_GET_TRIANGLE_FUNC 5 diff --git a/src/c/stream.c b/src/c/stream.c index 883cf4e..9d6ff58 100644 --- a/src/c/stream.c +++ b/src/c/stream.c @@ -4,34 +4,12 @@ #include #include -struct _internal_foug_stream -{ - void* cookie; - foug_stream_manip_t manip; -}; - -foug_stream_t* foug_stream_create(foug_malloc_func_t func, void* data, foug_stream_manip_t manip) -{ - foug_stream_t* stream; - - if (func == NULL) - return NULL; - stream = (*func)(sizeof(struct _internal_foug_stream)); - if (stream != NULL) { - stream->cookie = data; - stream->manip = manip; - } - return stream; -} - /*! - * \brief Create a null stream manipulator + * \brief Install a null stream */ -foug_stream_manip_t foug_stream_manip_null() +void foug_stream_set_null(foug_stream_t* stream) { - foug_stream_manip_t manip; - memset(&manip, 0, sizeof(foug_stream_manip_t)); - return manip; + memset(stream, 0, sizeof(foug_stream_t)); } static foug_bool_t foug_stream_stdio_at_end(foug_stream_t* stream) @@ -61,22 +39,21 @@ static size_t foug_stream_stdio_write(foug_stream_t* stream, } /*! - * \brief Create a stream manipulator for standard FILE* + * \brief Create a stream for standard FILE* */ -foug_stream_manip_t foug_stream_manip_stdio() +void foug_stream_set_stdio(foug_stream_t* stream, FILE* file) { - foug_stream_manip_t manip; - manip.at_end_func = foug_stream_stdio_at_end; - manip.error_func = foug_stream_stdio_error; - manip.read_func = foug_stream_stdio_read; - manip.write_func = foug_stream_stdio_write; - return manip; + stream->cookie = file; + stream->at_end_func = foug_stream_stdio_at_end; + stream->error_func = foug_stream_stdio_error; + stream->read_func = foug_stream_stdio_read; + stream->write_func = foug_stream_stdio_write; } foug_bool_t foug_stream_at_end(foug_stream_t* stream) { - if (stream != NULL && stream->manip.at_end_func != NULL) - return stream->manip.at_end_func(stream); + if (stream != NULL && stream->at_end_func != NULL) + return stream->at_end_func(stream); return 0; } @@ -90,8 +67,8 @@ foug_bool_t foug_stream_at_end(foug_stream_t* stream) */ int foug_stream_error(foug_stream_t* stream) { - if (stream != NULL && stream->manip.error_func != NULL) - return stream->manip.error_func(stream); + if (stream != NULL && stream->error_func != NULL) + return stream->error_func(stream); return 0; } @@ -112,8 +89,8 @@ int foug_stream_error(foug_stream_t* stream) */ size_t foug_stream_read(foug_stream_t* stream, void *ptr, size_t item_size, size_t item_count) { - if (stream != NULL && stream->manip.read_func != NULL) - return stream->manip.read_func(stream, ptr, item_size, item_count); + if (stream != NULL && stream->read_func != NULL) + return stream->read_func(stream, ptr, item_size, item_count); return 0; } @@ -127,18 +104,7 @@ size_t foug_stream_read(foug_stream_t* stream, void *ptr, size_t item_size, size */ size_t foug_stream_write(foug_stream_t* stream, const void *ptr, size_t item_size, size_t item_count) { - if (stream != NULL && stream->manip.write_func != NULL) - return stream->manip.write_func(stream, ptr, item_size, item_count); + if (stream != NULL && stream->write_func != NULL) + return stream->write_func(stream, ptr, item_size, item_count); return 0; } - -void* foug_stream_get_cookie(const foug_stream_t* stream) -{ - return stream != NULL ? stream->cookie : NULL; -} - -void foug_stream_set_cookie(foug_stream_t* stream, void* data) -{ - if (stream != NULL) - stream->cookie = data; -} diff --git a/src/c/stream.h b/src/c/stream.h index 67fafb4..d31451e 100644 --- a/src/c/stream.h +++ b/src/c/stream.h @@ -3,28 +3,24 @@ #include "global.h" #include "memory.h" +#include -/* foug_stream : opaque structure */ -typedef struct _internal_foug_stream foug_stream_t; - -/* foug_stream_manip */ -typedef struct foug_stream_manip +/* foug_stream */ +typedef struct foug_stream foug_stream_t; +struct foug_stream { + void* cookie; foug_bool_t (*at_end_func)(foug_stream_t*); int32_t (*error_func)(foug_stream_t*); size_t (*read_func)(foug_stream_t*, void*, size_t, size_t); size_t (*write_func)(foug_stream_t*, const void*, size_t, size_t); -} foug_stream_manip_t; +}; -FOUG_LIB_EXPORT foug_stream_manip_t foug_stream_manip_null(); -FOUG_LIB_EXPORT foug_stream_manip_t foug_stream_manip_stdio(); +FOUG_LIB_EXPORT void foug_stream_set_null(foug_stream_t* stream); +FOUG_LIB_EXPORT void foug_stream_set_stdio(foug_stream_t* stream, FILE* file); /* Services */ -FOUG_LIB_EXPORT foug_stream_t* foug_stream_create(foug_malloc_func_t func, - void* data, - foug_stream_manip_t manip); - FOUG_LIB_EXPORT foug_bool_t foug_stream_at_end(foug_stream_t* stream); FOUG_LIB_EXPORT int foug_stream_error(foug_stream_t* stream); @@ -39,8 +35,4 @@ FOUG_LIB_EXPORT size_t foug_stream_write(foug_stream_t* stream, size_t item_size, size_t item_count); -FOUG_LIB_EXPORT void* foug_stream_get_cookie(const foug_stream_t* stream); - -FOUG_LIB_EXPORT void foug_stream_set_cookie(foug_stream_t* stream, void* data); - #endif /* FOUG_C_STREAM_H */ diff --git a/src/c/task_control.c b/src/c/task_control.c index 4861efe..dadd5d8 100644 --- a/src/c/task_control.c +++ b/src/c/task_control.c @@ -5,131 +5,20 @@ /* foug_task_control */ -struct _internal_foug_task_control +foug_real32_t foug_task_progress_get_value_pc(const foug_task_progress_t *progress) { - foug_real32_t range_min; - foug_real32_t range_max; - foug_real32_t range_length; - int32_t step_id; - foug_real32_t progress_value; - foug_real32_t progress_threshold; - foug_bool_t is_stop_requested; - void* cookie; - foug_task_control_manip_t manip; -}; - -foug_task_control_t* foug_task_control_create(foug_malloc_func_t func, - void* data, foug_task_control_manip_t manip) -{ - foug_task_control_t* ctrl; - - if (func == NULL) - return NULL; - - ctrl = (*func)(sizeof(struct _internal_foug_task_control)); - if (ctrl != NULL) { - ctrl->range_min = -1.f; - ctrl->range_max = -2.f; - ctrl->range_length = -0.f; - ctrl->step_id = -1; - ctrl->progress_value = -1.f; - ctrl->progress_threshold = 0.01f; /* Notifies each percent only */ - ctrl->is_stop_requested = 0; - ctrl->cookie = data; - ctrl->manip = manip; - } - return ctrl; -} - -/* foug_task_control_manip */ - -void foug_task_control_manip_init(foug_task_control_manip_t* manip) -{ - if (manip != NULL) - memset(manip, 0, sizeof(foug_task_control_manip_t)); -} - -/* Range */ - -foug_real32_t foug_task_control_get_range_min(const foug_task_control_t* ctrl) -{ - return ctrl != NULL ? ctrl->range_min : -1.f; -} - -foug_real32_t foug_task_control_get_range_max(const foug_task_control_t* ctrl) -{ - return ctrl != NULL ? ctrl->range_max : -2.f; -} - -void foug_task_control_set_range(foug_task_control_t* ctrl, foug_real32_t min, foug_real32_t max) -{ - if (ctrl != NULL) { - ctrl->range_min = min; - ctrl->range_max = max; - ctrl->range_length = max - min; - } -} - -/* Step id */ - -int32_t foug_task_control_get_step_id(const foug_task_control_t* ctrl) -{ - return ctrl != NULL ? ctrl->step_id : -1; -} - -void foug_task_control_set_step_id(foug_task_control_t* ctrl, int32_t step_id) -{ - if (ctrl != NULL) - ctrl->step_id = step_id; -} - -/* Progress */ -foug_real32_t foug_task_control_get_progress_as_pc(const foug_task_control_t* ctrl) -{ - if (ctrl == NULL) + if (progress == NULL) return 0.f; - return fabs((ctrl->progress_value - ctrl->range_min) / ctrl->range_length); + return fabs((progress->value - progress->range_min) / (progress->range_max - progress->range_min)); } -foug_real32_t foug_task_control_get_progress(const foug_task_control_t* ctrl) +FOUG_LIB_EXPORT void foug_task_control_set_progress(foug_task_control_t* ctrl, + foug_task_progress_t *progress, + foug_real32_t value) { - return ctrl != NULL ? ctrl->progress_value : 0.f; -} - -void foug_task_control_set_progress(foug_task_control_t* ctrl, foug_real32_t v) -{ - if (ctrl == NULL) - return; - - if (fabs(v - ctrl->progress_value) > fabs(ctrl->progress_threshold * ctrl->range_length)) { - ctrl->progress_value = v; - if (ctrl->manip.handle_progress_update_func != NULL) - (*(ctrl->manip.handle_progress_update_func))(ctrl); - } -} - -foug_real32_t foug_task_control_get_progress_update_threshold(const foug_task_control_t* ctrl) -{ - return ctrl != NULL ? ctrl->progress_threshold : 0.01f; -} - -void foug_task_control_set_progress_update_threshold(foug_task_control_t* ctrl, foug_real32_t v) -{ - if (ctrl != NULL) - ctrl->progress_threshold = v; -} - -/* Reset */ - -void foug_task_control_reset(foug_task_control_t* ctrl) -{ - if (ctrl != NULL) { - ctrl->step_id = -1; - ctrl->progress_value = -1.f; - ctrl->range_min = -1.f; - ctrl->range_max = -2.f; - ctrl->is_stop_requested = 0; - } + progress->value = value; + if (ctrl->handle_progress_update_func != NULL) + ctrl->handle_progress_update_func(ctrl, progress); } /* Task stop */ @@ -144,25 +33,7 @@ void foug_task_control_handle_stop(foug_task_control_t* ctrl) { if (ctrl != NULL) { ctrl->is_stop_requested = 0; - if (ctrl->manip.handle_stop_func != NULL) - (*(ctrl->manip.handle_stop_func))(ctrl); + if (ctrl->handle_stop_func != NULL) + ctrl->handle_stop_func(ctrl); } } - -foug_bool_t foug_task_control_is_stop_requested(const foug_task_control_t* ctrl) -{ - return ctrl != NULL ? ctrl->is_stop_requested : 0; -} - -/* Cookie */ - -void* foug_task_control_get_cookie(const foug_task_control_t* ctrl) -{ - return ctrl != NULL ? ctrl->cookie : NULL; -} - -void foug_task_control_set_cookie(foug_task_control_t* ctrl, void* data) -{ - if (ctrl != NULL) - ctrl->cookie = data; -} diff --git a/src/c/task_control.h b/src/c/task_control.h index fe41ba4..7315eb4 100644 --- a/src/c/task_control.h +++ b/src/c/task_control.h @@ -4,69 +4,33 @@ #include "global.h" #include "memory.h" -/* foug_task_control : opaque structure */ -typedef struct _internal_foug_task_control foug_task_control_t; - - -/* foug_task_control_manip */ -typedef struct foug_task_control_manip +/* foug_task_progress */ +typedef struct foug_task_progress { - void (*handle_stop_func)(foug_task_control_t*); - void (*handle_progress_update_func)(foug_task_control_t*); -} foug_task_control_manip_t; - -FOUG_LIB_EXPORT void foug_task_control_manip_init(foug_task_control_manip_t* manip); - -/* Creation */ - -FOUG_LIB_EXPORT foug_task_control_t* foug_task_control_create(foug_malloc_func_t func, - void* data, - foug_task_control_manip_t manip); - -/* Range */ - -FOUG_LIB_EXPORT foug_real32_t foug_task_control_get_range_min(const foug_task_control_t* ctrl); -FOUG_LIB_EXPORT foug_real32_t foug_task_control_get_range_max(const foug_task_control_t* ctrl); - -FOUG_LIB_EXPORT void foug_task_control_set_range(foug_task_control_t* ctrl, - foug_real32_t min, - foug_real32_t max); - -/* Step id */ - -FOUG_LIB_EXPORT int32_t foug_task_control_get_step_id(const foug_task_control_t* ctrl); - -FOUG_LIB_EXPORT void foug_task_control_set_step_id(foug_task_control_t* ctrl, int32_t step_id); + foug_real32_t range_min; + foug_real32_t range_max; + foug_real32_t value; +} foug_task_progress_t; /* Progress */ +FOUG_LIB_EXPORT foug_real32_t foug_task_progress_get_value_pc(const foug_task_progress_t* progress); -FOUG_LIB_EXPORT foug_real32_t foug_task_control_get_progress_as_pc(const foug_task_control_t* ctrl); -FOUG_LIB_EXPORT foug_real32_t foug_task_control_get_progress(const foug_task_control_t* ctrl); +/* foug_task_control */ +typedef struct foug_task_control foug_task_control_t; +struct foug_task_control +{ + foug_bool_t is_stop_requested; + void* cookie; + void (*handle_stop_func)(foug_task_control_t*); + void (*handle_progress_update_func)(foug_task_control_t*, const foug_task_progress_t*); +}; -FOUG_LIB_EXPORT void foug_task_control_set_progress(foug_task_control_t* ctrl, foug_real32_t v); - -FOUG_LIB_EXPORT -foug_real32_t foug_task_control_get_progress_update_threshold(const foug_task_control_t* ctrl); - -FOUG_LIB_EXPORT void foug_task_control_set_progress_update_threshold(foug_task_control_t* ctrl, - foug_real32_t v); - -/* Reset */ - -FOUG_LIB_EXPORT void foug_task_control_reset(foug_task_control_t* ctrl); +FOUG_LIB_EXPORT void foug_task_control_set_progress(foug_task_control_t* ctrl, + foug_task_progress_t* progress, + foug_real32_t value); /* Task stop */ - FOUG_LIB_EXPORT void foug_task_control_async_stop(foug_task_control_t* ctrl); - FOUG_LIB_EXPORT void foug_task_control_handle_stop(foug_task_control_t* ctrl); -FOUG_LIB_EXPORT foug_bool_t foug_task_control_is_stop_requested(const foug_task_control_t* ctrl); - -/* Cookie */ - -FOUG_LIB_EXPORT void* foug_task_control_get_cookie(const foug_task_control_t* ctrl); - -FOUG_LIB_EXPORT void foug_task_control_set_cookie(foug_task_control_t* ctrl, void* data); - #endif /* FOUG_C_TASK_CONTROL_H */