Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
nakira974 committed Feb 23, 2024
1 parent ae8b2f0 commit ba69302
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 146 deletions.
58 changes: 29 additions & 29 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()


Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -70,6 +79,7 @@ event_handler* combine = event_handler_combine_create(b_topic, handleFuncs, nr);
```

#### Example

```c
int i = 0;

Expand Down
18 changes: 9 additions & 9 deletions headers/eventbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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

Expand Down
9 changes: 4 additions & 5 deletions headers/eventbus_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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);
}
Expand Down
19 changes: 10 additions & 9 deletions headers/eventhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 {
Expand All @@ -60,15 +61,15 @@ 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.
* @param thiz The event handler instance
* @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.
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion headers/eventhandler_combine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions headers/eventhandler_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ 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.
* @param thiz The event handler instance
* @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

Expand Down
2 changes: 1 addition & 1 deletion headers/eventhandler_limit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion headers/eventhandler_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
#ifndef _EVENT_HANDLER_PRINT_H_
#define _EVENT_HANDLER_PRINT_H_

#include "eventbus_typedef.h"
#include "eventhandler.h"

Expand All @@ -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

Expand Down
Loading

0 comments on commit ba69302

Please sign in to comment.