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