1919
2020#include < map>
2121#include < optional>
22+ #include < tuple>
2223
2324
2425namespace PHARE
@@ -127,6 +128,36 @@ namespace amr
127128 * we ask for them in a tuple, and recursively call registerResources() for all of the
128129 * unpacked elements
129130 */
131+
132+ void static handle_sub_resources (auto fn, auto & obj, auto &&... args)
133+ {
134+ using ResourcesView = decltype (obj);
135+
136+ if constexpr (has_runtime_subresourceview_list<ResourcesView>::value)
137+ {
138+ for (auto & runtimeResource : obj.getRunTimeResourcesViewList ())
139+ {
140+ using RuntimeResource = decltype (runtimeResource);
141+ if constexpr (has_sub_resources_v<RuntimeResource>)
142+ {
143+ fn (runtimeResource, args...);
144+ }
145+ else
146+ {
147+ std::visit ([&](auto && val) { fn (val, args...); }, runtimeResource);
148+ }
149+ }
150+ }
151+
152+ if constexpr (has_compiletime_subresourcesview_list<ResourcesView>::value)
153+ {
154+ // unpack the tuple subResources and apply for each element registerResources()
155+ // (recursively)
156+ std::apply ([&](auto &... subResource) { (fn (subResource, args...), ...); },
157+ obj.getCompileTimeResourcesViewList ());
158+ }
159+ }
160+
130161 template <typename ResourcesView>
131162 void registerResources (ResourcesView& obj)
132163 {
@@ -138,24 +169,8 @@ namespace amr
138169 {
139170 static_assert (has_sub_resources_v<ResourcesView>);
140171
141- if constexpr (has_runtime_subresourceview_list<ResourcesView>::value)
142- {
143- for (auto & resourcesUser : obj.getRunTimeResourcesViewList ())
144- {
145- this ->registerResources (resourcesUser);
146- }
147- }
148-
149- if constexpr (has_compiletime_subresourcesview_list<ResourcesView>::value)
150- {
151- // unpack the tuple subResources and apply for each element registerResources()
152- // (recursively)
153- std::apply (
154- [this ](auto &... subResource) {
155- (this ->registerResources (subResource), ...);
156- },
157- obj.getCompileTimeResourcesViewList ());
158- }
172+ handle_sub_resources ( //
173+ [&](auto &&... args) { this ->registerResources (args...); }, obj);
159174 }
160175 }
161176
@@ -180,21 +195,8 @@ namespace amr
180195 {
181196 static_assert (has_sub_resources_v<ResourcesView>);
182197
183- if constexpr (has_runtime_subresourceview_list<ResourcesView>::value)
184- {
185- for (auto & resourcesUser : obj.getRunTimeResourcesViewList ())
186- {
187- this ->allocate (resourcesUser, patch, allocateTime);
188- }
189- }
190-
191- if constexpr (has_compiletime_subresourcesview_list<ResourcesView>::value)
192- {
193- // unpack the tuple subResources and apply for each element registerResources()
194- std::apply ([this , &patch, allocateTime](auto &... subResource) //
195- { (this ->allocate (subResource, patch, allocateTime), ...); },
196- obj.getCompileTimeResourcesViewList ());
197- }
198+ handle_sub_resources ( //
199+ [&](auto &&... args) { this ->allocate (args...); }, obj, patch, allocateTime);
198200 }
199201 }
200202
@@ -403,24 +405,9 @@ namespace amr
403405 }
404406 else
405407 {
406- if constexpr (has_runtime_subresourceview_list<ResourcesView>::value)
407- {
408- for (auto & resourcesUser : obj.getRunTimeResourcesViewList ())
409- {
410- //
411- this ->getIDs_ (resourcesUser, IDs);
412- }
413- }
408+ static_assert (has_sub_resources_v<ResourcesView>);
414409
415- if constexpr (has_compiletime_subresourcesview_list<ResourcesView>::value)
416- {
417- // unpack the tuple subResources and apply for each element registerResources()
418- std::apply (
419- [this , &IDs](auto &... subResource) {
420- (this ->getIDs_ (subResource, IDs), ...);
421- },
422- obj.getCompileTimeResourcesViewList ());
423- }
410+ handle_sub_resources ([&](auto &&... args) { this ->getIDs_ (args...); }, obj, IDs);
424411 }
425412 }
426413
@@ -456,21 +443,6 @@ namespace amr
456443
457444
458445
459- void static handle_sub_resources (auto fn, auto & obj, auto &&... args)
460- {
461- using ResourcesView = decltype (obj);
462-
463- if constexpr (has_runtime_subresourceview_list<ResourcesView>::value)
464- for (auto & runtimeResource : obj.getRunTimeResourcesViewList ())
465- fn (runtimeResource, args...);
466-
467- // unpack the tuple subResources and apply for each element registerResources()
468- // (recursively)
469- if constexpr (has_compiletime_subresourcesview_list<ResourcesView>::value)
470- std::apply ([&](auto &... subResource) { (fn (subResource, args...), ...); },
471- obj.getCompileTimeResourcesViewList ());
472- }
473-
474446
475447 template <typename ResourcesView>
476448 void setResources_ (ResourcesView& obj, SAMRAI::hier::Patch const & patch) const
0 commit comments