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#pragma once
21
22#include "actor/traits.hpp"
23
24#include <functional>
25#include <memory>
26#include <optional>
27#include <string>
28#include <type_traits>
29#include <variant>
30
31/** @brief Get an actor of a specific type, recursively. */
32template <typename Actor> struct GetActorByType {
33 template <typename T> auto operator()(std::shared_ptr<T> const &obj) const {
34 if constexpr (std::is_same_v<T, Actor>) {
35 return obj;
36 }
38 return std::visit(*this, obj->base_solver);
39 }
40 return std::shared_ptr<Actor>{nullptr};
41 }
42};
43
44/** @brief Get an active actor of a specific type, recursively. */
45template <typename Actor, typename Variant>
46std::shared_ptr<Actor> get_actor_by_type(Variant const &variant) {
47 return std::visit(GetActorByType<Actor>(), variant);
48}
49
50template <typename Actor, typename Variant>
51std::shared_ptr<Actor>
52get_actor_by_type(std::optional<Variant> const &optional) {
53 return (optional) ? get_actor_by_type<Actor>(*optional) : nullptr;
54}
55
56/** @brief Check if an actor of a specific type is active, recursively. */
57template <typename Actor> struct HasActorOfType {
58 template <typename T> auto operator()(std::shared_ptr<T> const &obj) const {
59 if constexpr (std::is_same_v<T, Actor>) {
60 return true;
61 }
63 return std::visit(*this, obj->base_solver);
64 }
65 return false;
66 }
67};
68
69/** @brief Check if an actor of a specific type is active, recursively. */
70template <typename Actor, typename Variant>
71auto has_actor_of_type(Variant const &variant) {
72 return std::visit(HasActorOfType<Actor>(), variant);
73}
74
75template <typename Actor, typename Variant>
76auto has_actor_of_type(std::optional<Variant> const &optional) {
77 return (optional) ? has_actor_of_type<Actor>(*optional) : false;
78}
Get an actor of a specific type, recursively.
Definition visitors.hpp:32
auto operator()(std::shared_ptr< T > const &obj) const
Definition visitors.hpp:33
Check if an actor of a specific type is active, recursively.
Definition visitors.hpp:57
auto operator()(std::shared_ptr< T > const &obj) const
Definition visitors.hpp:58
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:71
std::shared_ptr< Actor > get_actor_by_type(Variant const &variant)
Get an active actor of a specific type, recursively.
Definition visitors.hpp:46