Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
FunctorUtils.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* FunctorUtils.h (C) 2000-2018 */
9/* */
10/* Utility functions for functors. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_FUNCTORUTILS_H
13#define ARCANE_UTILS_FUNCTORUTILS_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/base/Functor.h"
19
20/*---------------------------------------------------------------------------*/
21/*---------------------------------------------------------------------------*/
22
23namespace Arcane::functor
24{
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29//! Specialization for a lambda function with one argument
30template <typename LambdaType, typename A1> StdFunctorWithArgumentT<A1>
31make(const LambdaType& f, void (LambdaType::*)(A1 r) const)
32{
33 return StdFunctorWithArgumentT<A1>(f);
34}
35
36/*!
37 * \brief Creates and returns a functor for the lambda function \a f.
38 *
39 * The lambda \a f must correspond to a function with the
40 * prototype void(ArgType).
41 * The returned object is of type IFunctorWithArgumentT<ArgType> with
42 * \a ArgType being the only parameter of \a f.
43 */
44template <typename LambdaType> auto
45make(const LambdaType& f) -> decltype(make(f, &LambdaType::operator()))
46{
47 return make(f, &LambdaType::operator());
48}
49
50//! Specialization for a lambda function with one argument
51template <typename LambdaType, typename A1> StdFunctorWithArgumentT<A1>*
52makePointer(const LambdaType& f, void (LambdaType::*)(A1 r) const)
53{
54 return new StdFunctorWithArgumentT<A1>(f);
55}
56
57/*!
58 * \brief Creates and returns a pointer to a functor for the lambda function \a f.
59 *
60 * The lambda \a f must correspond to a function with the
61 * prototype void(ArgType).
62 * The returned pointer is of type IFunctorWithArgumentT<ArgType> with
63 * \a ArgType being the only parameter of \a f.
64 * The returned pointer must be destroyed by the delete operator.
65 */
66template <typename LambdaType> auto
67makePointer(const LambdaType& f) -> decltype(makePointer(f, &LambdaType::operator()))
68{
69 return makePointer(f, &LambdaType::operator());
70}
71
72/*---------------------------------------------------------------------------*/
73/*---------------------------------------------------------------------------*/
74
75/*!
76 * \brief Applies a functor derived from a lambda function to a given method.
77 *
78 * This function allows a lambda function to be applied directly
79 * to a method that takes an instance of
80 * IFunctorWithArgumentT.
81 *
82 * For example, if we have the following class:
83 *
84 * \code
85 * class A { void visit(IFunctorWithArgumentT<int>*); };
86 * \encode
87 *
88 * It can be used as follows:
89 *
90 * \code
91 * void f()
92 * {
93 * A a;
94 * auto f = [](int x) { std::cout << "X=" << x << '\n'; };
95 *
96 * functor::apply(&a,&A::visit,f);
97 * }
98 * \endcode
99 */
100template <typename LambdaType, typename T, typename ArgType> void
101apply(T* x, void (T::*ptr)(IFunctorWithArgumentT<ArgType>*), const LambdaType& f)
102{
103 auto xstr = make(f);
104 (x->*ptr)(&xstr);
105}
106
107/*---------------------------------------------------------------------------*/
108/*---------------------------------------------------------------------------*/
109
110} // namespace Arcane::functor
111
112/*---------------------------------------------------------------------------*/
113/*---------------------------------------------------------------------------*/
114
115#endif
Declarations of types used in Arcane.