Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 37 additions & 65 deletions src/amr/resources_manager/resources_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <map>
#include <optional>
#include <tuple>


namespace PHARE
Expand Down Expand Up @@ -127,6 +128,36 @@
* we ask for them in a tuple, and recursively call registerResources() for all of the
* unpacked elements
*/

void static handle_sub_resources(auto fn, auto& obj, auto&&... args)

Check notice

Code scanning / CodeQL

Unused static variable Note

Static variable args is never read.

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable args is not used.
{
using ResourcesView = decltype(obj);

if constexpr (has_runtime_subresourceview_list<ResourcesView>::value)
{
for (auto& runtimeResource : obj.getRunTimeResourcesViewList())
{
using RuntimeResource = decltype(runtimeResource);
if constexpr (has_sub_resources_v<RuntimeResource>)
{
fn(runtimeResource, args...);
}
else
{
std::visit([&](auto&& val) { fn(val, args...); }, runtimeResource);
}
}
}

if constexpr (has_compiletime_subresourcesview_list<ResourcesView>::value)
{
// unpack the tuple subResources and apply for each element registerResources()
// (recursively)
std::apply([&](auto&... subResource) { (fn(subResource, args...), ...); },
obj.getCompileTimeResourcesViewList());
}
}

template<typename ResourcesView>
void registerResources(ResourcesView& obj)
{
Expand All @@ -138,24 +169,8 @@
{
static_assert(has_sub_resources_v<ResourcesView>);

if constexpr (has_runtime_subresourceview_list<ResourcesView>::value)
{
for (auto& resourcesUser : obj.getRunTimeResourcesViewList())
{
this->registerResources(resourcesUser);
}
}

if constexpr (has_compiletime_subresourcesview_list<ResourcesView>::value)
{
// unpack the tuple subResources and apply for each element registerResources()
// (recursively)
std::apply(
[this](auto&... subResource) {
(this->registerResources(subResource), ...);
},
obj.getCompileTimeResourcesViewList());
}
handle_sub_resources( //
[&](auto&&... args) { this->registerResources(args...); }, obj);
}
}

Expand All @@ -180,21 +195,8 @@
{
static_assert(has_sub_resources_v<ResourcesView>);

if constexpr (has_runtime_subresourceview_list<ResourcesView>::value)
{
for (auto& resourcesUser : obj.getRunTimeResourcesViewList())
{
this->allocate(resourcesUser, patch, allocateTime);
}
}

if constexpr (has_compiletime_subresourcesview_list<ResourcesView>::value)
{
// unpack the tuple subResources and apply for each element registerResources()
std::apply([this, &patch, allocateTime](auto&... subResource) //
{ (this->allocate(subResource, patch, allocateTime), ...); },
obj.getCompileTimeResourcesViewList());
}
handle_sub_resources( //
[&](auto&&... args) { this->allocate(args...); }, obj, patch, allocateTime);
}
}

Expand Down Expand Up @@ -403,24 +405,9 @@
}
else
{
if constexpr (has_runtime_subresourceview_list<ResourcesView>::value)
{
for (auto& resourcesUser : obj.getRunTimeResourcesViewList())
{
//
this->getIDs_(resourcesUser, IDs);
}
}
static_assert(has_sub_resources_v<ResourcesView>);

if constexpr (has_compiletime_subresourcesview_list<ResourcesView>::value)
{
// unpack the tuple subResources and apply for each element registerResources()
std::apply(
[this, &IDs](auto&... subResource) {
(this->getIDs_(subResource, IDs), ...);
},
obj.getCompileTimeResourcesViewList());
}
handle_sub_resources([&](auto&&... args) { this->getIDs_(args...); }, obj, IDs);
}
}

Expand Down Expand Up @@ -456,21 +443,6 @@



void static handle_sub_resources(auto fn, auto& obj, auto&&... args)
{
using ResourcesView = decltype(obj);

if constexpr (has_runtime_subresourceview_list<ResourcesView>::value)
for (auto& runtimeResource : obj.getRunTimeResourcesViewList())
fn(runtimeResource, args...);

// unpack the tuple subResources and apply for each element registerResources()
// (recursively)
if constexpr (has_compiletime_subresourcesview_list<ResourcesView>::value)
std::apply([&](auto&... subResource) { (fn(subResource, args...), ...); },
obj.getCompileTimeResourcesViewList());
}


template<typename ResourcesView>
void setResources_(ResourcesView& obj, SAMRAI::hier::Patch const& patch) const
Expand All @@ -484,7 +456,7 @@
static_assert(has_sub_resources_v<ResourcesView>);

