openPMD-api
 
Loading...
Searching...
No Matches
Memory.hpp
1/* Copyright 2017-2025 Fabian Koller, Axel Huebl, Franz Poeschel
2 *
3 * This file is part of openPMD-api.
4 *
5 * openPMD-api is free software: you can redistribute it and/or modify
6 * it under the terms of of either the GNU General Public License or
7 * the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * openPMD-api is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License and the GNU Lesser General Public License
15 * for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * and the GNU Lesser General Public License along with openPMD-api.
19 * If not, see <http://www.gnu.org/licenses/>.
20 */
21#pragma once
22
23#include "openPMD/Dataset.hpp"
24#include "openPMD/Datatype.hpp"
25#include "openPMD/Error.hpp"
26#include "openPMD/auxiliary/UniquePtr.hpp"
27
28#include <any>
29#include <functional>
30#include <memory>
31#include <utility>
32
33namespace openPMD
34{
35namespace auxiliary
36{
37 std::unique_ptr<void, std::function<void(void *)>>
38 allocatePtr(Datatype dtype, uint64_t numPoints);
39
40 std::unique_ptr<void, std::function<void(void *)>>
41 allocatePtr(Datatype dtype, Extent const &e);
42
43 /*
44 * A buffer for the WRITE_DATASET task that can either be a std::shared_ptr
45 * or a std::unique_ptr.
46 */
47 struct WriteBuffer
48 {
49 /*
50 * Sic. Have to put the unique_ptr behind a shared_ptr because
51 * std::variant does not want non-copyable types.
52 * Use a separate class to avoid mistakes in double dereference.
53 */
54 struct CopyableUniquePtr
55 : private std::shared_ptr<UniquePtrWithLambda<void>>
56 {
57 private:
58 using parent_t = std::shared_ptr<UniquePtrWithLambda<void>>;
59
60 public:
61 CopyableUniquePtr();
62 CopyableUniquePtr(UniquePtrWithLambda<void> ptr_in);
63 auto get() -> void *;
64 [[nodiscard]] auto get() const -> void const *;
65 [[nodiscard]] auto release() -> UniquePtrWithLambda<void>;
66 };
67 using SharedPtr = std::shared_ptr<void const>;
68 /*
69 * Use std::any publically since some compilers have trouble with
70 * certain uses of std::variant, so hide it from them.
71 * Look into Memory_internal.hpp for the variant type.
72 * https://github.com/openPMD/openPMD-api/issues/1720
73 */
74 std::any m_buffer;
75
76 WriteBuffer();
77 WriteBuffer(std::shared_ptr<void const> ptr);
78 WriteBuffer(UniquePtrWithLambda<void> ptr);
79
80 WriteBuffer(WriteBuffer &&) noexcept;
81 WriteBuffer(WriteBuffer const &) = delete;
82 WriteBuffer &operator=(WriteBuffer &&) noexcept;
83 WriteBuffer &operator=(WriteBuffer const &) = delete;
84
85 WriteBuffer const &operator=(std::shared_ptr<void const> ptr);
86 WriteBuffer const &operator=(UniquePtrWithLambda<void> ptr);
87
88 void const *get() const;
89
90 template <typename variant_t>
91 auto as_variant() -> variant_t &
92 {
93 return *std::any_cast<variant_t>(&m_buffer);
94 }
95
96 template <typename variant_t>
97 auto as_variant() const -> variant_t const &
98 {
99 return *std::any_cast<variant_t>(&m_buffer);
100 }
101 };
102} // namespace auxiliary
103} // namespace openPMD
Unique Pointer class that uses a dynamic destructor type.
Definition UniquePtr.hpp:86
Public definitions of openPMD-api.
Definition Date.cpp:29
Datatype
Concrete datatype of an object available at runtime.
Definition Datatype.hpp:51
STL namespace.