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