int
Sample_callback_function(int arg)
{
LOG(
"Sample callback function, arg: %d", arg);
return arg + 10;
}
class Sample_class {
public:
int
Sample_callback_method(int arg)
{
LOG(
"Sample callback method, arg: %d", arg);
return arg + 10;
}
};
class Callable_class {
public:
int x;
Callable_class(int x):
x(x)
{}
int
operator ()(int y)
{
LOG(
"Sample callback method, arg: %d", y);
return x + y;
}
};
void
Simple_callbacks()
{
LOG(
"Callback call result: %d", func_cbk());
Sample_class class_instance;
&class_instance, 10);
LOG(
"Callback call result: %d", method_cbk());
Callable_class callable_instance(10);
LOG(
"Callback call result: %d", callable_cbk());
[](int arg)
{
LOG(
"Sample lambda callback, arg: %d", arg);
return arg + 10;
},
10);
LOG(
"Callback call result: %d", lambda_cbk());
}
void
Some_api_method(Sample_handler_type handler)
{
LOG(
"Callback result: %d", handler(20));
}
void
Callback_proxies()
{
auto My_callback_target = [](double enforced_arg, int user_arg)
{
LOG(
"Callback called, enforced argument %f user argument %d",
enforced_arg, user_arg);
return 30;
};
Some_api_method(Sample_handler_builder(My_callback_target, 10));
}
void
Requests_and_contexts()
{
processor->Enable();
comp_ctx->Enable();
req->Set_processing_handler(
{
LOG(
"Processing handler called, arg %d", arg);
req->Complete();
}, 10, req));
req->Set_completion_handler(comp_ctx,
[](int arg)
{
LOG(
"Completion notification handler called, arg %d", arg);
}, 20));
processor->Submit_request(req);
LOG(
"Request submitted");
LOG(
"Before processor running");
waiter->Wait_and_process({processor});
LOG(
"After processor running");
LOG(
"Before completion context running");
waiter->Wait_and_process({comp_ctx});
LOG(
"After completion context running");
processor->Disable();
comp_ctx->Disable();
std::initializer_list<ugcs::vsm::Request_container::Ptr>{processor, comp_ctx});
worker->Enable_containers();
worker->Enable();
req->Set_processing_handler(
{
LOG(
"Processing handler called, arg %d", arg);
req->Complete();
}, 10, req));
req->Set_completion_handler(comp_ctx,
[](int arg)
{
LOG(
"Completion notification handler called, arg %d", arg);
}, 20));
LOG(
"Before request submission");
processor->Submit_request(req);
std::this_thread::sleep_for(std::chrono::seconds(1));
LOG(
"After request submission");
worker->Disable_containers();
worker->Disable();
}
public:
typedef ugcs::vsm::Callback_proxy<void, double> Handler;
Sample_api_method(
int param,
Handler handler = ugcs::vsm::Make_dummy_callback<void, double>(),
private:
void
virtual void
virtual void
};
void
Sample_processor::On_enable()
{
def_comp_ctx->Enable();
std::initializer_list<ugcs::vsm::Request_container::Ptr>{Shared_from_this(), def_comp_ctx});
worker->Enable();
}
void
Sample_processor::On_disable()
{
Set_disabled();
worker->Disable();
def_comp_ctx->Disable();
def_comp_ctx = nullptr;
worker = nullptr;
}
Sample_processor::Sample_api_method(int param,
Handler handler,
{
req->Set_processing_handler(
param, req, handler));
req->Set_completion_handler(comp_ctx ? comp_ctx : def_comp_ctx,
handler);
Submit_request(req);
return req;
}
void
Handler handler)
{
auto lock = request->Lock();
if (!request->Is_processing()) {
return;
}
handler.Set_args(param * 2);
}
void
Custom_processor()
{
Sample_processor::Ptr processor = Sample_processor::Create();
processor->Enable();
auto handler = Sample_processor::Make_handler(
[](double result, int user_param)
{
LOG(
"Request completed, result %f, user param %d", result, user_param);
}, 30);
processor->Sample_api_method(10, handler);
std::this_thread::sleep_for(std::chrono::seconds(1));
processor->Disable();
}
int
main (int argc, char *argv[])
{
Simple_callbacks();
Callback_proxies();
Requests_and_contexts();
Custom_processor();
return 0;
}