Skip to content

Commit 283b2fe

Browse files
Derive PatchComponent from RecordComponent (#1594)
* Derive PatchRecordComponent from RecordComponent This way, we get the RecordComponent API too * Use generic read()/flush() logic for patch components
1 parent d9fce66 commit 283b2fe

File tree

13 files changed

+52
-202
lines changed

13 files changed

+52
-202
lines changed

include/openPMD/Mesh.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class Mesh : public BaseRecord<MeshRecordComponent>
230230

231231
void
232232
flush_impl(std::string const &, internal::FlushParams const &) override;
233-
void read() override;
233+
void read();
234234
}; // Mesh
235235

236236
template <typename T>

include/openPMD/Record.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Record : public BaseRecord<RecordComponent>
5252

5353
void
5454
flush_impl(std::string const &, internal::FlushParams const &) override;
55-
void read() override;
55+
void read();
5656
}; // Record
5757

5858
template <typename T>

include/openPMD/RecordComponent.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,11 @@ class RecordComponent : public BaseRecordComponent
481481

482482
static constexpr char const *const SCALAR = "\vScalar";
483483

484-
private:
484+
protected:
485485
void flush(std::string const &, internal::FlushParams const &);
486-
virtual void read();
486+
void read(bool require_unit_si);
487487

488+
private:
488489
/**
489490
* Internal method to be called by all methods that create an empty dataset.
490491
*
@@ -535,7 +536,7 @@ OPENPMD_protected
535536
BaseRecordComponent::setData(m_recordComponentData);
536537
}
537538

538-
void readBase();
539+
void readBase(bool require_unit_si);
539540
}; // RecordComponent
540541

541542
} // namespace openPMD

include/openPMD/backend/MeshRecordComponent.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class MeshRecordComponent : public RecordComponent
4646
private:
4747
MeshRecordComponent();
4848
MeshRecordComponent(NoInit);
49-
void read() override;
49+
void read();
5050
void flush(std::string const &, internal::FlushParams const &);
5151

5252
public:

include/openPMD/backend/PatchRecord.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ class PatchRecord : public BaseRecord<PatchRecordComponent>
4343

4444
void
4545
flush_impl(std::string const &, internal::FlushParams const &) override;
46-
void read() override;
46+
void read();
4747
}; // PatchRecord
4848
} // namespace openPMD

include/openPMD/backend/PatchRecordComponent.hpp

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
#pragma once
2222

23+
#include "openPMD/RecordComponent.hpp"
2324
#include "openPMD/auxiliary/ShareRawInternal.hpp"
2425
#include "openPMD/backend/BaseRecordComponent.hpp"
2526

@@ -36,41 +37,11 @@
3637

3738
namespace openPMD
3839
{
39-
namespace internal
40-
{
41-
class PatchRecordComponentData : public BaseRecordComponentData
42-
{
43-
public:
44-
/**
45-
* Chunk reading/writing requests on the contained dataset.
46-
*/
47-
std::queue<IOTask> m_chunks;
48-
49-
PatchRecordComponentData(PatchRecordComponentData const &) = delete;
50-
PatchRecordComponentData(PatchRecordComponentData &&) = delete;
51-
52-
PatchRecordComponentData &
53-
operator=(PatchRecordComponentData const &) = delete;
54-
PatchRecordComponentData &
55-
operator=(PatchRecordComponentData &&) = delete;
56-
57-
PatchRecordComponentData();
58-
59-
void reset() override
60-
{
61-
BaseRecordComponentData::reset();
62-
m_chunks = std::queue<IOTask>();
63-
}
64-
};
65-
66-
template <typename, typename>
67-
class BaseRecordData;
68-
} // namespace internal
6940

7041
/**
7142
* @todo add support for constant patch record components
7243
*/
73-
class PatchRecordComponent : public BaseRecordComponent
44+
class PatchRecordComponent : public RecordComponent
7445
{
7546
template <typename T, typename T_key, typename T_container>
7647
friend class Container;
@@ -81,7 +52,6 @@ class PatchRecordComponent : public BaseRecordComponent
8152
friend class ParticlePatches;
8253
friend class PatchRecord;
8354
friend class ParticleSpecies;
84-
friend class internal::PatchRecordComponentData;
8555

8656
public:
8757
/**
@@ -95,7 +65,7 @@ class PatchRecordComponent : public BaseRecordComponent
9565

9666
PatchRecordComponent &setUnitSI(double);
9767

98-
virtual PatchRecordComponent &resetDataset(Dataset);
68+
PatchRecordComponent &resetDataset(Dataset) override;
9969

10070
uint8_t getDimensionality() const;
10171
Extent getExtent() const;
@@ -119,48 +89,14 @@ class PatchRecordComponent : public BaseRecordComponent
11989
OPENPMD_private
12090
// clang-format on
12191

122-
void flush(std::string const &, internal::FlushParams const &);
123-
virtual void read();
124-
125-
/**
126-
* @brief Check recursively whether this RecordComponent is dirty.
127-
* It is dirty if any attribute or dataset is read from or written to
128-
* the backend.
129-
*
130-
* @return true If dirty.
131-
* @return false Otherwise.
132-
*/
133-
bool dirtyRecursive() const;
92+
using RecordComponent::flush;
13493

13594
// clang-format off
13695
OPENPMD_protected
13796
// clang-format on
13897

139-
using Data_t = internal::PatchRecordComponentData;
140-
141-
std::shared_ptr<Data_t> m_patchRecordComponentData;
142-
14398
PatchRecordComponent();
14499
PatchRecordComponent(NoInit);
145-
146-
inline Data_t const &get() const
147-
{
148-
// cannot call this in the const overload
149-
// setDatasetDefined(*m_recordComponentData);
150-
return *m_patchRecordComponentData;
151-
}
152-
153-
inline Data_t &get()
154-
{
155-
setDatasetDefined(*m_patchRecordComponentData);
156-
return *m_patchRecordComponentData;
157-
}
158-
159-
inline void setData(std::shared_ptr<Data_t> data)
160-
{
161-
m_patchRecordComponentData = std::move(data);
162-
BaseRecordComponent::setData(m_patchRecordComponentData);
163-
}
164100
}; // PatchRecordComponent
165101

