alibaba / yalantinglibs

A collection of modern C++ libraries, include coro_rpc, struct_pack, struct_json, struct_xml, struct_pb, easylog, async_simple
https://alibaba.github.io/yalantinglibs/
Apache License 2.0
1.52k stars 240 forks source link

[coro_rpc] Create the object of service on-demand may be supported #307

Open yanminhui opened 1 year ago

yanminhui commented 1 year ago

Search before asking

What happened + What you expected to happen

https://github.com/alibaba/yalantinglibs/blob/c75fdff50b43a153b9f86a50cafea060487e21e7/src/coro_rpc/examples/helloworld/server/main.cpp#L22-L38

Now, create the object of service (e.g. hello_service) before server.start() as pre-condition, but create the object of service is not necessary if service is not called.

It can be supported that allow user creates the object of service while the service is called.

for example, pass one function create() to register_handler().


HelloService* create() 
{
    static HelloService hello_service;
    return &hello_service;
} 

server.register_handler<&HelloService::hello>(create);

Reproduction way

Anything else

Are you willing to submit a PR?

poor-circle commented 1 year ago

Lazy evalutation is a good idea, would you like submit a PR? Or I will support this.