Skip to content

Commit df1aae2

Browse files
committed
Do not use templates for defaults setter
TODO: do sth similar for reading
1 parent d8c66a6 commit df1aae2

4 files changed

Lines changed: 213 additions & 311 deletions

File tree

include/openPMD/backend/Attributable.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace internal
5757
class IterationData;
5858
class SeriesData;
5959
struct HomogenizeExtents;
60-
template <typename, typename, typename, typename>
60+
template <typename>
6161
struct ConfigAttributeWithSetterAndReader;
6262

6363
class SharedAttributableData
@@ -246,7 +246,7 @@ class Attributable
246246
friend class internal::AttributableData;
247247
friend class Snapshots;
248248
friend struct internal::HomogenizeExtents;
249-
template <typename, typename, typename, typename>
249+
template <typename>
250250
friend struct internal::ConfigAttributeWithSetterAndReader;
251251

252252
protected:

include/openPMD/backend/ScientificDefaults.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace openPMD::internal
99
{
10-
template <typename, typename>
1110
struct ConfigAttribute;
1211

1312
template <typename Child> // CRT
@@ -20,10 +19,8 @@ class ScientificDefaults
2019
template <typename F>
2120
using setter_t = Child &(Child::*)();
2221

23-
template <typename GetDefaultValue>
24-
[[nodiscard]] auto
25-
defaultAttribute(char const *attrName, GetDefaultValue &&getDefaultValue)
26-
-> ConfigAttribute<Child, GetDefaultValue>;
22+
[[nodiscard]] auto defaultAttribute(char const *attrName)
23+
-> ConfigAttribute;
2724

2825
template <typename Parent, bool write>
2926
void addParentDefaults(OpenpmdStandard);

include/openPMD/backend/ScientificDefaults_internal.hpp

Lines changed: 78 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "openPMD/backend/Attribute.hpp"
99
#include "openPMD/backend/Variant_internal.hpp"
1010

11+
#include <functional>
1112
#include <iostream>
1213
#include <type_traits>
1314
#include <variant>
@@ -61,7 +62,7 @@ namespace
6162
[](auto const &val) { write_val_to_stderr(val); },
6263
a.getVariant<attribute_types>());
6364
return std::cerr;
64-
};
65+
}
6566

6667
} // namespace
6768
enum class WriteOrRead : std::uint8_t
@@ -70,70 +71,97 @@ enum class WriteOrRead : std::uint8_t
7071
Read
7172
};
7273

73-
struct GenericSetter
74-
{
75-
/* use setAttribute() */
76-
};
77-
78-
template <typename, typename, typename>
79-
struct ConfigAttributeWithSetter;
74+
template <typename>
75+
struct ConfigAttributeWithSetterAndReader;
8076

8177
/////////////////////
8278
// ConfigAttribute //
8379
// //////////////////
8480

85-
template <typename RecordType, typename GetDefaultValue>
8681
struct ConfigAttribute
8782
{
88-
using DefaultValue = detail::CallResult_t<GetDefaultValue>;
89-
template <typename S>
90-
using SetterType = RecordType &(
91-
RecordType::
92-
*)(std::conditional_t<
93-
std::is_void_v<S>,
94-
std::remove_reference_t<detail::CallResult_t<GetDefaultValue>>,
95-
S>);
96-
97-
RecordType &child;
83+
Attributable &child;
9884
char const *attrName;
99-
GetDefaultValue &&getDefaultValue;
85+
std::function<void(Attributable &)> initDefaultAttribute;
10086

101-
ConfigAttribute(
102-
RecordType &child_in,
103-
char const *attrName_in,
104-
GetDefaultValue &&getDefaultValue_in)
105-
: child(child_in)
106-
, attrName(attrName_in)
107-
, getDefaultValue(std::forward<GetDefaultValue>(getDefaultValue_in))
87+
ConfigAttribute(Attributable &child_in, char const *attrName_in)
88+
: child(child_in), attrName(attrName_in)
10889
{}
10990

11091
ConfigAttribute(ConfigAttribute const &) = delete;
111-
ConfigAttribute(ConfigAttribute &&) = default;
92+
ConfigAttribute(ConfigAttribute &&) = delete;
11293

11394
ConfigAttribute &operator=(ConfigAttribute const &) = delete;
114-
ConfigAttribute &operator=(ConfigAttribute &&) = default;
95+
ConfigAttribute &operator=(ConfigAttribute &&) = delete;
96+
97+
template <typename RecordType, typename S = void, typename GetDefaultValue>
98+
[[nodiscard]] auto withSetter(
99+
GetDefaultValue &&getDefaultVal,
100+
RecordType &(RecordType::*setDefaultVal)(
101+
std::conditional_t<
102+
std::is_void_v<S>,
103+
detail::CallResult_t<GetDefaultValue>,
104+
S>)) -> ConfigAttribute &
105+
{
106+
initDefaultAttribute = [getDefaultVal_lambda = std::move(getDefaultVal),
107+
setDefaultVal](Attributable &attr) {
108+
RecordType *record = dynamic_cast<RecordType *>(&attr);
109+
if (!record)
110+
{
111+
throw error::Internal("dynamic cast failure");
112+
}
113+
if constexpr (detail::IsCallable_v<GetDefaultValue>)
114+
{
115+
((*record).*setDefaultVal)(getDefaultVal_lambda());
116+
}
117+
else
118+
{
119+
((*record).*setDefaultVal)(std::move(getDefaultVal_lambda));
120+
}
121+
};
122+
return *this;
123+
}
115124

116-
template <typename S = void>
117-
[[nodiscard]] auto
118-
withSetter(SetterType<S>) && -> ConfigAttributeWithSetter<
119-
RecordType,
120-
GetDefaultValue,
121-
SetterType<S>>;
125+
template <typename DefaultValue>
126+
[[nodiscard]] auto withGenericSetter(DefaultValue &&defaultVal)
127+
-> ConfigAttribute &
128+
{
129+
initDefaultAttribute = [this,
130+
defaultVal_lambda =
131+
std::forward<DefaultValue &&>(defaultVal)](
132+
Attributable &attr) {
133+
attr.setAttribute(this->attrName, std::move(defaultVal_lambda));
134+
};
135+
return *this;
136+
}
122137

123-
[[nodiscard]] auto withGenericSetter() && -> ConfigAttributeWithSetter<
124-
RecordType,
125-
GetDefaultValue,
126-
GenericSetter>;
138+
template <typename... Args>
139+
[[nodiscard]] auto withReader(Args &&...) -> ConfigAttribute &
140+
{
141+
// TODO
142+
return *this;
143+
}
127144

128-
auto get() -> DefaultValue
145+
[[nodiscard]] auto configureReaders() -> ConfigAttribute &
129146
{
130-
if constexpr (detail::IsCallable_v<GetDefaultValue>)
131-
{
132-
return std::forward<GetDefaultValue>(getDefaultValue)();
133-
}
134-
else
147+
// TODO
148+
return *this;
149+
}
150+
void operator()(WriteOrRead wor)
151+
{
152+
switch (wor)
135153
{
136-
return std::forward<GetDefaultValue>(getDefaultValue);
154+
case WriteOrRead::Write:
155+
if (this->child.containsAttribute(this->attrName))
156+
{
157+
return;
158+
}
159+
this->initDefaultAttribute(this->child);
160+
break;
161+
case WriteOrRead::Read:
162+
// Reading implemented by subclass
163+
// ConfigAttributeWithSetterAndReader
164+
break;
137165
}
138166
}
139167
};
@@ -263,84 +291,12 @@ struct AttributeReader
263291
}
264292
};
265293

