Skip to content

Commit

Permalink
keep fixing things hehehe
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Oct 21, 2022
1 parent ad9cd98 commit 4f75a31
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 46 deletions.
2 changes: 2 additions & 0 deletions include/openPMD/RecordComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ OPENPMD_protected
BaseRecordComponent::setData(m_recordComponentData);
}

void datasetDefined() override;

void readBase();
}; // RecordComponent

Expand Down
71 changes: 64 additions & 7 deletions include/openPMD/backend/BaseRecord.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,17 @@ namespace internal
*/
template <
typename T_elem_maybe_void,
typename T_RecordComponent = T_elem_maybe_void>
typename T_RecordComponent_ = T_elem_maybe_void>
class BaseRecord
: public Container<
typename auxiliary::OkOr<T_elem_maybe_void, BaseRecord<void> >::type>
, public T_RecordComponent
, public T_RecordComponent_
{
protected:
// make it available for child classes

private:
using T_RecordComponent = T_RecordComponent_;
using T_elem =
typename auxiliary::OkOr<T_elem_maybe_void, BaseRecord<void> >::type;
using T_Container = Container<T_elem>;
Expand Down Expand Up @@ -138,9 +143,12 @@ class BaseRecord

mapped_type &operator[](key_type const &key) override;
mapped_type &operator[](key_type &&key) override;
mapped_type &at(key_type const &key) override;
mapped_type const &at(key_type const &key) const override;
size_type erase(key_type const &key) override;
iterator erase(iterator res) override;
bool empty() const noexcept;
size_type count(key_type const &key) const override;

//! @todo add also, as soon as added in Container:
// iterator erase(const_iterator first, const_iterator last) override;
Expand Down Expand Up @@ -264,13 +272,43 @@ BaseRecord<T_elem, T_RecordComponent>::operator[](key_type &&key)
}
/*
* datasetDefined() inits the container entry
* is this object slicing???? JA
*/
mapped_type &ret = keyScalar ? T_Container::at(std::move(key))
mapped_type &ret = keyScalar ? static_cast<mapped_type &>(*this)
: T_Container::operator[](std::move(key));
return ret;
}
}

template <typename T_elem, typename T_RecordComponent>
inline auto BaseRecord<T_elem, T_RecordComponent>::at(key_type const &key)
-> mapped_type &
{
return const_cast<mapped_type &>(
static_cast<BaseRecord<T_elem, T_RecordComponent> const *>(this)->at(
key));
}

template <typename T_elem, typename T_RecordComponent>
inline auto BaseRecord<T_elem, T_RecordComponent>::at(key_type const &key) const
-> mapped_type const &
{
bool const keyScalar = (key == RecordComponent::SCALAR);
if (keyScalar)
{
if (!get().m_containsScalar)
{
throw std::out_of_range(
"[at()] Requested scalar entry from non-scalar record.");
}
return static_cast<mapped_type const &>(*this);
}
else
{
return at(key);
}
}

template <typename T_elem, typename T_RecordComponent>
inline typename BaseRecord<T_elem, T_RecordComponent>::size_type
BaseRecord<T_elem, T_RecordComponent>::erase(key_type const &key)
Expand Down Expand Up @@ -337,6 +375,20 @@ bool BaseRecord<T_elem, T_RecordComponent>::empty() const noexcept
return T_Container::empty();
}

template <typename T_elem, typename T_RecordComponent>
auto BaseRecord<T_elem, T_RecordComponent>::count(key_type const &key) const
-> size_type
{
if (key == RecordComponent::SCALAR)
{
return get().m_containsScalar ? 1 : 0;
}
else
{
return T_Container::count(key);
}
}

