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