29#if !FUNCTIONAL_PROVIDED
31 static std::string
impl_name =
"jau/functional.hpp";
34#ifndef FUNCTIONAL_IMPL
35 #define FUNCTIONAL_IMPL 1
51 fprintf(stderr,
"Implementation: functional %d, is_rtti_available %d, limited_lambda_id %d\n",
61 fprintf(stderr,
"lambda.ref: %s\n", fa0.toString().c_str());
69 fprintf(stderr,
"lambda.copy: %s\n", fa0.toString().c_str());
78 fprintf(stderr,
"lambda.plain: %s\n", f_1.toString().c_str());
81#if ( FUNCTIONAL_IMPL == 1 )
91 fprintf(stderr,
"ylambda.plain1 %s\n", f_1.toString().c_str());
100 return x * self(x-1);
103 fprintf(stderr,
"ylambda.plain2 %s\n", f_1.toString().c_str());
109 typedef void(*cfunc)();
113 fprintf(stderr,
"freeA.0 %s\n", fl_0.toString().c_str());
118 jau::function<int(
int)> f2a_0(
this, &TestFunction01::func02a_member);
119 fprintf(stderr,
"member: %s\n", f2a_0.toString().c_str());
124 jau::function<void(
int&,
int)> f2a_0(
this, &TestFunction01::func12a_member);
125 fprintf(stderr,
"member: %s\n", f2a_0.toString().c_str());
132 typedef int(*cfunc)(
int&, int);
135 (cfunc) ( [](
int& capture,
int i)->
int {
136 int res = i+10000+capture;
139 fprintf(stderr,
"capval.small: %s\n", f5_o100_1.toString().c_str());
150 bool operator==(
const blob& rhs)
const noexcept {
151 return offset100 == rhs.offset100 &&
152 lala0 == rhs.lala0 &&
153 lala1 == rhs.lala1 &&
154 lala2 == rhs.lala2 &&
157 bool operator!=(
const blob& rhs)
const noexcept
158 {
return !( *
this == rhs ); }
163 typedef int(*cfunc)(blob&, int);
166 (cfunc) ( [](blob& capture,
int i)->
int {
167 int res = i+10000+capture.offset100;
170 fprintf(stderr,
"capval.big: %s\n", f5_o100_1.toString().c_str());
176 typedef int(*cfunc)(
int*, int);
178 jau::function<int(
int)> f7_o100_1 = jau::bind_capref<int, int, int>(&offset100,
179 (cfunc) ( [](
int* capture,
int i)->
int {
180 int res = i+10000+(*capture);
183 fprintf(stderr,
"capref: %s\n", f7_o100_1.toString().c_str());
188 std::function<int(
int i)> func4a_stdlambda = [](
int i)->
int {
193 fprintf(stderr,
"std.lambda pl: %s\n", f.toString().c_str());
194 fprintf(stderr,
" (net std.lambda): sizeof %zu\n",
sizeof(func4a_stdlambda));
199 volatile int i = 100;
200 std::function<int(
int)> func4a_stdlambda = [&](
int a) ->
int {
205 fprintf(stderr,
"std.lambda cp: %s\n", f.toString().c_str());
206 fprintf(stderr,
" (net std.lambda): sizeof %zu\n",
sizeof(func4a_stdlambda));
211 INFO(
"Test 00_usage: START");
215 BENCHMARK(
"free_rawfunc") {
217 for(
int i=0; i<
loops; ++i) {
218 r = r + TestFunction01::Func03a_static(i);
226 native_func_t f = TestFunction01::Func03a_static;
228 BENCHMARK(
"free_cfuncptr") {
230 for(
int i=0; i<
loops; ++i) {
239 std::function<int(
int)> f = TestFunction01::Func03a_static;
241 BENCHMARK(
"free_stdfunc") {
243 for(
int i=0; i<
loops; ++i) {
254 BENCHMARK(
"free_jaufunc") {
256 for(
int i=0; i<
loops; ++i) {
265 BENCHMARK(
"member_rawfunc") {
267 for(
int i=0; i<
loops; ++i) {
268 r = r + func02a_member(i);
276 using namespace std::placeholders;
277 auto f = std::bind(&TestFunction01::func02a_member,
this, _1);
279 BENCHMARK(
"member_stdbind_unspec") {
281 for(
int i=0; i<
loops; ++i) {
292 BENCHMARK(
"member_jaufunc") {
294 for(
int i=0; i<
loops; ++i) {
305 int(*func5a_capture)(
int&, int) = [](
int& capture,
int i)->int {
311 BENCHMARK(
"capval_small_jaufunc") {
313 for(
int i=0; i<
loops; ++i) {
329 bool operator==(
const blob& rhs)
const noexcept {
330 return offset100 == rhs.offset100 &&
331 lala0 == rhs.lala0 &&
332 lala1 == rhs.lala1 &&
333 lala2 == rhs.lala2 &&
336 bool operator!=(
const blob& rhs)
const noexcept
337 {
return !( *
this == rhs ); }
342 typedef int(*cfunc)(blob&, int);
345 (cfunc) ( [](blob& capture,
int i)->
int {
346 int res = i+10000+capture.offset100;
350 BENCHMARK(
"capval_big_jaufunc") {
352 for(
int i=0; i<
loops; ++i) {
363 int(*func7a_capture)(
int*, int) = [](
int* capture,
int i)->int {
364 int res = i+*capture;
369 BENCHMARK(
"capref_jaufunc") {
371 for(
int i=0; i<
loops; ++i) {
380 std::function<int(
int i)> f = [](
int i)->
int {
385 BENCHMARK(
"lambda_plain_std_function") {
387 for(
int i=0; i<
loops; ++i) {
400 BENCHMARK(
"lambda_plain_jaufunc") {
402 for(
int i=0; i<
loops; ++i) {
411 volatile int captured = 100;
413 std::function<int(
int)> f = [&](
int a) ->
int {
417 BENCHMARK(
"lambda_capt_std_function") {
419 for(
int i=0; i<
loops; ++i) {
428 volatile int captured = 100;
434 BENCHMARK(
"lambda_capt_jaufunc") {
436 for(
int i=0; i<
loops; ++i) {
442#if ( FUNCTIONAL_IMPL == 1 )
449 BENCHMARK(
"ylambda_none_jaufunc") {
451 for(
int i=0; i<
loops; ++i) {
460 REQUIRE(
true ==
true );
462 INFO(
"Test 00_usage: END");
467 typedef int(*native_func_t)(int);
468 typedef std::function<int(
int)> std_func_t;
472 typedef int(*MyCFunc0)(int);
478 int func02a_member(
int i) {
482 int func02b_member(
int i)
noexcept {
486 static int Func03a_static(
int i) {
490 static int Func03b_static(
int i)
noexcept {
495 void func12a_member(
int& r,
const int i) {
498 void func12b_member(
int& r,
const int i)
noexcept {
501 static void Func13a_static(
int& r,
const int i) {
504 static void Func13b_static(
int& r,
const int i)
noexcept {
508 void func20a_member() {
511 static void Func20a_static() {
void test00_usage()
Unit test covering most variants of jau::function<R(A...)
Class template jau::function is a general-purpose static-polymorphic function wrapper.
static constexpr const bool limited_lambda_id
Static constexpr boolean indicating whether resulting type_info uniqueness is limited for lambda func...
bool operator!=(const alphabet &lhs, const alphabet &rhs) noexcept
std::string to_string(const alphabet &v) noexcept
bool operator==(const alphabet &lhs, const alphabet &rhs) noexcept
consteval_cxx20 bool is_rtti_available() noexcept
Returns true if compiled with RTTI available.
jau::function< R(A...)> bind_member(C1 *base, R(C0::*mfunc)(A...)) noexcept
Bind given class instance and non-void member function to an anonymous function using func_member_tar...
jau::function< R(A...)> bind_free(R(*func)(A...)) noexcept
Bind given non-void free-function to an anonymous function using func::free_target_t.
jau::function< R(A...)> bind_std(uint64_t id, std::function< R(A...)> func) noexcept
Bind given non-void std::function to an anonymous function using func::std_target_t.
jau::function< R(A...)> bind_capval(const I &data, R(*func)(I &, A...)) noexcept
Bind given data by copying the captured value and the given non-void function to an anonymous functio...
jau::function< R(A...)> bind_capref(I *data_ptr, R(*func)(I *, A...)) noexcept
Bind given data by passing the captured reference (pointer) to the value and non-void function to an ...
@ lambda
Denotes a func::lambda_target_t.
@ capref
Denotes a func::capref_target_t.
@ member
Denotes a func::member_target_t.
@ free
Denotes a func::free_target_t.
@ std
Denotes a func::std_target_t.
@ ylambda
Denotes a func::ylambda_target_t.
METHOD_AS_TEST_CASE(TestFunction01::test00_usage, "00_usage")
static std::string impl_name