1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
// ------------------------------------------------------------------------
//
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) 2019 - 2023 by the deal.II authors
//
// This file is part of the deal.II library.
//
// Part of the source code is dual licensed under Apache-2.0 WITH
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
// governing the source code and code contributions can be found in
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
//
// ------------------------------------------------------------------------
#ifndef dealii_base_mutable_bind_h
#define dealii_base_mutable_bind_h
#include <deal.II/base/config.h>
#include <deal.II/base/patterns.h>
#include <tuple>
#include <utility>
DEAL_II_NAMESPACE_OPEN
namespace Utilities
{
/**
* A mutable version of std::bind, that binds all arguments of a function
* pointer to a stored tuple, and allows you to update the tuple between
* calls.
*
* An example usage of this class is through the helper function
* mutable_bind() that creates a MutableBind object on the fly, based on its
* arguments:
*
* @code
* void my_function(const int &a, const double &b);
*
* auto bound = mutable_bind(my_function, 1, 2.0);
*
* bound(); // will execute my_function(1, 2.0);
*
* bound.set_arguments(2, 3.0);
* bound(); // will execute my_function(2, 3.0);
*
* bound.parse_arguments("3: 4.0");
* bound(); // will execute my_function(3, 4.0);
* @endcode
*
* The arguments are copied to the tuple, with their reference and const
* attributes removed. Only copy constructible objects are allowed as
* function arguments. If you need to keep some references around, you may
* wrap your function into a lambda function:
*
* @code
* void
* example_function(const Point<2> &p,
* const double &d,
* const unsigned int i = 3) {
* ...
* };
*
* const Point<2> p(1, 2);
*
* Utilities::MutableBind<void, double, unsigned int> exp = {
* [&p](const double &d,
* const unsigned int i)
* {
* example_function(p, d, i);
* },
* {}};
*
* exp.parse_arguments("3.0 : 4");
* exp(); // calls example_function(p, 3.0, 4);
* @endcode
*/
template <typename ReturnType, class... FunctionArgs>
class MutableBind
{
public:
/**
* An alias to the stored std::tuple type. Only copy constructible
* objects are allowed as tuple members.
*/
using TupleType =
std::tuple<std::remove_cv_t<std::remove_reference_t<FunctionArgs>>...>;
/**
* Construct a MutableBind object specifying the function, and
* each arguments separately.
*/
template <typename FunctionType>
MutableBind(FunctionType function, FunctionArgs &&...arguments);
/**
* Construct a MutableBind object specifying the function, and
* the arguments as a tuple.
*/
template <typename FunctionType>
MutableBind(FunctionType function, TupleType &&arguments);
/**
* Construct a MutableBind object specifying only the function. By default,
* the arguments are left to their default constructor values.
*/
template <typename FunctionType>
MutableBind(FunctionType function);
/**
* Call the original function, passing as arguments the elements of the
* tuple of bound arguments.
*/
ReturnType
operator()() const;
/**
* Set the arguments to use in @p function, for next time
* operator()() is called, using move semantic.
*/
void
set_arguments(TupleType &&arguments);
/**
* Set the arguments to use in @p function, for next time
* operator()() is called, using move semantic.
*/
void
set_arguments(FunctionArgs &&...arguments);
/**
* Parse the arguments to use in @p function from a string, for next time
* operator()() is called.
*
* The conversion is performed using a user supplied Patterns::PatternBase
* object. By default, Patterns::Tools::Convert<TupleType>::to_pattern() is
* used to determine how to convert from @p value_string to a TupleType
* object.
*
* @param value_string The string to convert from
* @param pattern A unique pointer to the pattern to use when performing
* the conversion
*/
void
parse_arguments(const std::string &value_string,
const Patterns::PatternBase &pattern =
*Patterns::Tools::Convert<TupleType>::to_pattern());
private:
/**
* An std::function that stores the original function.
*/
const std::function<ReturnType(FunctionArgs...)> function;
/**
* Currently stored arguments. These are forwarded to the function object
* above, when calling operator()().
*/
TupleType arguments;
};
/**
* Create a MutableBind object from a function pointer and a list of
* arguments.
*
* An example usage is given by:
* @code
* void my_function(const int &a, const double &b);
*
* auto bound = mutable_bind(my_function, 1, 2.0);
*
* bound(); // will execute my_function(1, 2.0);
*
* bound.set_arguments(2, 3.0);
* bound(); // will execute my_function(2, 3.0);
*
* bound.parse_arguments("3: 4.0");
* bound(); // will execute my_function(3, 4.0);
* @endcode
*/
template <typename ReturnType, class... FunctionArgs>
MutableBind<ReturnType, FunctionArgs...>
mutable_bind(ReturnType (*function)(FunctionArgs...),
std_cxx20::type_identity_t<FunctionArgs> &&...arguments);
/**
* Same as above, using a std::function object.
*/
template <typename ReturnType, class... FunctionArgs>
MutableBind<ReturnType, FunctionArgs...>
mutable_bind(std::function<ReturnType(FunctionArgs...)>,
std_cxx20::type_identity_t<FunctionArgs> &&...arguments);
/**
* Create a MutableBind object from a function pointer, with uninitialized
* arguments.
*
* Notice that if you do not call one of the MutableBind::set_arguments()
* methods, or the MutableBind::parse_arguments() function on the returned
* object, then the arguments passed to the function object will be
* initialized with the values coming from each of the arguments' default
* constructors.
*/
template <typename ReturnType, class... FunctionArgs>
MutableBind<ReturnType, FunctionArgs...>
mutable_bind(ReturnType (*function)(FunctionArgs...));
/**
* Same as above, using a std::function object.
*/
template <typename ReturnType, class... FunctionArgs>
MutableBind<ReturnType, FunctionArgs...>
mutable_bind(std::function<ReturnType(FunctionArgs...)>);
#ifndef DOXYGEN
template <typename ReturnType, class... FunctionArgs>
template <typename FunctionType>
MutableBind<ReturnType, FunctionArgs...>::MutableBind(
FunctionType function,
FunctionArgs &&...arguments)
: function(function)
, arguments(std::make_tuple(std::move(arguments)...))
{}
template <typename ReturnType, class... FunctionArgs>
template <typename FunctionType>
MutableBind<ReturnType, FunctionArgs...>::MutableBind(FunctionType function,
TupleType &&arguments)
: function(function)
, arguments(std::move(arguments))
{}
template <typename ReturnType, class... FunctionArgs>
template <typename FunctionType>
MutableBind<ReturnType, FunctionArgs...>::MutableBind(FunctionType function)
: function(function)
{}
template <typename ReturnType, class... FunctionArgs>
ReturnType
MutableBind<ReturnType, FunctionArgs...>::operator()() const
{
return std::apply(function, arguments);
}
template <typename ReturnType, class... FunctionArgs>
void
MutableBind<ReturnType, FunctionArgs...>::set_arguments(
FunctionArgs &&...args)
{
arguments = std::make_tuple(std::move(args)...);
}
template <typename ReturnType, class... FunctionArgs>
void
MutableBind<ReturnType, FunctionArgs...>::set_arguments(TupleType &&args)
{
arguments = std::move(args);
}
template <typename ReturnType, class... FunctionArgs>
void
MutableBind<ReturnType, FunctionArgs...>::parse_arguments(
const std::string &value_string,
const Patterns::PatternBase &pattern)
{
arguments =
Patterns::Tools::Convert<TupleType>::to_value(value_string, pattern);
}
template <typename ReturnType, class... FunctionArgs>
MutableBind<ReturnType, FunctionArgs...>
mutable_bind(ReturnType (*function)(FunctionArgs...),
std_cxx20::type_identity_t<FunctionArgs> &&...arguments)
{
return MutableBind<ReturnType, FunctionArgs...>(function,
std::move(arguments)...);
}
template <typename ReturnType, class... FunctionArgs>
MutableBind<ReturnType, FunctionArgs...>
mutable_bind(ReturnType (*function)(FunctionArgs...))
{
return MutableBind<ReturnType, FunctionArgs...>(function);
}
template <typename ReturnType, class... FunctionArgs>
MutableBind<ReturnType, FunctionArgs...>
mutable_bind(std::function<ReturnType(FunctionArgs...)> function,
std_cxx20::type_identity_t<FunctionArgs> &&...arguments)
{
return MutableBind<ReturnType, FunctionArgs...>(function,
std::move(arguments)...);
}
template <typename ReturnType, class... FunctionArgs>
MutableBind<ReturnType, FunctionArgs...>
mutable_bind(std::function<ReturnType(FunctionArgs...)> function)
{
return MutableBind<ReturnType, FunctionArgs...>(function);
}
#endif
} // namespace Utilities
DEAL_II_NAMESPACE_CLOSE
#endif
|