From 54b5810ca6853a7ad4cbecb0fda0f823a8458b72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Mon, 21 Feb 2022 11:22:18 +0100 Subject: [PATCH] [wip] partially working implementation --- include/openPMD/backend/Attributable.hpp | 2 +- include/openPMD/backend/BaseRecord.hpp | 33 +++++++++++++++---- .../openPMD/backend/BaseRecordComponent.hpp | 2 ++ .../openPMD/backend/MeshRecordComponent.hpp | 3 ++ src/Mesh.cpp | 6 ---- src/RecordComponent.cpp | 3 ++ src/backend/BaseRecordComponent.cpp | 4 +++ src/backend/MeshRecordComponent.cpp | 10 ++++-- src/backend/PatchRecordComponent.cpp | 1 + 9 files changed, 48 insertions(+), 16 deletions(-) diff --git a/include/openPMD/backend/Attributable.hpp b/include/openPMD/backend/Attributable.hpp index 39b4694f27..e7d13ee9a9 100644 --- a/include/openPMD/backend/Attributable.hpp +++ b/include/openPMD/backend/Attributable.hpp @@ -122,7 +122,7 @@ class Attributable template friend class BaseRecord; template friend class BaseRecordInterface; - template friend class internal::BaseRecordData; + template friend class internal::BaseRecordData; template < typename T, typename T_key, diff --git a/include/openPMD/backend/BaseRecord.hpp b/include/openPMD/backend/BaseRecord.hpp index 3915fd9c7c..868592fc4c 100644 --- a/include/openPMD/backend/BaseRecord.hpp +++ b/include/openPMD/backend/BaseRecord.hpp @@ -138,6 +138,12 @@ class BaseRecord using iterator = typename T_container::iterator; using const_iterator = typename T_container::const_iterator; + // this avoids object slicing + operator T_RecordComponent() const + { + return {m_baseRecordData}; + } + virtual ~BaseRecord() = default; mapped_type &operator[](key_type const &key) override; @@ -176,6 +182,8 @@ class BaseRecord protected: void readBase(); + void datasetDefined() override; + private: void flush(std::string const &) final; virtual void flush_impl(std::string const &) = 0; @@ -235,12 +243,11 @@ BaseRecord::operator[](key_type const &key) "A scalar component can not be contained at " "the same time as one or more regular components."); - mapped_type &ret = T_container::operator[](key); if (keyScalar) { - get().m_containsScalar = true; - ret.parent() = this->parent(); + datasetDefined(); } + mapped_type &ret = keyScalar ? *this : T_container::operator[](key); return ret; } } @@ -261,12 +268,12 @@ inline auto BaseRecord::operator[](key_type &&key) "A scalar component can not be contained at " "the same time as one or more regular components."); - mapped_type &ret = T_container::operator[](std::move(key)); if (keyScalar) { - get().m_containsScalar = true; - ret.parent() = this->parent(); + datasetDefined(); } + mapped_type &ret = + keyScalar ? *this : T_container::operator[](std::move(key)); return ret; } } @@ -421,4 +428,18 @@ inline bool BaseRecord::dirtyRecursive() const } return false; } + +template +void BaseRecord::datasetDefined() +{ + // If the RecordComponent API of this object has been used, then the record + // is a scalar one + T_container::emplace( + RecordComponent::SCALAR, + // need to construct it in this line already, construction inside + // emplace is impossible due to private constructors + T_RecordComponent{m_baseRecordData}); + get().m_containsScalar = true; + T_RecordComponent::datasetDefined(); +} } // namespace openPMD diff --git a/include/openPMD/backend/BaseRecordComponent.hpp b/include/openPMD/backend/BaseRecordComponent.hpp index ea98daa6fa..8d436232be 100644 --- a/include/openPMD/backend/BaseRecordComponent.hpp +++ b/include/openPMD/backend/BaseRecordComponent.hpp @@ -125,6 +125,8 @@ class BaseRecordComponent : public Attributable BaseRecordComponent(std::shared_ptr); + virtual void datasetDefined(); + private: BaseRecordComponent(); }; // BaseRecordComponent diff --git a/include/openPMD/backend/MeshRecordComponent.hpp b/include/openPMD/backend/MeshRecordComponent.hpp index d869b9c309..6fcc92e7c9 100644 --- a/include/openPMD/backend/MeshRecordComponent.hpp +++ b/include/openPMD/backend/MeshRecordComponent.hpp @@ -79,6 +79,9 @@ class MeshRecordComponent : public RecordComponent * @return A reference to this RecordComponent. */ template MeshRecordComponent &makeConstant(T); + +protected: + void datasetDefined() override; }; template std::vector MeshRecordComponent::position() const diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 517c2ce800..e061e00faa 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -226,12 +226,8 @@ void Mesh::flush_impl(std::string const &name) if (scalar()) { MeshRecordComponent &mrc = at(RecordComponent::SCALAR); - mrc.parent() = parent(); mrc.flush(name); IOHandler()->flush(); - writable().abstractFilePosition = - mrc.writable().abstractFilePosition; - written() = true; } else { @@ -248,8 +244,6 @@ void Mesh::flush_impl(std::string const &name) for (auto &comp : *this) { comp.second.flush(name); - writable().abstractFilePosition = - comp.second.writable().abstractFilePosition; } } else diff --git a/src/RecordComponent.cpp b/src/RecordComponent.cpp index ad4ca07a83..62e4456260 100644 --- a/src/RecordComponent.cpp +++ b/src/RecordComponent.cpp @@ -88,6 +88,8 @@ RecordComponent &RecordComponent::resetDataset(Dataset d) throw error::WrongAPIUsage( "[RecordComponent] Must set specific datatype."); } + + datasetDefined(); // if( d.extent.empty() ) // throw std::runtime_error("Dataset extent must be at least 1D."); if (std::any_of( @@ -176,6 +178,7 @@ RecordComponent &RecordComponent::makeEmpty(Dataset d) if (rc.m_dataset.extent.size() == 0) throw std::runtime_error("Dataset extent must be at least 1D."); + datasetDefined(); rc.m_isEmpty = true; dirty() = true; if (!written()) diff --git a/src/backend/BaseRecordComponent.cpp b/src/backend/BaseRecordComponent.cpp index 1c86431e02..aac761a21b 100644 --- a/src/backend/BaseRecordComponent.cpp +++ b/src/backend/BaseRecordComponent.cpp @@ -35,6 +35,7 @@ BaseRecordComponent &BaseRecordComponent::resetDatatype(Datatype d) "A Records Datatype can not (yet) be changed after it has been " "written."); + datasetDefined(); get().m_dataset.dtype = d; return *this; } @@ -74,4 +75,7 @@ BaseRecordComponent::BaseRecordComponent() : Attributable{nullptr} { Attributable::setData(m_baseRecordComponentData); } + +void BaseRecordComponent::datasetDefined() +{} } // namespace openPMD diff --git a/src/backend/MeshRecordComponent.cpp b/src/backend/MeshRecordComponent.cpp index aa5afcce34..aab33456ce 100644 --- a/src/backend/MeshRecordComponent.cpp +++ b/src/backend/MeshRecordComponent.cpp @@ -23,9 +23,7 @@ namespace openPMD { MeshRecordComponent::MeshRecordComponent() : RecordComponent() -{ - setPosition(std::vector{0}); -} +{} void MeshRecordComponent::read() { @@ -61,6 +59,12 @@ MeshRecordComponent &MeshRecordComponent::setPosition(std::vector pos) return *this; } +void MeshRecordComponent::datasetDefined() +{ + setPosition(std::vector{0}); + RecordComponent::datasetDefined(); +} + template MeshRecordComponent & MeshRecordComponent::setPosition(std::vector pos); template MeshRecordComponent & diff --git a/src/backend/PatchRecordComponent.cpp b/src/backend/PatchRecordComponent.cpp index 6eb696f174..588d65bb3f 100644 --- a/src/backend/PatchRecordComponent.cpp +++ b/src/backend/PatchRecordComponent.cpp @@ -55,6 +55,7 @@ PatchRecordComponent &PatchRecordComponent::resetDataset(Dataset d) throw std::runtime_error( "Dataset extent must not be zero in any dimension."); + datasetDefined(); get().m_dataset = d; dirty() = true; return *this;