Skip to content

Commit f23ae2a

Browse files
committed
Extract non-template members to ConfigureStoreChunkCore
1 parent b9bedf8 commit f23ae2a

5 files changed

Lines changed: 71 additions & 112 deletions

File tree

include/openPMD/LoadStoreChunk.hpp

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,6 @@ namespace internal
3333
std::optional<MemorySelection> memorySelection;
3434
};
3535

36-
/*
37-
* Actual data members of `ConfigureLoadStore<>`. They don't depend on the
38-
* template parameter of that class template, so by extracting the members
39-
* to this struct, we can pass them around between different instances of
40-
* the class template.
41-
*/
42-
struct ConfigureLoadStoreData
43-
{
44-
ConfigureLoadStoreData(RecordComponent &);
45-
46-
RecordComponent &m_rc;
47-
std::optional<Offset> m_offset;
48-
std::optional<Extent> m_extent;
49-
};
5036
} // namespace internal
5137

5238
namespace auxiliary::detail
@@ -61,38 +47,27 @@ enum class EnqueuePolicy
6147
Immediate
6248
};
6349

64-
/** Basic configuration for a Load/Store operation.
65-
*
66-
* @tparam ChildClass CRT pattern.
67-
* The purpose is that in child classes `return *this` should return
68-
* an instance of the child class, not of ConfigureLoadStore.
69-
* Instantiate with void when using without subclass.
50+
/*
51+
* Actual data members of `ConfigureLoadStore<>` and methods that don't depend
52+
* on the ChildClass template parameter. By extracting the members to this
53+
* struct, we can pass them around between different instances of the class
54+
* template. Numbers of method instantiations can be reduced.
7055
*/
71-
template <typename ChildClass = void>
72-
class ConfigureLoadStore : protected internal::ConfigureLoadStoreData
56+
struct ConfigureLoadStoreCore
7357
{
74-
friend class RecordComponent;
75-
template <typename>
76-
friend class ConfigureLoadStore;
58+
ConfigureLoadStoreCore(RecordComponent &);
7759

78-
protected:
79-
ConfigureLoadStore(RecordComponent &rc);
80-
ConfigureLoadStore(ConfigureLoadStoreData &&);
60+
RecordComponent &m_rc;
61+
std::optional<Offset> m_offset;
62+
std::optional<Extent> m_extent;
8163

64+
protected:
8265
[[nodiscard]] auto dim() const -> uint8_t;
8366
auto getOffset() -> Offset const &;
8467
auto getExtent() -> Extent const &;
8568
auto storeChunkConfig() -> internal::LoadStoreConfig;
8669

8770
public:
88-
using return_type = std::conditional_t<
89-
std::is_void_v<ChildClass>,
90-
/*then*/ ConfigureLoadStore<void>,
91-
/*else*/ ChildClass>;
92-
93-
auto offset(Offset) -> return_type &;
94-
auto extent(Extent) -> return_type &;
95-
9671
/*
9772
* If the type is non-const, then the return type should be
9873
* ConfigureLoadStoreFromBuffer<>, ...
@@ -158,6 +133,33 @@ class ConfigureLoadStore : protected internal::ConfigureLoadStoreData
158133
-> std::future<auxiliary::detail::future_to_shared_ptr_dataset_types>;
159134
};
160135

136+
/** Basic configuration for a Load/Store operation.
137+
*
138+
* @tparam ChildClass CRT pattern.
139+
* The purpose is that in child classes `return *this` should return
140+
* an instance of the child class, not of ConfigureLoadStore.
141+
* Instantiate with void when using without subclass.
142+
*/
143+
template <typename ChildClass = void>
144+
class ConfigureLoadStore : public ConfigureLoadStoreCore
145+
{
146+
friend class RecordComponent;
147+
friend struct ConfigureLoadStoreCore;
148+
149+
protected:
150+
ConfigureLoadStore(RecordComponent &rc);
151+
ConfigureLoadStore(ConfigureLoadStoreCore &&);
152+
153+
public:
154+
using return_type = std::conditional_t<
155+
std::is_void_v<ChildClass>,
156+
/*then*/ ConfigureLoadStore<void>,
157+
/*else*/ ChildClass>;
158+
159+
auto offset(Offset) -> return_type &;
160+
auto extent(Extent) -> return_type &;
161+
};
162+
161163
/** Configuration for a Store operation with a buffer type.
162164
*
163165
* This class does intentionally not support Load operations since there are
@@ -186,8 +188,7 @@ class ConfigureStoreChunkFromBuffer
186188
using parent_t = ConfigureLoadStore<return_type>;
187189

188190
protected:
189-
template <typename T>
190-
friend class ConfigureLoadStore;
191+
friend struct ConfigureLoadStoreCore;
191192

192193
Ptr_Type m_buffer;
193194
std::optional<MemorySelection> m_mem_select;
@@ -238,8 +239,7 @@ class ConfigureLoadStoreFromBuffer
238239
using parent_t = ConfigureStoreChunkFromBuffer<
239240
Ptr_Type,
240241
ConfigureLoadStoreFromBuffer<Ptr_Type>>;
241-
template <typename>
242-
friend class ConfigureLoadStore;
242+
friend struct ConfigureLoadStoreCore;
243243
ConfigureLoadStoreFromBuffer(
244244
Ptr_Type buffer, typename parent_t::parent_t &&);
245245

include/openPMD/LoadStoreChunk.tpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
namespace openPMD
66
{
7-
template <typename ChildClass>
87
template <typename T>
9-
auto ConfigureLoadStore<ChildClass>::withSharedPtr(std::shared_ptr<T> data)
8+
auto ConfigureLoadStoreCore::withSharedPtr(std::shared_ptr<T> data)
109
-> shared_ptr_return_type<T>
1110
{
1211
if (!data)
@@ -18,9 +17,8 @@ auto ConfigureLoadStore<ChildClass>::withSharedPtr(std::shared_ptr<T> data)
1817
std::static_pointer_cast<std::remove_extent_t<T>>(std::move(data)),
1918
{std::move(*this)});
2019
}
21-
template <typename ChildClass>
2220
template <typename T>
23-
auto ConfigureLoadStore<ChildClass>::withUniquePtr(UniquePtrWithLambda<T> data)
21+
auto ConfigureLoadStoreCore::withUniquePtr(UniquePtrWithLambda<T> data)
2422
-> unique_ptr_return_type<T>
2523

2624
{
@@ -33,10 +31,8 @@ auto ConfigureLoadStore<ChildClass>::withUniquePtr(UniquePtrWithLambda<T> data)
3331
std::move(data).template static_cast_<std::remove_extent_t<T>>(),
3432
{std::move(*this)});
3533
}
36-
template <typename ChildClass>
3734
template <typename T>
38-
auto ConfigureLoadStore<ChildClass>::withRawPtr(T *data)
39-
-> shared_ptr_return_type<T>
35+
auto ConfigureLoadStoreCore::withRawPtr(T *data) -> shared_ptr_return_type<T>
4036
{
4137
if (!data)
4238
{
@@ -47,16 +43,14 @@ auto ConfigureLoadStore<ChildClass>::withRawPtr(T *data)
4743
auxiliary::shareRaw(data), {std::move(*this)});
4844
}
4945

50-
template <typename ChildClass>
5146
template <typename T, typename Del>
52-
auto ConfigureLoadStore<ChildClass>::withUniquePtr(std::unique_ptr<T, Del> data)
47+
auto ConfigureLoadStoreCore::withUniquePtr(std::unique_ptr<T, Del> data)
5348
-> unique_ptr_return_type<T>
5449
{
5550
return withUniquePtr(UniquePtrWithLambda<T>(std::move(data)));
5651
}
57-
template <typename ChildClass>
5852
template <typename T_ContiguousContainer>
59-
auto ConfigureLoadStore<ChildClass>::withContiguousContainer(
53+
auto ConfigureLoadStoreCore::withContiguousContainer(
6054
T_ContiguousContainer &data)
6155
-> std::enable_if_t<
6256
auxiliary::IsContiguousContainer_v<T_ContiguousContainer>,

include/openPMD/RecordComponent.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class RecordComponent : public BaseRecordComponent
133133
friend class DynamicMemoryView;
134134
friend class internal::RecordComponentData;
135135
friend class MeshRecordComponent;
136+
friend struct ConfigureLoadStoreCore;
136137
template <typename ChildClass>
137138
friend class ConfigureLoadStore;
138139
template <typename Ptr_Type, typename ChildClass>

include/openPMD/RecordComponent.tpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,8 @@ void RecordComponent::verifyChunk(Offset const &o, Extent const &e) const
399399
}
400400

401401
// definitions for LoadStoreChunk.hpp
402-
template <typename ChildClass>
403402
template <typename T, typename F>
404-
auto ConfigureLoadStore<ChildClass>::enqueueStore(F &&createBuffer)
403+
auto ConfigureLoadStoreCore::enqueueStore(F &&createBuffer)
405404
-> DynamicMemoryView<T>
406405
{
407406
return m_rc.storeChunkSpanCreateBuffer_impl<T>(

src/LoadStoreChunk.cpp

Lines changed: 24 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@
1919

2020
namespace openPMD
2121
{
22-
23-
namespace internal
24-
{
25-
ConfigureLoadStoreData::ConfigureLoadStoreData(RecordComponent &rc)
26-
: m_rc(rc)
27-
{}
28-
} // namespace internal
29-
3022
namespace
3123
{
3224
template <typename T>
@@ -63,25 +55,26 @@ namespace
6355
}
6456
} // namespace
6557

58+
ConfigureLoadStoreCore::ConfigureLoadStoreCore(RecordComponent &rc) : m_rc(rc)
59+
{}
60+
6661
template <typename ChildClass>
6762
ConfigureLoadStore<ChildClass>::ConfigureLoadStore(RecordComponent &rc)
68-
: ConfigureLoadStoreData(rc)
63+
: ConfigureLoadStoreCore(rc)
6964
{}
7065

7166
template <typename ChildClass>
7267
ConfigureLoadStore<ChildClass>::ConfigureLoadStore(
73-
internal::ConfigureLoadStoreData &&data)
74-
: ConfigureLoadStoreData(std::move(data))
68+
ConfigureLoadStoreCore &&data)
69+
: ConfigureLoadStoreCore(std::move(data))
7570
{}
7671

77-
template <typename ChildClass>
78-
auto ConfigureLoadStore<ChildClass>::dim() const -> uint8_t
72+
auto ConfigureLoadStoreCore::dim() const -> uint8_t
7973
{
8074
return m_rc.getDimensionality();
8175
}
8276

83-
template <typename ChildClass>
84-
auto ConfigureLoadStore<ChildClass>::getOffset() -> Offset const &
77+
auto ConfigureLoadStoreCore::getOffset() -> Offset const &
8578
{
8679
if (!m_offset.has_value())
8780
{
@@ -97,8 +90,7 @@ auto ConfigureLoadStore<ChildClass>::getOffset() -> Offset const &
9790
return *m_offset;
9891
}
9992

100-
template <typename ChildClass>
101-
auto ConfigureLoadStore<ChildClass>::getExtent() -> Extent const &
93+
auto ConfigureLoadStoreCore::getExtent() -> Extent const &
10294
{
10395
if (!m_extent.has_value())
10496
{
@@ -118,9 +110,7 @@ auto ConfigureLoadStore<ChildClass>::getExtent() -> Extent const &
118110
return *m_extent;
119111
}
120112

121-
template <typename ChildClass>
122-
auto ConfigureLoadStore<ChildClass>::storeChunkConfig()
123-
-> internal::LoadStoreConfig
113+
auto ConfigureLoadStoreCore::storeChunkConfig() -> internal::LoadStoreConfig
124114
{
125115
return internal::LoadStoreConfig{getOffset(), getExtent()};
126116
}
@@ -139,17 +129,14 @@ auto ConfigureLoadStore<ChildClass>::offset(Offset offset) -> return_type &
139129
return *static_cast<return_type *>(this);
140130
}
141131

142-
template <typename ChildClass>
143132
template <typename T>
144-
auto ConfigureLoadStore<ChildClass>::enqueueStore() -> DynamicMemoryView<T>
133+
auto ConfigureLoadStoreCore::enqueueStore() -> DynamicMemoryView<T>
145134
{
146135
return m_rc.storeChunkSpan_impl<T>(storeChunkConfig());
147136
}
148137

149-
template <typename ChildClass>
150138
template <typename T>
151-
auto ConfigureLoadStore<ChildClass>::enqueueLoad()
152-
-> std::future<std::shared_ptr<T>>
139+
auto ConfigureLoadStoreCore::enqueueLoad() -> std::future<std::shared_ptr<T>>
153140
{
154141
auto res = m_rc.loadChunkAllocate_impl<T>(storeChunkConfig());
155142
return std::async(
@@ -160,10 +147,8 @@ auto ConfigureLoadStore<ChildClass>::enqueueLoad()
160147
});
161148
}
162149

163-
template <typename ChildClass>
164150
template <typename T>
165-
auto ConfigureLoadStore<ChildClass>::load(EnqueuePolicy ep)
166-
-> std::shared_ptr<T>
151+
auto ConfigureLoadStoreCore::load(EnqueuePolicy ep) -> std::shared_ptr<T>
167152
{
168153
auto res = m_rc.loadChunkAllocate_impl<T>(storeChunkConfig());
169154
switch (ep)
@@ -201,8 +186,7 @@ struct VisitorEnqueueLoadVariant
201186
}
202187
};
203188

204-
template <typename ChildClass>
205-
auto ConfigureLoadStore<ChildClass>::enqueueLoadVariant()
189+
auto ConfigureLoadStoreCore::enqueueLoadVariant()
206190
-> std::future<auxiliary::detail::future_to_shared_ptr_dataset_types>
207191
{
208192
return VisitorEnqueueLoadVariant::non_templated_implementation(
@@ -297,21 +281,20 @@ auto ConfigureLoadStoreFromBuffer<Ptr_Type>::load(EnqueuePolicy ep) -> void
297281

298282
/* clang-format would destroy the NOLINT comments */
299283
// clang-format off
300-
#define INSTANTIATE_METHOD_TEMPLATES(base_class, dtype) \
301-
template auto base_class::enqueueStore()->DynamicMemoryView<dtype>; \
302-
template auto base_class::enqueueLoad() \
303-
/* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
284+
#define INSTANTIATE_METHOD_TEMPLATES(dtype) \
285+
template auto ConfigureLoadStoreCore::enqueueStore() \
286+
->DynamicMemoryView<dtype>; \
287+
template auto ConfigureLoadStoreCore::enqueueLoad() \
288+
/* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
304289
->std::future<std::shared_ptr<dtype>>; \
305-
template auto base_class::load(EnqueuePolicy)->std::shared_ptr<dtype>;
290+
template auto ConfigureLoadStoreCore::load(EnqueuePolicy) \
291+
->std::shared_ptr<dtype>;
306292
// clang-format on
307293

308-
#define INSTANTIATE_METHOD_TEMPLATES_FOR_BASE(dtype) \
309-
INSTANTIATE_METHOD_TEMPLATES(ConfigureLoadStore<void>, dtype)
310-
311294
template class ConfigureLoadStore<void>;
312-
OPENPMD_FOREACH_DATASET_DATATYPE(INSTANTIATE_METHOD_TEMPLATES_FOR_BASE)
295+
OPENPMD_FOREACH_DATASET_DATATYPE(INSTANTIATE_METHOD_TEMPLATES)
313296

314-
#undef INSTANTIATE_METHOD_TEMPLATES_FOR_BASE
297+
#undef INSTANTIATE_METHOD_TEMPLATES
315298

316299
/* clang-format would destroy the NOLINT comments */
317300
// clang-format off
@@ -325,37 +308,19 @@ OPENPMD_FOREACH_DATASET_DATATYPE(INSTANTIATE_METHOD_TEMPLATES_FOR_BASE)
325308
template class ConfigureLoadStore< \
326309
/* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
327310
ConfigureLoadStoreFromBuffer<std::shared_ptr<dtype>>>; \
328-
INSTANTIATE_METHOD_TEMPLATES( \
329-
ConfigureLoadStore< \
330-
/* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
331-
ConfigureLoadStoreFromBuffer<std::shared_ptr<dtype>>>, \
332-
dtype) \
333311
/* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
334312
template class ConfigureStoreChunkFromBuffer<UniquePtrWithLambda<dtype>>; \
335313
template class ConfigureLoadStore< \
336314
/* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
337315
ConfigureStoreChunkFromBuffer<UniquePtrWithLambda<dtype>>>; \
338-
INSTANTIATE_METHOD_TEMPLATES( \
339-
ConfigureLoadStore< \
340-
/* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
341-
ConfigureStoreChunkFromBuffer<UniquePtrWithLambda<dtype>>>, \
342-
dtype) \
343316
template class ConfigureStoreChunkFromBuffer< \
344317
std::shared_ptr<dtype const>>; \
345318
template class ConfigureLoadStore< \
346319
ConfigureStoreChunkFromBuffer<std::shared_ptr<dtype const>>>; \
347-
INSTANTIATE_METHOD_TEMPLATES( \
348-
ConfigureLoadStore< \
349-
ConfigureStoreChunkFromBuffer<std::shared_ptr<dtype const>>>, \
350-
dtype) \
351320
template class ConfigureStoreChunkFromBuffer< \
352321
UniquePtrWithLambda<dtype const>>; \
353322
template class ConfigureLoadStore< \
354-
ConfigureStoreChunkFromBuffer<UniquePtrWithLambda<dtype const>>>; \
355-
INSTANTIATE_METHOD_TEMPLATES( \
356-
ConfigureLoadStore< \
357-
ConfigureStoreChunkFromBuffer<UniquePtrWithLambda<dtype const>>>, \
358-
dtype)
323+
ConfigureStoreChunkFromBuffer<UniquePtrWithLambda<dtype const>>>;
359324
// clang-format on
360325

361326
OPENPMD_FOREACH_DATASET_DATATYPE(INSTANTIATE_STORE_CHUNK_FROM_BUFFER)

0 commit comments

Comments
 (0)