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"
33#include <unordered_map>
34#include <unordered_set>
38template <
typename FilePositionType = AbstractFilePosition>
39class AbstractIOHandlerImplCommon :
public AbstractIOHandlerImpl
45 ~AbstractIOHandlerImplCommon()
override;
52 std::unordered_map<Writable *, InvalidatableFile>
m_files;
53 std::unordered_set<InvalidatableFile> m_dirty;
57 PE_InvalidatableFile = 0,
64 std::unordered_map<Writable *, InvalidatableFile>::iterator,
66 getPossiblyExisting(std::string file);
99 std::shared_ptr<FilePositionType>
108 virtual std::shared_ptr<FilePositionType>
121 std::shared_ptr<FilePositionType>
const &, std::string) = 0;
124template <
typename FilePositionType>
125AbstractIOHandlerImplCommon<FilePositionType>::AbstractIOHandlerImplCommon(
130template <
typename FilePositionType>
131AbstractIOHandlerImplCommon<FilePositionType>::~AbstractIOHandlerImplCommon() =
134template <
typename FilePositionType>
137 std::unordered_map<Writable *, InvalidatableFile>::iterator,
139AbstractIOHandlerImplCommon<FilePositionType>::getPossiblyExisting(
143 auto it = std::find_if(
147 std::unordered_map<Writable *, InvalidatableFile>::value_type
const
149 return *entry.second == file && entry.second.valid();
153 InvalidatableFile name;
154 if (it == m_files.end())
162 newlyCreated =
false;
166 std::unordered_map<Writable *, InvalidatableFile>::iterator,
167 bool>(std::move(name), it, newlyCreated);
170template <
typename FilePositionType>
171void AbstractIOHandlerImplCommon<FilePositionType>::associateWithFile(
175 m_files[writable] = std::move(file);
178template <
typename FilePositionType>
185template <
typename FilePositionType>
189 if (auxiliary::ends_with(m_handler->directory,
"/"))
191 return m_handler->directory + fileName;
195 return m_handler->directory +
"/" + fileName;
199template <
typename FilePositionType>
202 Writable *writable,
bool preferParentFile)
204 auto getFileFromParent = [writable,
this]() {
205 auto file_it =
m_files.find(writable->parent);
209 s <<
"Parent Writable " << writable->parent <<
" of Writable "
210 << writable <<
" has no associated file.";
211 throw std::runtime_error(s.str());
213 auto file =
m_files.find(writable->parent)->second;
214 associateWithFile(writable, file);
217 if (preferParentFile && writable->parent)
219 return getFileFromParent();
223 auto it =
m_files.find(writable);
226 return m_files.find(writable)->second;
228 else if (writable->parent)
230 return getFileFromParent();
234 throw std::runtime_error(
235 "Internal error: Root object must be opened explicitly.");
240template <
typename FilePositionType>
241std::shared_ptr<FilePositionType>
245 std::shared_ptr<AbstractFilePosition> res;
247 if (writable->abstractFilePosition)
249 res = writable->abstractFilePosition;
251 else if (writable->parent)
253 res = writable->parent->abstractFilePosition;
257 res = std::make_shared<FilePositionType>();
261 writable->abstractFilePosition = res;
263 return std::dynamic_pointer_cast<FilePositionType>(res);
266template <
typename FilePositionType>
267std::shared_ptr<FilePositionType>
269 Writable *writable, std::string extend)
271 if (!auxiliary::starts_with(extend,
'/'))
273 extend =
"/" + extend;
278 writable->abstractFilePosition = res;
Interface for communicating between logical and physically persistent data.
Definition AbstractIOHandler.hpp:206
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:52
std::string fullPath(InvalidatableFile)
Definition AbstractIOHandlerImplCommon.hpp:179
InvalidatableFile refreshFileFromParent(Writable *writable, bool preferParentFile)
Get the writable's containing file.
Definition AbstractIOHandlerImplCommon.hpp:201
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:268
virtual std::shared_ptr< FilePositionType > extendFilePosition(std::shared_ptr< FilePositionType > const &, std::string)=0
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:242
Definition AbstractIOHandlerImpl.hpp:36
Layer to mirror structure of logical data and persistent data in file.
Definition Writable.hpp:76
Public definitions of openPMD-api.
Definition Date.cpp:29
Wrapper around a shared pointer to:
Definition InvalidatableFile.hpp:44