asrt
Automated System Runtime Testing library
Loading...
Searching...
No Matches
collect.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/callback.hpp"
17#include "../asrtlpp/flat_type_traits.hpp"
18#include "../asrtlpp/task.hpp"
19#include "../asrtlpp/util.hpp"
20#include "../asrtr/collect.h"
21
22namespace asrt
23{
24
28template < typename T >
30
31template <>
32struct collect_append_traits< uint32_t > : flat_type_traits< uint32_t >
33{
34 static constexpr auto member = &asrt_flat_scalar::u32_val;
35};
36
37template <>
38struct collect_append_traits< int32_t > : flat_type_traits< int32_t >
39{
40 static constexpr auto member = &asrt_flat_scalar::i32_val;
41};
42
43template <>
44struct collect_append_traits< float > : flat_type_traits< float >
45{
46 static constexpr auto member = &asrt_flat_scalar::float_val;
47};
48
49template <>
51{
52 static constexpr auto member = &asrt_flat_scalar::str_val;
53};
54
55template <>
57{
58 static constexpr auto member = &asrt_flat_scalar::bool_val;
59};
60
61template <>
63{
64};
65
66template <>
68{
69};
70
72template < typename T >
73concept collect_scalar = collect_append_traits< T >::is_scalar;
74
76template < typename T >
77concept collect_container = !collect_append_traits< T >::is_scalar;
78
79ASRT_NODISCARD inline status init( ref< asrt_collect_client > client, asrt_node& prev )
80{
81 return asrt_collect_client_init( client, &prev );
82}
83
84ASRT_NODISCARD inline flat_id root_id( ref< asrt_collect_client const > cc )
85{
86 return asrt_collect_client_root_id( cc );
87}
88
90template < typename T >
91struct _collect_append_ctx;
92
93template < collect_scalar T >
94struct _collect_append_ctx< T >
95{
96 using completion_signatures =
97 ecor::completion_signatures< ecor::set_value_t(), ecor::set_error_t( status ) >;
98
99 asrt_collect_client* client;
100 flat_id parent;
101 char const* key;
102 T val;
103
104 template < typename OP >
105 void start( OP& op )
106 {
107 using traits = collect_append_traits< T >;
108 using member_type = decltype( asrt_flat_scalar{}.*traits::member );
109 asrt_flat_value v = { .type = traits::flat_type };
110 v.data.s.*traits::member = static_cast< member_type >( val );
111 auto s = asrt_collect_client_append(
112 client,
113 parent,
114 key,
115 &v,
116 nullptr,
117 +[]( void* ptr, enum asrt_status st ) {
118 auto* o = static_cast< OP* >( ptr );
119 if ( st == ASRT_SUCCESS )
120 o->receiver.set_value();
121 else
122 o->receiver.set_error( static_cast< status >( st ) );
123 },
124 &op );
125 if ( s != ASRT_SUCCESS )
126 op.receiver.set_error( static_cast< status >( s ) );
127 }
128};
129
130template < collect_container T >
131struct _collect_append_ctx< T >
132{
133 using completion_signatures = ecor::
134 completion_signatures< ecor::set_value_t( flat_id ), ecor::set_error_t( status ) >;
135
136 asrt_collect_client* client;
137 flat_id parent;
138 char const* key;
139 flat_id out = 0;
140
141 template < typename OP >
142 void start( OP& op )
143 {
144 using traits = collect_append_traits< T >;
145 asrt_flat_value v = { .type = traits::flat_type };
146 auto s = asrt_collect_client_append(
147 client,
148 parent,
149 key,
150 &v,
151 &out,
152 +[]( void* ptr, enum asrt_status st ) {
153 auto* o = static_cast< OP* >( ptr );
154 if ( st == ASRT_SUCCESS )
155 o->receiver.set_value( o->ctx.out );
156 else
157 o->receiver.set_error( static_cast< status >( st ) );
158 },
159 &op );
160 if ( s != ASRT_SUCCESS )
161 op.receiver.set_error( static_cast< status >( s ) );
162 }
163};
164
165template < typename T >
166 requires collect_scalar< T > || collect_container< T >
167using collect_append_sender = ecor::sender_from< _collect_append_ctx< T > >;
168
170template < collect_scalar T >
171status append(
172 ref< asrt_collect_client > cc,
173 flat_id parent,
174 char const* key,
175 T val,
177{
178 using traits = collect_append_traits< T >;
179 using member_type = decltype( asrt_flat_scalar{}.*traits::member );
180 asrt_flat_value v = { .type = traits::flat_type };
181 v.data.s.*traits::member = static_cast< member_type >( val );
182 return asrt_collect_client_append( cc, parent, key, &v, NULL, done_cb.fn, done_cb.ptr );
183}
184
186template < collect_scalar T >
187ecor::sender auto append( ref< asrt_collect_client > cc, flat_id parent, char const* key, T val )
188{
189 return collect_append_sender< T >{ { cc, parent, key, val } };
190}
191
193template < collect_scalar T >
194status append(
195 ref< asrt_collect_client > cc,
196 flat_id parent,
197 T val,
199{
200 return append< T >( cc, parent, nullptr, val, done_cb );
201}
202
205template < collect_container T >
206status append(
207 ref< asrt_collect_client > cc,
208 flat_id parent,
209 char const* key,
210 flat_id& out,
212{
213 using traits = collect_append_traits< T >;
214 asrt_flat_value v = { .type = traits::flat_type };
215 return asrt_collect_client_append( cc, parent, key, &v, &out, done_cb.fn, done_cb.ptr );
216}
217
219template < collect_container T >
220ecor::sender auto append( ref< asrt_collect_client > client, flat_id parent, char const* key )
221{
222 return collect_append_sender< T >{ { client, parent, key } };
223}
224
226template < collect_container T >
227status append(
228 ref< asrt_collect_client > cc,
229 flat_id parent,
230 flat_id& out,
232{
233 return append< T >( cc, parent, nullptr, out, done_cb );
234}
235
237template < collect_scalar T >
238ecor::sender auto append( ref< asrt_collect_client > cc, flat_id parent, T val )
239{
240 return collect_append_sender< T >{ { cc, parent, nullptr, val } };
241}
242
244template < collect_container T >
245ecor::sender auto append( ref< asrt_collect_client > client, flat_id parent )
246{
247 return collect_append_sender< T >{ { client, parent, nullptr } };
248}
249
250inline void deinit( ref< asrt_collect_client > client )
251{
252 asrt_collect_client_deinit( client );
253}
254
255} // namespace asrt
Concept: T is a container tag (OBJECT or ARRAY).
Definition: collect.hpp:77
Concept: T is a scalar type with a flat_value member pointer.
Definition: collect.hpp:73
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee ...
Definition: callback.hpp:17
ASRT_NODISCARD status start(ref< asrt_controller > c, callback< asrt_init_callback > cb, uint32_t timeout)
Begin the protocol handshake; cb is invoked once the target responds or the operation times out.
Definition: controller.hpp:36
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
status append(ref< asrt_collect_client > cc, flat_id parent, char const *key, T val, callback< asrt_send_done_cb > done_cb)
Scalar append with key: append<uint32_t>(parent, "key", 42, cb).
Definition: collect.hpp:171
void deinit(ref< asrt_cntr_assm > assm)
Release all resources owned by the assembly.
Definition: cntr_assm.hpp:52
Tag type representing a flat-tree ARRAY container node.
Definition: flat_type_traits.hpp:28
A type-erasing wrapper for C-style callback + void* pairs.
Definition: callback.hpp:27
Trait mapping C++ types to flat_value scalar member pointers.
Definition: collect.hpp:29
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
Reactor-side collect client (ASRT_COLL channel).
Definition: collect.h:41
Definition: flat_tree.h:90
A node in a doubly-linked channel chain.
Definition: chann.h:122
Definition: flat_tree.h:75