Skip to content

Commit 0b3a8af

Browse files
committed
Readers for everything now except version-dependent stuff
1 parent 889646d commit 0b3a8af

File tree

7 files changed

+37
-78
lines changed

7 files changed

+37
-78
lines changed

include/openPMD/RecordComponent.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ class RecordComponent
491491

492492
protected:
493493
void flush(std::string const &, internal::FlushParams const &);
494-
void read(bool require_unit_si);
494+
void read();
495495

496496
private:
497497
/**
@@ -539,7 +539,7 @@ OPENPMD_protected
539539
BaseRecordComponent::setData(m_recordComponentData);
540540
}
541541

542-
void readBase(bool require_unit_si);
542+
void readBase();
543543

544544
template <typename T>
545545
void verifyChunk(Offset const &, Extent const &) const;

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.setDirty(false);
101101
try
102102
{
103-
prc.PatchRecordComponent::read(/* require_unit_si = */ false);
103+
prc.PatchRecordComponent::read();
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
@@ -120,7 +120,7 @@ auto Record::read() -> internal::HomogenizeExtents
120120
/* using operator[] will incorrectly update parent */
121121
try
122122
{
123-
T_RecordComponent::read(/* require_unit_si = */ true);
123+
T_RecordComponent::read();
124124
}
125125
catch (error::ReadError const &err)
126126
{
@@ -148,7 +148,7 @@ auto Record::read() -> internal::HomogenizeExtents
148148
rc.get().m_isConstant = true;
149149
try
150150
{
151-
rc.read(/* require_unit_si = */ true);
151+
rc.read();
152152
}
153153
catch (error::ReadError const &err)
154154
{
@@ -177,7 +177,7 @@ auto Record::read() -> internal::HomogenizeExtents
177177
rc.setWritten(true, Attributable::EnqueueAsynchronously::No);
178178
try
179179
{
180-
rc.read(/* require_unit_si = */ true);
180+
rc.read();
181181
}
182182
catch (error::ReadError const &err)
183183
{

src/RecordComponent.cpp

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,9 @@ void RecordComponent::flush(
512512
}
513513
}
514514

515-
void RecordComponent::read(bool require_unit_si)
515+
void RecordComponent::read()
516516
{
517-
readBase(require_unit_si);
517+
readBase();
518518
}
519519

520520
namespace
@@ -539,7 +539,7 @@ namespace
539539
};
540540
} // namespace
541541

542-
void RecordComponent::readBase(bool require_unit_si)
542+
void RecordComponent::readBase()
543543
{
544544
using DT = Datatype;
545545
auto &rc = get();
@@ -591,32 +591,6 @@ void RecordComponent::readBase(bool require_unit_si)
591591
{
592592
read_constant();
593593
}
594-
595-
if (require_unit_si)
596-
{
597-
if (!containsAttribute("unitSI"))
598-
{
599-
throw error::ReadError(
600-
error::AffectedObject::Attribute,
601-
error::Reason::NotFound,
602-
{},
603-
"Attribute unitSI required for record components, not found in "
604-
"'" +
605-
myPath().openPMDPath() + "'.");
606-
}
607-
if (auto attr = getAttribute("unitSI");
608-
!attr.getOptional<double>().has_value())
609-
{
610-
throw error::ReadError(
611-
error::AffectedObject::Attribute,
612-
error::Reason::UnexpectedContent,
613-
{},
614-
"Unexpected Attribute datatype for 'unitSI' (expected double, "
615-
"found " +
616-
datatypeToString(attr.dtype) + ") in '" +
617-
myPath().openPMDPath() + "'.");
618-
}
619-
}
620594
}
621595

