ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
LocalContext.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 SCRIPT_INTERFACE_LOCAL_CONTEXT_HPP
20#define SCRIPT_INTERFACE_LOCAL_CONTEXT_HPP
21
22#include "Context.hpp"
23#include "ObjectHandle.hpp"
25
26#include <utils/Factory.hpp>
27
28#include <boost/mpi/communicator.hpp>
29
30#include <cassert>
31#include <memory>
32#include <stdexcept>
33#include <string>
34#include <utility>
35
36namespace ScriptInterface {
37
38/**
39 * @brief Trivial context.
40 *
41 * This context just maintains a local copy of an object.
42 */
43class LocalContext : public Context {
45 bool m_is_head_node;
46 boost::mpi::communicator const &m_comm;
47 ParallelExceptionHandler m_parallel_exception_handler;
48
49public:
51 boost::mpi::communicator const &comm)
52 : m_factory(std::move(factory)), m_is_head_node(comm.rank() == 0),
53 m_comm(comm),
54 // NOLINTNEXTLINE(bugprone-throw-keyword-missing)
55 m_parallel_exception_handler(comm) {}
56
57 const Utils::Factory<ObjectHandle> &factory() const { return m_factory; }
58
59 void notify_call_method(const ObjectHandle *, std::string const &,
60 VariantMap const &) override {}
61 void notify_set_parameter(const ObjectHandle *, std::string const &,
62 Variant const &) override {}
63
64 std::shared_ptr<ObjectHandle>
65 make_shared(std::string const &name, const VariantMap &parameters) override {
66 auto sp = m_factory.make(name);
67 set_context(sp.get());
68
69 sp->construct(parameters);
70
71 return sp;
72 }
73
74 std::shared_ptr<ObjectHandle>
75 make_shared_local(std::string const &name,
76 VariantMap const &parameters) override {
78 }
79
80 boost::string_ref name(const ObjectHandle *o) const override {
81 assert(o);
82
83 return factory().type_name(*o);
84 }
85
86 bool is_head_node() const override { return m_is_head_node; }
87 void parallel_try_catch(std::function<void()> const &cb) const override {
88 m_parallel_exception_handler.parallel_try_catch<std::exception>(cb);
89 }
90 boost::mpi::communicator const &get_comm() const override { return m_comm; }
91};
92} // namespace ScriptInterface
93
94#endif
ScriptInterface::Context decorates ScriptInterface::ObjectHandle objects with a context: a creation p...
Context of an object handle.
Definition Context.hpp:54
void set_context(ObjectHandle *o)
Set the context of an object to this.
Definition Context.hpp:102
std::shared_ptr< ObjectHandle > make_shared_local(std::string const &name, VariantMap const &parameters) override
std::shared_ptr< ObjectHandle > make_shared(std::string const &name, const VariantMap &parameters) override
boost::string_ref name(const ObjectHandle *o) const override
boost::mpi::communicator const & get_comm() const override
bool is_head_node() const override
void notify_call_method(const ObjectHandle *, std::string const &, VariantMap const &) override
LocalContext(Utils::Factory< ObjectHandle > factory, boost::mpi::communicator const &comm)
void parallel_try_catch(std::function< void()> const &cb) const override
const Utils::Factory< ObjectHandle > & factory() const
void notify_set_parameter(const ObjectHandle *, std::string const &, Variant const &) override
Base class for interface handles.
Handle exceptions thrown in MPI parallel code.
void parallel_try_catch(std::function< void()> const &callback) const
Handle exceptions in synchronous code.
Factory template.
Definition Factory.hpp:78
pointer_type make(const std::string &name) const
Construct an instance by name.
Definition Factory.hpp:89
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
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