Skip to content

Commit 82eac56

Browse files
Fixes for deferred initialization (#1777)
* Fixes for deferred initialization * Turn gcc9 run into a Debug build
1 parent b7478e3 commit 82eac56

File tree

4 files changed

+58
-15
lines changed

4 files changed

+58
-15
lines changed

.github/workflows/linux.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,15 @@ jobs:
266266
env: {CXXFLAGS: -Werror, PKG_CONFIG_PATH: /usr/lib/x86_64-linux-gnu/pkgconfig}
267267
run: |
268268
share/openPMD/download_samples.sh build
269+
# Run a debug build,
270+
# Pybind has some builtin checks only activated in debug mode.
269271
cmake -S . -B build \
270272
-DopenPMD_USE_PYTHON=ON \
271273
-DopenPMD_USE_MPI=ON \
272274
-DopenPMD_USE_HDF5=ON \
273275
-DopenPMD_USE_FILESYSTEM_HEADER=ON \
274-
-DopenPMD_USE_INVASIVE_TESTS=ON
276+
-DopenPMD_USE_INVASIVE_TESTS=ON \
277+
-DCMAKE_BUILD_TYPE=Debug
275278
cmake --build build --parallel 4
276279
ctest --test-dir build --output-on-failure
277280

include/openPMD/snapshots/ContainerImpls.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ class StatefulSnapshotsContainer : public AbstractSnapshotsContainer
3434
* make_reading_stateful_iterator.
3535
* The iterator is resolved upon calling get() below.
3636
*/
37-
std::function<StatefulIterator *()> m_begin;
38-
std::optional<StatefulIterator *> m_bufferedIterator = std::nullopt;
37+
std::variant<std::function<StatefulIterator *()>, StatefulIterator *>
38+
m_bufferedIterator;
3939
};
4040
Members members;
4141

42-
StatefulSnapshotsContainer(std::function<StatefulIterator *()> begin);
42+
StatefulSnapshotsContainer(
43+
std::variant<std::function<StatefulIterator *()>, StatefulIterator *>
44+
begin);
4345

4446
auto get() -> StatefulIterator *;
4547
auto get() const -> StatefulIterator const *;
@@ -64,6 +66,7 @@ class StatefulSnapshotsContainer : public AbstractSnapshotsContainer
6466

6567
using AbstractSnapshotsContainer::currentIteration;
6668
auto currentIteration() const -> std::optional<value_type const *> override;
69+
auto currentIteration() -> std::optional<value_type *> override;
6770

6871
auto begin() -> iterator override;
6972
auto end() -> iterator override;

src/Series.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3194,20 +3194,24 @@ namespace
31943194
{
31953195
auto make_writing_stateful_iterator(
31963196
Series const &copied_series, internal::SeriesData &series)
3197-
-> std::function<StatefulIterator *()>
3197+
-> StatefulIterator *
31983198
{
31993199
if (!series.m_sharedStatefulIterator)
32003200
{
32013201
series.m_sharedStatefulIterator =
32023202
std::make_unique<StatefulIterator>(
32033203
StatefulIterator::tag_write, copied_series);
32043204
}
3205-
return [ptr = series.m_sharedStatefulIterator.get()]() { return ptr; };
3205+
return series.m_sharedStatefulIterator.get();
32063206
}
32073207
auto make_reading_stateful_iterator(
32083208
Series copied_series, internal::SeriesData &series)
3209-
-> std::function<StatefulIterator *()>
3209+
-> std::variant<std::function<StatefulIterator *()>, StatefulIterator *>
32103210
{
3211+
if (series.m_sharedStatefulIterator)
3212+
{
3213+
return series.m_sharedStatefulIterator.get();
3214+
}
32113215
return [s = std::move(copied_series), &series]() mutable {
32123216
if (!series.m_sharedStatefulIterator)
32133217
{
@@ -3317,7 +3321,7 @@ Snapshots Series::makeSynchronousSnapshots()
33173321
internal::default_or_explicit::default_);
33183322
}
33193323

3320-
std::function<StatefulIterator *()> begin;
3324+
std::variant<std::function<StatefulIterator *()>, StatefulIterator *> begin;
33213325

33223326
if (access::write(IOHandler()->m_frontendAccess))
33233327
{

src/snapshots/ContainerImpls.cpp

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "openPMD/snapshots/ContainerImpls.hpp"
22
#include "openPMD/Error.hpp"
33
#include "openPMD/IO/Access.hpp"
4+
#include "openPMD/auxiliary/Variant.hpp"
45
#include "openPMD/snapshots/ContainerTraits.hpp"
56
#include "openPMD/snapshots/IteratorHelpers.hpp"
67
#include "openPMD/snapshots/RandomAccessIterator.hpp"
@@ -12,7 +13,7 @@
1213
namespace openPMD
1314
{
1415
StatefulSnapshotsContainer::StatefulSnapshotsContainer(
15-
std::function<StatefulIterator *()> begin)
16+
std::variant<std::function<StatefulIterator *()>, StatefulIterator *> begin)
1617
: members{std::move(begin)}
1718
{}
1819

@@ -30,15 +31,34 @@ operator=(StatefulSnapshotsContainer &&other) noexcept(noexcept(
3031

3132
auto StatefulSnapshotsContainer::get() -> StatefulIterator *
3233
{
33-
if (!members.m_bufferedIterator.has_value())
34-
{
35-
members.m_bufferedIterator = members.m_begin();
36-
}
37-
return *members.m_bufferedIterator;
34+
return std::visit(
35+
auxiliary::overloaded{
36+
[this](
37+
std::function<StatefulIterator *()> &deferred_initialization) {
38+
auto it = deferred_initialization();
39+
this->members.m_bufferedIterator = it;
40+
return it;
41+
},
42+
[](StatefulIterator *it) { return it; }},
43+
members.m_bufferedIterator);
3844
}
45+
46+
void breakpoint()
47+
{}
3948
auto StatefulSnapshotsContainer::get() const -> StatefulIterator const *
4049
{
41-
return members.m_bufferedIterator.value_or(nullptr);
50+
return std::visit(
51+
auxiliary::overloaded{
52+
[](std::function<StatefulIterator *()> const &)
53+
-> StatefulIterator const * {
54+
breakpoint();
55+
throw std::runtime_error(
56+
"[StatefulSnapshotscontainer] Initialization has been "
57+
"deferred, but container is accessed as const, so cannot "
58+
"initialize.");
59+
},
60+
[](StatefulIterator const *it) { return it; }},
61+
members.m_bufferedIterator);
4262
}
4363

4464
auto StatefulSnapshotsContainer::stateful_to_opaque(StatefulIterator const &it)
@@ -60,6 +80,19 @@ auto StatefulSnapshotsContainer::currentIteration() const
6080
}
6181
}
6282

83+
auto StatefulSnapshotsContainer::currentIteration()
84+
-> std::optional<value_type *>
85+
{
86+
if (auto it = get(); it)
87+
{
88+
return it->peekCurrentlyOpenIteration();
89+
}
90+
else
91+
{
92+
return std::nullopt;
93+
}
94+
}
95+
6396
StatefulSnapshotsContainer::~StatefulSnapshotsContainer() = default;
6497

6598
auto StatefulSnapshotsContainer::begin() -> iterator

0 commit comments

Comments
 (0)