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