ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
ContextManager.hpp
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#ifndef ESPRESSO_CONTEXTMANAGER_HPP
20#define ESPRESSO_CONTEXTMANAGER_HPP
21
22/** @file
23 *
24 * @ref ScriptInterface::ContextManager manages object creation with policies
25 * @ref ScriptInterface::ContextManager::CreationPolicy "CreationPolicy".
26 * Object creation is delegated to @ref ScriptInterface::GlobalContext and
27 * @ref ScriptInterface::LocalContext. @ref ScriptInterface::ContextManager
28 * serves as their public interface. If there is only 1 MPI rank, no
29 * communication takes place and all objects are created locally via
30 * @ref ScriptInterface::LocalContext, including those with policy
31 * @ref ScriptInterface::ContextManager::CreationPolicy::GLOBAL "GLOBAL".
32 *
33 * Implementation in @ref ContextManager.cpp.
34 */
35
36#include "Context.hpp"
37#include "Variant.hpp"
38
39#include "core/MpiCallbacks.hpp"
40
41#include <utils/Factory.hpp>
42
43#include <cassert>
44#include <memory>
45#include <stdexcept>
46#include <string>
47
48namespace ScriptInterface {
49
50/**
51 * @brief Manage object contexts.
52 *
53 * This owns object contexts and allows for
54 * creation and serialization of objects preserving
55 * their context.
56 */
58 std::shared_ptr<Context> m_local_context;
59 std::shared_ptr<Context> m_global_context;
60
61public:
62 /** Labels for context */
63 enum class CreationPolicy {
64 /** Corresponding to @c LocalContext */
65 LOCAL,
66 /** Corresponding to @c GlobalContext */
67 GLOBAL
68 };
69
70 ContextManager(std::shared_ptr<Communication::MpiCallbacks> const &callbacks,
71 const Utils::Factory<ObjectHandle> &factory);
72
73 /**
74 * @brief Get a new reference counted instance of a script interface by
75 * name.
76 */
77 std::shared_ptr<ObjectHandle> make_shared(CreationPolicy policy,
78 std::string const &name,
79 const VariantMap &parameters);
80
81 /**
82 * @brief Get a new reference counted instance of a script interface from
83 * a serialized state.
84 */
85 std::shared_ptr<ObjectHandle> deserialize(std::string const &state_);
86
87 /**
88 * @brief Serialize a script interface object into a binary representation.
89 */
90 std::string serialize(const ObjectHandle *o) const;
91
92private:
93 /**
94 * @brief Map policy to context.
95 *
96 * Inverse of policy.
97 */
98 Context *context(CreationPolicy policy) const {
99 switch (policy) {
101 return assert(m_local_context), m_local_context.get();
103 return assert(m_global_context), m_global_context.get();
104 default:
105 throw std::runtime_error("Unknown context type.");
106 }
107 }
108
109 /**
110 * @brief Map context to policy.
111 *
112 * Inverse of context.
113 */
114 CreationPolicy policy(Context *c) const {
115 if (c == m_local_context.get()) {
117 }
118 if (c == m_global_context.get()) {
120 }
121
122 throw std::runtime_error("Invalid context.");
123 }
124};
125} // namespace ScriptInterface
126
127#endif // ESPRESSO_CONTEXTMANAGER_HPP
ScriptInterface::Context decorates ScriptInterface::ObjectHandle objects with a context: a creation p...
Communication::MpiCallbacks manages MPI communication using a visitor pattern.
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.
@ LOCAL
Corresponding to LocalContext.
@ GLOBAL
Corresponding to GlobalContext.
std::string serialize(const ObjectHandle *o) const
Serialize a script interface object into a binary representation.
std::shared_ptr< ObjectHandle > deserialize(std::string const &state_)
Get a new reference counted instance of a script interface from a serialized state.
Context of an object handle.
Definition Context.hpp:54
Base class for interface handles.
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