Skip to content

Commit a38353c

Browse files
committed
preparation for presentation
1 parent 149416f commit a38353c

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

async-call-helper/main.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,21 @@
77

88
using namespace std::chrono_literals;
99

10-
int main()
10+
int main(int argc, const char*[])
1111
{
12-
#if 1
12+
bool run_unsafe = argc == 1;
13+
if (run_unsafe)
1314
{
1415
unsafe_service s(5);
1516
s.execute();
1617
std::this_thread::sleep_for(1s);
1718
}
18-
(void) getchar();
19-
#else
20-
do
19+
else
2120
{
2221
safe_service s(5);
2322
s.execute();
24-
std::this_thread::sleep_for(3s);
25-
} while(getchar() != 'q');
26-
#endif
23+
std::this_thread::sleep_for(1s);
24+
}
25+
(void)getchar();
2726
return 0;
2827
}

async-call-helper/safe-service.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ safe_service::~safe_service() {
1212
}
1313

1414
static inline void safe_response_cb(void* context, int out_param) {
15+
std::cout << "safe_response_cb\n";
1516
auto srv_ptr = asyn_call_token::from_context<safe_service>(context);
1617
if (srv_ptr) {
1718
srv_ptr->response(out_param);
@@ -20,21 +21,38 @@ static inline void safe_response_cb(void* context, int out_param) {
2021
}
2122
}
2223

23-
void safe_service::execute() {
24-
// c::long_async_function(get_context(), safe_response_cb, *param);
24+
void safe_service::execute_c_style()
25+
{
26+
c_long_async_function(get_context(), safe_response_cb, *param);
27+
}
2528

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);
29+
void safe_service::execute_cpp_lambda()
30+
{
31+
auto context = get_context<int>([this](int o) {
32+
std::cout << "received response from lambda " << *param << ": " << o << "\n";
33+
});
34+
c_long_async_function(context.context, context.callback, *param);
35+
}
3036

37+
void safe_service::execute_cpp_bind()
38+
{
3139
using namespace std::placeholders;
3240
auto context_bind = get_context<int>(std::bind(&safe_service::response, this, _1));
3341
c_long_async_function(context_bind.context, context_bind.callback, *param * 3);
34-
35-
//cpp::long_async_function(context_bind, *param);
3642
}
3743

3844
void safe_service::response(int out_param) {
45+
std::cout << "response\n";
3946
std::cout << "received response. this->param: " << *param << "\n";
40-
}
47+
}
48+
49+
void safe_service::execute()
50+
{
51+
constexpr const auto cb_type = cb_type_e::c_style;
52+
switch (cb_type)
53+
{
54+
case cb_type_e::c_style: execute_c_style(); break;
55+
case cb_type_e::cpp_bind: execute_cpp_bind(); break;
56+
case cb_type_e::cpp_lambda: execute_cpp_lambda(); break;
57+
}
58+
}

async-call-helper/safe-service.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
#include <memory>
33
#include "async-call-helper.hpp"
44

5+
enum class cb_type_e {
6+
c_style,
7+
cpp_lambda,
8+
cpp_bind
9+
};
10+
511
class safe_service
612
: public async_call_helper<safe_service, multi_thread_usage>
713
{
@@ -12,5 +18,8 @@ class safe_service
1218
void response(int out_param);
1319
void execute();
1420
private:
21+
void execute_c_style();
22+
void execute_cpp_lambda();
23+
void execute_cpp_bind();
1524
std::unique_ptr<int> param;
1625
};

async-call-helper/unsafe-service.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ unsafe_service::~unsafe_service() {
1313
}
1414

1515
static inline void response_cb(void* context, int out_param) {
16+
std::cout << "response_cb\n";
1617
auto srv_ptr = static_cast<unsafe_service*>(context);
1718
srv_ptr->response(out_param);
1819
}
@@ -23,5 +24,6 @@ void unsafe_service::execute() {
2324

2425
void unsafe_service::response(int out_param)
2526
{
27+
std::cout << "response\n";
2628
std::cout << "received response. this->param: " << *param << "\n";
2729
}

0 commit comments

Comments
 (0)