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
24
#include <
script_interface/ScriptInterface.hpp
>
25
#include <
script_interface/auto_parameters/AutoParameter.hpp
>
26
#include <
script_interface/system/Leaf.hpp
>
27
28
#include "
core/system/System.hpp
"
29
30
#include <memory>
31
#include <optional>
32
#include <string>
33
34
namespace
ScriptInterface::LB
{
35
36
class
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
54
void
on_bind_system
(
::System::System
&)
override
{
55
auto
const
¶ms = *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
64
public
:
65
Container
() {
66
add_parameters
({
67
{
"solver"
,
68
[
this
](
Variant
const
&v) {
69
if
(
is_none
(v)) {
70
detach_solver();
71
}
else
{
72
auto
new_solver
=
get_value<ObjectRef>
(v);
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
¶ms)
override
{
93
m_params = std::make_unique<VariantMap>(params);
94
}
95
96
protected
:
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
AutoParameter.hpp
ScriptInterface.hpp
ScriptInterface::AutoParameters
Bind parameters in the script interface.
Definition
AutoParameters.hpp:94
ScriptInterface::AutoParameters< Container, System::Leaf >::do_set_parameter
void do_set_parameter(const std::string &name, const Variant &value) final
Definition
AutoParameters.hpp:154
ScriptInterface::AutoParameters< Container, System::Leaf >::add_parameters
void add_parameters(std::vector< AutoParameter > &¶ms)
Definition
AutoParameters.hpp:123
ScriptInterface::AutoParameters< Container, System::Leaf >::get_parameter_insertion_order
auto const & get_parameter_insertion_order() const
Definition
AutoParameters.hpp:137
ScriptInterface::Context::parallel_try_catch
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
ScriptInterface::LB::Container
Definition
lb/Container.hpp:36
ScriptInterface::LB::Container::Container
Container()
Definition
lb/Container.hpp:65
ScriptInterface::LB::Container::do_call_method
Variant do_call_method(std::string const &name, VariantMap const &) override
Definition
lb/Container.hpp:97
ScriptInterface::LB::Container::on_bind_system
void on_bind_system(::System::System &) override
Definition
lb/Container.hpp:54
ScriptInterface::LB::Container::do_construct
void do_construct(VariantMap const ¶ms) override
Definition
lb/Container.hpp:92
ScriptInterface::None
Type to indicate no value in Variant.
Definition
None.hpp:32
ScriptInterface::ObjectHandle::do_call_method
virtual Variant do_call_method(const std::string &, const VariantMap &)
Local implementation of call_method.
Definition
ObjectHandle.hpp:145
ScriptInterface::ObjectHandle::context
Context * context() const
Responsible context.
Definition
ObjectHandle.hpp:57
ScriptInterface::ObjectHandle::name
std::string_view name() const
Definition
ObjectHandle.cpp:117
ScriptInterface::System::Leaf::m_system
std::weak_ptr<::System::System > m_system
Definition
script_interface/system/Leaf.hpp:51
System::System
Main system class.
Definition
core/system/System.hpp:81
config.hpp
System.hpp
ScriptInterface::LB
Definition
lb/Container.hpp:34
ScriptInterface::is_none
constexpr bool is_none(Variant const &v)
Definition
Variant.hpp:163
ScriptInterface::ObjectRef
std::shared_ptr< ObjectHandle > ObjectRef
Definition
Variant.hpp:121
ScriptInterface::get_value
T get_value(Variant const &v)
Extract value of specific type T from a Variant.
Definition
get_value.hpp:398
ScriptInterface::VariantMap
std::unordered_map< std::string, Variant > VariantMap
Definition
Variant.hpp:133
Leaf.hpp
ScriptInterface::impl::recursive_variant
Recursive variant implementation.
Definition
Variant.hpp:84
src
script_interface
lb
Container.hpp
Generated on Thu Feb 12 2026 02:30:09 for ESPResSo by
1.9.8