ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
GlobalContext.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020-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/** @file
21 *
22 * Infrastructure to synchronize objects created on the head node with
23 * their corresponding remote copies. Methods of script interface
24 * classes may throw exceptions of type @ref ScriptInterface::Exception.
25 * These exceptions will halt the flow of the program on the head node.
26 * The same exceptions will be thrown in the remote copies but will
27 * be silenced, since they are redundant. Other types of exceptions
28 * are not silenced.
29 *
30 * Implementation of @ref GlobalContext.hpp.
31 */
32
33#include "GlobalContext.hpp"
34#include "Exception.hpp"
35#include "ObjectHandle.hpp"
36#include "packed_variant.hpp"
37
39
40#include <cassert>
41#include <string>
42#include <utility>
43
44namespace ScriptInterface {
45void GlobalContext::make_handle(ObjectId id, std::string const &name,
46 PackedMap const &parameters) {
47 try {
48 ObjectRef so = m_node_local_context->make_shared(
49 name, unpack(parameters, m_local_objects));
50
51 m_local_objects[id] = std::move(so);
52 } catch (Exception const &) { // NOLINT(bugprone-empty-catch)
53 }
54}
55
56void GlobalContext::remote_make_handle(ObjectId id, std::string const &name,
57 VariantMap const &parameters) {
58 cb_make_handle(id, name, pack(parameters));
59}
60
61void GlobalContext::set_parameter(ObjectId id, std::string const &name,
62 PackedVariant const &value) {
63 try {
64 m_local_objects.at(id)->set_parameter(name, unpack(value, m_local_objects));
65 } catch (Exception const &) { // NOLINT(bugprone-empty-catch)
66 }
67}
68
70 std::string const &name,
71 Variant const &value) {
72 cb_set_parameter(ObjectId(o), name, pack(value));
73}
74
75void GlobalContext::call_method(ObjectId id, std::string const &name,
76 PackedMap const &arguments) {
77 try {
78 m_local_objects.at(id)->call_method(name,
79 unpack(arguments, m_local_objects));
80 } catch (Exception const &) { // NOLINT(bugprone-empty-catch)
81 }
82}
83
85 std::string const &name,
86 VariantMap const &arguments) {
87 cb_call_method(ObjectId(o), name, pack(arguments));
88}
89
90std::shared_ptr<ObjectHandle>
91GlobalContext::make_shared(std::string const &name,
92 VariantMap const &parameters) {
94 auto sp = m_node_local_context->factory().make(name);
95 set_context(sp.get());
96
97 auto const id = ObjectId(sp.get());
98 remote_make_handle(id, name, parameters);
99
100 sp->construct(parameters);
101
102 return {sp.release(),
103 /* Custom deleter, we keep the corresponding global context,
104 * as well as the original deleter for the object. */
105 [global_context = this, deleter = sp.get_deleter()](ObjectHandle *o) {
106 /* Notify worker nodes before invoking the deleter. This is
107 * required by synchronous deleters used by some client code.
108 * Since a deleter cannot throw, exceptions thrown by Boost and
109 * MpiCallbacks must be silenced (such code paths arise when
110 * undefined behavior is invoked).
111 */
112 try {
113 global_context->cb_delete_handle(ObjectId(o));
114 } catch (...) { // NOLINT(bugprone-empty-catch)
115 }
116
117 /* Locally destroy the object. */
118 deleter(o);
119 }};
120}
121
122std::string_view GlobalContext::name(ObjectHandle const *o) const {
123 assert(o);
124
125 return m_node_local_context->factory().type_name(*o);
126}
127} // namespace ScriptInterface
Infrastructure to synchronize objects created on the head node with their corresponding remote copies...
void set_context(ObjectHandle *o)
Set the context of an object to this.
Definition Context.hpp:95
std::shared_ptr< ObjectHandle > make_shared(std::string const &name, VariantMap const &parameters) override
Get a new reference counted instance of a script interface object by name.
void notify_call_method(ObjectHandle const *o, std::string const &name, VariantMap const &arguments) override
void notify_set_parameter(ObjectHandle const *o, std::string const &name, Variant const &value) override
bool is_head_node() const override
std::string_view name(ObjectHandle const *o) const override
Base class for interface handles.
make_recursive_variant< ObjectId > PackedVariant
Packed version of Variant.
PackedVariant pack(const Variant &v)
Transform a Variant to a PackedVariant.
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
std::vector< std::pair< std::string, PackedVariant > > PackedMap
Variant unpack(const PackedVariant &v, std::unordered_map< ObjectId, ObjectRef > const &objects)
Unpack a PackedVariant.
Strongly typed integer type to hold a unique identifier for a ObjectHandle object,...
Definition ObjectId.hpp:37
Recursive variant implementation.
Definition Variant.hpp:84