24 #include "openPMD/IO/AbstractFilePosition.hpp"
25 #include "openPMD/IO/AbstractIOHandler.hpp"
26 #include "openPMD/IO/AbstractIOHandlerImpl.hpp"
27 #include "openPMD/IO/InvalidatableFile.hpp"
28 #include "openPMD/auxiliary/StringManip.hpp"
29 #include "openPMD/backend/Writable.hpp"
31 #include <unordered_map>
32 #include <unordered_set>
36 template <
typename FilePositionType = AbstractFilePosition>
50 std::unordered_map<Writable *, InvalidatableFile>
m_files;
51 std::unordered_set<InvalidatableFile> m_dirty;
55 PE_InvalidatableFile = 0,
62 std::unordered_map<Writable *, InvalidatableFile>::iterator,
64 getPossiblyExisting(std::string file);
97 std::shared_ptr<FilePositionType>
106 virtual std::shared_ptr<FilePositionType>
119 std::shared_ptr<FilePositionType>
const &, std::string) = 0;
122 template <
typename FilePositionType>
128 template <
typename FilePositionType>
129 AbstractIOHandlerImplCommon<FilePositionType>::~AbstractIOHandlerImplCommon() =
132 template <
typename FilePositionType>
135 std::unordered_map<Writable *, InvalidatableFile>::iterator,
137 AbstractIOHandlerImplCommon<FilePositionType>::getPossiblyExisting(
141 auto it = std::find_if(
145 std::unordered_map<Writable *, InvalidatableFile>::value_type
const
147 return *entry.second == file && entry.second.valid();
151 InvalidatableFile name;
152 if (it == m_files.end())
160 newlyCreated =
false;
164 std::unordered_map<Writable *, InvalidatableFile>::iterator,
165 bool>(std::move(name), it, newlyCreated);
168 template <
typename FilePositionType>
169 void AbstractIOHandlerImplCommon<FilePositionType>::associateWithFile(
170 Writable *writable, InvalidatableFile file)
173 m_files[writable] = std::move(file);
176 template <
typename FilePositionType>
180 return fullPath(*fileName);
183 template <
typename FilePositionType>
187 if (auxiliary::ends_with(m_handler->directory,
"/"))
189 return m_handler->directory + fileName;
193 return m_handler->directory +
"/" + fileName;
197 template <
typename FilePositionType>
200 Writable *writable,
bool preferParentFile)
202 auto getFileFromParent = [writable,
this]() {
203 auto file = m_files.find(writable->parent)->second;
204 associateWithFile(writable, file);
207 if (preferParentFile && writable->parent)
209 return getFileFromParent();
213 auto it = m_files.find(writable);
214 if (it != m_files.end())
216 return m_files.find(writable)->second;
218 else if (writable->parent)
220 return getFileFromParent();
224 throw std::runtime_error(
225 "Internal error: Root object must be opened explicitly.");
230 template <
typename FilePositionType>
231 std::shared_ptr<FilePositionType>
235 std::shared_ptr<AbstractFilePosition> res;
237 if (writable->abstractFilePosition)
239 res = writable->abstractFilePosition;
241 else if (writable->parent)
243 res = writable->parent->abstractFilePosition;
247 res = std::make_shared<FilePositionType>();
251 writable->abstractFilePosition = res;
253 return std::dynamic_pointer_cast<FilePositionType>(res);
256 template <
typename FilePositionType>
257 std::shared_ptr<FilePositionType>
259 Writable *writable, std::string extend)
261 if (!auxiliary::starts_with(extend,
'/'))
263 extend =
"/" + extend;
265 auto oldPos = setAndGetFilePosition(writable,
false);
266 auto res = extendFilePosition(oldPos, extend);
268 writable->abstractFilePosition = res;
Interface for communicating between logical and physically persistent data.
Definition: AbstractIOHandler.hpp:186
Definition: AbstractIOHandlerImplCommon.hpp:38
virtual std::shared_ptr< FilePositionType > extendFilePosition(std::shared_ptr< FilePositionType > const &, std::string)=0
std::unordered_map< Writable *, InvalidatableFile > m_files
map each Writable to its associated file contains only the filename, without the OS path
Definition: AbstractIOHandlerImplCommon.hpp:50
std::string fullPath(InvalidatableFile)
Definition: AbstractIOHandlerImplCommon.hpp:177
InvalidatableFile refreshFileFromParent(Writable *writable, bool preferParentFile)
Get the writable's containing file.
Definition: AbstractIOHandlerImplCommon.hpp:199
virtual std::shared_ptr< FilePositionType > setAndGetFilePosition(Writable *writable, std::string extend)
Figure out the file position of the writable and extend it.
Definition: AbstractIOHandlerImplCommon.hpp:258
virtual std::string filePositionToString(std::shared_ptr< FilePositionType >)=0
std::shared_ptr< FilePositionType > setAndGetFilePosition(Writable *writable, bool write=true)
Figure out the file position of the writable.
Definition: AbstractIOHandlerImplCommon.hpp:232
Definition: AbstractIOHandlerImpl.hpp:36
Layer to mirror structure of logical data and persistent data in file.
Definition: Writable.hpp:75
Public definitions of openPMD-api.
Definition: Date.cpp:29
Wrapper around a shared pointer to:
Definition: InvalidatableFile.hpp:44