From 3572fe3f6d1b8df42bb9bb3fd414bfff6cc09a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Mon, 24 Jun 2024 16:53:53 +0200 Subject: [PATCH] Frontend support for unique pointers with const value types --- include/openPMD/LoadStoreChunk.tpp | 4 ---- src/LoadStoreChunk.cpp | 24 ++++++++++++++++++++++++ test/SerialIOTest.cpp | 6 +++++- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/include/openPMD/LoadStoreChunk.tpp b/include/openPMD/LoadStoreChunk.tpp index 382d899deb..d6cb992896 100644 --- a/include/openPMD/LoadStoreChunk.tpp +++ b/include/openPMD/LoadStoreChunk.tpp @@ -24,10 +24,6 @@ auto ConfigureLoadStore::withUniquePtr(UniquePtrWithLambda data) -> unique_ptr_return_type { - // should we support them? - static_assert( - !std::is_const_v, - "Unique pointers to const types not supported as storeChunk buffers."); if (!data) { throw std::runtime_error( diff --git a/src/LoadStoreChunk.cpp b/src/LoadStoreChunk.cpp index f1b6e12b5d..f65fd8cde5 100644 --- a/src/LoadStoreChunk.cpp +++ b/src/LoadStoreChunk.cpp @@ -41,6 +41,22 @@ namespace return auxiliary::WriteBuffer( std::move(ptr).template static_cast_()); } + + /* + * There is no backend support currently for const unique pointers. + * We support these mostly for providing a clean API to users that have such + * pointers and want to store from them, but there will be no + * backend-specific optimizations for such buffers as there are for + * non-const unique pointers. + */ + template + auto + asWriteBuffer(UniquePtrWithLambda &&ptr) -> auxiliary::WriteBuffer + { + auto raw_ptr = ptr.get(); + return asWriteBuffer(std::shared_ptr{ + raw_ptr, [ptr_lambda = std::move(ptr)](auto const *) {}}); + } } // namespace template @@ -272,6 +288,14 @@ OPENPMD_FOREACH_DATASET_DATATYPE(INSTANTIATE_METHOD_TEMPLATES_FOR_BASE) INSTANTIATE_METHOD_TEMPLATES( \ ConfigureLoadStore< \ ConfigureStoreChunkFromBuffer>>, \ + dtype) \ + template class ConfigureStoreChunkFromBuffer< \ + UniquePtrWithLambda>; \ + template class ConfigureLoadStore< \ + ConfigureStoreChunkFromBuffer>>; \ + INSTANTIATE_METHOD_TEMPLATES( \ + ConfigureLoadStore< \ + ConfigureStoreChunkFromBuffer>>, \ dtype) // clang-format on diff --git a/test/SerialIOTest.cpp b/test/SerialIOTest.cpp index 41f5afa14a..c5a37ae718 100644 --- a/test/SerialIOTest.cpp +++ b/test/SerialIOTest.cpp @@ -928,7 +928,11 @@ inline void constant_scalar(std::string const &file_ending) new unsigned int[6], [](unsigned int const *p) { delete[] p; }); unsigned int e{0}; std::generate(E.get(), E.get() + 6, [&e] { return e++; }); - E_y.storeChunk(std::move(E), {0, 0, 0}, {1, 2, 3}); + // check that const-type unique pointers work in the builder pattern + E_y.prepareLoadStore() + .extent({1, 2, 3}) + .withUniquePtr(std::move(E).static_cast_()) + .enqueueStore(); // store a number of predefined attributes in E Mesh &E_mesh = s.iterations[1].meshes["E"];