Skip to content

Commit 2881268

Browse files
committed
cpp callback example added
1 parent e09c354 commit 2881268

File tree

4 files changed

+46
-33
lines changed

4 files changed

+46
-33
lines changed

async-call-helper/async-call-helper.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ class async_call_helper
7373
template <typename ...Args>
7474
struct callback_context {
7575
void *context;
76-
void (*callback)(Args...);
76+
void (*callback)(void*, Args...);
7777
void operator()(Args... args) noexcept {
78-
std::invoke(callback, std::forward<Args>(args)...);
78+
std::invoke(callback, context, std::forward<Args>(args)...);
7979
}
8080
};
8181

8282
template <typename ...Args, typename Fn>
83-
callback_context<void*, Args...>
83+
callback_context<Args...>
8484
get_context(Fn&& cb) noexcept
8585
{
8686
struct trampoline_t final
@@ -120,7 +120,7 @@ class async_call_helper
120120
std::function<void(Args...)> callback;
121121
};
122122
std::function<void(Args...)> callback = cb;
123-
return callback_context<void*, Args...> {
123+
return callback_context<Args...> {
124124
new trampoline_t(weak_ref(), guard, std::move(callback)),
125125
&trampoline_t::callback_handle
126126
};

async-call-helper/external_c_lib.h

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
#pragma once
22

3-
#include <thread>
43
#include <chrono>
4+
#include <functional>
5+
#include <thread>
56

67
namespace c {
7-
typedef void(*long_async_function_cb)(void*, int);
8+
typedef void (*long_async_function_cb)(void*, int);
9+
10+
static void long_async_function(void* context,
11+
long_async_function_cb cb,
12+
int in_param,
13+
int sleep_msec = 1000) {
14+
std::thread([=] {
15+
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_msec));
16+
auto out_param = in_param * 2;
17+
cb(context, out_param);
18+
}).detach();
19+
}
20+
21+
} // namespace c
22+
23+
namespace cpp {
24+
using long_async_function_cb_t = std::function<void(int)>;
25+
26+
static void long_async_function(long_async_function_cb_t cb, int in_param, int sleep_msec = 1000) {
27+
std::thread([=] {
28+
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_msec));
29+
auto out_param = in_param * 2;
30+
cb(out_param);
31+
}).detach();
32+
}
833

9-
static void long_async_function(void* context, long_async_function_cb cb, int in_param, int sleep_msec = 1000) {
10-
std::thread([=] {
11-
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_msec));
12-
auto out_param = in_param * 2;
13-
cb(context, out_param);
14-
}).detach();
15-
}
16-
}
34+
} // namespace cpp

async-call-helper/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int main()
2121
{
2222
safe_service s(5);
2323
s.execute();
24-
std::this_thread::sleep_for(1s);
24+
std::this_thread::sleep_for(3s);
2525
} while(getchar() != 'q');
2626

2727
return 0;

async-call-helper/safe-service.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
#include <iostream>
21
#include "safe-service.h"
2+
#include <iostream>
33
#include "external_c_lib.h"
44

5-
6-
safe_service::safe_service(int in_param)
7-
: param(new int(in_param))
8-
{
5+
safe_service::safe_service(int in_param) : param(new int(in_param)) {
96
std::cout << "ctor service \n";
107
}
118

@@ -16,30 +13,28 @@ safe_service::~safe_service() {
1613

1714
static inline void safe_response_cb(void* context, int out_param) {
1815
auto srv_ptr = asyn_call_token::from_context<safe_service>(context);
19-
if (srv_ptr)
20-
{
16+
if (srv_ptr) {
2117
srv_ptr->response(out_param);
22-
}
23-
else
24-
{
18+
} else {
2519
std::cerr << "no service instance to make callback call\n";
2620
}
2721
}
2822

2923
void safe_service::execute() {
30-
c::long_async_function(get_context(), safe_response_cb, *param);
31-
32-
auto context = get_context<int>([this] (int o) {
33-
std::cout << "received response from lambda " << *param << ": " << o << "\n";
34-
});
35-
c::long_async_function(context.context, context.callback, *param);
24+
// c::long_async_function(get_context(), safe_response_cb, *param);
25+
26+
// auto context = get_context<int>([this](int o) {
27+
// std::cout << "received response from lambda " << *param << ": " << o << "\n";
28+
// });
29+
// c::long_async_function(context.context, context.callback, *param);
3630

3731
using namespace std::placeholders;
3832
auto context_bind = get_context<int>(std::bind(&safe_service::response, this, _1));
3933
c::long_async_function(context_bind.context, context_bind.callback, *param * 3);
34+
35+
cpp::long_async_function(context_bind, *param);
4036
}
4137

42-
void safe_service::response(int out_param)
43-
{
38+
void safe_service::response(int out_param) {
4439
std::cout << "received response for " << *param << ": " << out_param << "\n";
4540
}

0 commit comments

Comments
 (0)