handle_sub_resources( //
[&](auto&&... args) { this->setResources_(args...); }, obj, patch);
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/core/data/tensorfield/tensorfield.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#ifndef PHARE_TENSORFIELD_HPP
#define PHARE_TENSORFIELD_HPP

#include <cstddef>
#include <string>
#include <array>
#include <vector>
#include <unordered_map>

#include "core/def.hpp"
#include "core/utilities/types.hpp"
#include "core/data/vecfield/vecfield_component.hpp"


#include <array>
#include <string>
#include <vector>
#include <cstddef>
#include <unordered_map>

namespace PHARE::core::detail
{
template<std::size_t rank>
Expand Down
133 changes: 133 additions & 0 deletions src/core/utilities/variants.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#ifndef PHARE_CORE_UTILITIES_RESOURCES_VARIANTS_HPP
#define PHARE_CORE_UTILITIES_RESOURCES_VARIANTS_HPP

#include "core/utilities/types.hpp"

#include <core/logger.hpp>
#include <tuple>
#include <variant>
#include <stdexcept>

namespace PHARE::core
{
template<typename T>
auto decay_to_ptr()
{
return [](T& arg) mutable -> void* { return const_cast<std::decay_t<T>*>(&arg); };
}

template<typename... Ts>
struct varient_visitor_overloads : Ts...
{
using Ts::operator()...;
};

template<typename... Ts>
varient_visitor_overloads(Ts&&...) -> varient_visitor_overloads<std::decay_t<Ts>...>;


template<typename... Args>
auto constexpr _visit_ptr_overloads(std::tuple<Args...>*)
{
return varient_visitor_overloads{decay_to_ptr<Args>()...,
[](auto&) mutable -> void* { return nullptr; }};
}


template<typename T, typename... Ts>
struct unique : std::type_identity<T>
{
};

template<typename... Ts, typename U, typename... Us>
struct unique<std::tuple<Ts...>, U, Us...>
: std::conditional_t<(std::is_same_v<U, Ts> || ...), unique<std::tuple<Ts...>, Us...>,
unique<std::tuple<Ts..., U>, Us...>>
{
};

template<typename... Ts>
using unique_tuple = typename unique<std::tuple<>, Ts...>::type;



template<typename... Args>
auto constexpr visit_ptr_overloads()
{
return _visit_ptr_overloads(static_cast<unique_tuple<Args...>*>(nullptr));
}



template<typename Type, typename Variants>
auto& get_as_ref_or_throw(Variants& variants, std::size_t const start = 0)
{
for (std::size_t idx = start; idx < variants.size(); ++idx)
if (auto type = std::visit(visit_ptr_overloads<Type>(), variants[idx]))
return *reinterpret_cast<Type*>(type);

throw std::runtime_error("No element in variant for type");
}


// ARGS MUST BE IN THE SAME ORDER AS VARIANT LIST TYPES!!!!!
template<typename... Args, typename Variants>
auto get_as_tuple_or_throw(Variants& variants, std::size_t start = 0)
{
using Tuple = std::tuple<Args...>;
auto constexpr tuple_size = std::tuple_size_v<Tuple>;

auto ptr_or_null = visit_ptr_overloads<Args...>();

auto pointer_tuple = for_N<tuple_size>([&](auto i) mutable {
using Type = std::tuple_element_t<i, Tuple>;

for (std::size_t idx = start; idx < variants.size(); ++idx)
if (auto ptr = std::visit(ptr_or_null, variants[idx]))
{
++start;
return reinterpret_cast<Type*>(ptr);
}
return static_cast<Type*>(nullptr);
});

for_N<tuple_size>([&](auto i) {
if (std::get<i>(pointer_tuple) == nullptr)
throw std::runtime_error("No element in variant for type");
});

return for_N<tuple_size, for_N_R_mode::forward_tuple>(
[&](auto i) -> auto& { return *std::get<i>(pointer_tuple); });
}

template<typename Type>
auto& get_from_variants(auto& variants, Type& arg)
{
std::size_t start = 0;

while (start < variants.size())
{
if (auto& res = get_as_ref_or_throw<Type>(variants, start); res.name() == arg.name())
return res;
++start;
}

if (start == variants.size())
throw std::runtime_error("Required name not found in variants: " + arg.name());
}


template<typename... Args>
auto get_from_variants(auto& variants, Args&... args)
requires(sizeof...(Args) > 1)
{
return std::forward_as_tuple(get_from_variants(variants, args)...);
}




} // namespace PHARE::core


#endif /*PHARE_CORE_UTILITIES_RESOURCES_VARIANTS_HPP*/
Loading
Loading