Documentation of CSL
librarydependency.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 CSL_LIBRARY_DEPENDENCY
24 #define CSL_LIBRARY_DEPENDENCY
25 
26 #include <string>
27 #include <vector>
28 #include <iostream>
29 #include "abstract.h"
30 
31 namespace csl {
32 
33  class LibDependency {
34 
35  public:
36 
37  static LibDependency const& cmath() {
38  static LibDependency cmath;
39  static bool created = false;
40  if (!created) {
41  cmath.addInclude("cmath", true);
42  created = true;
43  }
44  return cmath;
45  }
46 
47  static LibDependency const& complex() {
48  static LibDependency cmath;
49  static bool created = false;
50  if (!created) {
51  cmath.addInclude("complex", true);
52  created = true;
53  }
54  return cmath;
55  }
56 
57  static LibDependency const& csl() {
58  static LibDependency csl;
59  static bool created = false;
60  if (!created) {
61  csl.addInclude("csl", true);
62  csl.addLib("-lcsl");
63  created = true;
64  }
65  return csl;
66  }
67 
68  static LibDependency const& marty() {
69  static LibDependency marty;
70  static bool created = false;
71  if (!created) {
72  marty.addInclude("marty", true);
73  marty.addLib("-lmarty");
74  created = true;
75  }
76  return marty;
77  }
78 
79  struct IncludeType {
80 
81  std::string name;
82  bool global;
83 
84  IncludeType(std::string const& t_name)
85  :name(t_name),
86  global(false)
87  {}
88 
89  IncludeType(std::string const& t_name,
90  bool t_global)
91  :name(t_name),
92  global(t_global)
93  {}
94 
95  bool operator==(IncludeType const& other) const {
96  return name == other.name;
97  }
98  };
99 
100  static std::vector<IncludeType> defaultInclude() {
101  return {};
102  }
103 
104  static std::vector<std::string> defaultLib() {
105  return {};
106  }
107 
108  public:
109 
110  std::vector<IncludeType> const& getInclude() const {
111  return include;
112  }
113  std::vector<std::string> const& getLibrary() const {
114  return library;
115  }
116  std::vector<Expr> const& getParams() const {
117  return params;
118  }
119 
120  std::vector<IncludeType>& getInclude() {
121  return include;
122  }
123  std::vector<std::string>& getLibrary() {
124  return library;
125  }
126  std::vector<Expr>& getParams() {
127  return params;
128  }
129 
130  void addInclude(std::string const& t_include,
131  bool global = false) {
132  IncludeType inc{t_include, global};
133  addInclude(inc);
134  }
135 
136  void addInclude(IncludeType const& t_include) {
137  for (const auto& inc : include)
138  if (inc == t_include)
139  return;
140  include.push_back(t_include);
141  }
142 
143  void addLib(std::string const& t_lib) {
144  for (const auto& lib : library)
145  if (lib == t_lib)
146  return;
147  library.insert(library.begin(), t_lib);
148  }
149 
150  void addParam(Expr const& expr) {
151  for (const auto& p : params)
152  if (expr == p)
153  return;
154  params.push_back(expr);
155  }
156 
157  void removeInclude(std::string const& t_include) {
158  for (auto iter = include.begin(); iter != include.end(); ++iter)
159  if (*iter == t_include) {
160  include.erase(iter);
161  return;
162  }
163  }
164 
165  void removeLib(std::string const& t_lib) {
166  for (auto iter = library.begin(); iter != library.end(); ++iter)
167  if (*iter == t_lib) {
168  library.erase(iter);
169  return;
170  }
171  }
172 
173  void removeParam(Expr const& expr) {
174  for (auto p = params.begin(); p != params.end(); ++p)
175  if (expr == *p) {
176  params.erase(p);
177  return;
178  }
179  }
180 
181  void printInclude(std::ostream& out) const {
182  for (const IncludeType& inc : include) {
183  char separator[2];
184  separator[0] = (inc.global) ? '<' : '\"';
185  separator[1] = (inc.global) ? '>' : '\"';
186  out << "#include " << separator[0] << inc.name << separator[1];
187  out << '\n';
188  }
189  }
190 
191  void printLib(std::ostream& out, bool clang = false) const {
192  for (const std::string& lib : library) {
193  if (!clang or lib != "-lgfortran")
194  out << lib << " ";
195  }
196  }
197 
198  bool containsInclude(std::string const &includeType) const
199  {
200  for (const auto &i : include)
201  if (includeType == i.name)
202  return true;
203  return false;
204  }
205 
206  bool containsLib(std::string const &libType) const
207  {
208  for (const auto &l : library)
209  if (libType == l)
210  return true;
211  return false;
212  }
213 
214  LibDependency& operator+=(LibDependency const& other) {
215  for (const auto& inc : other.include)
216  addInclude(inc);
217  for (const auto& lib : other.library)
218  addLib(lib);
219  for (const auto& p : other.params)
220  addParam(p);
221 
222  return *this;
223  }
224 
225  LibDependency operator+(LibDependency const& other) {
226  LibDependency newLib(*this);
227  return newLib += other;
228  }
229 
230  protected:
231 
232  std::vector<IncludeType> include = defaultInclude();
233 
234  std::vector<std::string> library = defaultLib();
235 
236  std::vector<Expr> params;
237  };
238 }
239 
240 #endif // ifndef CSL_LIBRARY_DEPENDENCY
Namespace for csl library.
Definition: abreviation.h:34
Expr operator+(const Expr &a, const Expr &b)
Shortcut function that allows to use arithmetic operator + with Expr (== shared_ptr<Abstract>).
Definition: abstract.cpp:1298
Definition: librarydependency.h:33
Definition: librarydependency.h:79
Base classes for all exprs in the program.
Expression type/.
Definition: abstract.h:1573