template <typename T_elem, typename T_RecordComponent>
inline std::array<double, 7>
BaseRecord<T_elem, T_RecordComponent>::unitDimension() const
Expand Down Expand Up @@ -400,7 +452,7 @@ template <typename T_elem, typename T_RecordComponent>
inline void BaseRecord<T_elem, T_RecordComponent>::flush(
std::string const &name, internal::FlushParams const &flushParams)
{
if (!this->written() && this->empty())
if (!this->written() && this->empty() && !get().m_containsScalar)
throw std::runtime_error(
"A Record can not be written without any contained "
"RecordComponents: " +
Expand Down Expand Up @@ -433,9 +485,14 @@ void BaseRecord<T_elem, T_RecordComponent>::datasetDefined()
{
// If the RecordComponent API of this object has been used, then the record
// is a scalar one
T_RecordComponent copy;
copy.setData(m_baseRecordData);
T_Container::emplace(RecordComponent::SCALAR, std::move(copy));
T_RecordComponent &rc = *this;
/*
* No need to do any of the hierarchy linking business,
* rc and *this are the same object, so everything is linked already.
* Just need to init the RC-specific stuff.
*/
traits::GenerationPolicy<T_RecordComponent> gen;
gen(rc);
get().m_containsScalar = true;
T_RecordComponent::datasetDefined();
}
Expand Down
6 changes: 3 additions & 3 deletions include/openPMD/backend/Container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,11 @@ class Container : virtual public Attributable
container().swap(other.m_container);
}

mapped_type &at(key_type const &key)
virtual mapped_type &at(key_type const &key)
{
return container().at(key);
}
mapped_type const &at(key_type const &key) const
virtual mapped_type const &at(key_type const &key) const
{
return container().at(key);
}
Expand Down Expand Up @@ -357,7 +357,7 @@ class Container : virtual public Attributable
* @param key key value of the element to count
* @return since keys are unique in this container, returns 0 or 1
*/
size_type count(key_type const &key) const
virtual size_type count(key_type const &key) const
{
return container().count(key);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Iteration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,7 @@ void Iteration::readMeshes(std::string const &meshesPath)
dOpen.name = mesh_name;
IOHandler()->enqueue(IOTask(&m, dOpen));
IOHandler()->flush(internal::defaultFlushParams);
MeshRecordComponent &mrc = m[MeshRecordComponent::SCALAR];
mrc.parent() = m.parent();
MeshRecordComponent &mrc = m;
IOHandler()->enqueue(IOTask(&mrc, dOpen));
IOHandler()->flush(internal::defaultFlushParams);
mrc.written() = false;
Expand Down
16 changes: 3 additions & 13 deletions src/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,8 @@ void Mesh::flush_impl(
{
if (scalar())
{
MeshRecordComponent &mrc = at(RecordComponent::SCALAR);
mrc.parent() = parent();
MeshRecordComponent &mrc = *this;
mrc.flush(name, flushParams);
Parameter<Operation::KEEP_SYNCHRONOUS> pSynchronize;
pSynchronize.otherWritable = &mrc.writable();
IOHandler()->enqueue(IOTask(this, pSynchronize));
}
else
{
Expand All @@ -252,12 +248,7 @@ void Mesh::flush_impl(
{
if (scalar())
{
for (auto &comp : *this)
{
comp.second.flush(name, flushParams);
writable().abstractFilePosition =
comp.second.writable().abstractFilePosition;
}
T_RecordComponent::flush(name, flushParams);
}
else
{
Expand Down Expand Up @@ -384,8 +375,7 @@ void Mesh::read()

if (scalar())
{
/* using operator[] will incorrectly update parent */
map.at(MeshRecordComponent::SCALAR).read();
T_RecordComponent::read();
}
else
{
Expand Down
6 changes: 2 additions & 4 deletions src/ParticleSpecies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ void ParticleSpecies::read()
if (value != att_end && shape != att_end)
{
internal::EraseStaleEntries<Record &> scalarMap(r);
RecordComponent &rc = scalarMap[RecordComponent::SCALAR];
rc.parent() = r.parent();
RecordComponent &rc = r;
IOHandler()->enqueue(IOTask(&rc, pOpen));
IOHandler()->flush(internal::defaultFlushParams);
rc.get().m_isConstant = true;
Expand Down Expand Up @@ -125,8 +124,7 @@ void ParticleSpecies::read()
IOHandler()->enqueue(IOTask(&r, dOpen));
IOHandler()->flush(internal::defaultFlushParams);
internal::EraseStaleEntries<Record &> scalarMap(r);
RecordComponent &rc = scalarMap[RecordComponent::SCALAR];
rc.parent() = r.parent();
RecordComponent &rc = r;
IOHandler()->enqueue(IOTask(&rc, dOpen));
IOHandler()->flush(internal::defaultFlushParams);
rc.written() = false;
Expand Down
17 changes: 3 additions & 14 deletions src/Record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,8 @@ void Record::flush_impl(
{
if (scalar())
{
RecordComponent &rc = at(RecordComponent::SCALAR);
rc.parent() = parent();
RecordComponent &rc = *this;
rc.flush(name, flushParams);
Parameter<Operation::KEEP_SYNCHRONOUS> pSynchronize;
pSynchronize.otherWritable = &rc.writable();
IOHandler()->enqueue(IOTask(this, pSynchronize));
}
else
{
Expand All @@ -81,12 +77,7 @@ void Record::flush_impl(

if (scalar())
{
for (auto &comp : *this)
{
comp.second.flush(name, flushParams);
writable().abstractFilePosition =
comp.second.writable().abstractFilePosition;
}
T_RecordComponent::flush(name, flushParams);
}
else
{
Expand All @@ -104,17 +95,15 @@ void Record::read()
if (scalar())
{
/* using operator[] will incorrectly update parent */
auto &scalarComponent = this->at(RecordComponent::SCALAR);
try
{
scalarComponent.read();
T_RecordComponent::read();
}
catch (error::ReadError const &err)
{
std::cerr << "Cannot read scalar record component and will skip it "
"due to read error:\n"
<< err.what() << std::endl;
this->container().erase(RecordComponent::SCALAR);
}
}
else
Expand Down
16 changes: 13 additions & 3 deletions src/RecordComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ namespace openPMD
{
namespace internal
{
RecordComponentData::RecordComponentData() = default;
RecordComponentData::RecordComponentData()
{
m_dataset = Dataset(Datatype::CHAR, {1});
};
} // namespace internal

RecordComponent::RecordComponent()
{
BaseRecordComponent::setData(m_recordComponentData);
setUnitSI(1);
resetDataset(Dataset(Datatype::CHAR, {1}));
}

// We need to instantiate this somewhere otherwise there might be linker issues
Expand Down Expand Up @@ -272,6 +273,15 @@ void RecordComponent::flush(
}
}

void RecordComponent::datasetDefined()
{
if (!containsAttribute("unitSI"))
{
setUnitSI(1);
}
BaseRecordComponent::datasetDefined();
}

void RecordComponent::read()
{
readBase();
Expand Down

0 comments on commit 4f75a31

Please sign in to comment.