ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
ObjectHandle.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015-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#include "Variant.hpp"
23
24#include <algorithm>
25#include <memory>
26#include <string>
27#include <string_view>
28#include <utility>
29#include <vector>
30
31namespace ScriptInterface {
32class Context;
33
34/**
35 * @brief Base class for interface handles.
36 */
38public:
39 ObjectHandle() = default;
40
41 /* Copy has unclear semantics, so it should not be allowed. */
42 ObjectHandle(ObjectHandle const &) = delete;
44 virtual ~ObjectHandle() = default;
45
46private:
47 friend class Context;
48 std::shared_ptr<Context> m_context = {};
49
50public:
51 std::string_view name() const;
52
53public:
54 /**
55 * @brief Responsible context.
56 */
57 Context *context() const { return m_context.get(); }
58
59public:
60 /**
61 * @brief Construct the handled object.
62 *
63 * This function is called on object creation with user
64 * provided parameters. This can be used if the SO has required parameters,
65 * it represents some type that can not reasonably be default constructed,
66 * or if the core implementation has to be chosen by a parameter. It is
67 * guaranteed that no getter or setter functions from this interface is
68 * called before construction (only @ref name() and @ref valid_parameters()),
69 * and it is only called once.
70 *
71 * The default implementation just calls @ref set_parameter for every
72 * parameter.
73 *
74 * @param params The parameters to the constructor. Only parameters that
75 * are valid for a default-constructed object are valid.
76 */
78
79 virtual void do_construct(VariantMap const &params) {
80 for (auto const &[key, value] : params) {
81 do_set_parameter(key, value);
82 }
83 }
84
85public:
86 /**
87 * @brief Get current parameters.
88 * @return Parameters set in class.
89 */
92
93 for (auto const &name : valid_parameters()) {
94 values[std::string{name}] = get_parameter(std::string{name});
95 }
96
97 return values;
98 }
99
100 /**
101 * @brief Get required and optional parameters for class.
102 * @return Expected parameters.
103 */
104 virtual std::vector<std::string_view> valid_parameters() const { return {}; }
105
106 virtual bool has_parameter(std::string const &needle) const {
107 auto const names = valid_parameters();
108 return std::ranges::find(names, std::string_view{needle}) != names.end();
109 }
110
111 /**
112 * @brief Get single parameter.
113 *
114 * @param name Name of the parameter
115 * @return Value of parameter @p name
116 */
117 virtual Variant get_parameter(std::string const &name) const {
118 static_cast<void>(name);
119 return {};
120 }
121
122 /**
123 * @brief Set single parameter.
124 * Can only be called on the head node.
125 */
126 void set_parameter(const std::string &name, const Variant &value);
127
128 /**
129 * @brief Local implementation of @ref set_parameter.
130 */
131 virtual void do_set_parameter(const std::string &, const Variant &) {}
132
133public:
134 /**
135 * @brief Call a method on the object.
136 * Can only be called on the head node.
137 */
138 Variant call_method(const std::string &name, const VariantMap &params);
139
140 /**
141 * @brief Local implementation of @c call_method.
142 *
143 * If not overridden by the implementation, this does nothing.
144 */
145 virtual Variant do_call_method(const std::string &, const VariantMap &) {
146 return none;
147 }
148
149public:
150 std::string serialize() const;
151
152 /**
153 * @brief Make object from serialized state.
154 */
155 static ObjectRef deserialize(const std::string &state, Context &ctx);
156
157 /**
158 * @brief Serialize parameters.
159 * Can be overridden to e.g. serialize parameters in a specific order.
160 */
161 virtual std::vector<std::pair<std::string, Variant>>
163 auto const params = this->get_parameters();
164 return {params.begin(), params.end()};
165 }
166
167private:
168 virtual std::string get_internal_state() const { return {}; }
169 virtual void set_internal_state(std::string const &) {}
170};
171} /* namespace ScriptInterface */
Context of an object handle.
Definition Context.hpp:53
Base class for interface handles.
VariantMap get_parameters() const
Get current parameters.
ObjectHandle(ObjectHandle const &)=delete
ObjectHandle & operator=(ObjectHandle const &)=delete
std::string serialize() const
virtual std::vector< std::string_view > valid_parameters() const
Get required and optional parameters for class.
Variant call_method(const std::string &name, const VariantMap &params)
Call a method on the object.
virtual void set_internal_state(std::string const &)
virtual void do_set_parameter(const std::string &, const Variant &)
Local implementation of set_parameter.
virtual void do_construct(VariantMap const &params)
virtual bool has_parameter(std::string const &needle) const
virtual Variant do_call_method(const std::string &, const VariantMap &)
Local implementation of call_method.
virtual std::vector< std::pair< std::string, Variant > > serialize_parameters() const
Serialize parameters.
Context * context() const
Responsible context.
virtual Variant get_parameter(std::string const &name) const
Get single parameter.
virtual std::string get_internal_state() const
void construct(VariantMap const &params)
Construct the handled object.
void set_parameter(const std::string &name, const Variant &value)
Set single parameter.
static ObjectRef deserialize(const std::string &state, Context &ctx)
Make object from serialized state.
std::string_view name() const
virtual ~ObjectHandle()=default
std::shared_ptr< ObjectHandle > ObjectRef
Definition Variant.hpp:121
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
constexpr const None none
None-"literal".
Definition Variant.hpp:126
static SteepestDescentParameters params
Currently active steepest descent instance.
Recursive variant implementation.
Definition Variant.hpp:84