Lodestar
An integrated real-time control package in C++
FunctionBlock.hpp
1 //
2 // Created by Hamza El-Kebir on 12/23/21.
3 //
4 
5 #ifndef LODESTAR_FUNCTIONBLOCK_HPP
6 #define LODESTAR_FUNCTIONBLOCK_HPP
7 
8 
9 #include "Lodestar/blocks/Block.hpp"
10 #include "Lodestar/aux/TemplateTools.hpp"
11 
12 namespace ls {
13  namespace blocks {
14  namespace std {
15  template<typename TOutput = int, typename... TInputs>
16  class FunctionBlock :
17  public Block<
18  ::std::tuple<TInputs...>,
19  ::std::tuple<TOutput>,
20  ::std::tuple<::std::function<TOutput(TInputs...)>>
21  > {
22  public:
23  using Base =
24  Block<
25  ::std::tuple<TInputs...>,
26  ::std::tuple<TOutput>,
27  ::std::tuple<::std::function<TOutput(TInputs...)>>
28  >;
29  // TODO: Decide whether additional TInput parameters should be TParameters or not.
30  using Function = ::std::function<TOutput(TInputs...)>;
31 
33  {
34  bindEquation();
35  }
36 
37  FunctionBlock(const Function &fun)
38  {
39  this->template p<0>() = fun;
40  bindEquation();
41  }
42 
43  template<typename TTOutput, typename... TTInputs>
44  static FunctionBlock<TTOutput, TTInputs...>
45  make(const ::std::function<TTOutput(TTInputs...)> &fun)
46  {
47  return FunctionBlock<TTOutput, TTInputs...>{fun};
48  }
49 
50  protected:
51  void bindEquation()
52  {
53  this->equation = ::std::bind(
55  this,
56  ::std::placeholders::_1
57  );
58  }
59 
60  void triggerFunction(Base &b)
61  {
62  b.template o<0>() = ls::aux::TemplateTools::Executors::applyWrapped(
63  b.template p<0>(),
64  b.inputs);
65  }
66  };
67  }
68 
69  template<typename TOutput, typename... TInputs>
70  class BlockTraits<std::FunctionBlock<TOutput, TInputs...>> {
71  public:
72  static constexpr const BlockType blockType = BlockType::FunctionBlock;
73  enum {
74  directFeedthrough = true
75  };
76 
77  using type = std::FunctionBlock<TOutput, TInputs...>;
78  using Base = typename type::Base;
79 
80  enum {
81  kIns = Base::kIns,
82  kOuts = Base::kOuts,
83  kPars = Base::kPars
84  };
85 
86  static const ::std::array<::std::string, kIns> inTypes;
87  static const ::std::array<::std::string, kOuts> outTypes;
88  static const ::std::array<::std::string, kPars> parTypes;
89 
90  static const ::std::array<::std::string, kIns + kOuts> templateTypes;
91  };
92 
93  template<typename TOutput, typename... TInputs>
94  const ::std::array<::std::string, BlockTraits<std::FunctionBlock<TOutput, TInputs...>>::kIns> BlockTraits<std::FunctionBlock<TOutput, TInputs...>>::inTypes =
95  {demangle(typeid(TInputs).name())...};
96 
97  template<typename TOutput, typename... TInputs>
98  const ::std::array<::std::string, BlockTraits<std::FunctionBlock<TOutput, TInputs...>>::kOuts> BlockTraits<std::FunctionBlock<TOutput, TInputs...>>::outTypes =
99  {demangle(typeid(TOutput).name())};
100 
101  template<typename TOutput, typename... TInputs>
102  const ::std::array<::std::string, BlockTraits<std::FunctionBlock<TOutput, TInputs...>>::kPars> BlockTraits<std::FunctionBlock<TOutput, TInputs...>>::parTypes =
103  {demangle(typeid(::std::function<TOutput(TInputs...)>).name())};
104 
105  template<typename TOutput, typename... TInputs>
106  const ::std::array<::std::string, BlockTraits<std::FunctionBlock<TOutput, TInputs...>>::kIns + BlockTraits<std::FunctionBlock<TOutput, TInputs...>>::kOuts> BlockTraits<std::FunctionBlock<TOutput, TInputs...>>::templateTypes =
107  {demangle(typeid(TOutput).name()), demangle(typeid(TInputs).name())...};
108  }
109 }
110 
111 
112 #endif //LODESTAR_FUNCTIONBLOCK_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
Main Lodestar code.
Definition: BilinearTransformation.hpp:12
ls::blocks::BlockType::FunctionBlock
@ FunctionBlock
Function block.
ls::blocks::BlockTraits::outTypes
static const ::std::array<::std::string, kOuts > outTypes
Output types (as strings).
Definition: BlockTraits.hpp:61
ls::blocks::BlockTraits::blockType
static constexpr const BlockType blockType
Block type.
Definition: BlockTraits.hpp:42
ls::blocks::BlockTraits::templateTypes
static const ::std::array<::std::string, 1 > templateTypes
Template parameter types (as strings).
Definition: BlockTraits.hpp:66
ls::blocks::std::FunctionBlock
Definition: FunctionBlock.hpp:16
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::BlockTraits::kOuts
static const constexpr int kOuts
Number of output slots.
Definition: BlockTraits.hpp:54