Skip to content

Commit 332932f

Browse files
committed
Frontend support for unique pointers with const value types
1 parent 4d1e260 commit 332932f

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

include/openPMD/LoadStoreChunk.tpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ auto ConfigureLoadStore<ChildClass>::withUniquePtr(UniquePtrWithLambda<T> data)
2424
-> unique_ptr_return_type<T>
2525

2626
{
27-
// should we support them?
28-
static_assert(
29-
!std::is_const_v<T>,
30-
"Unique pointers to const types not supported as storeChunk buffers.");
3127
if (!data)
3228
{
3329
throw std::runtime_error(

src/LoadStoreChunk.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ namespace
4141
return auxiliary::WriteBuffer(
4242
std::move(ptr).template static_cast_<void>());
4343
}
44+
45+
/*
46+
* There is no backend support currently for const unique pointers.
47+
* We support these mostly for providing a clean API to users that have such
48+
* pointers and want to store from them, but there will be no
49+
* backend-specific optimizations for such buffers as there are for
50+
* non-const unique pointers.
51+
*/
52+
template <typename T>
53+
auto
54+
asWriteBuffer(UniquePtrWithLambda<T const> &&ptr) -> auxiliary::WriteBuffer
55+
{
56+
auto raw_ptr = ptr.get();
57+
return asWriteBuffer(std::shared_ptr<T const>{
58+
raw_ptr, [ptr_lambda = std::move(ptr)](auto const *) {}});
59+
}
4460
} // namespace
4561

4662
template <typename ChildClass>
@@ -272,6 +288,14 @@ OPENPMD_FOREACH_DATASET_DATATYPE(INSTANTIATE_METHOD_TEMPLATES_FOR_BASE)
272288
INSTANTIATE_METHOD_TEMPLATES( \
273289
ConfigureLoadStore< \
274290
ConfigureStoreChunkFromBuffer<std::shared_ptr<dtype const>>>, \
291+
dtype) \
292+
template class ConfigureStoreChunkFromBuffer< \
293+
UniquePtrWithLambda<dtype const>>; \
294+
template class ConfigureLoadStore< \
295+
ConfigureStoreChunkFromBuffer<UniquePtrWithLambda<dtype const>>>; \
296+
INSTANTIATE_METHOD_TEMPLATES( \
297+
ConfigureLoadStore< \
298+
ConfigureStoreChunkFromBuffer<UniquePtrWithLambda<dtype const>>>, \
275299
dtype)
276300
// clang-format on
277301

test/SerialIOTest.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,11 @@ inline void constant_scalar(std::string const &file_ending)
928928
new unsigned int[6], [](unsigned int const *p) { delete[] p; });
929929
unsigned int e{0};
930930
std::generate(E.get(), E.get() + 6, [&e] { return e++; });
931-
E_y.storeChunk(std::move(E), {0, 0, 0}, {1, 2, 3});
931+
// check that const-type unique pointers work in the builder pattern
932+
E_y.prepareLoadStore()
933+
.extent({1, 2, 3})
934+
.withUniquePtr(std::move(E).static_cast_<unsigned int const>())
935+
.enqueueStore();
932936

933937
// store a number of predefined attributes in E
934938
Mesh &E_mesh = s.iterations[1].meshes["E"];

0 commit comments

Comments
 (0)