From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2205) id 076CE3858294; Fri, 5 Aug 2022 14:13:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 076CE3858294 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom de Vries To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Introduce gdb::make_function_view X-Act-Checkin: binutils-gdb X-Git-Author: Pedro Alves X-Git-Refname: refs/heads/master X-Git-Oldrev: 5ee285ca3e5cca998c76ca1c92927008849ff00e X-Git-Newrev: 377c3a9c91785d5df5c0d96121160e6210204cc0 Message-Id: <20220805141302.076CE3858294@sourceware.org> Date: Fri, 5 Aug 2022 14:13:02 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 05 Aug 2022 14:13:02 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D377c3a9c9178= 5d5df5c0d96121160e6210204cc0 commit 377c3a9c91785d5df5c0d96121160e6210204cc0 Author: Pedro Alves Date: Fri Aug 5 16:12:56 2022 +0200 Introduce gdb::make_function_view =20 This adds gdb::make_function_view, which lets you create a function view from a callable without specifying the function_view's template parameter. For example, this: =20 auto lambda =3D [&] (int) { ... }; auto fv =3D gdb::make_function_view (lambda); =20 instead of: =20 auto lambda =3D [&] (int) { ... }; gdb::function_view fv =3D lambda; =20 It is particularly useful if you have a template function with an optional function_view parameter, whose type depends on the function's template parameters. Like: =20 template void my_function (T v, gdb::function_view callback =3D nul= lptr); =20 For such a function, the type of the callback argument you pass must already be a function_view. I.e., this wouldn't compile: =20 auto lambda =3D [&] (int) { ... }; my_function (1, lambda); =20 With gdb::make_function_view, you can write the call like so: =20 auto lambda =3D [&] (int) { ... }; my_function (1, gdb::make_function_view (lambda)); =20 Unit tests included. =20 Tested by building with GCC 9.4, Clang 10, and GCC 4.8.5, on x86_64 GNU/Linux, and running the unit tests. =20 Change-Id: I5c4b3b4455ed6f0d8878cf1be189bea3ee63f626 Diff: --- gdb/unittests/function-view-selftests.c | 82 ++++++++++++++++++++- gdbsupport/function-view.h | 127 ++++++++++++++++++++++++++++= ++++ 2 files changed, 208 insertions(+), 1 deletion(-) diff --git a/gdb/unittests/function-view-selftests.c b/gdb/unittests/functi= on-view-selftests.c index 7af0245c570..726c22323a8 100644 --- a/gdb/unittests/function-view-selftests.c +++ b/gdb/unittests/function-view-selftests.c @@ -61,7 +61,7 @@ struct plus_one_int_func_obj }; =20 static void -run_tests () +test_function_view () { /* A simple lambda. */ auto plus_one_lambda =3D [] (int val) { return ++val; }; @@ -168,6 +168,86 @@ run_tests () SELF_CHECK (!check_op_eq_null); } =20 +/* A template function where the function_view type is dependent on a + template parameter. */ + +template +static int +tmpl_func (T val, gdb::function_view callback) +{ + return callback (val) + 1; +} + +static int +make_fv_test_func (int val) +{ + return val + 1; +} + +/* A function object with const operator(). */ + +struct func_obj_const_op +{ + int operator() (int val) const + { + return val + 1; + } +}; + +/* A function object with non-const operator(). */ + +struct func_obj_non_const_op +{ + int operator() (int val) + { + return val + 1; + } +}; + +static void +test_make_function_view () +{ + /* Function reference. */ + SELF_CHECK (3 =3D=3D tmpl_func (1, gdb::make_function_view (make_fv_test= _func))); + + /* Function pointer. */ + SELF_CHECK (3 =3D=3D tmpl_func (1, gdb::make_function_view (&make_fv_tes= t_func))); + + /* Reference to const and non-const function pointers. */ + typedef int (*func_ptr) (int); + func_ptr ptr =3D make_fv_test_func; + const func_ptr cptr =3D make_fv_test_func; + SELF_CHECK (3 =3D=3D tmpl_func (1, gdb::make_function_view (ptr))); + SELF_CHECK (3 =3D=3D tmpl_func (1, gdb::make_function_view (cptr))); + + /* Lambdas. */ + + auto lambda =3D [] (int val) -> int { return val + 1; }; + + /* This wouldn't compile, since tmpl_func is a template and its + function_view argument's callable type is a dependent type. The + passed argument must be of the exact type of the function's + parameter. */ + // SELF_CHECK (3 =3D=3D tmpl_func (1, lambda)); + + SELF_CHECK (3 =3D=3D tmpl_func (1, gdb::make_function_view (lambda))); + + /* Regular function objects. */ + + func_obj_non_const_op fobj; + SELF_CHECK (3 =3D=3D tmpl_func (1, gdb::make_function_view (fobj))); + + func_obj_const_op cfobj; + SELF_CHECK (3 =3D=3D tmpl_func (1, gdb::make_function_view (cfobj))); +} + +static void +run_tests () +{ + test_function_view (); + test_make_function_view (); +} + } /* namespace function_view */ } /* namespace selftests */ =20 diff --git a/gdbsupport/function-view.h b/gdbsupport/function-view.h index 1875cd104b7..9f8a8680cf4 100644 --- a/gdbsupport/function-view.h +++ b/gdbsupport/function-view.h @@ -148,6 +148,47 @@ =20 iterate_over_foos (process_one_foo); =20 + There's also a gdb::make_function_view function that you can use to + automatically create a function_view from a callable without having + to specify the function_view's template parameter. E.g.: + + auto lambda =3D [&] (int) { ... }; + auto fv =3D gdb::make_function_view (lambda); + + This can be useful for example when calling a template function + whose function_view parameter type depends on the function's + template parameters. In such case, you can't rely on implicit + callable->function_view conversion for the function_view argument. + You must pass a function_view argument already of the right type to + the template function. E.g., with this: + + template + void my_function (T v, gdb::function_view callback =3D nullpt= r); + + this wouldn't compile: + + auto lambda =3D [&] (int) { ... }; + my_function (1, lambda); + + Note that this immediately dangles the temporary lambda object: + + gdb::function_view fv =3D [&] (int) { ... }; // dangles + my_function (fv); + + To avoid the dangling you'd have to use a named temporary for the + lambda: + + auto lambda =3D [&] (int) { ... }; + gdb::function_view fv =3D lambda; + my_function (fv); + + Using gdb::make_function_view instead automatically deduces the + function_view's full type, and, avoids worrying about dangling. For + the example above, we could write instead: + + auto lambda =3D [&] (int) { ... }; + my_function (1, gdb::make_function_view (lambda)); + You can find unit tests covering the whole API in unittests/function-view-selftests.c. */ =20 @@ -318,6 +359,92 @@ constexpr inline bool operator!=3D (std::nullptr_t, const function_view &f) noexc= ept { return static_cast (f); } =20 +namespace fv_detail { + +/* Helper traits type to automatically find the right function_view + type for a callable. */ + +/* Use partial specialization to get access to the callable's + signature, for all the different callable variants. */ + +template +struct function_view_traits; + +/* Main partial specialization with plain function signature type. + All others end up redirected here. */ +template +struct function_view_traits +{ + using type =3D gdb::function_view; +}; + +/* Function pointers. */ +template +struct function_view_traits + : function_view_traits +{ +}; + +/* Function references. */ +template +struct function_view_traits + : function_view_traits +{ +}; + +/* Reference to function pointers. */ +template +struct function_view_traits + : function_view_traits +{ +}; + +/* Reference to const function pointers. */ +template +struct function_view_traits + : function_view_traits +{ +}; + +/* Const member functions. function_view doesn't support these, but + we need this in order to extract the type of function objects. + Lambdas pass here, after starting at the operator() case, + below. */ +template +struct function_view_traits + : function_view_traits +{ +}; + +/* Member functions. Ditto, for function objects with non-const + operator(). */ +template +struct function_view_traits + : function_view_traits +{ +}; + +/* Function objects, lambdas, std::function, any type that defines + operator(). */ +template +struct function_view_traits + : function_view_traits ::type::operator())> +{ +}; + +} /* namespace fv_detail */ + +/* Make a function_view from a callable. Useful to automatically + deduce the function_view's template argument type. */ +template +auto make_function_view (Callable &&callable) + -> typename fv_detail::function_view_traits::type +{ + using fv =3D typename fv_detail::function_view_traits::type; + return fv (std::forward (callable)); +} + } /* namespace gdb */ =20 #endif