622596
void RecordComponent::storeChunk(

src/backend/MeshRecordComponent.cpp

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
#include "openPMD/backend/MeshRecordComponent.hpp"
2222
#include "openPMD/backend/BaseRecord.hpp"
23+
#include "openPMD/backend/ScientificDefaults.hpp"
2324

2425
namespace openPMD
2526
{
@@ -38,35 +39,9 @@ MeshRecordComponent::MeshRecordComponent(
3839

3940
void MeshRecordComponent::read()
4041
{
41-
using DT = Datatype;
42-
Parameter<Operation::READ_ATT> aRead;
42+
internal::ScientificDefaults<MeshRecordComponent>::readDefaults();
4343

44-
aRead.name = "position";
45-
IOHandler()->enqueue(IOTask(this, aRead));
46-
IOHandler()->flush(internal::defaultFlushParams);
47-
Attribute a = Attribute(Attribute::from_any, *aRead.m_resource);
48-
if (*aRead.dtype == DT::VEC_FLOAT || *aRead.dtype == DT::FLOAT)
49-
setPosition(a.get<std::vector<float> >());
50-
else if (*aRead.dtype == DT::VEC_DOUBLE || *aRead.dtype == DT::DOUBLE)
51-
setPosition(a.get<std::vector<double> >());
52-
else if (
53-
*aRead.dtype == DT::VEC_LONG_DOUBLE || *aRead.dtype == DT::LONG_DOUBLE)
54-
setPosition(a.get<std::vector<long double> >());
55-
// conversion cast if a backend reports an integer type
56-
else if (auto val = a.getOptional<std::vector<double> >(); val.has_value())
57-
setPosition(val.value());
58-
else
59-
throw error::ReadError(
60-
error::AffectedObject::Attribute,
61-
error::Reason::UnexpectedContent,
62-
{},
63-
"Unexpected Attribute datatype for 'position' (expected a vector "
64-
"of any floating point type, found " +
65-
datatypeToString(
66-
Attribute(Attribute::from_any, *aRead.m_resource).dtype) +
67-
")");
68-
69-
readBase(/* require_unit_si = */ true);
44+
readBase();
7045
}
7146

7247
void MeshRecordComponent::flush(

src/backend/PatchRecord.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void PatchRecord::read()
109109
prc.setWritten(true, Attributable::EnqueueAsynchronously::No);
110110
try
111111
{
112-
prc.read(/* require_unit_si = */ false);
112+
prc.read();
113113
}
114114
catch (error::ReadError const &err)
115115
{

src/backend/ScientificDefaults.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,8 @@ void ScientificDefaults<Child>::defaults_impl()
382382
{
383383
defaultAttribute("timeOffset", 0.f)
384384
.withSetter (&Record::setTimeOffset)(wor);
385-
auto const &keyInParent = asChild().writable().ownKeyWithinParent;
386385

386+
auto const &keyInParent = asChild().writable().ownKeyWithinParent;
387387
if (keyInParent == "position" || keyInParent == "positionOffset")
388388
{
389389
defaultAttribute(
@@ -410,27 +410,37 @@ void ScientificDefaults<Child>::defaults_impl()
410410
else if constexpr (std::is_same_v<Child, RecordComponent>)
411411
{
412412
defaultAttribute("unitSI", 1.0)
413-
.withSetter (&RecordComponent::setUnitSI)(wor);
413+
.withSetter(&RecordComponent::setUnitSI)
414+
.withReader()(wor);
414415
}
415416
else if constexpr (std::is_same_v<Child, MeshRecordComponent>)
416417
{
417-
// position
418418
auto dimensionality = asChild().getDimensionality();
419-
defaultAttribute("position", [&]() {
420-
if (dimensionality < 100)
421-
{
422-
return std::vector<double>(dimensionality, 0.5);
423-
}
424-
else
425-
{
426-
return std::vector<double>{0.0};
427-
}
428-
}).withSetter (&MeshRecordComponent::setPosition)(wor);
419+
420+
defaultAttribute(
421+
"position",
422+
[&]() {
423+
if (dimensionality < 100)
424+
{
425+
return std::vector<double>(dimensionality, 0.5);
426+
}
427+
else
428+
{
429+
return std::vector<double>{0.0};
430+
}
431+
})
432+
.withSetter(&MeshRecordComponent::setPosition)
433+
.withReader(ensureFloatingVector([this](auto &&val) {
434+
this->asChild().setPosition(static_cast<decltype(val)>(val));
435+
}))(wor);
436+
429437
addParentDefaults<RecordComponent, write>();
430438
}
431439
else if constexpr (std::is_same_v<Child, PatchRecordComponent>)
432440
{
433-
addParentDefaults<RecordComponent, write>();
441+
// We don't require unitSI for PatchRecordComponent
442+
//
443+
// addParentDefaults<RecordComponent, write>();
434444
}
435445
else if constexpr (auxiliary::IsTemplateBaseOf_v<BaseRecord, Child>)
436446
{

0 commit comments

Comments
 (0)