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#ifndef ESPRESSO_SCRIPT_INTERFACE_CONTEXT_HPP
20#define ESPRESSO_SCRIPT_INTERFACE_CONTEXT_HPP
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 <boost/utility/string_ref.hpp>
34
35#include <memory>
36#include <string>
37
38namespace boost {
39namespace mpi {
40class communicator;
41} // namespace mpi
42} // namespace boost
43
44namespace ScriptInterface {
45/**
46 * @brief Context of an object handle.
47 *
48 * Each instance of @ref ObjectHandle can have an
49 * attached context, which can e.g. synchronize
50 * distributed copies of the instance. The context
51 * does also provide facilities for serializing
52 * Objects into a string representation.
53 */
54class Context : public std::enable_shared_from_this<Context> {
55public:
56 /**
57 * @brief Call method on remote instances
58 *
59 * @param self Internal identifier of the instance
60 * @param name Name of the method to call
61 * @param arguments Arguments to the call
62 */
63 virtual void notify_call_method(const ObjectHandle *self,
64 std::string const &name,
65 VariantMap const &arguments) = 0;
66
67 /**
68 * @brief Set a parameter on remote instances
69 *
70 * @param self Internal identifier of the instance to be modified
71 * @param name Name of the parameter to change
72 * @param value Value to set it to
73 */
74 virtual void notify_set_parameter(const ObjectHandle *self,
75 std::string const &name,
76 Variant const &value) = 0;
77
78 /**
79 * @brief Get a new reference counted instance of a script interface by
80 * name.
81 *
82 * Objects created thru a Context get shared ownership of that context,
83 * e.g. the lifetime of the context is at least as long as the objects
84 * created by it. Therefore the object can always assume that the context
85 * is present.
86 */
87 virtual std::shared_ptr<ObjectHandle>
88 make_shared(std::string const &name, const VariantMap &parameters) = 0;
89
90 /**
91 * @copydoc Context::make_shared
92 */
93 virtual std::shared_ptr<ObjectHandle>
94 make_shared_local(std::string const &name, VariantMap const &parameters) = 0;
95
96protected:
97 /**
98 * @brief Set the context of an object to this.
99 *
100 * @param o Object to set the context for.
101 */
102 void set_context(ObjectHandle *o) { o->m_context = this->shared_from_this(); }
103
104public:
105 /**
106 * @brief Get the class name for an @ref ObjectHandle instance.
107 *
108 * This returns the name by which the object can be created.
109 */
110 virtual boost::string_ref name(const ObjectHandle *o) const = 0;
111
112 virtual bool is_head_node() const = 0;
113 virtual void parallel_try_catch(std::function<void()> const &cb) const = 0;
114 virtual boost::mpi::communicator const &get_comm() const = 0;
115
116 virtual ~Context() = default;
117};
118} // namespace ScriptInterface
119#endif // ESPRESSO_CONTEXT_HPP
Context of an object handle.
Definition Context.hpp:54
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::shared_ptr< ObjectHandle > make_shared_local(std::string const &name, VariantMap const &parameters)=0
Get a new reference counted instance of a script interface by name.
virtual boost::string_ref 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:102
virtual ~Context()=default
virtual boost::mpi::communicator const & get_comm() const =0
Base class for interface handles.
Communicator communicator
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