Loading [MathJax]/extensions/TeX/AMSmath.js
ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages Concepts
ContextManager.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#include "ContextManager.hpp"
20
21#include "GlobalContext.hpp"
22#include "LocalContext.hpp"
23
25
26#include <cassert>
27#include <memory>
28#include <string>
29#include <utility>
30
31namespace ScriptInterface {
32std::shared_ptr<ObjectHandle>
33ContextManager::make_shared(CreationPolicy policy, std::string const &name,
34 const VariantMap &parameters) {
35 return context(policy)->make_shared(name, parameters);
36}
37
38std::shared_ptr<ObjectHandle>
40 auto const state =
41 Utils::unpack<std::pair<CreationPolicy, std::string>>(state_);
42
43 auto ctx = context(state.first);
44 assert(ctx);
45
46 return ObjectHandle::deserialize(state.second, *ctx);
47}
48
49std::string ContextManager::serialize(const ObjectHandle *o) const {
50 /* We treat objects without a context as local. */
51 auto ctx = o->context() ? o->context() : m_local_context.get();
52
53 return Utils::pack(std::make_pair(policy(ctx), o->serialize()));
54}
55
57 std::shared_ptr<Communication::MpiCallbacks> const &callbacks,
58 const Utils::Factory<ObjectHandle> &factory) {
59 auto local_context =
60 std::make_shared<LocalContext>(factory, callbacks->comm());
61
62 /* If there is only one node, we can treat all objects as local, and thus
63 * never invoke any callback. */
64 m_global_context =
65 (callbacks->comm().size() > 1)
66 ? std::make_shared<GlobalContext>(callbacks, local_context)
67 : std::static_pointer_cast<Context>(local_context);
68
69 m_local_context = std::move(local_context);
70}
71} // namespace ScriptInterface
ScriptInterface::ContextManager manages object creation with policies CreationPolicy.
Infrastructure to synchronize objects created on the head node with their corresponding remote copies...
std::shared_ptr< ObjectHandle > make_shared(CreationPolicy policy, std::string const &name, const VariantMap &parameters)
Get a new reference counted instance of a script interface by name.
std::string serialize(const ObjectHandle *o) const
Serialize a script interface object into a binary representation.
ContextManager(std::shared_ptr< Communication::MpiCallbacks > const &callbacks, const Utils::Factory< ObjectHandle > &factory)
std::shared_ptr< ObjectHandle > deserialize(std::string const &state_)
Get a new reference counted instance of a script interface from a serialized state.
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.
Base class for interface handles.
static ObjectRef deserialize(const std::string &state, Context &ctx)
Make object from serialized state.
Factory template.
Definition Factory.hpp:78
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
std::string pack(T const &v)
Pack a serialize type into a string.
Definition pack.hpp:38