Skip to content

Commit f4354eb

Browse files
Merge pull request #2 from polesskiy-dev/move-from-macros-to-generic-void
Move from macros to generic void
2 parents 3fd114e + 5ff0cd7 commit f4354eb

File tree

22 files changed

+796
-749
lines changed

22 files changed

+796
-749
lines changed

.DS_Store

0 Bytes
Binary file not shown.

.github/workflows/ci.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ name: ci
22
on:
33
pull_request:
44
push:
5-
branches:
6-
[ main, master ]
5+
branches: [main, master]
76

87
jobs:
98
deploy-gh-pages:
@@ -22,10 +21,12 @@ jobs:
2221
2322
# Deploy the HTML documentation to GitHub Pages
2423
- name: GH Pages Deployment
25-
uses: peaceiris/actions-gh-pages@v3 # https://github.com/peaceiris/actions-gh-pages
24+
# https://github.com/peaceiris/actions-gh-pages
25+
uses: peaceiris/actions-gh-pages@v3
2626
with:
2727
github_token: ${{ secrets.GITHUB_TOKEN }}
28-
publish_dir: ./html #deploy ./html directory to the remote gh-pages branch.
28+
# deploy ./html directory to the remote gh-pages branch.
29+
publish_dir: ./html
2930
enable_jekyll: false
3031
allow_empty_commit: false
3132
force_orphan: true

src/active-object/active_object.c

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/active-object/active_object.h

Lines changed: 0 additions & 147 deletions
This file was deleted.

src/active-object/active_object_impl.h

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/active_object/active_object.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "./active_object.h"
2+
3+
void ActiveObject_Initialize(TActiveObject* me, const uint8_t id, TEvent* events, uint32_t capacity) {
4+
me->id = id;
5+
me->state = NULL;
6+
EventQueue_Initialize(&me->queue, events, capacity);
7+
}
8+
9+
void ActiveObject_Dispatch(TActiveObject* me, TEvent event) {
10+
EventQueue_Enqueue(&me->queue, event);
11+
}
12+
13+
TEvent ActiveObject_ProcessQueue(TActiveObject* me) {
14+
if (EventQueue_IsEmpty(&me->queue)) {
15+
return (TEvent){.sig = 0, .payload = NULL, .size = 0};
16+
};
17+
18+
return EventQueue_Dequeue(&me->queue);
19+
}

src/active_object/active_object.h

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

Comments
 (0)