266-
template <typename, typename, typename, typename>
267-
struct ConfigAttributeWithSetterAndReader;
268-
269-
///////////////////////////////
270-
// ConfigAttributeWithSetter //
271-
///////////////////////////////
272-
273-
template <
274-
typename RecordType,
275-
typename GetDefaultValue,
276-
typename SetDefaultValue>
277-
struct ConfigAttributeWithSetter : ConfigAttribute<RecordType, GetDefaultValue>
278-
{
279-
using parent_t = ConfigAttribute<RecordType, GetDefaultValue>;
280-
using DefaultValue = typename parent_t::DefaultValue;
281-
282-
ConfigAttributeWithSetter(parent_t &&par, SetDefaultValue setter_in)
283-
: parent_t(std::move(par)), setter(setter_in)
284-
{}
285-
286-
SetDefaultValue setter;
287-
static_assert(
288-
std::is_member_function_pointer_v<SetDefaultValue> ||
289-
std::is_same_v<SetDefaultValue, GenericSetter>);
290-
291-
template <typename ExpectedAttributeType = DefaultValue, typename... Args>
292-
[[nodiscard]] auto withReader(Args &&...) &&;
293-
294-
[[nodiscard]] auto
295-
configureReaders() && -> ConfigAttributeWithSetterAndReader<
296-
RecordType,
297-
GetDefaultValue,
298-
SetDefaultValue,
299-
AttributeReaderBottom>;
300-
301-
void set(DefaultValue &&val)
302-
{
303-
if constexpr (std::is_same_v<SetDefaultValue, GenericSetter>)
304-
{
305-
this->child.setAttribute(
306-
this->attrName, std::forward<DefaultValue>(val));
307-
}
308-
else
309-
{
310-
(this->child.*setter)(std::forward<DefaultValue>(val));
311-
}
312-
}
313-
314-
void operator()(WriteOrRead wor)
315-
{
316-
switch (wor)
317-
{
318-
case WriteOrRead::Write:
319-
if (this->child.containsAttribute(this->attrName))
320-
{
321-
return;
322-
}
323-
this->set(this->get());
324-
break;
325-
case WriteOrRead::Read:
326-
// Reading implemented by subclass
327-
// ConfigAttributeWithSetterAndReader
328-
break;
329-
}
330-
}
331-
};
332-
333294
////////////////////////////////////////
334295
// ConfigAttributeWithSetterAndReader //
335296
////////////////////////////////////////
336-
337-
template <
338-
typename RecordType,
339-
typename GetDefaultValue,
340-
typename SetDefaultValue,
341-
typename AttributeReader_t>
342-
struct ConfigAttributeWithSetterAndReader
343-
: ConfigAttributeWithSetter<RecordType, GetDefaultValue, SetDefaultValue>
297+
#if 0
298+
template <typename AttributeReader_t>
299+
struct ConfigAttributeWithSetterAndReader : ConfigAttribute
344300
{
345301
AttributeReader_t attributeReader;
346302
using parent_t =
@@ -462,4 +418,5 @@ struct ConfigAttributeWithSetterAndReader
462418
}
463419
}
464420
};
421+
#endif
465422
} // namespace openPMD::internal

0 commit comments

Comments
 (0)