asrt
Automated System Runtime Testing library
Loading...
Searching...
No Matches
util.hpp
1
11#pragma once
12
13#ifdef ASRT_NODISCARD_ENABLED
14#define ASRT_NODISCARD [[nodiscard]]
15#else
16#define ASRT_NODISCARD
17#endif
18
19#include "../asrtl/allocator.h"
20#include "../asrtl/asrt_assert.h"
21#include "../asrtl/chann.h"
22#include "../asrtl/source.h"
23
24#include <functional>
25#include <memory>
26#include <optional>
27#include <span>
28
29namespace asrt
30{
31using source = asrt_source;
32using chann_id = asrt_chann_id;
33using status = asrt_status;
34using allocator = asrt_allocator;
35using span = asrt_span;
36using span_span = asrt_span_span;
37template < typename T >
38using uptr = std::unique_ptr< T >;
39template < typename T >
40using opt = std::optional< T >;
41
42inline asrt_span cnv( std::span< uint8_t > buff )
43{
44 return {
45 .b = buff.data(),
46 .e = buff.data() + buff.size(),
47 };
48}
49
50inline std::span< uint8_t > cnv( asrt_span buff )
51{
52 return { buff.b, buff.e };
53}
54
55ASRT_NODISCARD inline enum asrt_status dispatch( struct asrt_node& head, std::span< uint8_t > buff )
56{
57 return asrt_chann_dispatch(
58 &head, asrt_span{ .b = buff.data(), .e = buff.data() + buff.size() } );
59}
60
61ASRT_NODISCARD inline enum asrt_status tick( struct asrt_node& head, uint32_t now )
62{
63 return asrt_chann_tick( &head, now );
64}
65
66inline asrt_node& node( auto* comp )
67{
68 return comp->node;
69}
70
71inline asrt_node& node( auto& comp )
72{
73 return comp.node;
74}
75
76ASRT_NODISCARD inline status recv( asrt_node& n, std::span< uint8_t > buff )
77{
78 return asrt_chann_recv( &n, cnv( buff ) );
79}
80
81ASRT_NODISCARD inline status recv( asrt_node& n, asrt_span buff )
82{
83 return asrt_chann_recv( &n, buff );
84}
85
86template < typename T >
87struct ref
88{
89 T& operator*() { return *_p; }
90 T* operator->() { return _p; }
91
92 operator T&() { return *_p; }
93 operator T*() { return _p; }
94
95 ref( T& t )
96 : _p( &t )
97 {
98 }
99 ref( T* t )
100 : _p( t )
101 {
102 ASRT_ASSERT( t != nullptr );
103 }
104
105private:
106 T* _p;
107};
108
109// ---------------------------------------------------------------------------
110// send_queue helper
111
115struct send_req_ref
116{
117 explicit operator bool() const noexcept { return _cur != nullptr; }
118 asrt_send_req* operator->() const noexcept { return _cur; }
119 asrt_send_req& operator*() const noexcept { return *_cur; }
120
121 void finish( enum asrt_status s ) noexcept { asrt_send_req_list_done( _list, s ); }
122
123private:
124 asrt_send_req_list* _list;
125 asrt_send_req* _cur;
126
127 friend send_req_ref next( asrt_send_req_list& ) noexcept;
128 send_req_ref( asrt_send_req_list* l ) noexcept
129 : _list( l )
130 , _cur( l->head )
131 {
132 }
133};
134
137inline send_req_ref next( asrt_send_req_list& list ) noexcept
138{
139 return send_req_ref{ &list };
140}
141
142} // namespace asrt
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee ...
Definition: callback.hpp:17
void tick(ref< asrt_cntr_assm > assm, uint32_t now)
Advance all assembly modules by one tick.
Definition: cntr_assm.hpp:32
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee ...
Definition: allocator.h:28
A node in a doubly-linked channel chain.
Definition: chann.h:122
Intrusive FIFO of outgoing send requests.
Definition: chann.h:76
An outgoing message request placed in a module's send queue.
Definition: chann.h:64
Scatter-gather buffer: a primary byte range [b, e) followed by rest_count additional spans.
Definition: span.h:37
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee ...
Definition: span.h:23
uint8_t * b
Pointer to the first byte.
Definition: span.h:24
uint8_t * e
One-past-the-end pointer.
Definition: span.h:25