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
32
33#include <algorithm>
34#include <iterator>
35#include <memory>
36#include <string>
37#include <unordered_map>
38#include <utility>
39
40namespace ScriptInterface {
41void ObjectHandle::set_parameter(const std::string &name,
42 const Variant &value) {
43 if (m_context)
44 m_context->notify_set_parameter(this, name, value);
45
46#ifdef FPE
47 auto const trap = fe_trap::make_shared_scoped();
48#endif
49 this->do_set_parameter(name, value);
50}
51
52Variant ObjectHandle::call_method(const std::string &name,
53 const VariantMap &params) {
54 if (m_context)
55 m_context->notify_call_method(this, name, params);
56
57#ifdef FPE
58 auto const trap = fe_trap::make_shared_scoped();
59#endif
60 return this->do_call_method(name, params);
61}
62
63std::string ObjectHandle::serialize() const {
64 ObjectState state;
65
66 auto const params = serialize_parameters();
67 state.params.resize(params.size());
68
69 PackVisitor visit;
70
71 /* Pack parameters and keep track of ObjectRef parameters */
72 std::ranges::transform(params, state.params.begin(),
73 [&visit](auto const &kv) -> PackedMap::value_type {
74 return {kv.first,
75 boost::apply_visitor(visit, kv.second)};
76 });
77
78 /* Packed Object parameters */
79 state.objects.resize(visit.objects().size());
80 std::ranges::transform(
81 visit.objects(), state.objects.begin(), [](auto const &kv) {
82 return std::make_pair(kv.first, kv.second->serialize());
83 });
84
85 state.name = name().to_string();
86 state.internal_state = get_internal_state();
87
88 return Utils::pack(state);
89}
90
91ObjectRef ObjectHandle::deserialize(const std::string &packed_state,
92 Context &ctx) {
93 auto const state = Utils::unpack<ObjectState>(packed_state);
94
95 std::unordered_map<ObjectId, ObjectRef> objects;
96 std::ranges::transform(state.objects, std::inserter(objects, objects.end()),
97 [&ctx](auto const &kv) {
98 return std::make_pair(kv.first,
99 deserialize(kv.second, ctx));
100 });
101
103 for (auto const &kv : state.params) {
104 params[kv.first] = boost::apply_visitor(UnpackVisitor(objects), kv.second);
105 }
106
107 auto o = ctx.make_shared(state.name, params);
108 o->set_internal_state(state.internal_state);
109
110 return o;
111}
112
113boost::string_ref ObjectHandle::name() const {
114 return context() ? context()->name(this) : boost::string_ref{};
115}
116
117} /* namespace ScriptInterface */
ScriptInterface::Context decorates ScriptInterface::ObjectHandle objects with a context: a creation p...
Context of an object handle.
Definition Context.hpp:54
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.
boost::string_ref name() const
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.
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
This file contains the defaults for ESPResSo.
std::shared_ptr< ObjectHandle > ObjectRef
Definition Variant.hpp:46
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:69
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:67
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.