From 95b79af98f9ea6ae4588629bd98b3c9bad1bf29f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Fri, 14 Nov 2025 17:39:40 +0100 Subject: [PATCH 1/9] Remove variant from public interface of Memory.hpp This should fix the Cuda issues. Will not yet work with ADIOS2 --- include/openPMD/auxiliary/Memory.hpp | 37 +++++++++++------ include/openPMD/auxiliary/Memory_internal.hpp | 32 +++++++++++++++ src/auxiliary/Memory.cpp | 40 ++++++++++++++----- 3 files changed, 86 insertions(+), 23 deletions(-) create mode 100644 include/openPMD/auxiliary/Memory_internal.hpp diff --git a/include/openPMD/auxiliary/Memory.hpp b/include/openPMD/auxiliary/Memory.hpp index 75199f0985..5070d3c016 100644 --- a/include/openPMD/auxiliary/Memory.hpp +++ b/include/openPMD/auxiliary/Memory.hpp @@ -24,6 +24,7 @@ #include "openPMD/Datatype.hpp" #include "openPMD/auxiliary/UniquePtr.hpp" +#include #include #include #include @@ -48,30 +49,40 @@ namespace auxiliary */ struct WriteBuffer { - using EligibleTypes = std:: - variant, UniquePtrWithLambda>; - EligibleTypes m_buffer; + using UniquePtr = std::shared_ptr>; + using SharedPtr = std::shared_ptr; + std::any m_buffer; WriteBuffer(); - template - explicit WriteBuffer(Args &&...args) - : m_buffer(std::forward(args)...) - {} + WriteBuffer(std::shared_ptr ptr); + // WriteBuffer(std::shared_ptr const &ptr); - WriteBuffer(WriteBuffer &&) noexcept( - noexcept(EligibleTypes(std::declval()))); + WriteBuffer(UniquePtrWithLambda ptr); + + WriteBuffer(WriteBuffer &&) noexcept; WriteBuffer(WriteBuffer const &) = delete; - WriteBuffer &operator=(WriteBuffer &&) noexcept(noexcept( - std::declval() = - std::declval())); + WriteBuffer &operator=(WriteBuffer &&) noexcept; WriteBuffer &operator=(WriteBuffer const &) = delete; WriteBuffer const &operator=(std::shared_ptr ptr); + // WriteBuffer const &operator=(std::shared_ptr const &ptr); - WriteBuffer const &operator=(UniquePtrWithLambda ptr); + WriteBuffer const &operator=(UniquePtrWithLambda ptr); void const *get() const; + + template + auto as_variant() -> variant_t & + { + return *std::any_cast(&m_buffer); + } + + template + auto as_variant() const -> variant_t const & + { + return *std::any_cast(&m_buffer); + } }; } // namespace auxiliary } // namespace openPMD diff --git a/include/openPMD/auxiliary/Memory_internal.hpp b/include/openPMD/auxiliary/Memory_internal.hpp new file mode 100644 index 0000000000..d5f553eaee --- /dev/null +++ b/include/openPMD/auxiliary/Memory_internal.hpp @@ -0,0 +1,32 @@ +/* Copyright 2017-2021 Fabian Koller + * + * This file is part of openPMD-api. + * + * openPMD-api is free software: you can redistribute it and/or modify + * it under the terms of of either the GNU General Public License or + * the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * openPMD-api is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License and the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * and the GNU Lesser General Public License along with openPMD-api. + * If not, see . + */ +#pragma once + +#include "openPMD/auxiliary/Memory.hpp" +#include "openPMD/auxiliary/UniquePtr.hpp" +#include + +namespace openPMD::auxiliary +{ +// cannot use a unique_ptr inside a std::variant, so we represent it with this +using WriteBufferTypes = + std::variant; +} // namespace openPMD::auxiliary diff --git a/src/auxiliary/Memory.cpp b/src/auxiliary/Memory.cpp index cf95ecf582..8b3869abde 100644 --- a/src/auxiliary/Memory.cpp +++ b/src/auxiliary/Memory.cpp @@ -20,7 +20,11 @@ */ #include "openPMD/auxiliary/Memory.hpp" +#include "openPMD/ChunkInfo.hpp" +#include "openPMD/auxiliary/Memory_internal.hpp" +#include "openPMD/auxiliary/UniquePtr.hpp" +#include #include #include #include @@ -157,24 +161,40 @@ allocatePtr(Datatype dtype, Extent const &e) return allocatePtr(dtype, numPoints); } -WriteBuffer::WriteBuffer() : m_buffer(UniquePtrWithLambda()) +WriteBuffer::WriteBuffer() : m_buffer(std::make_any()) {} -WriteBuffer::WriteBuffer(WriteBuffer &&) noexcept( - noexcept(EligibleTypes(std::declval()))) = default; -WriteBuffer &WriteBuffer::operator=(WriteBuffer &&) noexcept(noexcept( - std::declval() = std::declval())) = - default; +WriteBuffer::WriteBuffer(std::shared_ptr ptr) + : m_buffer(std::make_any(std::move(ptr))) +{} +// WriteBuffer::WriteBuffer(std::shared_ptr const &ptr) +// : WriteBuffer{std::static_pointer_cast(ptr)} +// {} + +WriteBuffer::WriteBuffer(UniquePtrWithLambda ptr) + : m_buffer( + std::make_any( + std::make_shared>(std::move(ptr)))) +{} + +WriteBuffer::WriteBuffer(WriteBuffer &&) noexcept = default; +WriteBuffer &WriteBuffer::operator=(WriteBuffer &&) noexcept = default; WriteBuffer const &WriteBuffer::operator=(std::shared_ptr ptr) { - m_buffer = std::move(ptr); + m_buffer = std::make_any(std::move(ptr)); return *this; } +// WriteBuffer const &WriteBuffer::operator=(std::shared_ptr const &ptr) +// { +// operator=(std::static_pointer_cast(ptr)); +// return *this; +// } -WriteBuffer const &WriteBuffer::operator=(UniquePtrWithLambda ptr) +WriteBuffer const &WriteBuffer::operator=(UniquePtrWithLambda ptr) { - m_buffer = std::move(ptr); + m_buffer = std::make_any( + std::make_shared>(std::move(ptr))); return *this; } @@ -186,6 +206,6 @@ void const *WriteBuffer::get() const // we're being sneaky and don't distinguish the types here return static_cast(arg.get()); }, - m_buffer); + as_variant()); } } // namespace openPMD::auxiliary From 0862f612cd1ed944ece3b75a2bcbb29e2a50f60d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Fri, 14 Nov 2025 17:40:34 +0100 Subject: [PATCH 2/9] Remove include guard --- include/openPMD/openPMD.hpp | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/include/openPMD/openPMD.hpp b/include/openPMD/openPMD.hpp index bc6ea782f7..268ddaa491 100644 --- a/include/openPMD/openPMD.hpp +++ b/include/openPMD/openPMD.hpp @@ -25,21 +25,6 @@ namespace openPMD {} -#if defined(CUDA_VERSION) && !defined(OPENPMD_SKIP_CHECK_ISSUE_1720) -static_assert(__cplusplus < 202002L || CUDA_VERSION >= 12040, R"( -Cannot use the openPMD-api in C++20 projects under a Cuda version lower -than 12.4.0 due to a bug in the implementation of std::variant. -Further information at: -https://github.com/openPMD/openPMD-api/issues/1720 -https://forums.developer.nvidia.com/t/nvcc-c-20-std-variant-complie-failed/270162/5 -This cannot be fixed on our side, please either upgrade to CUDA >= 12.4.0 -or use C++17. -If you think that this assertion is shown wrongly, please apply -'#define OPENPMD_SKIP_CHECK_ISSUE_1720' before including -''. -)"); -#endif - // IWYU pragma: begin_exports #include "openPMD/Dataset.hpp" #include "openPMD/Datatype.hpp" From d8b7f7600c99a9b0cdf43fc524b14f08cc625902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Fri, 14 Nov 2025 17:53:20 +0100 Subject: [PATCH 3/9] Fix ADIOS2 --- src/IO/ADIOS/ADIOS2File.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/IO/ADIOS/ADIOS2File.cpp b/src/IO/ADIOS/ADIOS2File.cpp index 30d6f0e981..74a88b60bc 100644 --- a/src/IO/ADIOS/ADIOS2File.cpp +++ b/src/IO/ADIOS/ADIOS2File.cpp @@ -26,6 +26,7 @@ #include "openPMD/IO/AbstractIOHandler.hpp" #include "openPMD/IterationEncoding.hpp" #include "openPMD/auxiliary/Environment.hpp" +#include "openPMD/auxiliary/Memory_internal.hpp" #include "openPMD/auxiliary/StringManip.hpp" #include @@ -114,7 +115,7 @@ void WriteDataset::call(ADIOS2File &ba, detail::BufferedPut &bp) } else if constexpr (std::is_same_v< ptr_type, - UniquePtrWithLambda>) + std::shared_ptr>>) { BufferedUniquePtrPut bput; bput.name = std::move(bp.name); @@ -128,7 +129,7 @@ void WriteDataset::call(ADIOS2File &ba, detail::BufferedPut &bp) * (ptr_type does not work for this case). */ // clang-format off - bput.data = std::move(arg); // NOLINT(bugprone-move-forwarding-reference) + bput.data = std::move(*arg); // NOLINT(bugprone-move-forwarding-reference) // clang-format on bput.dtype = bp.param.dtype; ba.m_uniquePtrPuts.push_back(std::move(bput)); @@ -139,7 +140,7 @@ void WriteDataset::call(ADIOS2File &ba, detail::BufferedPut &bp) always_false_v, "Unhandled std::variant branch"); } }, - bp.param.data.m_buffer); + bp.param.data.as_variant()); } template From a994025d5782c6b6dd239a7896d2877703592c4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Mon, 17 Nov 2025 09:55:46 +0100 Subject: [PATCH 4/9] Fix invasive test --- test/CoreTest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/CoreTest.cpp b/test/CoreTest.cpp index 6c62e1d82e..e4a8d17c70 100644 --- a/test/CoreTest.cpp +++ b/test/CoreTest.cpp @@ -11,7 +11,7 @@ #include "openPMD/IO/ADIOS/macros.hpp" #include "openPMD/auxiliary/Filesystem.hpp" -#include "openPMD/auxiliary/JSON.hpp" +#include "openPMD/auxiliary/Memory_internal.hpp" #include "openPMD/auxiliary/UniquePtr.hpp" #include @@ -1252,7 +1252,7 @@ TEST_CASE("use_count_test", "[core]") std::get>( static_cast *>( pprc.get().m_chunks.front().parameter.get()) - ->data.m_buffer) + ->data.as_variant()) .use_count() == 1); #endif } From aed17141fb578e026378fb1fc1548c2705774741 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Mon, 17 Nov 2025 10:17:20 +0100 Subject: [PATCH 5/9] comments --- include/openPMD/auxiliary/Memory.hpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/openPMD/auxiliary/Memory.hpp b/include/openPMD/auxiliary/Memory.hpp index 5070d3c016..1d1dd1cae0 100644 --- a/include/openPMD/auxiliary/Memory.hpp +++ b/include/openPMD/auxiliary/Memory.hpp @@ -49,8 +49,18 @@ namespace auxiliary */ struct WriteBuffer { + /* + * Sic. Have to put the unique_ptr behind a shared_ptr because + * std::variant does not want immovable types. + */ using UniquePtr = std::shared_ptr>; using SharedPtr = std::shared_ptr; + /* + * Use std::any publically since some compilers have trouble with + * certain uses of std::variant, so hide it from them. + * Look into Memory_internal.hpp for the variant type. + * https://github.com/openPMD/openPMD-api/issues/1720 + */ std::any m_buffer; WriteBuffer(); From c779abdf3fc53977d8c1aa89cba3669bd99d7933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Mon, 17 Nov 2025 16:18:01 +0100 Subject: [PATCH 6/9] Use own class for MovableUniquePtr --- include/openPMD/auxiliary/Memory.hpp | 40 ++++++++++++++++--- include/openPMD/auxiliary/Memory_internal.hpp | 4 +- src/IO/ADIOS/ADIOS2File.cpp | 14 ++----- src/auxiliary/Memory.cpp | 9 ++--- 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/include/openPMD/auxiliary/Memory.hpp b/include/openPMD/auxiliary/Memory.hpp index 1d1dd1cae0..b61249fd06 100644 --- a/include/openPMD/auxiliary/Memory.hpp +++ b/include/openPMD/auxiliary/Memory.hpp @@ -22,16 +22,13 @@ #include "openPMD/Dataset.hpp" #include "openPMD/Datatype.hpp" +#include "openPMD/Error.hpp" #include "openPMD/auxiliary/UniquePtr.hpp" #include -#include #include -#include #include -#include #include -#include namespace openPMD { @@ -52,8 +49,41 @@ namespace auxiliary /* * Sic. Have to put the unique_ptr behind a shared_ptr because * std::variant does not want immovable types. + * Use a separate class to avoid mistakes in double dereference. */ - using UniquePtr = std::shared_ptr>; + struct MovableUniquePtr + : private std::shared_ptr> + { + private: + using parent_t = std::shared_ptr>; + + public: + MovableUniquePtr() = default; + MovableUniquePtr(UniquePtrWithLambda ptr_in) + : parent_t{std::make_shared>( + std::move(ptr_in))} + {} + auto get() -> void * + { + return (**this).get(); + } + [[nodiscard]] auto get() const -> void const * + { + return (**this).get(); + } + [[nodiscard]] auto release() -> UniquePtrWithLambda + { + if (parent_t::use_count() > 1) + { + throw error::Internal( + "Control flow error: UniquePtr variant of WriteBuffer " + "has been copied."); + } + UniquePtrWithLambda res = std::move(**this); + this->reset(); + return res; + } + }; using SharedPtr = std::shared_ptr; /* * Use std::any publically since some compilers have trouble with diff --git a/include/openPMD/auxiliary/Memory_internal.hpp b/include/openPMD/auxiliary/Memory_internal.hpp index d5f553eaee..d85ecf4c63 100644 --- a/include/openPMD/auxiliary/Memory_internal.hpp +++ b/include/openPMD/auxiliary/Memory_internal.hpp @@ -21,12 +21,10 @@ #pragma once #include "openPMD/auxiliary/Memory.hpp" -#include "openPMD/auxiliary/UniquePtr.hpp" -#include namespace openPMD::auxiliary { // cannot use a unique_ptr inside a std::variant, so we represent it with this using WriteBufferTypes = - std::variant; + std::variant; } // namespace openPMD::auxiliary diff --git a/src/IO/ADIOS/ADIOS2File.cpp b/src/IO/ADIOS/ADIOS2File.cpp index 74a88b60bc..1a2fe8a8b5 100644 --- a/src/IO/ADIOS/ADIOS2File.cpp +++ b/src/IO/ADIOS/ADIOS2File.cpp @@ -26,6 +26,7 @@ #include "openPMD/IO/AbstractIOHandler.hpp" #include "openPMD/IterationEncoding.hpp" #include "openPMD/auxiliary/Environment.hpp" +#include "openPMD/auxiliary/Memory.hpp" #include "openPMD/auxiliary/Memory_internal.hpp" #include "openPMD/auxiliary/StringManip.hpp" @@ -115,22 +116,13 @@ void WriteDataset::call(ADIOS2File &ba, detail::BufferedPut &bp) } else if constexpr (std::is_same_v< ptr_type, - std::shared_ptr>>) + auxiliary::WriteBuffer::MovableUniquePtr>) { BufferedUniquePtrPut bput; bput.name = std::move(bp.name); bput.offset = std::move(bp.param.offset); bput.extent = std::move(bp.param.extent); - /* - * Note: Moving is required here since it's a unique_ptr. - * std::forward<>() would theoretically work, but it - * requires the type parameter and we don't have that - * inside the lambda. - * (ptr_type does not work for this case). - */ - // clang-format off - bput.data = std::move(*arg); // NOLINT(bugprone-move-forwarding-reference) - // clang-format on + bput.data = arg.release(); bput.dtype = bp.param.dtype; ba.m_uniquePtrPuts.push_back(std::move(bput)); } diff --git a/src/auxiliary/Memory.cpp b/src/auxiliary/Memory.cpp index 8b3869abde..e1897680b9 100644 --- a/src/auxiliary/Memory.cpp +++ b/src/auxiliary/Memory.cpp @@ -161,7 +161,7 @@ allocatePtr(Datatype dtype, Extent const &e) return allocatePtr(dtype, numPoints); } -WriteBuffer::WriteBuffer() : m_buffer(std::make_any()) +WriteBuffer::WriteBuffer() : m_buffer(std::make_any()) {} WriteBuffer::WriteBuffer(std::shared_ptr ptr) @@ -173,8 +173,7 @@ WriteBuffer::WriteBuffer(std::shared_ptr ptr) WriteBuffer::WriteBuffer(UniquePtrWithLambda ptr) : m_buffer( - std::make_any( - std::make_shared>(std::move(ptr)))) + std::make_any(MovableUniquePtr(std::move(ptr)))) {} WriteBuffer::WriteBuffer(WriteBuffer &&) noexcept = default; @@ -193,8 +192,8 @@ WriteBuffer const &WriteBuffer::operator=(std::shared_ptr ptr) WriteBuffer const &WriteBuffer::operator=(UniquePtrWithLambda ptr) { - m_buffer = std::make_any( - std::make_shared>(std::move(ptr))); + m_buffer = + std::make_any(MovableUniquePtr(std::move(ptr))); return *this; } From d5e529d9800f96c436d109dae511cf20c1e27317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Mon, 17 Nov 2025 16:29:28 +0100 Subject: [PATCH 7/9] extract definitions --- include/openPMD/auxiliary/Memory.hpp | 30 +++++----------------------- src/auxiliary/Memory.cpp | 30 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/include/openPMD/auxiliary/Memory.hpp b/include/openPMD/auxiliary/Memory.hpp index b61249fd06..c388184547 100644 --- a/include/openPMD/auxiliary/Memory.hpp +++ b/include/openPMD/auxiliary/Memory.hpp @@ -58,31 +58,11 @@ namespace auxiliary using parent_t = std::shared_ptr>; public: - MovableUniquePtr() = default; - MovableUniquePtr(UniquePtrWithLambda ptr_in) - : parent_t{std::make_shared>( - std::move(ptr_in))} - {} - auto get() -> void * - { - return (**this).get(); - } - [[nodiscard]] auto get() const -> void const * - { - return (**this).get(); - } - [[nodiscard]] auto release() -> UniquePtrWithLambda - { - if (parent_t::use_count() > 1) - { - throw error::Internal( - "Control flow error: UniquePtr variant of WriteBuffer " - "has been copied."); - } - UniquePtrWithLambda res = std::move(**this); - this->reset(); - return res; - } + MovableUniquePtr(); + MovableUniquePtr(UniquePtrWithLambda ptr_in); + auto get() -> void *; + [[nodiscard]] auto get() const -> void const *; + [[nodiscard]] auto release() -> UniquePtrWithLambda; }; using SharedPtr = std::shared_ptr; /* diff --git a/src/auxiliary/Memory.cpp b/src/auxiliary/Memory.cpp index e1897680b9..a1f6afbd7e 100644 --- a/src/auxiliary/Memory.cpp +++ b/src/auxiliary/Memory.cpp @@ -161,6 +161,36 @@ allocatePtr(Datatype dtype, Extent const &e) return allocatePtr(dtype, numPoints); } +WriteBuffer::MovableUniquePtr::MovableUniquePtr() = default; + +WriteBuffer::MovableUniquePtr::MovableUniquePtr( + UniquePtrWithLambda ptr_in) + : parent_t{std::make_shared>(std::move(ptr_in))} +{} + +auto WriteBuffer::MovableUniquePtr::get() -> void * +{ + return (**this).get(); +} + +auto WriteBuffer::MovableUniquePtr::get() const -> void const * +{ + return (**this).get(); +} + +auto WriteBuffer::MovableUniquePtr::release() -> UniquePtrWithLambda +{ + if (parent_t::use_count() > 1) + { + throw error::Internal( + "Control flow error: UniquePtr variant of WriteBuffer " + "has been copied."); + } + UniquePtrWithLambda res = std::move(**this); + this->reset(); + return res; +} + WriteBuffer::WriteBuffer() : m_buffer(std::make_any()) {} From c638b058cd772597dd96ca0d230d164924cb9cc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Thu, 20 Nov 2025 11:54:10 +0100 Subject: [PATCH 8/9] Cleanup --- include/openPMD/auxiliary/Memory.hpp | 5 ----- include/openPMD/auxiliary/Memory_internal.hpp | 2 +- src/auxiliary/Memory.cpp | 11 ----------- 3 files changed, 1 insertion(+), 17 deletions(-) diff --git a/include/openPMD/auxiliary/Memory.hpp b/include/openPMD/auxiliary/Memory.hpp index c388184547..209adde1ad 100644 --- a/include/openPMD/auxiliary/Memory.hpp +++ b/include/openPMD/auxiliary/Memory.hpp @@ -74,10 +74,7 @@ namespace auxiliary std::any m_buffer; WriteBuffer(); - WriteBuffer(std::shared_ptr ptr); - // WriteBuffer(std::shared_ptr const &ptr); - WriteBuffer(UniquePtrWithLambda ptr); WriteBuffer(WriteBuffer &&) noexcept; @@ -86,8 +83,6 @@ namespace auxiliary WriteBuffer &operator=(WriteBuffer const &) = delete; WriteBuffer const &operator=(std::shared_ptr ptr); - // WriteBuffer const &operator=(std::shared_ptr const &ptr); - WriteBuffer const &operator=(UniquePtrWithLambda ptr); void const *get() const; diff --git a/include/openPMD/auxiliary/Memory_internal.hpp b/include/openPMD/auxiliary/Memory_internal.hpp index d85ecf4c63..ddcaf5ba70 100644 --- a/include/openPMD/auxiliary/Memory_internal.hpp +++ b/include/openPMD/auxiliary/Memory_internal.hpp @@ -1,4 +1,4 @@ -/* Copyright 2017-2021 Fabian Koller +/* Copyright 2025 Franz Poeschel * * This file is part of openPMD-api. * diff --git a/src/auxiliary/Memory.cpp b/src/auxiliary/Memory.cpp index a1f6afbd7e..d30eec4e6d 100644 --- a/src/auxiliary/Memory.cpp +++ b/src/auxiliary/Memory.cpp @@ -193,14 +193,9 @@ auto WriteBuffer::MovableUniquePtr::release() -> UniquePtrWithLambda WriteBuffer::WriteBuffer() : m_buffer(std::make_any()) {} - WriteBuffer::WriteBuffer(std::shared_ptr ptr) : m_buffer(std::make_any(std::move(ptr))) {} -// WriteBuffer::WriteBuffer(std::shared_ptr const &ptr) -// : WriteBuffer{std::static_pointer_cast(ptr)} -// {} - WriteBuffer::WriteBuffer(UniquePtrWithLambda ptr) : m_buffer( std::make_any(MovableUniquePtr(std::move(ptr)))) @@ -214,12 +209,6 @@ WriteBuffer const &WriteBuffer::operator=(std::shared_ptr ptr) m_buffer = std::make_any(std::move(ptr)); return *this; } -// WriteBuffer const &WriteBuffer::operator=(std::shared_ptr const &ptr) -// { -// operator=(std::static_pointer_cast(ptr)); -// return *this; -// } - WriteBuffer const &WriteBuffer::operator=(UniquePtrWithLambda ptr) { m_buffer = From 05a2c98843bd9110b20cbda06d41efe7f5d818e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Wed, 3 Dec 2025 17:09:16 +0100 Subject: [PATCH 9/9] Rename MovableUniquePtr --> CopyableUniquePtr --- include/openPMD/auxiliary/Memory.hpp | 8 ++++---- include/openPMD/auxiliary/Memory_internal.hpp | 2 +- src/IO/ADIOS/ADIOS2File.cpp | 2 +- src/auxiliary/Memory.cpp | 16 ++++++++-------- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/include/openPMD/auxiliary/Memory.hpp b/include/openPMD/auxiliary/Memory.hpp index 209adde1ad..19c36a1ee6 100644 --- a/include/openPMD/auxiliary/Memory.hpp +++ b/include/openPMD/auxiliary/Memory.hpp @@ -48,18 +48,18 @@ namespace auxiliary { /* * Sic. Have to put the unique_ptr behind a shared_ptr because - * std::variant does not want immovable types. + * std::variant does not want non-copyable types. * Use a separate class to avoid mistakes in double dereference. */ - struct MovableUniquePtr + struct CopyableUniquePtr : private std::shared_ptr> { private: using parent_t = std::shared_ptr>; public: - MovableUniquePtr(); - MovableUniquePtr(UniquePtrWithLambda ptr_in); + CopyableUniquePtr(); + CopyableUniquePtr(UniquePtrWithLambda ptr_in); auto get() -> void *; [[nodiscard]] auto get() const -> void const *; [[nodiscard]] auto release() -> UniquePtrWithLambda; diff --git a/include/openPMD/auxiliary/Memory_internal.hpp b/include/openPMD/auxiliary/Memory_internal.hpp index ddcaf5ba70..bf3c8ccb4b 100644 --- a/include/openPMD/auxiliary/Memory_internal.hpp +++ b/include/openPMD/auxiliary/Memory_internal.hpp @@ -26,5 +26,5 @@ namespace openPMD::auxiliary { // cannot use a unique_ptr inside a std::variant, so we represent it with this using WriteBufferTypes = - std::variant; + std::variant; } // namespace openPMD::auxiliary diff --git a/src/IO/ADIOS/ADIOS2File.cpp b/src/IO/ADIOS/ADIOS2File.cpp index 1a2fe8a8b5..1d20aeb04d 100644 --- a/src/IO/ADIOS/ADIOS2File.cpp +++ b/src/IO/ADIOS/ADIOS2File.cpp @@ -116,7 +116,7 @@ void WriteDataset::call(ADIOS2File &ba, detail::BufferedPut &bp) } else if constexpr (std::is_same_v< ptr_type, - auxiliary::WriteBuffer::MovableUniquePtr>) + auxiliary::WriteBuffer::CopyableUniquePtr>) { BufferedUniquePtrPut bput; bput.name = std::move(bp.name); diff --git a/src/auxiliary/Memory.cpp b/src/auxiliary/Memory.cpp index d30eec4e6d..d60523b2e6 100644 --- a/src/auxiliary/Memory.cpp +++ b/src/auxiliary/Memory.cpp @@ -161,24 +161,24 @@ allocatePtr(Datatype dtype, Extent const &e) return allocatePtr(dtype, numPoints); } -WriteBuffer::MovableUniquePtr::MovableUniquePtr() = default; +WriteBuffer::CopyableUniquePtr::CopyableUniquePtr() = default; -WriteBuffer::MovableUniquePtr::MovableUniquePtr( +WriteBuffer::CopyableUniquePtr::CopyableUniquePtr( UniquePtrWithLambda ptr_in) : parent_t{std::make_shared>(std::move(ptr_in))} {} -auto WriteBuffer::MovableUniquePtr::get() -> void * +auto WriteBuffer::CopyableUniquePtr::get() -> void * { return (**this).get(); } -auto WriteBuffer::MovableUniquePtr::get() const -> void const * +auto WriteBuffer::CopyableUniquePtr::get() const -> void const * { return (**this).get(); } -auto WriteBuffer::MovableUniquePtr::release() -> UniquePtrWithLambda +auto WriteBuffer::CopyableUniquePtr::release() -> UniquePtrWithLambda { if (parent_t::use_count() > 1) { @@ -191,14 +191,14 @@ auto WriteBuffer::MovableUniquePtr::release() -> UniquePtrWithLambda return res; } -WriteBuffer::WriteBuffer() : m_buffer(std::make_any()) +WriteBuffer::WriteBuffer() : m_buffer(std::make_any()) {} WriteBuffer::WriteBuffer(std::shared_ptr ptr) : m_buffer(std::make_any(std::move(ptr))) {} WriteBuffer::WriteBuffer(UniquePtrWithLambda ptr) : m_buffer( - std::make_any(MovableUniquePtr(std::move(ptr)))) + std::make_any(CopyableUniquePtr(std::move(ptr)))) {} WriteBuffer::WriteBuffer(WriteBuffer &&) noexcept = default; @@ -212,7 +212,7 @@ WriteBuffer const &WriteBuffer::operator=(std::shared_ptr ptr) WriteBuffer const &WriteBuffer::operator=(UniquePtrWithLambda ptr) { m_buffer = - std::make_any(MovableUniquePtr(std::move(ptr))); + std::make_any(CopyableUniquePtr(std::move(ptr))); return *this; }