WRApplication
External kratos "application" for multiscale time integration.
Journal.hpp
Go to the documentation of this file.
1 
3 #pragma once
4 
5 // --- WRApplication Includes ---
7 
8 // --- Core Includes ---
9 #include "includes/kratos_parameters.h"
10 #include "containers/model.h"
11 #include "includes/smart_pointers.h"
12 
13 // --- STL Includes ---
14 #include <filesystem>
15 #include <functional>
16 
17 #ifdef KRATOS_SMP_CXX11
18 #include <thread>
19 #endif
20 
21 
22 namespace Kratos {
23 
24 
29 
30 
42 {
43 public:
45 
47 
49 
50  using size_type = std::size_t;
51 
52  using Extractor = std::function<value_type(const Model&)>;
53 
55 
56 public:
59 
62  JournalBase(const std::filesystem::path& rJournalPath);
63 
66  JournalBase(const std::filesystem::path& rJournalPath,
67  const Extractor& rExtractor);
68 
70  JournalBase(JournalBase&& rOther) = delete;
71 
75  explicit JournalBase(const JournalBase& rOther);
76 
78  JournalBase& operator=(JournalBase&& rOther) = delete;
79 
84 
86  const std::filesystem::path& GetFilePath() const noexcept;
87 
96  void SetExtractor(Extractor&& rExtractor);
97 
99  void SetExtractor(const Extractor& rExtractor);
100 
108  void Push(const Model& rModel);
109 
111  void Push(const value_type& rEntry);
112 
121  void Erase(const_iterator itEntry);
122 
133 
141  void EraseIf(const std::function<bool(const value_type&)>& rPredicate);
142 
145  void Clear();
146 
148  bool IsOpen() const noexcept;
149 
152  [[nodiscard]] const_iterator begin() const;
153 
156  [[nodiscard]] const_iterator end() const;
157 
159  size_type size() const;
160 
161 protected:
163  class ThreadID
164  {
165  private:
166  #ifdef KRATOS_SMP_OPENMP
167  using ID = int;
168  ID mID;
169  #elif defined(KRATOS_SMP_CXX11)
170  using ID = std::thread::thread_id;
171  ID mID;
172  #endif
173 
174  public:
176 
177  friend bool operator==(ThreadID Left, ThreadID Right)
178  {return Left.mID == Right.mID;}
179 
180  friend bool operator!=(ThreadID Left, ThreadID Right)
181  {return Left.mID != Right.mID;}
182 
183  friend std::ostream& operator<<(std::ostream& rStream, ThreadID threadID)
184  {return rStream << threadID.mID;}
185  }; // class ThreadID
186 
187 private:
189  void Erase(const std::set<const_iterator>& rLines);
190 
192  static bool IsValidEntry(const value_type& rEntry);
193 
195  [[nodiscard]] std::shared_ptr<iterator::FileAccess> Open(std::ios::openmode OpenMode = std::ios::in) const;
196 
197  std::filesystem::path mJournalPath;
198 
199  std::pair<Extractor,ThreadID> mExtractor;
200 
201  mutable std::weak_ptr<iterator::FileAccess> mpFileAccess;
202 }; // class JournalBase
203 
204 
211 class Journal
212 {
213 public:
215  class iterator
216  {
217  public:
218  friend class Journal;
219 
220  using iterator_category = std::forward_iterator_tag;
221 
222  using value_type = Parameters;
223 
224  using difference_type = std::ptrdiff_t;
225 
226  using pointer = void;
227 
228  using reference = void;
229 
230  public:
231  iterator() = delete;
232 
233  iterator(iterator&& rOther) = default;
234 
235  iterator(const iterator& rOther) = default;
236 
237  iterator& operator=(iterator&& rOther) = default;
238 
239  iterator& operator=(const iterator& rOther) = default;
240 
242 
244 
246 
248  {return mWrapped;}
249 
250  friend bool operator==(const iterator& rLeft, const iterator& rRight);
251 
252  friend bool operator!=(const iterator& rLeft, const iterator& rRight);
253 
254  private:
255  iterator(JournalBase::iterator&& rWrapped);
256 
257  JournalBase::iterator mWrapped;
258  }; // class iterator
259 
261 
263 
264  using size_type = std::size_t;
265 
266  using Extractor = std::function<Parameters(const Model&)>;
267 
269 
270 public:
273 
275  Journal(const std::filesystem::path& rJournalPath);
276 
278  Journal(const std::filesystem::path& rJournalPath,
279  const Extractor& rExtractor);
280 
282  Journal(Journal&& rOther) = delete;
283 
285  explicit Journal(const Journal& rOther) = default;
286 
288  Journal& operator=(Journal&& rOther) = delete;
289 
291  Journal& operator=(const Journal& rOther) = default;
292 
294  const std::filesystem::path& GetFilePath() const noexcept;
295 
297  void SetExtractor(Extractor&& rExtractor);
298 
300  void SetExtractor(const Extractor& rExtractor);
301 
303  void Push(const Model& rModel);
304 
306  void Erase(const_iterator itEntry);
307 
310 
312  void EraseIf(const std::function<bool(const value_type&)>& rPredicate);
313 
315  void Clear();
316 
318  bool IsOpen() const noexcept;
319 
321  [[nodiscard]] const_iterator begin() const;
322 
324  [[nodiscard]] const_iterator end() const;
325 
327  size_type size() const;
328 
329 private:
330  JournalBase mBase;
331 }; // class Journal
332 
333 
336 
337 
338 } // namespace Kratos
A read-only iterator over the items of a delimited file.
Definition: FileContentIterator.hpp:129
std::string value_type
Definition: FileContentIterator.hpp:133
A wrapper class for identifying threads.
Definition: Journal.hpp:164
friend std::ostream & operator<<(std::ostream &rStream, ThreadID threadID)
Definition: Journal.hpp:183
friend bool operator==(ThreadID Left, ThreadID Right)
Definition: Journal.hpp:177
friend bool operator!=(ThreadID Left, ThreadID Right)
Definition: Journal.hpp:180
A class for keeping administrational text data throughout an analysis.
Definition: Journal.hpp:42
std::function< value_type(const Model &)> Extractor
Definition: Journal.hpp:52
void SetExtractor(Extractor &&rExtractor)
Set the extractor handling the conversion from Model to a string.
JournalBase(JournalBase &&rOther)=delete
The move constructor is deleted because it's ambiguous what should happen to the associated file.
bool IsOpen() const noexcept
Check whether the associated file is opened by this object.
const_iterator begin() const
Create an iterator to the first line of the associated file.
iterator::value_type value_type
Definition: Journal.hpp:48
JournalBase(const JournalBase &rOther)
Copy constructor.
JournalBase(const std::filesystem::path &rJournalPath)
Construct a registry given an associated file path and a no-op extractor.
FileStringIterator iterator
Definition: Journal.hpp:44
KRATOS_CLASS_POINTER_DEFINITION(JournalBase)
size_type size() const
Count the number of lines in the file (including the last empty one).
JournalBase()
Construct the object with an empty file name (invalid).
JournalBase & operator=(const JournalBase &rOther)
Copy assignment operator.
const std::filesystem::path & GetFilePath() const noexcept
Get the path to the associated file.
void Push(const Model &rModel)
Call the extractor and append the results to the associated file.
JournalBase & operator=(JournalBase &&rOther)=delete
The move assignment operator is deleted because it's ambiguous what should happen to the associated f...
void Clear()
Delete the associated file.
JournalBase(const std::filesystem::path &rJournalPath, const Extractor &rExtractor)
Construct a registry given an associated file path and extractor.
const_iterator end() const
Create an iterator to the last (empty) line of the associated file.
void Erase(const_iterator itEntry)
Erase an entry from the associated file.
void EraseIf(const std::function< bool(const value_type &)> &rPredicate)
Erase all lines from the associated file matching the input predicate.
std::size_t size_type
Definition: Journal.hpp:50
An iterator wrapping JournalBase::iterator but with Parameters as value_type.
Definition: Journal.hpp:216
void pointer
Definition: Journal.hpp:226
iterator(const iterator &rOther)=default
Parameters value_type
Definition: Journal.hpp:222
std::forward_iterator_tag iterator_category
Definition: Journal.hpp:220
iterator & operator=(const iterator &rOther)=default
friend bool operator!=(const iterator &rLeft, const iterator &rRight)
friend bool operator==(const iterator &rLeft, const iterator &rRight)
void reference
Definition: Journal.hpp:228
JournalBase::iterator GetBase() const
Definition: Journal.hpp:247
iterator(iterator &&rOther)=default
value_type operator*() const
std::ptrdiff_t difference_type
Definition: Journal.hpp:224
iterator & operator=(iterator &&rOther)=default
A class for keeping administrational text data throughout an analysis.
Definition: Journal.hpp:212
Journal & operator=(const Journal &rOther)=default
Copy assignment operator.
void SetExtractor(Extractor &&rExtractor)
Set the extractor handling the conversion from Model to a string.
std::function< Parameters(const Model &)> Extractor
Definition: Journal.hpp:266
bool IsOpen() const noexcept
Check whether the associated file is opened by this object.
const_iterator begin() const
Create an iterator to the first line of the associated file.
Journal(const std::filesystem::path &rJournalPath, const Extractor &rExtractor)
Construct a registry given an associated file path and extractor.
iterator::value_type value_type
Definition: Journal.hpp:262
Journal()
Construct the object with an empty file name (invalid).
KRATOS_CLASS_POINTER_DEFINITION(Journal)
size_type size() const
Count the number of lines in the file (including the last empty one).
const std::filesystem::path & GetFilePath() const noexcept
Get the path to the associated file.
void Push(const Model &rModel)
Call the extractor and append the results to the associated file.
void Clear()
JournalBase::Clear()
Journal & operator=(Journal &&rOther)=delete
The move assignment operator is deleted because it's ambiguous what should happen to the associated f...
Journal(const std::filesystem::path &rJournalPath)
Construct a registry given an associated file path and a no-op extractor.
Journal(Journal &&rOther)=delete
The move constructor is deleted because it's ambiguous what should happen to the associated file.
Journal(const Journal &rOther)=default
Copy constructor.
const_iterator end() const
Create an iterator to the last (empty) line of the associated file.
void Erase(const_iterator itEntry)
Erase an entry from the associated file.
void EraseIf(const std::function< bool(const value_type &)> &rPredicate)
Erase all lines from the associated file matching the input predicate.
std::size_t size_type
Definition: Journal.hpp:264
Definition: AddIOToPython.hpp:12
Definition: CheckpointID.hpp:68