Skip to content

Commit 941b3fd

Browse files
committed
WIP Make the default specification more flexible
prep for reading
1 parent 2115ede commit 941b3fd

File tree

2 files changed

+115
-4
lines changed

2 files changed

+115
-4
lines changed

include/openPMD/backend/ScientificDefaults.hpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "openPMD/IO/Access.hpp"
4+
#include <iostream>
45
#include <type_traits>
56

67
namespace openPMD::detail
@@ -27,6 +28,95 @@ using CallResult_t = typename IsCallable<F>::type;
2728

2829
namespace openPMD::internal
2930
{
31+
template <typename, typename, typename>
32+
struct ConfigAttributeWithSetter;
33+
34+
template <typename Child, typename GetDefaultValue>
35+
struct ConfigAttribute
36+
{
37+
using DefaultValue = detail::CallResult_t<GetDefaultValue>;
38+
template <typename S>
39+
using SetterType = Child &(
40+
Child::
41+
*)(std::conditional_t<
42+
std::is_void_v<S>,
43+
std::remove_reference_t<detail::CallResult_t<GetDefaultValue>>,
44+
S>);
45+
46+
Child &child;
47+
char const *attrName;
48+
GetDefaultValue &&getDefaultValue;
49+
50+
ConfigAttribute(
51+
Child &child_in,
52+
char const *attrName_in,
53+
GetDefaultValue &&getDefaultValue_in)
54+
: child(child_in)
55+
, attrName(attrName_in)
56+
, getDefaultValue(std::forward<GetDefaultValue>(getDefaultValue_in))
57+
{}
58+
59+
ConfigAttribute(ConfigAttribute const &) = delete;
60+
ConfigAttribute(ConfigAttribute &&) = default;
61+
62+
ConfigAttribute &operator=(ConfigAttribute const &) = delete;
63+
ConfigAttribute &operator=(ConfigAttribute &&) = default;
64+
65+
template <typename S = void>
66+
auto withSetter(SetterType<S>)
67+
&& -> ConfigAttributeWithSetter<Child, GetDefaultValue, SetterType<S>>;
68+
69+
~ConfigAttribute()
70+
{
71+
run();
72+
}
73+
74+
void run()
75+
{
76+
if (child.containsAttribute(attrName))
77+
{
78+
return;
79+
}
80+
set(get());
81+
}
82+
83+
virtual void set(DefaultValue &&val)
84+
{
85+
std::cout << "USING PARENT SETTER" << std::endl;
86+
child.setAttribute(attrName, std::forward<DefaultValue>(val));
87+
}
88+
89+
auto get() -> DefaultValue
90+
{
91+
if constexpr (detail::IsCallable_v<GetDefaultValue>)
92+
{
93+
return std::forward<GetDefaultValue>(getDefaultValue)();
94+
}
95+
else
96+
{
97+
return std::forward<GetDefaultValue>(getDefaultValue);
98+
}
99+
}
100+
};
101+
102+
template <typename Child, typename GetDefaultValue, typename SetDefaultValue>
103+
struct ConfigAttributeWithSetter : ConfigAttribute<Child, GetDefaultValue>
104+
{
105+
using parent_t = ConfigAttribute<Child, GetDefaultValue>;
106+
using DefaultValue = typename parent_t::DefaultValue;
107+
108+
ConfigAttributeWithSetter(parent_t &&par, SetDefaultValue setter_in)
109+
: parent_t(std::move(par)), setter(setter_in)
110+
{}
111+
112+
SetDefaultValue setter;
113+
void set(DefaultValue &&val) override
114+
{
115+
std::cout << "USING CUSTOM SETTER" << std::endl;
116+
(this->child.*setter)(std::forward<DefaultValue>(val));
117+
}
118+
};
119+
30120
template <typename Child> // CRT
31121
class ScientificDefaults
32122
{
@@ -37,6 +127,17 @@ class ScientificDefaults
37127
template <typename F>
38128
using setter_t = Child &(Child::*)();
39129

130+
template <typename GetDefaultValue>
131+
auto
132+
defaultAttribute(char const *attrName, GetDefaultValue &&getDefaultValue)
133+
-> ConfigAttribute<Child, GetDefaultValue>
134+
{
135+
return ConfigAttribute{
136+
asChild(),
137+
attrName,
138+
std::forward<GetDefaultValue>(getDefaultValue)};
139+
}
140+
40141
template <typename Setter = void, typename V>
41142
void addDefaultFor_worker(char const *key, V &&value, Setter &&setter);
42143

src/backend/ScientificDefaults.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818

1919
namespace openPMD::internal
2020
{
21+
template <typename Child, typename GetDefaultValue>
22+
template <typename S>
23+
auto ConfigAttribute<Child, GetDefaultValue>::withSetter(SetterType<S> setter)
24+
&& -> ConfigAttributeWithSetter<Child, GetDefaultValue, SetterType<S>>
25+
{
26+
return ConfigAttributeWithSetter<Child, GetDefaultValue, SetterType<S>>{
27+
std::move(*this), setter};
28+
}
29+
2130
template <typename Child>
2231
auto ScientificDefaults<Child>::asChild() -> Child &
2332
{
@@ -189,7 +198,7 @@ void ScientificDefaults<Child>::addDefaults()
189198

190199
if constexpr (std::is_same_v<Child, Iteration>)
191200
{
192-
addDefaultFor("time", 0., &Iteration::setTime);
201+
defaultAttribute("time", 0.).withSetter(&Iteration::setTime);
193202
addDefaultFor("dt", 1., &Iteration::setDt);
194203
addDefaultFor("timeUnitSI", 1.0, &Iteration::setTimeUnitSI);
195204
}
@@ -248,7 +257,7 @@ void ScientificDefaults<Child>::addDefaults()
248257
}
249258
},
250259
&Mesh::setGridSpacing);
251-
addDefaultFor<std::vector<double> const &>(
260+
defaultAttribute(
252261
"gridGlobalOffset",
253262
[&]() {
254263
if (dimensionality < 100)
@@ -259,8 +268,9 @@ void ScientificDefaults<Child>::addDefaults()
259268
{
260269
return std::vector<double>{0.0};
261270
}
262-
},
263-
&Mesh::setGridGlobalOffset);
271+
})
272+
.template withSetter<std::vector<double> const &>(
273+
&Mesh::setGridGlobalOffset);
264274

265275
addParentDefaults<BaseRecord<MeshRecordComponent>>();
266276
}

0 commit comments

Comments
 (0)