Lodestar
An integrated real-time control package in C++
SwitchBlock.hpp
1 //
2 // Created by Hamza El-Kebir on 12/23/21.
3 //
4 
5 #ifndef LODESTAR_SWITCHBLOCK_HPP
6 #define LODESTAR_SWITCHBLOCK_HPP
7 
8 #include "Lodestar/blocks/Block.hpp"
9 
10 namespace ls {
11  namespace blocks {
12  namespace std {
13  enum class SwitchBlockParameter {
14  Parametric,
15  AdditionalInput
16  };
17 
18  template<typename TType,
19  SwitchBlockParameter TPar = SwitchBlockParameter::Parametric>
20  class SwitchBlock {
21  static_assert(::std::is_same<TType, TType>::value,
22  "SwitchBlock not defined for this type.");
23  };
24 
25  template<typename TType>
26  class SwitchBlock<TType, SwitchBlockParameter::Parametric> :
27  public Block<
28  ::std::tuple<TType, TType>,
29  ::std::tuple<TType>,
30  ::std::tuple<bool>
31  > {
32  public:
33  using Base =
34  Block<
35  ::std::tuple<TType, TType>,
36  ::std::tuple<TType>,
37  ::std::tuple<bool>
38  >;
39 
41 
42  SwitchBlock()
43  {
44  state(false);
45  bindEquation();
46  }
47 
48  SwitchBlock(const bool switchState)
49  {
50  state(switchState);
51  bindEquation();
52  }
53 
54  bool &state(bool setting)
55  {
56  this->template p<0>() = setting;
57  return this->template p<0>();
58  }
59 
60  bool state() const
61  {
62  return this->template p<0>();
63  }
64 
65  bool &state()
66  {
67  return this->template p<0>();
68  }
69 
70  void toggle()
71  {
72  state(!state());
73  }
74 
75  protected:
76  void bindEquation()
77  {
78  this->equation = ::std::bind(
79  &type::triggerFunction,
80  this,
81  ::std::placeholders::_1
82  );
83  }
84 
85  void triggerFunction(Base &b)
86  {
87  if (state())
88  this->template o<0>() = this->template i<1>();
89  else
90  this->template o<0>() = this->template i<0>();
91  }
92  };
93 
94  template<typename TType>
96  public Block<
97  ::std::tuple<TType, TType, bool>,
98  ::std::tuple<TType>,
99  BlockProto::empty
100  > {
101  public:
102  using Base =
103  Block<
104  ::std::tuple<TType, TType, bool>,
105  ::std::tuple<TType>,
106  BlockProto::empty
107  >;
108 
110 
111  SwitchBlock()
112  {
113  state(false);
114  bindEquation();
115  }
116 
117  SwitchBlock(const bool switchState)
118  {
119  state(switchState);
120  bindEquation();
121  }
122 
123  Signal<bool> &state(bool setting)
124  {
125  this->template i<2>() = setting;
126  return this->template i<2>();
127  }
128 
129  Signal<bool> &state(const Signal<bool> &setting)
130  {
131  this->template i<2>() = setting;
132  return this->template i<2>();
133  }
134 
135  const Signal<bool> &state() const
136  {
137  return this->template i<2>();
138  }
139 
140  Signal<bool> &state()
141  {
142  return this->template i<2>();
143  }
144 
145  void toggle()
146  {
147  state(!state().object);
148  }
149 
150  protected:
151  void bindEquation()
152  {
153  this->equation = ::std::bind(
154  &type::triggerFunction,
155  this,
156  ::std::placeholders::_1
157  );
158  }
159 
160  void triggerFunction(Base &b)
161  {
162  if (state().object)
163  this->template o<0>() = this->template i<1>();
164  else
165  this->template o<0>() = this->template i<0>();
166  }
167  };
168 
169  }
170 
171  template<typename TType>
173  public:
174  static constexpr const BlockType blockType = BlockType::SwitchBlock;
175  enum {
176  directFeedthrough = true
177  };
178 
180  using Base = typename type::Base;
181 
182  enum {
183  kIns = Base::kIns,
184  kOuts = Base::kOuts,
185  kPars = Base::kPars
186  };
187 
188  static const ::std::array<::std::string, kIns> inTypes;
189  static const ::std::array<::std::string, kOuts> outTypes;
190  static const ::std::array<::std::string, kPars> parTypes;
191 
192  static const ::std::array<::std::string, 2> templateTypes;
193  };
194 
195  template<typename TType>
196  const ::std::array<::std::string, BlockTraits<std::SwitchBlock<TType, std::SwitchBlockParameter::AdditionalInput>>::kIns> BlockTraits<std::SwitchBlock<TType, std::SwitchBlockParameter::AdditionalInput>>::inTypes =
197  {demangle(typeid(TType).name())};
198 
199  template<typename TType>
200  const ::std::array<::std::string, BlockTraits<std::SwitchBlock<TType, std::SwitchBlockParameter::AdditionalInput>>::kOuts> BlockTraits<std::SwitchBlock<TType, std::SwitchBlockParameter::AdditionalInput>>::outTypes =
201  {demangle(typeid(TType).name())};
202 
203  template<typename TType>
204  const ::std::array<::std::string, BlockTraits<std::SwitchBlock<TType, std::SwitchBlockParameter::AdditionalInput>>::kPars> BlockTraits<std::SwitchBlock<TType, std::SwitchBlockParameter::AdditionalInput>>::parTypes =
205  {};
206 
207  template<typename TType>
208  const ::std::array<::std::string, 2> BlockTraits<std::SwitchBlock<TType, std::SwitchBlockParameter::AdditionalInput>>::templateTypes =
209  {demangle(typeid(TType).name()), demangle(typeid(std::SwitchBlockParameter::AdditionalInput).name())};
210 
211  template<typename TType>
212  class BlockTraits<std::SwitchBlock<TType, std::SwitchBlockParameter::Parametric>> {
213  public:
214  static constexpr const BlockType blockType = BlockType::SwitchBlock;
215  enum {
216  directFeedthrough = true
217  };
218 
220  using Base = typename type::Base;
221 
222  enum {
223  kIns = Base::kIns,
224  kOuts = Base::kOuts,
225  kPars = Base::kPars
226  };
227 
228  static const ::std::array<::std::string, kIns> inTypes;
229  static const ::std::array<::std::string, kOuts> outTypes;
230  static const ::std::array<::std::string, kPars> parTypes;
231 
232  static const ::std::array<::std::string, 2> templateTypes;
233  };
234 
235  template<typename TType>
236  const ::std::array<::std::string, BlockTraits<std::SwitchBlock<TType, std::SwitchBlockParameter::Parametric>>::kIns> BlockTraits<std::SwitchBlock<TType, std::SwitchBlockParameter::Parametric>>::inTypes =
237  {demangle(typeid(TType).name())};
238 
239  template<typename TType>
240  const ::std::array<::std::string, BlockTraits<std::SwitchBlock<TType, std::SwitchBlockParameter::Parametric>>::kOuts> BlockTraits<std::SwitchBlock<TType, std::SwitchBlockParameter::Parametric>>::outTypes =
241  {demangle(typeid(TType).name())};
242 
243  template<typename TType>
244  const ::std::array<::std::string, BlockTraits<std::SwitchBlock<TType, std::SwitchBlockParameter::Parametric>>::kPars> BlockTraits<std::SwitchBlock<TType, std::SwitchBlockParameter::Parametric>>::parTypes =
245  {"bool"};
246 
247  template<typename TType>
248  const ::std::array<::std::string, 2> BlockTraits<std::SwitchBlock<TType, std::SwitchBlockParameter::Parametric>>::templateTypes =
249  {demangle(typeid(TType).name()), demangle(typeid(std::SwitchBlockParameter::Parametric).name())};
250 
251 
252  }
253 }
254 
255 
256 #endif //LODESTAR_SWITCHBLOCK_HPP
ls::blocks::BlockTraits::directFeedthrough
static constexpr const bool directFeedthrough
Whether or not the block has direct feedthrough.
Definition: BlockTraits.hpp:44
ls::blocks::BlockTraits
A traits object that exposes information about TBlock.
Definition: BlockTraits.hpp:37
ls::blocks::BlockTraits::inTypes
static const ::std::array<::std::string, kIns > inTypes
Input types (as strings).
Definition: BlockTraits.hpp:59
ls::blocks::BlockType
BlockType
Block type information.
Definition: BlockType.hpp:25
ls::blocks::std::SwitchBlock
Definition: SwitchBlock.hpp:20
ls
Main Lodestar code.
Definition: BilinearTransformation.hpp:12
ls::blocks::Signal
Definition: Signal.hpp:22
ls::blocks::BlockTraits::outTypes
static const ::std::array<::std::string, kOuts > outTypes
Output types (as strings).
Definition: BlockTraits.hpp:61
ls::blocks::std::SwitchBlock< TType, SwitchBlockParameter::AdditionalInput >
Definition: SwitchBlock.hpp:95
ls::blocks::BlockTraits::blockType
static constexpr const BlockType blockType
Block type.
Definition: BlockTraits.hpp:42
ls::blocks::BlockType::SwitchBlock
@ SwitchBlock
Switch (manual) block.
ls::blocks::BlockTraits::templateTypes
static const ::std::array<::std::string, 1 > templateTypes
Template parameter types (as strings).
Definition: BlockTraits.hpp:66
ls::blocks::Block
Generic base template class for all tuple-based Block instances.
Definition: Block.hpp:45
ls::blocks::BlockTraits::kIns
static const constexpr int kIns
Number of input slots.
Definition: BlockTraits.hpp:52
ls::blocks::BlockTraits::parTypes
static const ::std::array<::std::string, kPars > parTypes
Parameter types (as strings).
Definition: BlockTraits.hpp:63
ls::blocks::BlockTraits::kPars
static const constexpr int kPars
Number of parameters.
Definition: BlockTraits.hpp:56
ls::blocks::std::SwitchBlock< TType, SwitchBlockParameter::Parametric >
Definition: SwitchBlock.hpp:26
ls::blocks::BlockTraits::kOuts
static const constexpr int kOuts
Number of output slots.
Definition: BlockTraits.hpp:54