|
| 1 | +/** |
| 2 | + * @file active_object.h |
| 3 | + * |
| 4 | + * @brief Active Object Design Pattern Implementation |
| 5 | + * @see events_queue.h for the underlying queue implementation. |
| 6 | + * |
| 7 | + * @details This header file provides the API for implementing an Active Object. |
| 8 | + * The Active Object pattern is used to decouple method execution from |
| 9 | + * method invocation in concurrent systems. The pattern uses asynchronous |
| 10 | + * messaging to delegate method invocation. It provides |
| 11 | + * a framework for handling complex states through state hooks, as well as |
| 12 | + * mechanisms for dispatching and processing messages from a queue. |
| 13 | + * Also, the base Active Object struct can be extended (inherited) to provide |
| 14 | + * user-defined functionality. |
| 15 | + * |
| 16 | + * ### Example: |
| 17 | + * @code |
| 18 | + * // Active Object inheritance |
| 19 | + * typedef enum { NO_STATE, STATE_1 = 1, STATES_MAX } TEST_STATE; // state names |
| 20 | + * typedef enum { NO_SIG, EVENT_SIG_1 = 1, EVENTS_MAX } TEST_EVENT_SIG; // event signals names |
| 21 | + * TEvent events[QUEUE_MAX_SIZE]; // events list for the queue |
| 22 | + * |
| 23 | + * // sub Active Object |
| 24 | + * typedef struct { |
| 25 | + * TActiveObject super; |
| 26 | + * bool additionalField; |
| 27 | + * } TSubActiveObject; |
| 28 | + * |
| 29 | + * // states description list |
| 30 | + * TState statesList[STATES_MAX] = { [STATE_1] = {.name = STATE_1, .onEnter = NULL, .onTraverse = NULL, .onExit = NULL} }; |
| 31 | + * |
| 32 | + * // sub Active Object constructor |
| 33 | + * void TSubActiveObject_Initialize(TSubActiveObject* me, const uint8_t id, TEvent* events, uint32_t capacity) { |
| 34 | + * ActiveObject_Initialize(&(me->super), id, events, capacity); |
| 35 | + * me->additionalField = false; |
| 36 | + * }} |
| 37 | + * @endcode |
| 38 | + * |
| 39 | + * @author apolisskyi |
| 40 | + */ |
| 41 | + |
| 42 | +#ifndef ACTIVE_OBJECT_H |
| 43 | +#define ACTIVE_OBJECT_H |
| 44 | + |
| 45 | +#include <stdbool.h> |
| 46 | +#include <stdint.h> |
| 47 | +#include <stddef.h> |
| 48 | + |
| 49 | +#include "../event_queue/event_queue.h" |
| 50 | + |
| 51 | +/** @brief Macro to create FSM entry point - inittial empty state. */ |
| 52 | +#define EMPTY_STATE ((TState){.name = 0}) |
| 53 | + |
| 54 | +/** @brief Macro to create invalid state indication */ |
| 55 | +#define INVALID_STATE ((TState){.name = -1}) |
| 56 | + |
| 57 | +/** @brief Function pointer type for state hooks. |
| 58 | + * |
| 59 | + * @param activeObject Pointer to the active object. |
| 60 | + * @param ctx Context pointer. |
| 61 | + * @return A boolean value to indicate hook success or failure result. |
| 62 | + */ |
| 63 | +typedef bool (*TStateHook)(void *const activeObject, void *const ctx); |
| 64 | + |
| 65 | +/** @brief Struct representing a single state of an active object. */ |
| 66 | +typedef struct { |
| 67 | + int32_t name; |
| 68 | + TStateHook onEnter; |
| 69 | + TStateHook onTraverse; |
| 70 | + TStateHook onExit; |
| 71 | +} TState; |
| 72 | + |
| 73 | +/** @brief Struct representing an active object. */ |
| 74 | +typedef struct { |
| 75 | + uint8_t id; |
| 76 | + const TState *state; |
| 77 | + TEventQueue queue; |
| 78 | +} TActiveObject; |
| 79 | + |
| 80 | +/** @brief Initialize an active object. |
| 81 | + * @note The events array must be allocated by the user. |
| 82 | + * |
| 83 | + * @param me Pointer to the active object. |
| 84 | + * @param id Object ID. |
| 85 | + * @param events Pointer to the event array. |
| 86 | + * @param capacity Capacity of the event queue. |
| 87 | + * |
| 88 | + * ### Example: |
| 89 | + * @code |
| 90 | + * TEvent eventArray[10]; |
| 91 | + * TActiveObject activeObject; |
| 92 | + * ActiveObject_Initialize(&activeObject, 1, eventArray, 10); |
| 93 | + * @endcode |
| 94 | + */ |
| 95 | +void ActiveObject_Initialize(TActiveObject* me, const uint8_t id, TEvent* events, uint32_t capacity); |
| 96 | + |
| 97 | +/** @brief Dispatch an event to the active object. |
| 98 | + * |
| 99 | + * @param me Pointer to the active object. |
| 100 | + * @param event The event to be dispatched. |
| 101 | + * |
| 102 | + * ### Example: |
| 103 | + * @code |
| 104 | + * TEvent myEvent = {1, NULL, 0}; |
| 105 | + * ActiveObject_Dispatch(&activeObject, myEvent); |
| 106 | + * @endcode |
| 107 | + */ |
| 108 | +void ActiveObject_Dispatch(TActiveObject* me, TEvent event); |
| 109 | + |
| 110 | +/** @brief Process the queue of the active object and return an event. |
| 111 | + * |
| 112 | + * @param me Pointer to the active object. |
| 113 | + * @return The next event from the queue. |
| 114 | + * |
| 115 | + * ### Example: |
| 116 | + * @code |
| 117 | + * TEvent nextEvent = ActiveObject_ProcessQueue(&activeObject); |
| 118 | + * @endcode |
| 119 | + */ |
| 120 | +TEvent ActiveObject_ProcessQueue(TActiveObject* me); |
| 121 | + |
| 122 | +#endif //ACTIVE_OBJECT_H |
0 commit comments