ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
lb/Container.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2023-2026 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 <config/config.hpp>
23
27
29
30#include <memory>
31#include <optional>
32#include <string>
33
35
36class Container : public AutoParameters<Container, System::Leaf> {
37 ObjectRef m_solver;
38 std::unique_ptr<VariantMap> m_params;
39
40 void detach_solver() {
41 if (m_solver) {
42 m_solver->do_call_method("deactivate", {});
43 std::dynamic_pointer_cast<System::Leaf>(m_solver)->detach_system();
44 }
45 m_solver.reset();
46 }
47
48 void bind_solver() {
49 auto system = m_system.lock();
50 std::dynamic_pointer_cast<System::Leaf>(m_solver)->bind_system(system);
51 m_solver->do_call_method("activate", {});
52 }
53
55 auto const &params = *m_params;
56 for (auto const &key : get_parameter_insertion_order()) {
57 if (params.contains(key)) {
58 do_set_parameter(key.c_str(), params.at(key));
59 }
60 }
61 m_params.reset();
62 }
63
64public:
67 {"solver",
68 [this](Variant const &v) {
69 if (is_none(v)) {
70 detach_solver();
71 } else {
73 auto old_solver = m_solver;
74 detach_solver();
75 try {
76 m_solver = new_solver;
77 context()->parallel_try_catch([this]() { bind_solver(); });
78 } catch (...) {
79 detach_solver();
80 m_solver = old_solver;
81 if (m_solver) {
82 bind_solver();
83 }
84 throw;
85 }
86 }
87 },
88 [this]() { return m_solver ? Variant{m_solver} : Variant{None{}}; }},
89 });
90 }
91
92 void do_construct(VariantMap const &params) override {
93 m_params = std::make_unique<VariantMap>(params);
94 }
95
96protected:
97 Variant do_call_method(std::string const &name, VariantMap const &) override {
98 if (name == "clear") {
99 detach_solver();
100 return {};
101 }
102 return {};
103 }
104};
105
106} // namespace ScriptInterface::LB
Bind parameters in the script interface.
void do_set_parameter(const std::string &name, const Variant &value) final
void add_parameters(std::vector< AutoParameter > &&params)
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
Variant do_call_method(std::string const &name, VariantMap const &) override
void on_bind_system(::System::System &) override
void do_construct(VariantMap const &params) override
Type to indicate no value in Variant.
Definition None.hpp:32
virtual Variant do_call_method(const std::string &, const VariantMap &)
Local implementation of call_method.
Context * context() const
Responsible context.
std::string_view name() const
std::weak_ptr<::System::System > m_system
Main system class.
constexpr bool is_none(Variant const &v)
Definition Variant.hpp:163
std::shared_ptr< ObjectHandle > ObjectRef
Definition Variant.hpp:121
T get_value(Variant const &v)
Extract value of specific type T from a Variant.
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:133
Recursive variant implementation.
Definition Variant.hpp:84