1212
1313namespace cib {
1414 /* *
15- * Extension point/builder for simple callbacks.
15+ * Builder for simple callbacks.
1616 *
17- * Modules can add their own callback function to this builder to be executed when
18- * the builder is executed with the same function arguments.
17+ * Components can add their own callback function to this builder to be
18+ * executed when the service is executed with the same function arguments.
1919 *
20- * @tparam Size
21- * Maximum number of callbacks that may be registered .
20+ * @tparam NumFuncs
21+ * The number of functions currently registered with this builder .
2222 *
23- * @tparam Args
23+ * @tparam ArgTypes
2424 * List of argument types that must be passed into the callback when it is invoked.
25+ *
26+ * @see cib::callback_meta
2527 */
26- template <int Size = 0 , typename ... Args >
28+ template <int NumFuncs = 0 , typename ... ArgTypes >
2729 struct callback {
2830 private:
29- using func_ptr_t = void (*)(Args ...);
31+ using func_ptr_t = void (*)(ArgTypes ...);
3032
31- std::array<func_ptr_t , Size > funcs;
33+ std::array<func_ptr_t , NumFuncs > funcs;
3234
35+ /* *
36+ * Runtime implementation of a callback service.
37+ *
38+ * Calls each registered function in an undefined order. The order
39+ * functions are called should not be depended upon and could
40+ * change from one release to the next.
41+ *
42+ * This function will be available from nexus::builder<...> or
43+ * cib::built<...>.
44+ *
45+ * @tparam BuilderValue
46+ * A type that contains a constexpr static value field with the
47+ * fully initialized callback builder.
48+ *
49+ * @param args
50+ * The arguments to be passed to every registered function.
51+ *
52+ * @see cib::nexus
53+ * @see cib::built
54+ */
3355 template <typename BuilderValue>
34- static void run (Args ... args) {
35- CIB_CONSTEXPR auto handlerBuilder = BuilderValue::value;
36- CIB_CONSTEXPR auto numFuncs = std::integral_constant<int , Size >{};
56+ static void run (ArgTypes ... args) {
57+ CIB_CONSTEXPR auto handler_builder = BuilderValue::value;
58+ CIB_CONSTEXPR auto num_funcs = std::integral_constant<int , NumFuncs >{};
3759
38- detail::for_each (numFuncs , [&](auto i){
39- CIB_CONSTEXPR auto func = handlerBuilder .funcs [i];
60+ detail::for_each (num_funcs , [&](auto i){
61+ CIB_CONSTEXPR auto func = handler_builder .funcs [i];
4062 func (args...);
4163 });
4264 }
4365
4466 public:
4567 CIB_CONSTEVAL callback () = default;
4668
47- template <typename PrevFuncsT >
69+ template <typename PrevFuncsType >
4870 CIB_CONSTEVAL callback (
49- PrevFuncsT const & prev_funcs,
71+ PrevFuncsType const & prev_funcs,
5072 func_ptr_t new_func
5173 )
5274 : funcs{}
@@ -55,36 +77,67 @@ namespace cib {
5577 funcs[i] = prev_funcs[i];
5678 }
5779
58- funcs[Size - 1 ] = new_func;
80+ funcs[NumFuncs - 1 ] = new_func;
5981 }
6082
61- // cib uses "add(...)" to add features to service builders
62- CIB_CONSTEVAL auto add (func_ptr_t const & func) const {
63- return callback<Size + 1 , Args...>{funcs, func};
83+ /* *
84+ * Add a function to be executed when the callback service is invoked.
85+ *
86+ * Do not call this function directly. The library will add functions
87+ * to service builders based on a project's cib::config and cib::extend
88+ * declarations.
89+ *
90+ * @param func
91+ *
92+ * @return
93+ * A version of this callback builder with the addition of func.
94+ *
95+ * @see cib::extend
96+ * @see cib::nexus
97+ */
98+ [[nodiscard]] CIB_CONSTEVAL auto add (func_ptr_t const & func) const {
99+ return callback<NumFuncs + 1 , ArgTypes...>{funcs, func};
64100 }
65101
66102 /* *
67- * Build and return a function pointer to the implemented callback builder. Used
68- * by cib library to automatically build an initialized builder. Do not call.
103+ * Build and return a function pointer to the implemented callback
104+ * builder. Used by cib nexus to automatically build an initialized
105+ * builder.
106+ *
107+ * Do not call directly.
69108 *
70109 * @tparam BuilderValue
71110 * Struct that contains a "static constexpr auto value" field with the initialized
72111 * builder.
73112 *
74113 * @return
75- * Function pointer to callback builder .
114+ * Function pointer to the implemented callback service .
76115 */
77116 template <typename BuilderValue>
78117 [[nodiscard]] CIB_CONSTEVAL static auto build () {
79118 return run<BuilderValue>;
80119 }
81120 };
82121
83- template <typename ... Args>
122+ /* *
123+ * Extend this to create named callback services.
124+ *
125+ * Types that extend callback_meta can be used as unique names with
126+ * cib::exports and cib::extend.
127+ *
128+ * @tparam ArgTypes
129+ * The function arguments that must be passed into the callback
130+ * services implementation. Any function registered with this
131+ * callback service must also have a compatible signature.
132+ *
133+ * @see cib::exports
134+ * @see cib::extend
135+ */
136+ template <typename ... ArgTypes>
84137 struct callback_meta :
85138 public cib::builder_meta<
86- callback<0 , Args ...>,
87- void (*)(Args ...)>
139+ callback<0 , ArgTypes ...>,
140+ void (*)(ArgTypes ...)>
88141 {};
89142}
90143
0 commit comments