asrt
Automated System Runtime Testing library
Loading...
Searching...
No Matches
param.hpp
1
11#pragma once
12
13#include "../asrtl/asrt_assert.h"
14#include "../asrtl/log.h"
15#include "../asrtl/status_to_str.h"
16#include "../asrtlpp/flat_type_traits.hpp"
17#include "../asrtlpp/util.hpp"
18#include "../asrtr/param.h"
19#include "./task_unit.hpp"
20
21namespace asrt
22{
23
24using status = asrt_status;
25
28template < typename T >
30
31template <>
32struct param_query_traits< uint32_t > : flat_type_traits< uint32_t >
33{
34 using cb_type = asrt_param_u32_cb;
35 static constexpr auto cb_member = &asrt_param_cb::u32;
36};
37
38template <>
39struct param_query_traits< int32_t > : flat_type_traits< int32_t >
40{
41 using cb_type = asrt_param_i32_cb;
42 static constexpr auto cb_member = &asrt_param_cb::i32;
43};
44
45template <>
46struct param_query_traits< float > : flat_type_traits< float >
47{
48 using cb_type = asrt_param_float_cb;
49 static constexpr auto cb_member = &asrt_param_cb::flt;
50};
51
52template <>
54{
55 using cb_type = asrt_param_str_cb;
56 static constexpr auto cb_member = &asrt_param_cb::str;
57};
58
59template <>
60struct param_query_traits< bool > : flat_type_traits< bool >
61{
62 using cb_type = asrt_param_bool_cb;
63 static constexpr auto cb_member = &asrt_param_cb::bln;
64};
65
66template <>
68{
69 using cb_type = asrt_param_obj_cb;
70 static constexpr auto cb_member = &asrt_param_cb::obj;
71};
72
73template <>
75{
76 using cb_type = asrt_param_arr_cb;
77 static constexpr auto cb_member = &asrt_param_cb::arr;
78};
79
80template <>
82{
85 using cb_type = asrt_param_any_cb;
86 static constexpr auto flat_type = ASRT_FLAT_STYPE_NONE;
87 static constexpr auto cb_member = &asrt_param_cb::any;
88};
89
90// Convenience aliases
91template <>
92struct param_query_traits< void > : param_query_traits< asrt_flat_value >
93{
94};
95
96template < typename T >
97concept has_param_query_traits = requires { typename param_query_traits< T >; };
98
100template < typename CB, typename T >
102 has_param_query_traits< T > && requires(
103 CB cb,
106 param_query_traits< T >::value_type v ) {
107 { cb( c, q, v ) } -> std::same_as< void >;
108 };
109
112ASRT_NODISCARD inline status init(
113 ref< asrt_param_client > client,
114 asrt_node& prev,
115 asrt_span msg_buffer,
116 uint32_t timeout )
117{
118 return asrt_param_client_init( client, &prev, msg_buffer, timeout );
119}
120
122ASRT_NODISCARD inline bool ready( ref< asrt_param_client const > client )
123{
124 return client->ready != 0;
125}
126
128ASRT_NODISCARD inline bool query_pending( ref< asrt_param_client const > client )
129{
130 return asrt_param_query_pending( client ) != 0;
131}
132
134ASRT_NODISCARD inline flat_id root_id( ref< asrt_param_client const > client )
135{
136 return asrt_param_client_root_id( client );
137}
140template < has_param_query_traits T, typed_param_query_callable< T > CB >
141ASRT_NODISCARD asrt_status
142query( ref< asrt_param_client > cl, asrt_param_query* q, flat_id node_id, char const* key, CB& cb )
143{
144 using traits = param_query_traits< T >;
145 q->expected_type = traits::flat_type;
146 q->cb.*traits::cb_member =
147 []( asrt_param_client* c, asrt_param_query* qq, traits::raw_type raw ) {
148 ( *reinterpret_cast< CB* >( qq->cb_ptr ) )(
149 c, qq, static_cast< traits::value_type >( raw ) );
150 };
151 q->cb_ptr = &cb;
152 return asrt_param_client_query( q, cl, node_id, key );
153}
155template < has_param_query_traits T >
156ASRT_NODISCARD asrt_status query(
157 ref< asrt_param_client > cl,
159 flat_id node_id,
160 char const* key,
162 void* cb_ptr )
163{
164 using traits = param_query_traits< T >;
165 q->expected_type = traits::flat_type;
166 q->cb.*traits::cb_member = cb;
167 q->cb_ptr = cb_ptr;
168 return asrt_param_client_query( q, cl, node_id, key );
169}
170
172ASRT_NODISCARD inline asrt_status query(
173 ref< asrt_param_client > cl,
175 flat_id node_id,
176 char const* key,
177 asrt_param_any_cb cb,
178 void* cb_ptr )
179{
180 q->expected_type = ASRT_FLAT_STYPE_NONE;
181 q->cb.any = cb;
182 q->cb_ptr = cb_ptr;
183 return asrt_param_client_query( q, cl, node_id, key );
184}
185
190template < has_param_query_traits T, typed_param_query_callable< T > CB >
191ASRT_NODISCARD asrt_status
192fetch( ref< asrt_param_client > cl, asrt_param_query* q, flat_id node_id, CB& cb )
193{
194 return query< T >( cl, q, node_id, nullptr, cb );
195}
196
199template < has_param_query_traits T >
200ASRT_NODISCARD asrt_status fetch(
201 ref< asrt_param_client > cl,
203 flat_id node_id,
205 void* cb_ptr )
206{
207 return query< T >( cl, q, node_id, nullptr, cb, cb_ptr );
208}
209
211ASRT_NODISCARD inline asrt_status fetch(
212 ref< asrt_param_client > cl,
214 flat_id node_id,
215 asrt_param_any_cb cb,
216 void* cb_ptr )
217{
218 return query( cl, q, node_id, nullptr, cb, cb_ptr );
219}
220
222template < has_param_query_traits T, typed_param_query_callable< T > CB >
223ASRT_NODISCARD asrt_status
224find( ref< asrt_param_client > cl, asrt_param_query* q, flat_id parent_id, char const* key, CB& cb )
225{
226 return query< T >( cl, q, parent_id, key, cb );
227}
228
230template < has_param_query_traits T >
231ASRT_NODISCARD asrt_status find(
232 ref< asrt_param_client > cl,
234 flat_id parent_id,
235 char const* key,
237 void* cb_ptr )
238{
239 return query< T >( cl, q, parent_id, key, cb, cb_ptr );
240}
241
243ASRT_NODISCARD inline asrt_status find(
244 ref< asrt_param_client > cl,
246 flat_id parent_id,
247 char const* key,
248 asrt_param_any_cb cb,
249 void* cb_ptr )
250{
251 return query( cl, q, parent_id, key, cb, cb_ptr );
252}
253
255inline void deinit( ref< asrt_param_client > client )
256{
257 asrt_param_client_deinit( client );
258}
259
262template < has_param_query_traits T >
264{
265 using value_type = param_query_traits< T >::value_type;
266
267 value_type value;
268 char const* key; // Points to internal buffer of param client, valid until next query
269 flat_id next_sibling;
270
271 operator value_type() const { return value; }
272};
273
274
277template < has_param_query_traits T >
278struct _param_query_ctx
279{
280 using completion_signatures = ecor::completion_signatures<
281 ecor::set_value_t( param_result< T > ),
282 ecor::set_error_t( status ) >;
283
284 asrt_param_client* client;
285 char const* key;
286 flat_id node_id;
287 asrt_param_query q = {};
288
289 template < typename OP >
290 void start( OP& op )
291 {
292 using traits = param_query_traits< T >;
293 auto cb = +[]( asrt_param_client*, asrt_param_query* q, traits::raw_type raw ) {
294 auto* o = static_cast< OP* >( q->cb_ptr );
295 if ( q->error_code != ASRT_PARAM_ERR_NONE )
296 o->receiver.set_error( ASRT_RECV_ERR );
297 else
298 o->receiver.set_value( param_result< T >{
299 static_cast< traits::value_type >( raw ),
300 q->key,
301 q->next_sibling } );
302 };
303 auto s = query< T >( client, &q, node_id, key, cb, &op );
304 if ( s != ASRT_SUCCESS )
305 op.receiver.set_error( ASRT_RECV_ERR );
306 }
307};
308
311template < has_param_query_traits T >
312using param_query_sender = ecor::sender_from< _param_query_ctx< T > >;
313
315template < typename T >
316ecor::sender auto fetch( ref< asrt_param_client > client, flat_id node_id )
317{
318 return param_query_sender< T >{ { client, nullptr, node_id } };
319}
320
322template < typename T >
323ecor::sender auto find( ref< asrt_param_client > client, flat_id parent_id, char const* key )
324{
325 return param_query_sender< T >{ { client, key, parent_id } };
326}
327
330inline ecor::sender auto fetch( ref< asrt_param_client > client, flat_id node_id )
331{
332 return param_query_sender< void >{ { client, nullptr, node_id } };
333}
334
337inline ecor::sender auto find( ref< asrt_param_client > client, flat_id parent_id, char const* key )
338{
339 return param_query_sender< void >{ { client, key, parent_id } };
340}
341
342} // namespace asrt
Concept: CB is a typed callback for T (callable as cb(client*, query*, value_type)).
Definition: param.hpp:101
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee ...
Definition: callback.hpp:17
ASRT_NODISCARD asrt_status query(ref< asrt_param_client > cl, asrt_param_query *q, flat_id node_id, char const *key, CB &cb)
Submit a typed QUERY or FIND_BY_KEY.
Definition: param.hpp:142
ASRT_NODISCARD bool query_pending(ref< asrt_param_client const > client)
Returns true if a query is currently in flight.
Definition: param.hpp:128
ASRT_NODISCARD asrt_status find(ref< asrt_param_client > cl, asrt_param_query *q, flat_id parent_id, char const *key, CB &cb)
Find child with key under parent_id, call cb with the value if successful.
Definition: param.hpp:224
ASRT_NODISCARD enum asrt_status init(ref< asrt_cntr_assm > assm, asrt_allocator alloc)
Initialise the controller assembly — wires controller, diag, param, collect and stream channels.
Definition: cntr_assm.hpp:25
void deinit(ref< asrt_cntr_assm > assm)
Release all resources owned by the assembly.
Definition: cntr_assm.hpp:52
ecor::sender_from< _param_query_ctx< T > > param_query_sender
Sender backing co_await fetch<T> and co_await find<T>.
Definition: param.hpp:312
ASRT_NODISCARD bool ready(ref< asrt_param_client const > client)
Returns true once the controller's READY has been received.
Definition: param.hpp:122
ASRT_NODISCARD asrt_status fetch(ref< asrt_param_client > cl, asrt_param_query *q, flat_id node_id, CB &cb)
Fetch information about node node_id from param client, call cb with the value if successful.
Definition: param.hpp:192
Tag type representing a flat-tree ARRAY container node.
Definition: flat_type_traits.hpp:28
Trait mapping a C++ type to its flat_tree metadata.
Definition: flat_type_traits.hpp:38
Tag type representing a flat-tree OBJECT container node.
Definition: flat_type_traits.hpp:24
Trait mapping a C++ type T to its corresponding C callback type and asrt_param_cb union member pointe...
Definition: param.hpp:29
Result of a completed param query.
Definition: param.hpp:264
Definition: flat_tree.h:90
A node in a doubly-linked channel chain.
Definition: chann.h:122
Param client module — PARAM channel, reactor side.
Definition: param.h:47
Active query state for a pending QUERY or FIND_BY_KEY request.
Definition: param.h:149
asrt_flat_value_type expected_type
Type filter; NONE means accept any type.
Definition: param.h:155
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee ...
Definition: span.h:23