|
5 | 5 | // @see https://github.com/endurodave/DelegateMQ |
6 | 6 | // David Lafreniere, 2025. |
7 | 7 |
|
8 | | -/// @file |
9 | | -/// @brief DelegateMQ.h is a single include to obtain all delegate functionality. |
| 8 | +/// @file DelegateMQ.h |
| 9 | +/// @brief A single-include header for the complete DelegateMQ library functionality. |
10 | 10 | /// |
11 | | -/// A C++ delegate library capable of invoking any callable function either synchronously |
12 | | -/// or asynchronously on a user specified thread of control. It is also capable of calling |
13 | | -/// a function remotely over any transport protocol. |
14 | | -/// |
15 | | -/// Asynchronous function calls support both non-blocking and blocking modes with a timeout. |
16 | | -/// The library supports all types of target functions, including free functions, class |
17 | | -/// member functions, static class functions, lambdas, and `std::function`. It is capable of |
18 | | -/// handling any function signature, regardless of the number of arguments or return value. |
19 | | -/// All argument types are supported, including by value, pointers, pointers to pointers, |
20 | | -/// and references. The delegate library takes care of the intricate details of function |
21 | | -/// invocation across thread boundaries. Thread-safe delegate containers stores delegate |
22 | | -/// instances with a matching function signature. |
23 | | -/// |
24 | | -/// A delegate instance can be: |
| 11 | +/// @details |
| 12 | +/// DelegateMQ is a robust C++ delegate library that enables invoking any callable function |
| 13 | +/// (synchronously or asynchronously) on a specific user-defined thread of control. It also |
| 14 | +/// supports remote function invocation over any transport protocol. |
25 | 15 | /// |
26 | | -/// * Copied freely. |
27 | | -/// * Compared to same type delegatesand `nullptr`. |
28 | | -/// * Reassigned. |
29 | | -/// * Called. |
30 | | -/// |
31 | | -/// Typical use cases are: |
| 16 | +/// **Key Features:** |
| 17 | +/// * **Universal Target Support:** Binds to free functions, class member functions, static functions, |
| 18 | +/// lambdas, and `std::function`. |
| 19 | +/// * **Any Signature:** Handles any function signature with any number of arguments or return values. |
| 20 | +/// * **Argument Safety:** Supports all argument types (value, pointer, pointer-to-pointer, reference) |
| 21 | +/// and safely marshals them across thread boundaries for asynchronous calls. |
| 22 | +/// * **Thread Control:** Unlike `std::async` (which uses a random thread pool), DelegateMQ executes |
| 23 | +/// the target function on a *specific* destination thread you control. |
| 24 | +/// * **Remote Invocation:** Capable of serializing arguments and invoking functions across network |
| 25 | +/// boundaries (UDP, TCP, ZeroMQ, etc.). |
32 | 26 | /// |
33 | | -/// * Asynchronous Method Invocation(AMI) |
34 | | -/// * Publish / Subscribe(Observer) Pattern |
35 | | -/// * Anonymous, Asynchronous Thread - Safe Callbacks |
36 | | -/// * Event - Driven Programming |
37 | | -/// * Thread - Safe Asynchronous API |
38 | | -/// * Design Patterns(Active Object) |
| 27 | +/// **Delegate Capabilities:** |
| 28 | +/// A delegate instance behaves like a first-class object: |
| 29 | +/// * **Copyable:** Can be copied freely. |
| 30 | +/// * **Comparable:** Supports equality checks against other delegates or `nullptr`. |
| 31 | +/// * **Assignable:** Can be reassigned at runtime. |
| 32 | +/// * **Callable:** Invoked via `operator()`. |
39 | 33 | /// |
40 | | -/// The delegate library's asynchronous features differ from `std::async` in that the |
41 | | -/// caller-specified thread of control is used to invoke the target function bound to |
42 | | -/// the delegate, rather than a random thread from the thread pool. The asynchronous |
43 | | -/// variants copy the argument data into the event queue, ensuring safe transport to the |
44 | | -/// destination thread, regardless of the argument type. This approach provides 'fire and |
45 | | -/// forget' functionality, allowing the caller to avoid waiting or worrying about |
46 | | -/// out-of-scope stack variables being accessed by the target thread. |
47 | | -/// |
48 | | -/// The `Async` and `AsyncWait` class variants may throw `std::bad_alloc` if heap allocation |
49 | | -/// fails within `operator()(Args... args)`. Alternatively, define `DMQ_ASSERTS` to use `assert` |
50 | | -/// as opposed to exceptions. All other delegate class functions do not throw exceptions. |
51 | | -/// |
52 | | -/// Github repository location: |
53 | | -/// https://github.com/endurodave/DelegateMQ |
| 34 | +/// **Common Use Cases:** |
| 35 | +/// * Asynchronous Method Invocation (AMI) on specific worker threads. |
| 36 | +/// * Publish / Subscribe (Observer) patterns. |
| 37 | +/// * Anonymous, thread-safe asynchronous callbacks. |
| 38 | +/// * Event-Driven Programming architectures. |
| 39 | +/// * Thread-Safe Asynchronous APIs. |
| 40 | +/// * Active Object design patterns. |
54 | 41 | /// |
55 | | -/// See README.md, DETAILS.md, EXAMPLES.md, and source code Doxygen comments for more information. |
| 42 | +/// **Asynchronous Safety:** |
| 43 | +/// Asynchronous variants automatically copy argument data into the event queue. This provides true |
| 44 | +/// 'fire and forget' functionality, ensuring that out-of-scope stack variables in the caller |
| 45 | +/// do not cause data races or corruption in the target thread. |
| 46 | +/// |
| 47 | +/// **Error Handling:** |
| 48 | +/// The `Async` and `AsyncWait` variants may throw `std::bad_alloc` if heap allocation fails during |
| 49 | +/// invocation. Alternatively, defining `DMQ_ASSERTS` switches error handling to assertions. |
| 50 | +/// All other delegate functions are `noexcept`. |
| 51 | +/// |
| 52 | +/// **Documentation & Source:** |
| 53 | +/// * Repository: https://github.com/endurodave/DelegateMQ |
| 54 | +/// * See `README.md`, `DETAILS.md`, and `EXAMPLES.md` for comprehensive guides. |
56 | 55 |
|
57 | | -#include "delegate/DelegateOpt.h" |
58 | | -#include "delegate/MulticastDelegateSafe.h" |
59 | | -#include "delegate/UnicastDelegateSafe.h" |
60 | | -#include "delegate/SignalSafe.h" |
61 | | -#include "delegate/DelegateAsync.h" |
62 | | -#include "delegate/DelegateAsyncWait.h" |
| 56 | +// ----------------------------------------------------------------------------- |
| 57 | +// 1. Core Non-Thread-Safe Delegates |
| 58 | +// (Always available: Bare Metal, FreeRTOS, Windows, Linux) |
| 59 | +// ----------------------------------------------------------------------------- |
| 60 | +#include "delegate/Delegate.h" |
63 | 61 | #include "delegate/DelegateRemote.h" |
| 62 | +#include "delegate/MulticastDelegate.h" |
| 63 | +#include "delegate/UnicastDelegate.h" |
| 64 | +#include "delegate/Signal.h" |
| 65 | + |
| 66 | +// ----------------------------------------------------------------------------- |
| 67 | +// 2. Thread-Safe Wrappers (Mutex Only) |
| 68 | +// ----------------------------------------------------------------------------- |
| 69 | +// - FreeRTOS: Uses FreeRTOSRecursiveMutex |
| 70 | +// - Bare Metal: Uses NullMutex |
| 71 | +// - StdLib: Uses std::recursive_mutex |
| 72 | +#if defined(DMQ_THREAD_STDLIB) || defined(DMQ_THREAD_FREERTOS) || defined(DMQ_THREAD_NONE) |
| 73 | + #include "delegate/MulticastDelegateSafe.h" |
| 74 | + #include "delegate/UnicastDelegateSafe.h" |
| 75 | + #include "delegate/SignalSafe.h" |
| 76 | +#endif |
| 77 | + |
| 78 | +// ----------------------------------------------------------------------------- |
| 79 | +// 3. Asynchronous "Fire and Forget" Delegates |
| 80 | +// ----------------------------------------------------------------------------- |
| 81 | +// - FreeRTOS: OK |
| 82 | +// - Bare Metal: OK (Requires you to implement IThread wrapper for Event Loop) |
| 83 | +// - StdLib: OK |
| 84 | +#if defined(DMQ_THREAD_STDLIB) || defined(DMQ_THREAD_FREERTOS) || defined(DMQ_THREAD_NONE) |
| 85 | + #include "delegate/DelegateAsync.h" |
| 86 | +#endif |
| 87 | + |
| 88 | +// ----------------------------------------------------------------------------- |
| 89 | +// 4. Asynchronous "Blocking" Delegates (Wait for Result) |
| 90 | +// ----------------------------------------------------------------------------- |
| 91 | +// - FreeRTOS: NO (Requires Semaphore/Condition Variable) |
| 92 | +// - Bare Metal: NO (Cannot block/sleep) |
| 93 | +// - StdLib: OK |
| 94 | +#if defined(DMQ_THREAD_STDLIB) |
| 95 | + #include "delegate/DelegateAsyncWait.h" |
| 96 | +#endif |
64 | 97 |
|
65 | 98 | #if defined(DMQ_THREAD_STDLIB) |
66 | 99 | #include "predef/os/stdlib/Thread.h" |
|
69 | 102 | #include "predef/os/freertos/Thread.h" |
70 | 103 | #include "predef/os/freertos/ThreadMsg.h" |
71 | 104 | #elif defined(DMQ_THREAD_NONE) |
72 | | - // Create a custom application-specific thread |
| 105 | + // Bare metal: User must implement their own polling/interrupt logic |
73 | 106 | #else |
74 | 107 | #error "Thread implementation not found." |
75 | 108 | #endif |
|
119 | 152 | #endif |
120 | 153 |
|
121 | 154 | #include "predef/util/Fault.h" |
122 | | -#include "predef/util/Timer.h" |
123 | | -#include "predef/util/TransportMonitor.h" |
124 | | -#include "predef/util/AsyncInvoke.h" |
| 155 | + |
| 156 | +// Only include Timer and AsyncInvoke if threads exist |
| 157 | +#if !defined(DMQ_THREAD_NONE) |
| 158 | + #include "predef/util/Timer.h" |
| 159 | + #include "predef/util/AsyncInvoke.h" |
| 160 | + #include "predef/util/TransportMonitor.h" |
| 161 | +#endif |
125 | 162 |
|
126 | 163 | #endif |
0 commit comments