msl 1.3.0
Loading...
Searching...
No Matches
Case.h
Go to the documentation of this file.
1// Copyright 2024-2025 Julien Lamy, ICube, Université de Strasbourg-CNRS.
2// Part of msl, distributed under the terms of the MIT license.
3
4#ifndef _c1c4f441_160f_46bf_869e_1bf9c199aa91
5#define _c1c4f441_160f_46bf_869e_1bf9c199aa91
6
7#include <unordered_map>
8
9#include <MrMeasSrv/MeasUtils/NLSStatus.h>
10#include <MrMeasSrv/SeqIF/Sequence/sequmsg.h>
11
12#include <MrProtSrv/Domain/CoreNative/SeqLim.h>
13#include <MrProtSrv/Domain/MrProtData/MrProt/MrProt.h>
14#include <MrProtSrv/Domain/MrProtData/MrProt/SeqIF/SeqExpo.h>
15#include <MrProtSrv/Domain/MrProtData/MrProt/SeqIF/SeqExpoRFBlockInfo.h>
16
17#include "msl/Dictionary.h"
20#include "msl/graph/Node.h"
21#include "msl/helpers.h"
22
23namespace msl
24{
25namespace graph
26{
27
32template<typename T>
33class Case: public AbstractNode
34{
35public:
37 using Map = std::unordered_map<T, AbstractNode::Pointer>;
38
41
43
45 static Pointer New(
46 std::string const & key, Map const & cases,
48 {
49 return Pointer(new Case(key, cases, registry));
50 }
51
58 NLSStatus prepare(
59 MrProt & protocol, SeqLim & limits, SeqExpo & exports) override
60 {
61 auto child = this->_child(protocol, limits, exports);
62 ON_ERROR_RETURN_STATUS(child->prepare(protocol, limits, exports));
63
64 // Update the duration and rfInfo cache
65 this->_duration = child->duration();
66 this->_rfInfo = child->rfInfo();
67
68 return MRI_SEQ_SEQU_NORMAL;
69 }
70
77 NLSStatus run(
78 MrProt & protocol, SeqLim & limits, SeqExpo & exports) override
79 {
80 auto child = this->_child(protocol, limits, exports);
81 return child->run(protocol, limits, exports);
82 }
83
86 {
87 this->AbstractNode::setRegistry(registry);
88 for(auto && item: this->_cases)
89 {
90 auto & child = item.second;
91 child->setRegistry(this->_registry);
92 }
93 return *this;
94 }
95
100 uint64_t duration() const override
101 {
102 return this->_duration;
103 }
104
109 MrProtocolData::SeqExpoRFInfo rfInfo() const override
110 {
111 return this->_rfInfo;
112 }
113
114private:
115 std::string _key;
116 Map _cases;
117
118 // Cache for duration and rfInfo, updated when _condition is called
119 mutable uint64_t _duration;
120 mutable MrProtocolData::SeqExpoRFInfo _rfInfo;
121
122 Case(
123 std::string const & key, Map const & cases,
125 : AbstractNode(registry), _key(key), _cases(cases)
126 {
127 this->setRegistry(registry);
128 }
129
131 MrProt & protocol, SeqLim & limits, SeqExpo & exports) const
132 {
133 auto const child = this->_cases.at(
134 this->type(this->_key) == typeid(T)
135 ? this->get<T>(this->_key)
136 : function_adapter(this->get<Function>(this->_key))(
137 protocol, limits, exports, this->_registry));
138
139 return child;
140 }
141
143 MrProt & protocol, SeqLim & limits, SeqExpo & exports)
144 {
145 auto const child = this->_cases.at(
146 this->type(this->_key) == typeid(T)
147 ? this->get<T>(this->_key)
148 : function_adapter(this->get<Function>(this->_key))(
149 protocol, limits, exports, this->_registry));
150
151 return child;
152 }
153};
154
155}
156}
157
158#endif // _c1c4f441_160f_46bf_869e_1bf9c199aa91
#define DECLARE_POINTERS(name)
Declare pointer type aliases.
Definition Dictionary.h:18
std::shared_ptr< Dictionary > Pointer
Reference-counted pointer to Dictionary
Definition Dictionary.h:39
Base class for all graph nodes.
Definition AbstractNode.h:28
std::shared_ptr< AbstractNode > Pointer
Reference-counted pointer to AbstractNode
Definition AbstractNode.h:30
boost::typeindex::type_info const & type(std::string const &key) const
Return the type of an object in the dictionary.
Dictionary::ConstPointer registry() const
Return the registry.
T const & get(std::string const &key) const
Return an object from the dictionary.
Definition AbstractNode.h:65
virtual AbstractNode & setRegistry(Dictionary::Pointer registry)
Set the registry.
std::shared_ptr< AbstractNode const > ConstPointer
Reference-counted constant pointer to AbstractNode
Definition AbstractNode.h:30
Node encapsulating a switch/case structure based on a boolean key or a function key.
Definition Case.h:34
static Pointer New(std::string const &key, Map const &cases, Dictionary::Pointer registry={})
Create a case node pointing to a key in the registry.
Definition Case.h:45
NLSStatus prepare(MrProt &protocol, SeqLim &limits, SeqExpo &exports) override
Prepare the selected child, throw an exception if unknown value.
Definition Case.h:58
NLSStatus run(MrProt &protocol, SeqLim &limits, SeqExpo &exports) override
Run the selected child, throw an exception if unknown value.
Definition Case.h:77
FlexibleFunction< T > Function
Case function.
Definition Case.h:40
Case & setRegistry(Dictionary::Pointer registry) override
Set the registry to this node and its children.
Definition Case.h:85
std::shared_ptr< Case< T > > Pointer
Reference-counted pointer to Case<T>
Definition Case.h:42
std::unordered_map< T, AbstractNode::Pointer > Map
Dictionary to dispatch key to function.
Definition Case.h:37
MrProtocolData::SeqExpoRFInfo rfInfo() const override
Return the RF information of the selected child, throw an exception if unknown value.
Definition Case.h:109
uint64_t duration() const override
Return the duration of the selected child, throw an exception if unknown value.
Definition Case.h:100
#define ON_ERROR_RETURN_STATUS(S)
Execute statement S, and, if not MRRESULT_SUCCESS, return the status.
Definition helpers.h:21
boost::variant< std::function< T()>, std::function< T(Dictionary::Pointer)>, std::function< T(MrProt &)>, std::function< T(MrProt &, Dictionary::Pointer)>, std::function< T(MrProt &, SeqLim &, SeqExpo &)>, std::function< T(MrProt &, SeqLim &, SeqExpo &, Dictionary::Pointer)> > FlexibleFunction
A graph function (e.g. Case or If), with flexible arguments.
Definition FlexibleFunction.h:35
std::function< T(MrProt &, SeqLim &, SeqExpo &, Dictionary::Pointer)> function_adapter(FlexibleFunction< T > const &f)
Adapt a FlexibleFunction to its nominal form.
Definition FlexibleFunction.h:96
Top-level namespace of the msl library.
Definition acceleration.h:17