Skip to content

Commit d79ed2f

Browse files
committed
Extract verifyChunk
1 parent b3a43a8 commit d79ed2f

File tree

3 files changed

+28
-42
lines changed

3 files changed

+28
-42
lines changed

include/openPMD/RecordComponent.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,11 @@ OPENPMD_protected
536536
}
537537

538538
void readBase();
539+
540+
template <typename T>
541+
void verifyChunk(Offset const &, Extent const &) const;
542+
543+
void verifyChunk(Datatype, Offset const &, Extent const &) const;
539544
}; // RecordComponent
540545

541546
} // namespace openPMD

include/openPMD/RecordComponent.tpp

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -278,38 +278,7 @@ template <typename T, typename F>
278278
inline DynamicMemoryView<T>
279279
RecordComponent::storeChunk(Offset o, Extent e, F &&createBuffer)
280280
{
281-
if (constant())
282-
throw std::runtime_error(
283-
"Chunks cannot be written for a constant RecordComponent.");
284-
if (empty())
285-
throw std::runtime_error(
286-
"Chunks cannot be written for an empty RecordComponent.");
287-
Datatype dtype = determineDatatype<T>();
288-
if (dtype != getDatatype())
289-
{
290-
std::ostringstream oss;
291-
oss << "Datatypes of chunk data (" << dtype
292-
<< ") and record component (" << getDatatype() << ") do not match.";
293-
throw std::runtime_error(oss.str());
294-
}
295-
uint8_t dim = getDimensionality();
296-
if (e.size() != dim || o.size() != dim)
297-
{
298-
std::ostringstream oss;
299-
oss << "Dimensionality of chunk ("
300-
<< "offset=" << o.size() << "D, "
301-
<< "extent=" << e.size() << "D) "
302-
<< "and record component (" << int(dim) << "D) "
303-
<< "do not match.";
304-
throw std::runtime_error(oss.str());
305-
}
306-
Extent dse = getExtent();
307-
for (uint8_t i = 0; i < dim; ++i)
308-
if (dse[i] < o[i] + e[i])
309-
throw std::runtime_error(
310-
"Chunk does not reside inside dataset (Dimension on index " +
311-
std::to_string(i) + ". DS: " + std::to_string(dse[i]) +
312-
" - Chunk: " + std::to_string(o[i] + e[i]) + ")");
281+
verifyChunk<T>(o, e);
313282

314283
/*
315284
* The openPMD backend might not yet know about this dataset.
@@ -407,4 +376,9 @@ auto RecordComponent::visit(Args &&...args)
407376
getDatatype(), *this, std::forward<Args>(args)...);
408377
}
409378

379+
template <typename T>
380+
void RecordComponent::verifyChunk(Offset const &o, Extent const &e) const
381+
{
382+
verifyChunk(determineDatatype<T>(), o, e);
383+
}
410384
} // namespace openPMD

src/RecordComponent.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,21 @@ bool RecordComponent::dirtyRecursive() const
442442

443443
void RecordComponent::storeChunk(
444444
auxiliary::WriteBuffer buffer, Datatype dtype, Offset o, Extent e)
445+
{
446+
verifyChunk(dtype, o, e);
447+
448+
Parameter<Operation::WRITE_DATASET> dWrite;
449+
dWrite.offset = std::move(o);
450+
dWrite.extent = std::move(e);
451+
dWrite.dtype = dtype;
452+
/* std::static_pointer_cast correctly reference-counts the pointer */
453+
dWrite.data = std::move(buffer);
454+
auto &rc = get();
455+
rc.m_chunks.push(IOTask(this, std::move(dWrite)));
456+
}
457+
458+
void RecordComponent::verifyChunk(
459+
Datatype dtype, Offset const &o, Extent const &e) const
445460
{
446461
if (constant())
447462
throw std::runtime_error(
@@ -457,6 +472,8 @@ void RecordComponent::storeChunk(
457472
throw std::runtime_error(oss.str());
458473
}
459474
uint8_t dim = getDimensionality();
475+
Extent dse = getExtent();
476+
460477
if (e.size() != dim || o.size() != dim)
461478
{
462479
std::ostringstream oss;
@@ -467,22 +484,12 @@ void RecordComponent::storeChunk(
467484
<< "do not match.";
468485
throw std::runtime_error(oss.str());
469486
}
470-
Extent dse = getExtent();
471487
for (uint8_t i = 0; i < dim; ++i)
472488
if (dse[i] < o[i] + e[i])
473489
throw std::runtime_error(
474490
"Chunk does not reside inside dataset (Dimension on index " +
475491
std::to_string(i) + ". DS: " + std::to_string(dse[i]) +
476492
" - Chunk: " + std::to_string(o[i] + e[i]) + ")");
477-
478-
Parameter<Operation::WRITE_DATASET> dWrite;
479-
dWrite.offset = o;
480-
dWrite.extent = e;
481-
dWrite.dtype = dtype;
482-
/* std::static_pointer_cast correctly reference-counts the pointer */
483-
dWrite.data = std::move(buffer);
484-
auto &rc = get();
485-
rc.m_chunks.push(IOTask(this, std::move(dWrite)));
486493
}
487494

488495
namespace

0 commit comments

Comments
 (0)