Documentation of CSL
functional.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
23 #ifndef FUNCTIONAL_H_INCLUDED
24 #define FUNCTIONAL_H_INCLUDED
25 
26 #include <tuple>
27 #include "abstract.h"
28 #include "interface.h"
29 #include "utils.h"
30 
31 namespace csl {
32 
33 template<class ... Args>
34 class Functional {
35 
36  private:
37 
38  Expr init;
39 
40  std::tuple<Args...> args;
41 
42  public:
43 
44  Functional(Expr const& t_init,
45  Args ...t_args)
46  :init(t_init)
47  {
48  args = std::tuple<Args...>(t_args...);
49  }
50 
51  ~Functional() {}
52 
53  template<class ... T_Args>
54  Expr operator()(T_Args ... t_args)
55  {
56  std::tuple<T_Args...> new_args(t_args...);
57  static_assert(sizeof...(Args) >= sizeof...(T_Args));
58 
59  Expr res = DeepCopy(init);
60  Replacer<Args...> rep;
61  rep.template replace<0, T_Args...>(
62  res,
63  args,
64  new_args);
65 
66  return res;
67  }
68 
69  template<class ...T_Args>
70  friend
71  std::ostream& operator<<(
72  std::ostream& out,
74 
75  template<class ...Arg1>
76  struct Replacer {
77 
78  template<size_t i, class ...Arg2>
79  Expr replace(
80  Expr & init,
81  std::tuple<Arg1...> & oldArg,
82  std::tuple<Arg2...> & newArg)
83  {
84  if constexpr (i == sizeof...(Arg2))
85  return init;
86  else {
87  init = Replaced(init,
88  std::get<i>(oldArg),
89  std::get<i>(newArg));
90  return replace<i + 1, Arg2...>(
91  init,
92  oldArg,
93  newArg);
94  }
95  }
96  };
97 };
98 
99 }
100 
101 #endif
Namespace for csl library.
Definition: abreviation.h:34
Expr DeepCopy(const Abstract *expr)
See DeepCopy(const Expr& expr).
Definition: utils.cpp:113
Definition: functional.h:34
Definition: functional.h:76
Base classes for all exprs in the program.
Expression type/.
Definition: abstract.h:1573