166102
template <typename T>

src/ParticlePatches.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void ParticlePatches::read()
100100
pr.dirty() = false;
101101
try
102102
{
103-
prc.PatchRecordComponent::read();
103+
prc.PatchRecordComponent::read(/* require_unit_si = */ false);
104104
}
105105
catch (error::ReadError const &err)
106106
{

src/Record.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ void Record::read()
104104
/* using operator[] will incorrectly update parent */
105105
try
106106
{
107-
T_RecordComponent::read();
107+
T_RecordComponent::read(/* require_unit_si = */ true);
108108
}
109109
catch (error::ReadError const &err)
110110
{
@@ -128,7 +128,7 @@ void Record::read()
128128
rc.get().m_isConstant = true;
129129
try
130130
{
131-
rc.read();
131+
rc.read(/* require_unit_si = */ true);
132132
}
133133
catch (error::ReadError const &err)
134134
{
@@ -155,7 +155,7 @@ void Record::read()
155155
rc.written() = true;
156156
try
157157
{
158-
rc.read();
158+
rc.read(/* require_unit_si = */ true);
159159
}
160160
catch (error::ReadError const &err)
161161
{

src/RecordComponent.cpp

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,9 @@ void RecordComponent::flush(
340340
}
341341
}
342342

343-
void RecordComponent::read()
343+
void RecordComponent::read(bool require_unit_si)
344344
{
345-
readBase();
345+
readBase(require_unit_si);
346346
}
347347

348348
namespace
@@ -367,7 +367,7 @@ namespace
367367
};
368368
} // namespace
369369

370-
void RecordComponent::readBase()
370+
void RecordComponent::readBase(bool require_unit_si)
371371
{
372372
using DT = Datatype;
373373
// auto & rc = get();
@@ -413,22 +413,32 @@ void RecordComponent::readBase()
413413
written() = true;
414414
}
415415

416-
aRead.name = "unitSI";
417-
IOHandler()->enqueue(IOTask(this, aRead));
418-
IOHandler()->flush(internal::defaultFlushParams);
419-
if (auto val = Attribute(*aRead.resource).getOptional<double>();
420-
val.has_value())
421-
setUnitSI(val.value());
422-
else
423-
throw error::ReadError(
424-
error::AffectedObject::Attribute,
425-
error::Reason::UnexpectedContent,
426-
{},
427-
"Unexpected Attribute datatype for 'unitSI' (expected double, "
428-
"found " +
429-
datatypeToString(Attribute(*aRead.resource).dtype) + ")");
430-
431416
readAttributes(ReadMode::FullyReread);
417+
418+
if (require_unit_si)
419+
{
420+
if (!containsAttribute("unitSI"))
421+
{
422+
throw error::ReadError(
423+
error::AffectedObject::Attribute,
424+
error::Reason::NotFound,
425+
{},
426+
"Attribute unitSI required for record components, not found in "
427+
"'" +
428+
myPath().openPMDPath() + "'.");
429+
}
430+
if (!getAttribute("unitSI").getOptional<double>().has_value())
431+
{
432+
throw error::ReadError(
433+
error::AffectedObject::Attribute,
434+
error::Reason::UnexpectedContent,
435+
{},
436+
"Unexpected Attribute datatype for 'unitSI' (expected double, "
437+
"found " +
438+
datatypeToString(Attribute(*aRead.resource).dtype) +
439+
") in '" + myPath().openPMDPath() + "'.");
440+
}
441+
}
432442
}
433443

434444
bool RecordComponent::dirtyRecursive() const

src/backend/MeshRecordComponent.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void MeshRecordComponent::read()
6464
"of any floating point type, found " +
6565
datatypeToString(Attribute(*aRead.resource).dtype) + ")");
6666

67-
readBase();
67+
readBase(/* require_unit_si = */ true);
6868
}
6969

7070
void MeshRecordComponent::flush(

0 commit comments

Comments
 (0)