diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e11043..b1baeb0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,37 +3,37 @@ project(cbus C) set(CMAKE_C_STANDARD 99) if (CMAKE_GENERATOR MATCHES "Visual Studio") - set(CMAKE_ROOT_DIRECTORY ${CMAKE_SOURCE_DIR}) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build/) - #set(CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build/compiled) - #set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build/pdb) - set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build/archive) - set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build/lib) + set(CMAKE_ROOT_DIRECTORY ${CMAKE_SOURCE_DIR}) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build/) + #set(CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build/compiled) + #set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build/pdb) + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build/archive) + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build/lib) elseif (CMAKE_GENERATOR MATCHES "MinGW Makefiles") - set(CMAKE_SYSTEM_PROCESSOR x64) - set(CMAKE_ROOT_DIRECTORY ${CMAKE_SOURCE_DIR}) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/mingw32_x86_build/) - #set(CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/mingw32_x86_build/compiled) - #set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/mingw32_x86_build/pdb) - set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/mingw32_x86_build/archive) - set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/mingw32_x86_build/lib) - if (CMAKE_SYSTEM_NAME MATCHES "Linux") - if (CMAKE_C_COMPILER_ID STREQUAL "GNU") - set(CMAKE_ROOT_DIRECTORY ${CMAKE_SOURCE_DIR}) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/gnu_build/) - #set(CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/gnu_build/compiled) - #set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/gnu_build/pdb) - set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/gnu_build/archive) - set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/gnu_build/lib) - elseif (CMAKE_C_COMPILER_ID STREQUAL "Clang") - set(CMAKE_ROOT_DIRECTORY ${CMAKE_SOURCE_DIR}) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/clang_build/) - #set(CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/clang_build/compiled) - #set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/clang_build/pdb) - set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/clang_build/archive) - set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/clang_build/lib) - endif () + set(CMAKE_SYSTEM_PROCESSOR x64) + set(CMAKE_ROOT_DIRECTORY ${CMAKE_SOURCE_DIR}) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/mingw32_x86_build/) + #set(CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/mingw32_x86_build/compiled) + #set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/mingw32_x86_build/pdb) + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/mingw32_x86_build/archive) + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/mingw32_x86_build/lib) + if (CMAKE_SYSTEM_NAME MATCHES "Linux") + if (CMAKE_C_COMPILER_ID STREQUAL "GNU") + set(CMAKE_ROOT_DIRECTORY ${CMAKE_SOURCE_DIR}) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/gnu_build/) + #set(CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/gnu_build/compiled) + #set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/gnu_build/pdb) + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/gnu_build/archive) + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/gnu_build/lib) + elseif (CMAKE_C_COMPILER_ID STREQUAL "Clang") + set(CMAKE_ROOT_DIRECTORY ${CMAKE_SOURCE_DIR}) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/clang_build/) + #set(CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/clang_build/compiled) + #set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/clang_build/pdb) + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/clang_build/archive) + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/clang_build/lib) endif () + endif () endif () diff --git a/README.md b/README.md index d4c6469..8ef9afa 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,8 @@ ## Introduction -CBus is a lightweight event bus library for C. It provides a simple mechanism for event handling and propagation within your C applications. +CBus is a lightweight event bus library for C. It provides a simple mechanism for event handling and propagation within +your C applications. ## Usage @@ -31,7 +32,8 @@ int main(int argc, char* argv[]) { ## Getting Started -To start using CBUS in your project, simply include the necessary headers and link against the library. Don't forget to initialize the event handlers and the event bus before using them. +To start using CBUS in your project, simply include the necessary headers and link against the library. Don't forget to +initialize the event handlers and the event bus before using them. ```cmake # Add CBUS library to your CMakeLists.txt @@ -46,20 +48,27 @@ install(TARGETS cbus DESTINATION lib) ``` ### Using Event Handlers + #### 1. Print Handler + The `eventhandler_print` handler is used to print event data to the console. + ```c event_handler* print = event_handler_print_create(a_topic); ``` #### 2. Limit Handler + The `eventhandler_limit` handler limits the number of times an event can be handled. + ```c event_handler* limit_print = event_handler_limit_create(print, 3); ``` #### 3. Combine Handler + The `eventhandler_combine` handler combines multiple event handlers into one. + ```c event_handler_handle_func handleFuncs[] = { event_handler_print_handle, @@ -70,6 +79,7 @@ event_handler* combine = event_handler_combine_create(b_topic, handleFuncs, nr); ``` #### Example + ```c int i = 0; diff --git a/headers/eventbus.h b/headers/eventbus.h index 662cd5d..72abd4d 100644 --- a/headers/eventbus.h +++ b/headers/eventbus.h @@ -23,7 +23,7 @@ struct _eventbus { * @param data The event data * @return 0 on success, -1 on failure */ - int (*post)(eventbus* thiz, topic_t topic, void* data); + int (*post)(eventbus *thiz, topic_t topic, void *data); /** * Subscribes a handler to the event bus. @@ -32,7 +32,7 @@ struct _eventbus { * @param context Additional context for the handler * @return 0 on success, -1 on failure */ - int (*subscribe)(eventbus* thiz, event_handler* handler, void* context); + int (*subscribe)(eventbus *thiz, event_handler *handler, void *context); /** * Unsubscribes a handler from the event bus. @@ -41,13 +41,13 @@ struct _eventbus { * @param context Additional context for the handler * @return 0 on success, -1 on failure */ - int (*unsubscribe)(eventbus* thiz, event_handler* handler, void* context); + int (*unsubscribe)(eventbus *thiz, event_handler *handler, void *context); /** * Destroys the event bus instance. * @param thiz The event bus instance to destroy */ - void (*destroy)(eventbus* thiz); + void (*destroy)(eventbus *thiz); /** * Extension field for future use. @@ -59,13 +59,13 @@ struct _eventbus { * Creates a new event bus instance. * @return A pointer to the created event bus */ -eventbus* eventbus_create(void); +eventbus *eventbus_create(void); /** * Destroys an event bus instance. * @param thiz The event bus instance to destroy */ -void eventbus_destroy(eventbus* thiz); +void eventbus_destroy(eventbus *thiz); /** * Posts an event to the event bus. @@ -74,7 +74,7 @@ void eventbus_destroy(eventbus* thiz); * @param data The event data * @return 0 on success, -1 on failure */ -int eventbus_post(eventbus* thiz, topic_t topic, void* data); +int eventbus_post(eventbus *thiz, topic_t topic, void *data); /** * Subscribes a handler to the event bus. @@ -83,7 +83,7 @@ int eventbus_post(eventbus* thiz, topic_t topic, void* data); * @param context Additional context for the handler * @return 0 on success, -1 on failure */ -int eventbus_subscribe(eventbus* thiz, event_handler* handler, void* context); +int eventbus_subscribe(eventbus *thiz, event_handler *handler, void *context); /** * Unsubscribes a handler from the event bus. @@ -92,7 +92,7 @@ int eventbus_subscribe(eventbus* thiz, event_handler* handler, void* context); * @param context Additional context for the handler * @return 0 on success, -1 on failure */ -int eventbus_unsubscribe(eventbus* thiz, event_handler* handler, void* context); +int eventbus_unsubscribe(eventbus *thiz, event_handler *handler, void *context); C_STMT_END diff --git a/headers/eventbus_utils.h b/headers/eventbus_utils.h index 154b98c..7a0eef2 100644 --- a/headers/eventbus_utils.h +++ b/headers/eventbus_utils.h @@ -16,8 +16,7 @@ C_STMT_BGN * @param b Pointer to the second value * @return Result of the comparison */ -inline int int_compare (void* a, void *b) -{ +inline int int_compare(void *a, void *b) { int result; __asm__ __volatile__ ( "sub %1, %0 \n\t" @@ -37,9 +36,9 @@ inline int int_compare (void* a, void *b) * @param that Pointer to the second string * @return Result of the comparison */ -inline int string_compare(void* thiz, void* that) { - const char* thiz_str = (const char*) thiz; - const char* that_str = (const char*) that; +inline int string_compare(void *thiz, void *that) { + const char *thiz_str = (const char *) thiz; + const char *that_str = (const char *) that; return strcmp(thiz_str, that_str); } diff --git a/headers/eventhandler.h b/headers/eventhandler.h index 7214435..840e4fc 100644 --- a/headers/eventhandler.h +++ b/headers/eventhandler.h @@ -16,14 +16,15 @@ struct _topic_t; /** Definition of a topic */ typedef struct _topic_t { /** The key associated with the topic */ - void* key; + void *key; + /** * Comparison function for topics. * @param thiz The topic itself * @param that Another topic to compare with * @return Result of the comparison */ - int (*compare)(void* thiz, void* that); + int (*compare)(void *thiz, void *that); } topic_t; /** Data structure for an event handler */ @@ -33,13 +34,13 @@ struct _event_handler; typedef struct _event_handler event_handler; /** Function pointer type for checking if an event should be handled */ -typedef int (*event_handler_should_handle_func)(event_handler* thiz, topic_t topic); +typedef int (*event_handler_should_handle_func)(event_handler *thiz, topic_t topic); /** Function pointer type for handling an event */ -typedef int (*event_handler_handle_func)(event_handler* thiz, topic_t topic, void* data, void* context); +typedef int (*event_handler_handle_func)(event_handler *thiz, topic_t topic, void *data, void *context); /** Function pointer type for destroying an event handler */ -typedef void (*event_handler_destroy_func)(event_handler* thiz); +typedef void (*event_handler_destroy_func)(event_handler *thiz); /** Structure representing an event handler */ struct _event_handler { @@ -60,7 +61,7 @@ struct _event_handler { * @param thiz The event handler instance * @return The topic associated with the event handler */ -void* event_handler_get_topic(event_handler* thiz); +void *event_handler_get_topic(event_handler *thiz); /** * Check if an event should be handled by the event handler. @@ -68,7 +69,7 @@ void* event_handler_get_topic(event_handler* thiz); * @param topic The topic of the event * @return 1 if the event should be handled, 0 otherwise */ -int event_handler_should_handle(event_handler* thiz, topic_t topic); +int event_handler_should_handle(event_handler *thiz, topic_t topic); /** * Handle an event using the event handler. @@ -78,13 +79,13 @@ int event_handler_should_handle(event_handler* thiz, topic_t topic); * @param context Additional context for the event * @return 0 on success, -1 on failure */ -int event_handler_handle(event_handler* thiz, topic_t topic, void* data, void* context); +int event_handler_handle(event_handler *thiz, topic_t topic, void *data, void *context); /** * Destroy an event handler and free up resources. * @param thiz The event handler instance to destroy */ -void event_handler_destroy(event_handler* thiz); +void event_handler_destroy(event_handler *thiz); C_STMT_END diff --git a/headers/eventhandler_combine.h b/headers/eventhandler_combine.h index 200eaf4..0a53c8d 100644 --- a/headers/eventhandler_combine.h +++ b/headers/eventhandler_combine.h @@ -17,7 +17,7 @@ C_STMT_BGN * @param size Size of the handle functions array * @return Pointer to the created combined event handler */ -event_handler* event_handler_combine_create(topic_t topic, +event_handler *event_handler_combine_create(topic_t topic, event_handler_handle_func handleFuncs[], int size); C_STMT_END diff --git a/headers/eventhandler_default.h b/headers/eventhandler_default.h index bb17611..6f8a95a 100644 --- a/headers/eventhandler_default.h +++ b/headers/eventhandler_default.h @@ -18,7 +18,7 @@ C_STMT_BGN * @param context Additional context for the event * @return 0 always */ -int event_handler_default_handle(event_handler* thiz, topic_t topic, void* data, void* context); +int event_handler_default_handle(event_handler *thiz, topic_t topic, void *data, void *context); /** * Default function to check if the event should be handled by the handler. @@ -26,13 +26,13 @@ int event_handler_default_handle(event_handler* thiz, topic_t topic, void* data, * @param topic The topic of the event * @return 1 always */ -int event_handler_default_should_handle(event_handler* thiz, topic_t topic); +int event_handler_default_should_handle(event_handler *thiz, topic_t topic); /** * Default function to destroy the event handler (no action performed). * @param thiz The event handler instance to destroy */ -void event_handler_default_destroy(event_handler* thiz); +void event_handler_default_destroy(event_handler *thiz); C_STMT_END diff --git a/headers/eventhandler_limit.h b/headers/eventhandler_limit.h index 0a15d33..9d42b36 100644 --- a/headers/eventhandler_limit.h +++ b/headers/eventhandler_limit.h @@ -16,7 +16,7 @@ C_STMT_BGN * @param maxLimit The maximum number of times the event can be handled * @return Pointer to the created limit event handler */ -event_handler* event_handler_limit_create(event_handler* proxyHandler, int maxLimit); +event_handler *event_handler_limit_create(event_handler *proxyHandler, int maxLimit); C_STMT_END diff --git a/headers/eventhandler_print.h b/headers/eventhandler_print.h index fd54133..a84d49c 100644 --- a/headers/eventhandler_print.h +++ b/headers/eventhandler_print.h @@ -5,6 +5,7 @@ */ #ifndef _EVENT_HANDLER_PRINT_H_ #define _EVENT_HANDLER_PRINT_H_ + #include "eventbus_typedef.h" #include "eventhandler.h" @@ -15,7 +16,7 @@ C_STMT_BGN * @param topic The topic for which the print handler is created * @return Pointer to the created print event handler */ -event_handler* event_handler_print_create(topic_t topic); +event_handler *event_handler_print_create(topic_t topic); C_STMT_END diff --git a/src/eventbus.c b/src/eventbus.c index 3779025..2e89ea4 100644 --- a/src/eventbus.c +++ b/src/eventbus.c @@ -5,11 +5,13 @@ */ #include "eventbus.h" -typedef enum {ENABLED = 1, DISABLED = 2} entry_mark; +typedef enum { + ENABLED = 1, DISABLED = 2 +} entry_mark; typedef struct _eventbus_handler_entry { entry_mark mark; - event_handler* proxy; - void* context; + event_handler *proxy; + void *context; } eventbus_handler_entry; #define EVENTBUS_EXT_MAX_SIZE 100 @@ -20,34 +22,34 @@ typedef struct _eventbus_ext { eventbus_handler_entry handle_pool[EVENTBUS_EXT_MAX_SIZE]; } eventbus_ext; -void eventbus_destroy(eventbus* thiz) { +void eventbus_destroy(eventbus *thiz) { int i = 0; return_if_fail(NULL != thiz); - eventbus_ext* ext = (eventbus_ext*) thiz->ext; + eventbus_ext *ext = (eventbus_ext *) thiz->ext; ext->used = 0; ext->enabled = 0; SAFE_FREE(thiz); } -int eventbus_post(eventbus* thiz, topic_t topic, void* data) { +int eventbus_post(eventbus *thiz, topic_t topic, void *data) { int i = 0; int successed = 0; return_val_if_fail(NULL != thiz && NULL != data, 0); - eventbus_ext* ext = (eventbus_ext*) thiz->ext; + eventbus_ext *ext = (eventbus_ext *) thiz->ext; - if(ext->enabled == 0) { + if (ext->enabled == 0) { return 0; } - for(i = 0; i < ext->used; i++) { - if(ext->handle_pool[i].mark == ENABLED) { + for (i = 0; i < ext->used; i++) { + if (ext->handle_pool[i].mark == ENABLED) { - if(event_handler_should_handle(ext->handle_pool[i].proxy, topic)) { + if (event_handler_should_handle(ext->handle_pool[i].proxy, topic)) { successed += event_handler_handle(ext->handle_pool[i].proxy, topic, data, ext->handle_pool[i].context); } } @@ -56,11 +58,11 @@ int eventbus_post(eventbus* thiz, topic_t topic, void* data) { return successed; } -int eventbus_subscribe(eventbus* thiz, event_handler* handler, void* context) { +int eventbus_subscribe(eventbus *thiz, event_handler *handler, void *context) { return_val_if_fail(NULL != thiz && NULL != handler, -1); - eventbus_ext* ext = (eventbus_ext*) thiz->ext; - if(ext->used >= EVENTBUS_EXT_MAX_SIZE) { + eventbus_ext *ext = (eventbus_ext *) thiz->ext; + if (ext->used >= EVENTBUS_EXT_MAX_SIZE) { return -1; } @@ -73,20 +75,20 @@ int eventbus_subscribe(eventbus* thiz, event_handler* handler, void* context) { return 0; } -int eventbus_unsubscribe(eventbus* thiz, event_handler* handler, void* context) { +int eventbus_unsubscribe(eventbus *thiz, event_handler *handler, void *context) { int i = 0; return_val_if_fail(NULL != thiz && NULL != handler, 0); - eventbus_ext* ext = (eventbus_ext*) thiz->ext; + eventbus_ext *ext = (eventbus_ext *) thiz->ext; - if(ext->enabled == 0) { + if (ext->enabled == 0) { return 0; } - for(i = 0; i < ext->used; i++) { - if(ext->handle_pool[i].mark == ENABLED) { - if(ext->handle_pool[i].proxy == handler) { + for (i = 0; i < ext->used; i++) { + if (ext->handle_pool[i].mark == ENABLED) { + if (ext->handle_pool[i].proxy == handler) { ext->handle_pool[i].mark = DISABLED; ext->enabled--; } @@ -96,12 +98,12 @@ int eventbus_unsubscribe(eventbus* thiz, event_handler* handler, void* context) return 0; } -eventbus* eventbus_create(void) { +eventbus *eventbus_create(void) { - eventbus* thiz = (eventbus*) malloc(sizeof(eventbus) + sizeof(eventbus_ext)); + eventbus *thiz = (eventbus *) malloc(sizeof(eventbus) + sizeof(eventbus_ext)); - if(NULL != thiz) { - eventbus_ext* ext = (eventbus_ext*)thiz->ext; + if (NULL != thiz) { + eventbus_ext *ext = (eventbus_ext *) thiz->ext; ext->used = 0; ext->enabled = 0; diff --git a/src/eventbus_test.c.1 b/src/eventbus_test.c.1 index dc0cd6d..08549dd 100644 --- a/src/eventbus_test.c.1 +++ b/src/eventbus_test.c.1 @@ -7,12 +7,12 @@ #include "eventhandler_print.h" #include "eventbus_utils.h" -int main(int argc, char* argv[]) { - topic_t a_topic = {.key = (void*) 1, .compare = &int_compare}; +int main(int argc, char *argv[]) { + topic_t a_topic = {.key = (void *) 1, .compare = &int_compare}; - event_handler* print = event_handler_print_create(a_topic); + event_handler *print = event_handler_print_create(a_topic); - eventbus* bus = eventbus_create(); + eventbus *bus = eventbus_create(); eventbus_subscribe(bus, print, NULL); eventbus_subscribe(bus, print, NULL); diff --git a/src/eventhandler.c b/src/eventhandler.c index d5729d5..8587e31 100644 --- a/src/eventhandler.c +++ b/src/eventhandler.c @@ -5,25 +5,25 @@ */ #include "eventhandler.h" -inline int event_handler_handle(event_handler* thiz, topic_t topic, void* data, void* context) { +inline int event_handler_handle(event_handler *thiz, topic_t topic, void *data, void *context) { return_val_if_fail(NULL != thiz && NULL != thiz->handle, -1); return thiz->handle(thiz, topic, data, context); } -inline int event_handler_should_handle(event_handler* thiz, topic_t topic) { +inline int event_handler_should_handle(event_handler *thiz, topic_t topic) { return_val_if_fail(NULL != thiz && NULL != thiz->should_handle, 0); return thiz->should_handle(thiz, topic); } -void event_handler_destroy(event_handler* thiz) { +void event_handler_destroy(event_handler *thiz) { return_if_fail(NULL != thiz && NULL != thiz->destroy); return thiz->destroy(thiz); } -void* event_handler_get_topic(event_handler* thiz) { +void *event_handler_get_topic(event_handler *thiz) { return_val_if_fail(NULL != thiz, NULL); return thiz->topic.key; diff --git a/src/eventhandler_combine.c b/src/eventhandler_combine.c index 5d4b5fb..6bcddaf 100644 --- a/src/eventhandler_combine.c +++ b/src/eventhandler_combine.c @@ -13,24 +13,24 @@ typedef struct _event_handler_combine_ext { int func_nr; } event_handler_combine_ext; -static int event_handler_combine_handle(event_handler* thiz, topic_t topic, void* data, void* context) { +static int event_handler_combine_handle(event_handler *thiz, topic_t topic, void *data, void *context) { int i = 0; - event_handler_combine_ext* ext = (event_handler_combine_ext*) thiz->ext; + event_handler_combine_ext *ext = (event_handler_combine_ext *) thiz->ext; printf("func_nr: %d\n", ext->func_nr); - for(i = 0; i < ext->func_nr; i++) { + for (i = 0; i < ext->func_nr; i++) { ext->handleFuncs[i](thiz, topic, data, context); } return 1; } -static void event_handler_combine_destroy(event_handler* thiz) { +static void event_handler_combine_destroy(event_handler *thiz) { SAFE_FREE(thiz); } -event_handler* event_handler_combine_create(topic_t topic, event_handler_handle_func handleFuncs[], int size) { +event_handler *event_handler_combine_create(topic_t topic, event_handler_handle_func handleFuncs[], int size) { int i = 0; int nr = size; @@ -39,12 +39,12 @@ event_handler* event_handler_combine_create(topic_t topic, event_handler_handle_ return_val_if_fail(NULL != handleFuncs, NULL); - event_handler* thiz = (event_handler*) malloc(sizeof(event_handler) + sizeof(event_handler_combine_ext)); - if(NULL != thiz) { + event_handler *thiz = (event_handler *) malloc(sizeof(event_handler) + sizeof(event_handler_combine_ext)); + if (NULL != thiz) { - event_handler_combine_ext* ext = (event_handler_combine_ext*) thiz->ext; + event_handler_combine_ext *ext = (event_handler_combine_ext *) thiz->ext; - for( i = 0; i < nr; i++) { + for (i = 0; i < nr; i++) { ext->handleFuncs[i] = handleFuncs[i]; } ext->func_nr = nr; diff --git a/src/eventhandler_default.c b/src/eventhandler_default.c index 7d1bdd6..9803d04 100644 --- a/src/eventhandler_default.c +++ b/src/eventhandler_default.c @@ -5,27 +5,27 @@ */ #include "eventhandler_default.h" -int event_handler_default_handle(event_handler* thiz, topic_t topic, void* data, void* context) { +int event_handler_default_handle(event_handler *thiz, topic_t topic, void *data, void *context) { return -1; } -int event_handler_default_should_handle(event_handler* thiz, topic_t topic) { +int event_handler_default_should_handle(event_handler *thiz, topic_t topic) { return_val_if_fail(NULL != thiz, 0); /* not of the same type */ - if(thiz->topic.compare != topic.compare) { + if (thiz->topic.compare != topic.compare) { return 0; } /* not with the same key */ - if(thiz->topic.compare(thiz->topic.key, topic.key) == 0) { + if (thiz->topic.compare(thiz->topic.key, topic.key) == 0) { return 1; } return 0; } -void event_handler_default_destroy(event_handler* thiz) { +void event_handler_default_destroy(event_handler *thiz) { SAFE_FREE(thiz); } diff --git a/src/eventhandler_limit.c b/src/eventhandler_limit.c index 276dd1e..263d2de 100644 --- a/src/eventhandler_limit.c +++ b/src/eventhandler_limit.c @@ -6,32 +6,32 @@ #include "eventhandler_limit.h" typedef struct _event_handler_limit_ext { - event_handler* proxy; + event_handler *proxy; int limit; int cur; } event_handler_limit_ext; -static int event_handler_limit_should_handle(event_handler* thiz, topic_t topic) { +static int event_handler_limit_should_handle(event_handler *thiz, topic_t topic) { - event_handler_limit_ext* ext = (event_handler_limit_ext*) thiz->ext; + event_handler_limit_ext *ext = (event_handler_limit_ext *) thiz->ext; - if(!event_handler_should_handle(ext->proxy, topic)) { + if (!event_handler_should_handle(ext->proxy, topic)) { return 0; } - if(ext->cur > ext->limit) { + if (ext->cur > ext->limit) { return 0; } return 1; } -static int event_handler_limit_handle(event_handler* thiz, topic_t topic, void* data, void* context) { +static int event_handler_limit_handle(event_handler *thiz, topic_t topic, void *data, void *context) { int ret = -1; return_val_if_fail(NULL != data, -1); - event_handler_limit_ext* ext = (event_handler_limit_ext*)thiz->ext; + event_handler_limit_ext *ext = (event_handler_limit_ext *) thiz->ext; printf("i am limit handler, now: %d print, limit: %d.\n", ext->cur, ext->limit); @@ -42,20 +42,20 @@ static int event_handler_limit_handle(event_handler* thiz, topic_t topic, void* return 0; } -static void event_handler_limit_destroy(event_handler* thiz) { +static void event_handler_limit_destroy(event_handler *thiz) { return_if_fail(NULL != thiz); SAFE_FREE(thiz); } -event_handler* event_handler_limit_create(event_handler* proxyhandler, int maxlimit) { +event_handler *event_handler_limit_create(event_handler *proxyhandler, int maxlimit) { return_val_if_fail(NULL != proxyhandler && 0 < maxlimit, NULL); - event_handler* thiz = (event_handler*) malloc(sizeof(event_handler) + sizeof(event_handler_limit_ext)); + event_handler *thiz = (event_handler *) malloc(sizeof(event_handler) + sizeof(event_handler_limit_ext)); - if(NULL != thiz) { - event_handler_limit_ext* ext = (event_handler_limit_ext*) thiz->ext; + if (NULL != thiz) { + event_handler_limit_ext *ext = (event_handler_limit_ext *) thiz->ext; ext->limit = maxlimit; ext->cur = 0; ext->proxy = proxyhandler; diff --git a/src/eventhandler_print.c b/src/eventhandler_print.c index fd506df..26afc45 100644 --- a/src/eventhandler_print.c +++ b/src/eventhandler_print.c @@ -6,20 +6,20 @@ #include "eventhandler_default.h" #include "eventhandler_print.h" -static int event_handler_print_handle(event_handler* thiz, topic_t topic, void* data, void* context) { +static int event_handler_print_handle(event_handler *thiz, topic_t topic, void *data, void *context) { return_val_if_fail(NULL != data || NULL != context, -1); printf("I am print handler.\n"); - printf("data: %s\n", (const char*) data); + printf("data: %s\n", (const char *) data); return 0; } -event_handler* event_handler_print_create(topic_t topic) { +event_handler *event_handler_print_create(topic_t topic) { - event_handler* thiz = (event_handler*) malloc(sizeof(event_handler)); + event_handler *thiz = (event_handler *) malloc(sizeof(event_handler)); - if(NULL != thiz) { + if (NULL != thiz) { thiz->topic = topic; thiz->should_handle = event_handler_default_should_handle; thiz->handle = event_handler_print_handle; diff --git a/src/eventhandler_test.c.1 b/src/eventhandler_test.c.1 index 356d4bf..bf086d4 100644 --- a/src/eventhandler_test.c.1 +++ b/src/eventhandler_test.c.1 @@ -12,51 +12,51 @@ void test_one(void) { int i = 0; - topic_t a_topic = {.key = (void*)1, .compare = &int_compare}; - - event_handler* print = event_handler_print_create(a_topic); + topic_t a_topic = {.key = (void *) 1, .compare = &int_compare}; - event_handler* limit_print = event_handler_limit_create(print, 3); + event_handler *print = event_handler_print_create(a_topic); - for( i = 0; i < 5; i++ ) { - if(event_handler_should_handle(print, a_topic)) { - event_handler_handle(print, a_topic, "hello", NULL); + event_handler *limit_print = event_handler_limit_create(print, 3); + + for (i = 0; i < 5; i++) { + if (event_handler_should_handle(print, a_topic)) { + event_handler_handle(print, a_topic, "hello", NULL); } - if(event_handler_should_handle(limit_print, a_topic)) { + if (event_handler_should_handle(limit_print, a_topic)) { event_handler_handle(limit_print, a_topic, "world", NULL); } } - + event_handler_destroy(print); event_handler_destroy(limit_print); } -int event_handler_print_handle(event_handler* thiz, topic_t topic, void* data, void* context) { +int event_handler_print_handle(event_handler *thiz, topic_t topic, void *data, void *context) { return_val_if_fail(NULL != data || NULL != context, -1); printf("I am print handler.\n"); - printf("data: %s\n", (const char*) data); + printf("data: %s\n", (const char *) data); return 0; } void test_two() { - + event_handler_handle_func handleFuncs[] = { - event_handler_print_handle, - event_handler_print_handle, - }; - - int nr = ARRAY_SIZE(handleFuncs); - topic_t b_topic = {.key=(void*) "test.topic", .compare = &string_compare }; - event_handler* combine = event_handler_combine_create(b_topic, handleFuncs, nr); - - event_handler_handle(combine, b_topic, "hello", NULL); - event_handler_destroy(combine); + event_handler_print_handle, + event_handler_print_handle, + }; + + int nr = ARRAY_SIZE(handleFuncs); + topic_t b_topic = {.key=(void *) "test.topic", .compare = &string_compare}; + event_handler *combine = event_handler_combine_create(b_topic, handleFuncs, nr); + + event_handler_handle(combine, b_topic, "hello", NULL); + event_handler_destroy(combine); } -int main(int argc, char* argv[]) { +int main(int argc, char *argv[]) { test_one(); test_two(); }