ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
ObjectHandle.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "ObjectHandle.hpp"
23#include "Context.hpp"
24#include "ObjectState.hpp"
25#include "packed_variant.hpp"
26
27#include <config/config.hpp>
28
30
33
34#include <algorithm>
35#include <iterator>
36#include <memory>
37#include <string>
38#include <string_view>
39#include <unordered_map>
40#include <utility>
41#include <variant>
42
43namespace ScriptInterface {
44void ObjectHandle::set_parameter(const std::string &name,
45 const Variant &value) {
46 if (m_context)
47 m_context->notify_set_parameter(this, name, value);
48
49#ifdef ESPRESSO_FPE
50 auto const trap = fe_trap::make_shared_scoped();
51#endif
52 this->do_set_parameter(name, value);
53}
54
55Variant ObjectHandle::call_method(const std::string &name,
56 const VariantMap &params) {
57 if (m_context)
58 m_context->notify_call_method(this, name, params);
59
60#ifdef ESPRESSO_FPE
61 auto const trap = fe_trap::make_shared_scoped();
62#endif
63 return this->do_call_method(name, params);
64}
65
66std::string ObjectHandle::serialize() const {
68
69 auto const params = serialize_parameters();
70 state.params.reserve(params.size());
71
73
74 /* Pack parameters and keep track of ObjectRef parameters */
75 std::ranges::transform(params, std::back_inserter(state.params),
76 [&visitor](auto const &kv) -> PackedMap::value_type {
77 auto const &[name, value] = kv;
78 return {name, std::visit(visitor, value)};
79 });
80
81 /* Packed Object parameters */
82 state.objects.reserve(visitor.objects().size());
83 std::ranges::transform(visitor.objects(), std::back_inserter(state.objects),
84 [](auto const &kv) {
85 auto const &[name, obj] = kv;
86 return std::make_pair(name, obj->serialize());
87 });
88
89 state.name = name();
90 state.internal_state = get_internal_state();
91
92 return Utils::pack(state);
93}
94
95ObjectRef ObjectHandle::deserialize(const std::string &packed_state,
96 Context &ctx) {
97 auto const state = Utils::unpack<ObjectState>(packed_state);
98
99 std::unordered_map<ObjectId, ObjectRef> objects;
100 std::ranges::transform(state.objects, std::inserter(objects, objects.end()),
101 [&ctx](auto const &kv) {
102 auto const &[name, buf] = kv;
103 return std::make_pair(name, deserialize(buf, ctx));
104 });
105
107 for (auto const &[name, variant] : state.params) {
108 params[name] = std::visit(UnpackVisitor(objects), variant);
109 }
110
111 auto o = ctx.make_shared(state.name, params);
112 o->set_internal_state(state.internal_state);
113
114 return o;
115}
116
117std::string_view ObjectHandle::name() const {
118 return context() ? context()->name(this) : std::string_view{};
119}
120
121} /* namespace ScriptInterface */
ScriptInterface::Context decorates ScriptInterface::ObjectHandle objects with a context: a creation p...
Context of an object handle.
Definition Context.hpp:53
virtual std::shared_ptr< ObjectHandle > make_shared(std::string const &name, const VariantMap &parameters)=0
Get a new reference counted instance of a script interface by name.
std::string serialize() const
Variant call_method(const std::string &name, const VariantMap &params)
Call a method on the object.
virtual void do_set_parameter(const std::string &, const Variant &)
Local implementation of set_parameter.
virtual Variant do_call_method(const std::string &, const VariantMap &)
Local implementation of call_method.
virtual std::vector< std::pair< std::string, Variant > > serialize_parameters() const
Serialize parameters.
void set_parameter(const std::string &name, const Variant &value)
Set single parameter.
std::string_view name() const
static scoped_instance make_shared_scoped(std::optional< int > excepts=std::nullopt)
Generate a shared trap with the lifetime of the current scope.
Definition fe_trap.cpp:121
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::string pack(T const &v)
Pack a serialize type into a string.
Definition pack.hpp:38
static SteepestDescentParameters params
Currently active steepest descent instance.
State of an object ready for serialization.
Visitor that converts a Variant to a PackedVariant.
auto const & objects() const
Map of objects whose references were replaced by ids.
Visitor that converts a PackedVariant to a Variant.
Recursive variant implementation.
Definition Variant.hpp:84