Stiffstream / restinio

Cross-platform, efficient, customizable, and robust asynchronous HTTP(S)/WebSocket server C++ library with the right balance between performance and ease of use
Other
1.13k stars 93 forks source link

Need for a base class? #176

Closed gegles closed 1 year ago

gegles commented 1 year ago

Hello,

I've got a high-level class (RestAPI) that needs hold on to a running_server_handle_t obtained from a call torestinio::run_async and so as to be able to call stop() on it at some point later.

Depending on some config file, the restinio server can be of two distinct traits (either regular or tls).

It would be really nice if running_server_handle_t/running_server_instance_t could inherit from a non-templated base class so that a more generic handle could be used regardless of the traits if only to access methods like stop().

Currently what I've done is:

In my header:

struct RunningServerInstance {
  virtual ~RunningServerInstance() = default;
  virtual void stop()              = 0;
};

class RestAPI {
....
private:
  RunningServerHandle server_;
}

In the private impl:

template <typename ServerTraits>
class RunningServerInstanceImpl : public RunningServerInstance {
public:
  using ServerHandle = restinio::running_server_handle_t<ServerTraits>;

  explicit RunningServerInstanceImpl(ServerHandle&& server_handle) : server_handle_{std::move(server_handle)} {}

  void stop() override { server_handle_->stop(); }

private:
  ServerHandle server_handle_;
};

....
server_     = std::make_unique<RunningServerInstanceImpl<MyTraits>>(std::move(server));

But if the restinio classes had some shared base class, I wouldn't need this cruft...

Let me know if maybe I am missing a better way or if this base class could be implement in restinio.

Thanks!

eao197 commented 1 year ago

Hi!

I think you've already found the best way to solve your case.

I don't think we'll introduce some kind of base class with pure virtual method(s) in RESTinio for representing an abstract http server. It's because this will recure a (probably seriour) redisign of a part of the library. But the case like your encounters not to often and can easily solved by a solution like your.

gegles commented 1 year ago

Ok, thanks!

I still think everybody could benefit from that base class, but we can live without it for now.