Skip to content

Commit b021bd6

Browse files
committed
Remove variant from public interface of Memory.hpp
This should fix the Cuda issues. Will not yet work with ADIOS2
1 parent c5fb5de commit b021bd6

3 files changed

Lines changed: 86 additions & 23 deletions

File tree

include/openPMD/auxiliary/Memory.hpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "openPMD/Datatype.hpp"
2525
#include "openPMD/auxiliary/UniquePtr.hpp"
2626

27+
#include <any>
2728
#include <complex>
2829
#include <functional>
2930
#include <iostream>
@@ -48,30 +49,40 @@ namespace auxiliary
4849
*/
4950
struct WriteBuffer
5051
{
51-
using EligibleTypes = std::
52-
variant<std::shared_ptr<void const>, UniquePtrWithLambda<void>>;
53-
EligibleTypes m_buffer;
52+
using UniquePtr = std::shared_ptr<UniquePtrWithLambda<void>>;
53+
using SharedPtr = std::shared_ptr<void const>;
54+
std::any m_buffer;
5455

5556
WriteBuffer();
5657

57-
template <typename... Args>
58-
explicit WriteBuffer(Args &&...args)
59-
: m_buffer(std::forward<Args>(args)...)
60-
{}
58+
WriteBuffer(std::shared_ptr<void const> ptr);
59+
// WriteBuffer(std::shared_ptr<void> const &ptr);
6160

62-
WriteBuffer(WriteBuffer &&) noexcept(
63-
noexcept(EligibleTypes(std::declval<EligibleTypes &&>())));
61+
WriteBuffer(UniquePtrWithLambda<void> ptr);
62+
63+
WriteBuffer(WriteBuffer &&) noexcept;
6464
WriteBuffer(WriteBuffer const &) = delete;
65-
WriteBuffer &operator=(WriteBuffer &&) noexcept(noexcept(
66-
std::declval<EligibleTypes &>() =
67-
std::declval<EligibleTypes &&>()));
65+
WriteBuffer &operator=(WriteBuffer &&) noexcept;
6866
WriteBuffer &operator=(WriteBuffer const &) = delete;
6967

7068
WriteBuffer const &operator=(std::shared_ptr<void const> ptr);
69+
// WriteBuffer const &operator=(std::shared_ptr<void> const &ptr);
7170

72-
WriteBuffer const &operator=(UniquePtrWithLambda<void const> ptr);
71+
WriteBuffer const &operator=(UniquePtrWithLambda<void> ptr);
7372

7473
void const *get() const;
74+
75+
template <typename variant_t>
76+
auto as_variant() -> variant_t &
77+
{
78+
return *std::any_cast<variant_t>(&m_buffer);
79+
}
80+
81+
template <typename variant_t>
82+
auto as_variant() const -> variant_t const &
83+
{
84+
return *std::any_cast<variant_t>(&m_buffer);
85+
}
7586
};
7687
} // namespace auxiliary
7788
} // namespace openPMD
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* Copyright 2017-2021 Fabian Koller
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/auxiliary/Memory.hpp"
24+
#include "openPMD/auxiliary/UniquePtr.hpp"
25+
#include <memory>
26+
27+
namespace openPMD::auxiliary
28+
{
29+
// cannot use a unique_ptr inside a std::variant, so we represent it with this
30+
using WriteBufferTypes =
31+
std::variant<WriteBuffer::UniquePtr, WriteBuffer::SharedPtr>;
32+
} // namespace openPMD::auxiliary

src/auxiliary/Memory.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
*/
2121

2222
#include "openPMD/auxiliary/Memory.hpp"
23+
#include "openPMD/ChunkInfo.hpp"
24+
#include "openPMD/auxiliary/Memory_internal.hpp"
25+
#include "openPMD/auxiliary/UniquePtr.hpp"
2326

27+
#include <any>
2428
#include <complex>
2529
#include <functional>
2630
#include <iostream>
@@ -157,24 +161,40 @@ allocatePtr(Datatype dtype, Extent const &e)
157161
return allocatePtr(dtype, numPoints);
158162
}
159163

160-
WriteBuffer::WriteBuffer() : m_buffer(UniquePtrWithLambda<void>())
164+
WriteBuffer::WriteBuffer() : m_buffer(std::make_any<UniquePtr>())
161165
{}
162166

163-
WriteBuffer::WriteBuffer(WriteBuffer &&) noexcept(
164-
noexcept(EligibleTypes(std::declval<EligibleTypes &&>()))) = default;
165-
WriteBuffer &WriteBuffer::operator=(WriteBuffer &&) noexcept(noexcept(
166-
std::declval<EligibleTypes &>() = std::declval<EligibleTypes &&>())) =
167-
default;
167+
WriteBuffer::WriteBuffer(std::shared_ptr<void const> ptr)
168+
: m_buffer(std::make_any<WriteBufferTypes>(std::move(ptr)))
169+
{}
170+
// WriteBuffer::WriteBuffer(std::shared_ptr<void> const &ptr)
171+
// : WriteBuffer{std::static_pointer_cast<void const>(ptr)}
172+
// {}
173+
174+
WriteBuffer::WriteBuffer(UniquePtrWithLambda<void> ptr)
175+
: m_buffer(
176+
std::make_any<WriteBufferTypes>(
177+
std::make_shared<UniquePtrWithLambda<void>>(std::move(ptr))))
178+
{}
179+
180+
WriteBuffer::WriteBuffer(WriteBuffer &&) noexcept = default;
181+
WriteBuffer &WriteBuffer::operator=(WriteBuffer &&) noexcept = default;
168182

169183
WriteBuffer const &WriteBuffer::operator=(std::shared_ptr<void const> ptr)
170184
{
171-
m_buffer = std::move(ptr);
185+
m_buffer = std::make_any<WriteBufferTypes>(std::move(ptr));
172186
return *this;
173187
}
188+
// WriteBuffer const &WriteBuffer::operator=(std::shared_ptr<void> const &ptr)
189+
// {
190+
// operator=(std::static_pointer_cast<void const>(ptr));
191+
// return *this;
192+
// }
174193

175-
WriteBuffer const &WriteBuffer::operator=(UniquePtrWithLambda<void const> ptr)
194+
WriteBuffer const &WriteBuffer::operator=(UniquePtrWithLambda<void> ptr)
176195
{
177-
m_buffer = std::move(ptr);
196+
m_buffer = std::make_any<WriteBufferTypes>(
197+
std::make_shared<UniquePtrWithLambda<void>>(std::move(ptr)));
178198
return *this;
179199
}
180200

@@ -186,6 +206,6 @@ void const *WriteBuffer::get() const
186206
// we're being sneaky and don't distinguish the types here
187207
return static_cast<void const *>(arg.get());
188208
},
189-
m_buffer);
209+
as_variant<WriteBufferTypes>());
190210
}
191211
} // namespace openPMD::auxiliary

0 commit comments

Comments
 (0)