ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
AutoParameter.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-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_AUTO_PARAMETERS_AUTO_PARAMETER_HPP
20#define SCRIPT_INTERFACE_AUTO_PARAMETERS_AUTO_PARAMETER_HPP
21
24
25#include <functional>
26#include <memory>
27#include <string>
28#include <utility>
29
30namespace ScriptInterface {
31/**
32 * @brief Description and getter/setter for a parameter.
33 *
34 * This is to be used with @c AutoParameters.
35 */
37 /* Exception types */
38 struct WriteError {};
39
40 /* Result types */
41 struct ReadOnly {};
42 static constexpr const ReadOnly read_only = ReadOnly{};
43
44 /** @brief Read-write parameter that is bound to an object.
45 *
46 * @param name The name the parameter should be bound to in the interface.
47 * @param obj The object the parameter should be bound to.
48 * @param setter The setter.
49 * @param getter The getter.
50 */
51 template <typename T, class O>
52 AutoParameter(const char *name, std::shared_ptr<O> &obj,
53 void (O::*setter)(T const &), T const &(O::*getter)() const)
54 : name(name), setter_([&obj, setter](Variant const &v) {
55 (obj.get()->*setter)(get_value<T>(v));
56 }),
57 getter_([&obj, getter]() { return (obj.get()->*getter)(); }) {}
58
59 /** @brief Read-write parameter that is bound to an object.
60 * @overload
61 */
62 template <typename T, class O>
63 AutoParameter(const char *name, std::shared_ptr<O> &obj,
64 void (O::*setter)(T const &), T (O::*getter)() const)
65 : name(name), setter_([&obj, setter](Variant const &v) {
66 (obj.get()->*setter)(get_value<T>(v));
67 }),
68 getter_([&obj, getter]() { return (obj.get()->*getter)(); }) {}
69
70 /** @brief Read-only parameter that is bound to an object.
71 *
72 * @param name The name the parameter should be bound to in the interface.
73 * @param obj The object the parameter should be bound to.
74 * @param getter The getter.
75 */
76 template <typename T, class O>
77 AutoParameter(const char *name, std::shared_ptr<O> &obj,
78 T const &(O::*getter)())
79 : name(name), setter_([](Variant const &) { throw WriteError{}; }),
80 getter_([&obj, getter]() { return (obj.get()->*getter)(); }) {}
81
82 /** @brief Read-only parameter that is bound to an object.
83 * @overload
84 */
85 template <typename T, class O>
86 AutoParameter(const char *name, std::shared_ptr<O> &obj,
87 T (O::*getter)() const)
88 : name(name), setter_([](Variant const &) { throw WriteError{}; }),
89 getter_([&obj, getter]() { return (obj.get()->*getter)(); }) {}
90
91 /** @brief Read-only parameter that is bound to an object.
92 * @overload
93 */
94 template <typename T, class O>
95 AutoParameter(const char *name, std::shared_ptr<O> &obj, T O::*getter)
96 : name(name), setter_([](Variant const &) { throw WriteError{}; }),
97 getter_([&obj, getter]() { return (obj.get()->*getter)(); }) {}
98
99 /** @brief Read-write parameter that is bound to an object.
100 * @overload
101 */
102 template <typename T, class O>
103 AutoParameter(const char *name, std::shared_ptr<O> &obj,
104 T &(O::*getter_setter)())
105 : name(name), setter_([&obj, getter_setter](Variant const &v) {
106 (obj.get()->*getter_setter)() = get_value<T>(v);
107 }),
108 getter_([&obj, getter_setter]() {
109 return (obj.get()->*getter_setter)();
110 }) {}
111
112 /** @brief Read-write parameter that is bound to an object.
113 *
114 * @param name The name the parameter should be bound to in the interface.
115 * @param binding The reference the parameter should be bound to.
116 */
117 template <typename T>
118 AutoParameter(const char *name, T &binding)
119 : name(name),
120 setter_([&binding](Variant const &v) { binding = get_value<T>(v); }),
121 getter_([&binding]() { return Variant{binding}; }) {}
122
123 /** @brief Read-only parameter that is bound to an object.
124 * @overload
125 */
126 template <typename T>
127 AutoParameter(const char *name, T const &binding)
128 : name(name), setter_([](Variant const &) { throw WriteError{}; }),
129 getter_([&binding]() -> Variant { return binding; }) {}
130
131 /**
132 * @brief Read-write parameter with a user-provided getter and setter.
133 *
134 * @param name The name the parameter should be bound to in the interface.
135 * @param set A setter, which can be a Functor, a Lambda or a std::function
136 * that accepts a @c Variant const&.
137 * @param get A getter, which can be a Functor, a Lambda or a std::function
138 * that return the parameter. The return type must be convertible
139 * to Variant.
140 */
141 template <typename Setter, typename Getter>
142 AutoParameter(const char *name, Setter const &set, Getter const &get)
143 : name(name), setter_(set), getter_(get) {}
144
145 /**
146 * @brief Read-only parameter with a user-provided getter.
147 * @overload
148 */
149 template <typename Getter>
150 AutoParameter(const char *name, ReadOnly, Getter const &get)
151 : name(name), setter_([](Variant const &) { throw WriteError{}; }),
152 getter_(get) {}
153
154 /** The interface name. */
155 const std::string name;
156
157 /**
158 * @brief Set the parameter.
159 */
160 const std::function<void(Variant const &)> setter_;
161 /**
162 * @brief Get the parameter.
163 */
164 const std::function<Variant()> getter_;
165
166 void set(Variant const &v) const { setter_(v); }
167
168 Variant get() const { return getter_(); }
169};
170} // namespace ScriptInterface
171
172#endif
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
Description and getter/setter for a parameter.
const std::string name
The interface name.
AutoParameter(const char *name, ReadOnly, Getter const &get)
Read-only parameter with a user-provided getter.
AutoParameter(const char *name, std::shared_ptr< O > &obj, T const &(O::*getter)())
Read-only parameter that is bound to an object.
AutoParameter(const char *name, std::shared_ptr< O > &obj, T(O::*getter)() const)
Read-only parameter that is bound to an object.
AutoParameter(const char *name, std::shared_ptr< O > &obj, T O::*getter)
Read-only parameter that is bound to an object.
const std::function< void(Variant const &)> setter_
Set the parameter.
void set(Variant const &v) const
AutoParameter(const char *name, T &binding)
Read-write parameter that is bound to an object.
AutoParameter(const char *name, std::shared_ptr< O > &obj, void(O::*setter)(T const &), T const &(O::*getter)() const)
Read-write parameter that is bound to an object.
AutoParameter(const char *name, std::shared_ptr< O > &obj, T &(O::*getter_setter)())
Read-write parameter that is bound to an object.
AutoParameter(const char *name, Setter const &set, Getter const &get)
Read-write parameter with a user-provided getter and setter.
const std::function< Variant()> getter_
Get the parameter.
static constexpr const ReadOnly read_only
AutoParameter(const char *name, std::shared_ptr< O > &obj, void(O::*setter)(T const &), T(O::*getter)() const)
Read-write parameter that is bound to an object.
AutoParameter(const char *name, T const &binding)
Read-only parameter that is bound to an object.