Cerata
A library to generate structural hardware designs
utils.h
1 // Copyright 2018-2019 Delft University of Technology
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <string>
18 #include <memory>
19 #include <utility>
20 #include <vector>
21 #include <algorithm>
22 #include <optional>
23 #include <fstream>
24 #include <unordered_map>
25 
26 namespace cerata {
27 
29 std::string version();
30 
32 std::string ToUpper(std::string str);
34 std::string ToLower(std::string str);
35 
37 std::string ToString(const std::unordered_map<std::string, std::string> &meta);
38 
40 struct Named {
41  public:
43  explicit Named(std::string name) : name_(std::move(name)) {}
45  [[nodiscard]] std::string name() const { return name_; }
47  void SetName(std::string name) { name_ = std::move(name); }
48 
50  virtual ~Named() = default;
51  private:
53  std::string name_;
54 };
55 
57 template<typename T>
58 bool Contains(const std::vector<std::shared_ptr<T>> &list, const std::shared_ptr<T> &item) {
59  return std::find(std::begin(list), std::end(list), item) != std::end(list);
60 }
61 
63 template<typename T>
64 bool Contains(const std::vector<std::weak_ptr<T>> &list, const std::weak_ptr<T> &item) {
65  return std::find(std::begin(list), std::end(list), item) != std::end(list);
66 }
67 
69 template<typename T>
70 bool Contains(const std::vector<T *> &list, T *item) {
71  return std::find(std::begin(list), std::end(list), item) != std::end(list);
72 }
73 
75 template<typename T>
76 void Append(std::vector<T> *list_a, const std::vector<T> &list_b) {
77  list_a->insert(list_a->end(), list_b.begin(), list_b.end());
78 }
79 
81 template<typename T>
82 std::vector<T> Merge(std::initializer_list<std::vector<T>> lists) {
83  std::vector<T> result;
84  for (const auto &l : lists) {
85  result.insert(result.end(), l.begin(), l.end());
86  }
87  return result;
88 }
89 
91 template<typename T, typename U>
92 std::vector<T> Merge(std::initializer_list<std::unordered_map<T, U>> lists) {
93  std::vector<T> result;
94  for (const auto &l : lists) {
95  result.insert(result.end(), l.begin(), l.end());
96  }
97  return result;
98 }
99 
107 template<typename T>
108 bool Remove(std::vector<std::shared_ptr<T>> *list, const std::shared_ptr<T> &item) {
109  auto it = std::find(std::begin(*list), std::end(*list), item);
110  if (it != std::end(*list)) {
111  list->erase(it);
112  return true;
113  } else {
114  return false;
115  }
116 }
117 
124 template<typename T>
125 std::vector<T *> ToRawPointers(const std::vector<std::shared_ptr<T>> &list) {
126  std::vector<T *> result;
127  for (auto &value : list) {
128  result.push_back(value.get());
129  }
130  return result;
131 }
132 
139 template<typename T>
140 std::vector<T *> ToRawPointers(const std::vector<std::unique_ptr<T>> &list) {
141  std::vector<T *> result;
142  for (auto &value : list) {
143  result.push_back(value.get());
144  }
145  return result;
146 }
147 
154 template<typename T, typename U>
155 std::vector<T *> As(const std::vector<U *> &vec) {
156  std::vector<T *> result;
157  for (auto &value : vec) {
158  result.push_back(dynamic_cast<T *>(value));
159  }
160  return result;
161 }
162 
164 template<typename T>
165 std::vector<T> Unique(const std::vector<T> &vec) {
166  auto result = vec;
167  auto last = std::unique(result.begin(), result.end());
168  result.erase(last, result.end());
169  return vec;
170 }
171 
173 template<typename T>
174 void FilterDuplicates(std::vector<T> *vec) {
175  auto last = std::unique(vec->begin(), vec->end());
176  vec->erase(last, vec->end());
177 }
178 
184 template<typename T>
185 std::string ToString() {
186  return "UNKOWN TYPE";
187 }
188 
190 void CreateDir(const std::string &dir_name);
191 
193 bool FileExists(const std::string &name);
194 
195 } // namespace cerata
cerata::Named::~Named
virtual ~Named()=default
Destructor.
cerata::ToLower
std::string ToLower(std::string str)
Convert string to lower-case.
Definition: utils.cc:30
cerata::Named::SetName
void SetName(std::string name)
Change the name of the object.
Definition: utils.h:47
cerata::Append
void Append(std::vector< T > *list_a, const std::vector< T > &list_b)
Append list b to list a.
Definition: utils.h:76
cerata::ToRawPointers
std::vector< T * > ToRawPointers(const std::vector< std::shared_ptr< T >> &list)
Convert a list of shared pointers to raw pointers.
Definition: utils.h:125
cerata::FilterDuplicates
void FilterDuplicates(std::vector< T > *vec)
Filter duplicate entries from a vector.
Definition: utils.h:174
cerata
Contains every Cerata class, function, etc...
Definition: api.h:41
cerata::As
std::vector< T * > As(const std::vector< U * > &vec)
Cast a vector of pointers to some other type.
Definition: utils.h:155
cerata::Named::name
std::string name() const
Return the name of the object.
Definition: utils.h:45
cerata::ToString
std::string ToString(Expression::Op operation)
Human-readable expression operator.
Definition: expression.cc:149
cerata::Remove
bool Remove(std::vector< std::shared_ptr< T >> *list, const std::shared_ptr< T > &item)
Remove an item from a vector, returning false if it was not in the vector, true otherwise.
Definition: utils.h:108
cerata::ToUpper
std::string ToUpper(std::string str)
Convert string to upper-case.
Definition: utils.cc:25
cerata::Named
Convenience structure for anything that is named. Names are case-sensitive.
Definition: utils.h:40
cerata::Merge
std::vector< T > Merge(std::initializer_list< std::vector< T >> lists)
Merge a list of vectors into one vector.
Definition: utils.h:82
cerata::CreateDir
void CreateDir(const std::string &dir_name)
Create a directory.
Definition: utils.cc:52
cerata::Contains
bool Contains(const std::vector< std::shared_ptr< T >> &list, const std::shared_ptr< T > &item)
Return true if vector contains item, false otherwise.
Definition: utils.h:58
cerata::Named::Named
Named(std::string name)
Named constructor.
Definition: utils.h:43
cerata::version
std::string version()
Return library version.
Definition: utils.cc:65
cerata::FileExists
bool FileExists(const std::string &name)
Check if file exists.
Definition: utils.cc:60
cerata::Unique
std::vector< T > Unique(const std::vector< T > &vec)
Return a copy of a vector without any duplicates.
Definition: utils.h:165