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-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/** @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 <stdexcept>
42#include <string>
43#include <utility>
44
45namespace ScriptInterface {
46void GlobalContext::make_handle(ObjectId id, const std::string &name,
47 const PackedMap &parameters) {
48 try {
49 ObjectRef so = m_node_local_context->make_shared(
50 name, unpack(parameters, m_local_objects));
51
52 m_local_objects[id] = std::move(so);
53 } catch (Exception const &) { // NOLINT(bugprone-empty-catch)
54 }
55}
56
57void GlobalContext::remote_make_handle(ObjectId id, const std::string &name,
58 const VariantMap &parameters) {
59 cb_make_handle(id, name, pack(parameters));
60}
61
62void GlobalContext::set_parameter(ObjectId id, std::string const &name,
63 PackedVariant const &value) {
64 try {
65 m_local_objects.at(id)->set_parameter(name, unpack(value, m_local_objects));
66 } catch (Exception const &) { // NOLINT(bugprone-empty-catch)
67 }
68}
69
71 std::string const &name,
72 Variant const &value) {
73 cb_set_parameter(object_id(o), name, pack(value));
74}
75
76void GlobalContext::call_method(ObjectId id, std::string const &name,
77 PackedMap const &arguments) {
78 try {
79 m_local_objects.at(id)->call_method(name,
80 unpack(arguments, m_local_objects));
81 } catch (Exception const &) { // NOLINT(bugprone-empty-catch)
82 }
83}
84
86 std::string const &name,
87 VariantMap const &arguments) {
88 cb_call_method(object_id(o), name, pack(arguments));
89}
90
91std::shared_ptr<ObjectHandle>
92GlobalContext::make_shared_local(std::string const &name,
93 VariantMap const &parameters) {
94 auto sp = m_node_local_context->factory().make(name);
95 set_context(sp.get());
96
97 sp->construct(parameters);
98
99 return sp;
100}
101
102std::shared_ptr<ObjectHandle>
103GlobalContext::make_shared(std::string const &name,
104 const VariantMap &parameters) {
105 assert(is_head_node());
106 auto sp = m_node_local_context->factory().make(name);
107 set_context(sp.get());
108
109 auto const id = object_id(sp.get());
110 remote_make_handle(id, name, parameters);
111
112 sp->construct(parameters);
113
114 return {sp.release(),
115 /* Custom deleter, we keep the corresponding global context,
116 * as well as the original deleter for the object. */
117 [global_context = this, deleter = sp.get_deleter()](ObjectHandle *o) {
118 /* Tell the other nodes before invoking the destructor, this is
119 * required
120 * to have synchronous destructors, which is needed by some client
121 * code. */
122 global_context->cb_delete_handle(object_id(o));
123
124 /* Locally destroy the object. */
125 deleter(o);
126 }};
127}
128
129boost::string_ref GlobalContext::name(const ObjectHandle *o) const {
130 assert(o);
131
132 return m_node_local_context->factory().type_name(*o);
133}
134} // 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:102
std::shared_ptr< ObjectHandle > make_shared(std::string const &name, const VariantMap &parameters) override
Get a new reference counted instance of a script interface object by name.
void notify_set_parameter(const ObjectHandle *o, std::string const &name, Variant const &value) override
boost::string_ref name(const ObjectHandle *o) const override
bool is_head_node() const override
std::shared_ptr< ObjectHandle > make_shared_local(std::string const &name, VariantMap const &parameters) override
void notify_call_method(const ObjectHandle *o, std::string const &name, VariantMap const &arguments) override
Base class for interface handles.
PackedVariant pack(const Variant &v)
Transform a Variant to a PackedVariant.
std::shared_ptr< ObjectHandle > ObjectRef
Definition Variant.hpp:59
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:82
boost::make_recursive_variant< None, bool, int, std::size_t, double, std::string, ObjectRef, Utils::Vector3b, Utils::Vector3i, Utils::Vector2d, Utils::Vector3d, Utils::Vector4d, std::vector< int >, std::vector< double >, std::vector< boost::recursive_variant_ >, std::unordered_map< int, boost::recursive_variant_ >, std::unordered_map< std::string, boost::recursive_variant_ > >::type Variant
Possible types for parameters.
Definition Variant.hpp:80
std::vector< std::pair< std::string, PackedVariant > > PackedMap
Variant unpack(const PackedVariant &v, std::unordered_map< ObjectId, ObjectRef > const &objects)
Unpack a PackedVariant.
std::size_t ObjectId
boost::make_recursive_variant< None, bool, int, std::size_t, double, std::string, ObjectId, Utils::Vector3b, Utils::Vector3i, Utils::Vector2d, Utils::Vector3d, Utils::Vector4d, std::vector< int >, std::vector< double >, std::vector< boost::recursive_variant_ >, std::unordered_map< int, boost::recursive_variant_ >, std::unordered_map< std::string, boost::recursive_variant_ > >::type PackedVariant
Packed version of Variant.
ObjectId object_id(const ObjectHandle *p)
Id for object.