ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
Context.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
20#pragma once
21
22/** @file
23 *
24 * @ref ScriptInterface::Context decorates @ref ScriptInterface::ObjectHandle
25 * objects with a context: a creation policy (local object, local object
26 * with remote copies) and a communication facility to synchronize an object
27 * on the head node with remote copies (serialization, callback mechanism).
28 */
29
30#include "ObjectHandle.hpp"
31#include "Variant.hpp"
32
33#include <memory>
34#include <string>
35#include <string_view>
36
37namespace boost {
38namespace mpi {
39class communicator;
40} // namespace mpi
41} // namespace boost
42
43namespace ScriptInterface {
44/**
45 * @brief Context of an object handle.
46 *
47 * Each instance of @ref ObjectHandle can have an
48 * attached context, which can e.g. synchronize
49 * distributed copies of the instance. The context
50 * does also provide facilities for serializing
51 * Objects into a string representation.
52 */
53class Context : public std::enable_shared_from_this<Context> {
54public:
55 /**
56 * @brief Call method on remote instances
57 *
58 * @param self Internal identifier of the instance
59 * @param name Name of the method to call
60 * @param arguments Arguments to the call
61 */
63 std::string const &name,
64 VariantMap const &arguments) = 0;
65
66 /**
67 * @brief Set a parameter on remote instances
68 *
69 * @param self Internal identifier of the instance to be modified
70 * @param name Name of the parameter to change
71 * @param value Value to set it to
72 */
74 std::string const &name,
75 Variant const &value) = 0;
76
77 /**
78 * @brief Get a new reference counted instance of a script interface by
79 * name.
80 *
81 * Objects created thru a Context get shared ownership of that context,
82 * e.g. the lifetime of the context is at least as long as the objects
83 * created by it. Therefore the object can always assume that the context
84 * is present.
85 */
86 virtual std::shared_ptr<ObjectHandle>
87 make_shared(std::string const &name, const VariantMap &parameters) = 0;
88
89protected:
90 /**
91 * @brief Set the context of an object to this.
92 *
93 * @param o Object to set the context for.
94 */
95 void set_context(ObjectHandle *o) { o->m_context = this->shared_from_this(); }
96
97public:
98 /**
99 * @brief Get the class name for an @ref ObjectHandle instance.
100 *
101 * This returns the name by which the object can be created.
102 */
103 virtual std::string_view name(const ObjectHandle *o) const = 0;
104
105 virtual bool is_head_node() const = 0;
106 virtual void parallel_try_catch(std::function<void()> const &cb) const = 0;
107 virtual boost::mpi::communicator const &get_comm() const = 0;
108
109 virtual ~Context() = default;
110};
111} // namespace ScriptInterface
Context of an object handle.
Definition Context.hpp:53
virtual void notify_set_parameter(const ObjectHandle *self, std::string const &name, Variant const &value)=0
Set a parameter on remote instances.
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
virtual std::string_view name(const ObjectHandle *o) const =0
Get the class name for an ObjectHandle instance.
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.
virtual void notify_call_method(const ObjectHandle *self, std::string const &name, VariantMap const &arguments)=0
Call method on remote instances.
virtual bool is_head_node() const =0
void set_context(ObjectHandle *o)
Set the context of an object to this.
Definition Context.hpp:95
virtual ~Context()=default
virtual boost::mpi::communicator const & get_comm() const =0
Base class for interface handles.
Communicator communicator
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:133
Recursive variant implementation.
Definition Variant.hpp:84