MySQL 26.7.0
Source Code Documentation
ut0function_reference.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2026, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify it under
4the terms of the GNU General Public License, version 2.0, as published by the
5Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
18for more details.
19
20You should have received a copy of the GNU General Public License along with
21this program; if not, write to the Free Software Foundation, Inc.,
2251 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#pragma once
26#include <utility>
27namespace ut {
28template <typename>
30
31/** A non-owning reference to a callable - a free function, like `sin`, or the
32operator() of an object, together with pointer to that object, such a Less
33functor or a lambda.
34It is the users responsibility to ensure that the lifetime of the callable
35referenced covers the lifetime of this Function_reference.
36This is the price to be paid for the benefit that this object has fixed size
37(just two pointers) and does not require dynamic allocation, which makes it
38useful for callbacks and type erasure, in places where std::function would be to
39expensive due to its need to copy the (variable size) object to dynamically
40allocated space.*/
41template <typename Return_type, typename... Args>
42class Function_reference<Return_type(Args...)> {
43 public:
44 /* Most compilers allow storing pointer to function in pointer to obj, but
45 this is not guaranteed by the C++17 standard, so we use a union. */
46 union context_t {
47 /* All function pointers are interchangeable, so lets use the simplest one.
48 see https://stackoverflow.com/a/61617252/575699. The function pointer is
49 first member of the union to assure one of the types will not cast to the
50 first member's type in case someone uses {val} constructor, which is not
51 able to init the second member of the union. Use C++20 designated
52 initializers instead. */
53 void (*fn_ptr)();
54 void *obj_ptr;
55 };
56 using signature_t = Return_type(Args...);
57 using ptr_t = Return_type (*)(const context_t &, Args...);
58
59 private:
60 /** A crucial context for making a call to the referenced function.
61 In case of an object's method, that would be the pointer to the object.
62 In case of a free function, this is just the free function itself. */
63 context_t m_context;
64
65 /** How to call the referenced function given the m_context.
66 In case of an object's method, that would be a state-less non-capturing lambda
67 which just calls the m_context->operator() with provided args.
68 In case of a free function this will simply call m_context with provided args.
69 It is a non-capturing lambda, i.e. lambda which does not require "this"
70 pointer to be called because it has no state - for such a lambda one can take
71 its address and store it (in m_call_with_context) and subsequently use it to
72 call it as a regular function, passing m_context as the first argument.
73 This is a crucial trick: instead of storing the pointer to operator() as a
74 pointer-to-member of Callable, which would require the Function_reference type
75 to depend on Callable, we hide any mention of Callable inside the lambda. */
77
78 public:
79 /** Creates a reference to the callable->operator().
80 @param[in] callable
81 An object with operator() which you want to reference.
82 Note that the constness of callable impacts constness of
83 the operator() which will be picked if there are two.
84 For example a lambda.
85 This object must have life-time covering the life-time of
86 Function_reference.
87 */
88 template <typename Callable>
89 Function_reference(Callable *callable)
90 : m_context{.obj_ptr = (void *)callable},
91 m_call_with_context([](const context_t &t, Args... args) {
92 return (static_cast<Callable *>(t.obj_ptr)->operator())(
93 std::forward<Args>(args)...);
94 }) {}
95
96 using rawfn_ptr_t = Return_type (*)(Args...);
97
98 /** Creates a reference to a free function fn_ptr.
99 @param[in] fn_ptr
100 A pointer to a free function which you want to reference */
102 : m_context{.fn_ptr = fn_ptr},
103 m_call_with_context([](const context_t &t, Args... args) {
104 return (reinterpret_cast<Return_type (*)(Args...)>(t.fn_ptr))(
105 std::forward<Args>(args)...);
106 }) {}
107
108 Return_type operator()(Args... args) const {
109 return m_call_with_context(m_context, std::forward<Args>(args)...);
110 }
111};
112} // namespace ut
Function_reference(Callable *callable)
Creates a reference to the callable->operator().
Definition: ut0function_reference.h:89
context_t m_context
A crucial context for making a call to the referenced function.
Definition: ut0function_reference.h:63
Return_type(*)(const context_t &, Args...) ptr_t
Definition: ut0function_reference.h:57
ptr_t m_call_with_context
How to call the referenced function given the m_context.
Definition: ut0function_reference.h:76
Return_type(*)(Args...) rawfn_ptr_t
Definition: ut0function_reference.h:96
Return_type(Args...) signature_t
Definition: ut0function_reference.h:56
Function_reference(rawfn_ptr_t fn_ptr)
Creates a reference to a free function fn_ptr.
Definition: ut0function_reference.h:101
Return_type operator()(Args... args) const
Definition: ut0function_reference.h:108
Definition: ut0function_reference.h:29
This file contains a set of libraries providing overloads for regular dynamic allocation routines whi...
Definition: aligned_alloc.h:48
void * obj_ptr
Definition: ut0function_reference.h:54