ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
visitors.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef ESPRESSO_SRC_CORE_ACTOR_VISITORS_HPP
21#define ESPRESSO_SRC_CORE_ACTOR_VISITORS_HPP
22
23#include "actor/traits.hpp"
24
25#include <functional>
26#include <memory>
27#include <optional>
28#include <string>
29#include <type_traits>
30#include <variant>
31
32/** @brief Get an actor of a specific type, recursively. */
33template <typename Actor> struct GetActorByType {
34private:
35 template <typename T>
36 static constexpr bool is_exact_match_v = std::is_same_v<T, Actor>;
37 template <typename T>
38 static constexpr bool is_layer_correction_v =
40
41public:
42 template <typename T, std::enable_if_t<is_exact_match_v<T>> * = nullptr>
43 auto operator()(std::shared_ptr<T> const &obj) const {
44 return obj;
45 }
46
47 template <typename T, std::enable_if_t<not is_exact_match_v<T> and
48 is_layer_correction_v<T>> * = nullptr>
49 auto operator()(std::shared_ptr<T> const &obj) const {
50 return std::visit(*this, obj->base_solver);
51 }
52
53 template <typename T,
54 std::enable_if_t<not is_exact_match_v<T> and
55 not is_layer_correction_v<T>> * = nullptr>
56 auto operator()(std::shared_ptr<T> const &) const {
57 return std::shared_ptr<Actor>{nullptr};
58 }
59};
60
61/** @brief Get an active actor of a specific type, recursively. */
62template <typename Actor, typename Variant>
63std::shared_ptr<Actor> get_actor_by_type(Variant const &variant) {
64 return std::visit(GetActorByType<Actor>(), variant);
65}
66
67template <typename Actor, typename Variant>
68std::shared_ptr<Actor>
69get_actor_by_type(std::optional<Variant> const &optional) {
70 return (optional) ? get_actor_by_type<Actor>(*optional) : nullptr;
71}
72
73/** @brief Check if an actor of a specific type is active, recursively. */
74template <typename Actor> struct HasActorOfType {
75private:
76 template <typename T>
77 static constexpr bool is_exact_match_v = std::is_same_v<T, Actor>;
78 template <typename T>
79 static constexpr bool is_layer_correction_v =
81
82public:
83 template <typename T, std::enable_if_t<is_exact_match_v<T>> * = nullptr>
84 auto operator()(std::shared_ptr<T> const &) const {
85 return true;
86 }
87
88 template <typename T, std::enable_if_t<not is_exact_match_v<T> and
89 is_layer_correction_v<T>> * = nullptr>
90 auto operator()(std::shared_ptr<T> const &obj) const {
91 return std::visit(*this, obj->base_solver);
92 }
93
94 template <typename T,
95 std::enable_if_t<not is_exact_match_v<T> and
96 not is_layer_correction_v<T>> * = nullptr>
97 auto operator()(std::shared_ptr<T> const &) const {
98 return false;
99 }
100};
101
102/** @brief Check if an actor of a specific type is active, recursively. */
103template <typename Actor, typename Variant>
104auto has_actor_of_type(Variant const &variant) {
105 return std::visit(HasActorOfType<Actor>(), variant);
106}
107
108template <typename Actor, typename Variant>
109auto has_actor_of_type(std::optional<Variant> const &optional) {
110 return (optional) ? has_actor_of_type<Actor>(*optional) : false;
111}
112
113#endif
Get an actor of a specific type, recursively.
Definition visitors.hpp:33
auto operator()(std::shared_ptr< T > const &obj) const
Definition visitors.hpp:43
auto operator()(std::shared_ptr< T > const &) const
Definition visitors.hpp:56
Check if an actor of a specific type is active, recursively.
Definition visitors.hpp:74
auto operator()(std::shared_ptr< T > const &) const
Definition visitors.hpp:84
auto operator()(std::shared_ptr< T > const &obj) const
Definition visitors.hpp:90
Whether an actor is a layer correction method.
Definition traits.hpp:26
auto has_actor_of_type(Variant const &variant)
Check if an actor of a specific type is active, recursively.
Definition visitors.hpp:104
std::shared_ptr< Actor > get_actor_by_type(Variant const &variant)
Get an active actor of a specific type, recursively.
Definition visitors.hpp:63