WRApplication
External kratos "application" for multiscale time integration.
FileContentIterator.hpp
Go to the documentation of this file.
1 
3 #pragma once
4 
5 // --- Core Includes ---
6 #include "includes/lock_object.h" // Kratos::LockObject
7 
8 // --- STL Includes ---
9 #include <iterator> // std::forward_iterator_tag, std::ptrdiff_t
10 #include <optional> // std::optional
11 #include <fstream> // std::fstream
12 #include <utility> // std::pair
13 #include <mutex> // std::unique_lock
14 
15 
16 namespace Kratos {
17 
18 
23 
24 
46 {
47 public:
48  using FileAccess = std::optional<std::pair<
49  std::fstream,
50  LockObject
51  >>;
52 
53  using iterator_category = std::forward_iterator_tag;
54 
57  using value_type = std::pair<FileContentIterator,FileContentIterator>;
58 
59  using difference_type = std::ptrdiff_t;
60 
61  using pointer = void;
62 
63  using reference = void;
64 
65 public:
66  FileContentIterator(std::shared_ptr<FileAccess>&& rpFileAccess,
67  char delimiter = '\n');
68 
69  FileContentIterator(const std::shared_ptr<FileAccess>& rpFileAccess,
70  char delimiter = '\n');
71 
72  FileContentIterator() = delete;
73 
75 
76  FileContentIterator(const FileContentIterator& rOther) = default;
77 
79 
81 
83 
85 
87 
89  std::string value() const;
90 
91  friend bool operator==(const FileContentIterator& rLeft, const FileContentIterator& rRight);
92 
93  friend bool operator!=(const FileContentIterator& rLeft, const FileContentIterator& rRight);
94 
95  friend bool operator<(const FileContentIterator& rLeft, const FileContentIterator& rRight);
96 
98  void SeekEOF();
99 
100 private:
101  using Position = std::fstream::pos_type;
102 
104  Position GetPosition() const noexcept;
105 
107  bool SeekNext(char Target);
108 
110  void StoreState();
111 
113  void RestoreState() const;
114 
115  mutable std::shared_ptr<FileAccess> mpFileAccess;
116 
117  char mDelimiter;
118 
119  Position mPosition;
120 }; // class FileContentIterator
121 
122 
129 {
130 public:
131  using iterator_category = std::forward_iterator_tag;
132 
133  using value_type = std::string;
134 
135  using difference_type = std::ptrdiff_t;
136 
137  using pointer = void;
138 
139  using reference = void;
140 
142 
143 public:
144  FileStringIterator(std::shared_ptr<FileAccess>&& rpFileAccess,
145  char delimiter = '\n');
146 
147  FileStringIterator(const std::shared_ptr<FileAccess>& rpFileAccess,
148  char delimiter = '\n');
149 
151 
152  FileStringIterator(const FileStringIterator& rOther) = default;
153 
155 
157 
159 
161 
163 
165  void SeekEOF();
166 
167  friend bool operator==(const FileStringIterator& rLeft, const FileStringIterator& rRight);
168 
169  friend bool operator!=(const FileStringIterator& rLeft, const FileStringIterator& rRight);
170 
171  friend bool operator<(const FileStringIterator& rLeft, const FileStringIterator& rRight);
172 
173 private:
174  FileContentIterator mWrapped;
175 }; // class FileStringIterator
176 
177 
180 
181 
182 } // namespace Kratos
Iterator interface around a file pointer.
Definition: FileContentIterator.hpp:46
void pointer
Definition: FileContentIterator.hpp:61
std::pair< FileContentIterator, FileContentIterator > value_type
An iterator pair {begin,end} representing a range within the file.
Definition: FileContentIterator.hpp:57
friend bool operator<(const FileContentIterator &rLeft, const FileContentIterator &rRight)
FileContentIterator(FileContentIterator &&rOther)=default
std::optional< std::pair< std::fstream, LockObject > > FileAccess
Definition: FileContentIterator.hpp:51
FileContentIterator(std::shared_ptr< FileAccess > &&rpFileAccess, char delimiter='\n')
void SeekEOF()
Set the iterator to the end of the file.
FileContentIterator & operator=(const FileContentIterator &rOther)=default
FileContentIterator operator++(int)
FileContentIterator(const std::shared_ptr< FileAccess > &rpFileAccess, char delimiter='\n')
std::forward_iterator_tag iterator_category
Definition: FileContentIterator.hpp:53
friend bool operator!=(const FileContentIterator &rLeft, const FileContentIterator &rRight)
FileContentIterator & operator++()
friend bool operator==(const FileContentIterator &rLeft, const FileContentIterator &rRight)
FileContentIterator & operator=(FileContentIterator &&rOther)=default
void reference
Definition: FileContentIterator.hpp:63
FileContentIterator(const FileContentIterator &rOther)=default
value_type operator*() const
std::ptrdiff_t difference_type
Definition: FileContentIterator.hpp:59
std::string value() const
Read the associated range from the file into a string.
A read-only iterator over the items of a delimited file.
Definition: FileContentIterator.hpp:129
FileStringIterator(const FileStringIterator &rOther)=default
void pointer
Definition: FileContentIterator.hpp:137
FileStringIterator(FileStringIterator &&rOther)=default
void SeekEOF()
Set the iterator to the end of the file.
FileContentIterator::FileAccess FileAccess
Definition: FileContentIterator.hpp:141
FileStringIterator(std::shared_ptr< FileAccess > &&rpFileAccess, char delimiter='\n')
std::forward_iterator_tag iterator_category
Definition: FileContentIterator.hpp:131
friend bool operator==(const FileStringIterator &rLeft, const FileStringIterator &rRight)
void reference
Definition: FileContentIterator.hpp:139
FileStringIterator & operator++()
FileStringIterator operator++(int)
FileStringIterator & operator=(const FileStringIterator &rOther)=default
FileStringIterator(const std::shared_ptr< FileAccess > &rpFileAccess, char delimiter='\n')
value_type operator*() const
friend bool operator<(const FileStringIterator &rLeft, const FileStringIterator &rRight)
std::ptrdiff_t difference_type
Definition: FileContentIterator.hpp:135
FileStringIterator & operator=(FileStringIterator &&rOther)=default
std::string value_type
Definition: FileContentIterator.hpp:133
friend bool operator!=(const FileStringIterator &rLeft, const FileStringIterator &rRight)
Definition: AddIOToPython.hpp:12
Definition: CheckpointID.hpp:68