🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.3 KiB

  1. # Crash Course: service locator
  2. <!--
  3. @cond TURN_OFF_DOXYGEN
  4. -->
  5. # Table of Contents
  6. * [Introduction](#introduction)
  7. * [Service locator](#service-locator)
  8. <!--
  9. @endcond TURN_OFF_DOXYGEN
  10. -->
  11. # Introduction
  12. Usually service locators are tightly bound to the services they expose and it's
  13. hard to define a general purpose solution. This template based implementation
  14. tries to fill the gap and to get rid of the burden of defining a different
  15. specific locator for each application.<br/>
  16. This class is tiny, partially unsafe and thus risky to use. Moreover it doesn't
  17. fit probably most of the scenarios in which a service locator is required. Look
  18. at it as a small tool that can sometimes be useful if users know how to handle
  19. it.
  20. # Service locator
  21. The API is straightforward. The basic idea is that services are implemented by
  22. means of interfaces and rely on polymorphism.<br/>
  23. The locator is instantiated with the base type of the service if any and a
  24. concrete implementation is provided along with all the parameters required to
  25. initialize it. As an example:
  26. ```cpp
  27. // the service has no base type, a locator is used to treat it as a kind of singleton
  28. entt::service_locator<my_service>::set(params...);
  29. // sets up an opaque service
  30. entt::service_locator<audio_interface>::set<audio_implementation>(params...);
  31. // resets (destroys) the service
  32. entt::service_locator<audio_interface>::reset();
  33. ```
  34. The locator can also be queried to know if an active service is currently set
  35. and to retrieve it if necessary (either as a pointer or as a reference):
  36. ```cpp
  37. // no service currently set
  38. auto empty = entt::service_locator<audio_interface>::empty();
  39. // gets a (possibly empty) shared pointer to the service ...
  40. std::shared_ptr<audio_interface> ptr = entt::service_locator<audio_interface>::get();
  41. // ... or a reference, but it's undefined behaviour if the service isn't set yet
  42. audio_interface &ref = entt::service_locator<audio_interface>::ref();
  43. ```
  44. A common use is to wrap the different locators in a container class, creating
  45. aliases for the various services:
  46. ```cpp
  47. struct locator {
  48. using camera = entt::service_locator<camera_interface>;
  49. using audio = entt::service_locator<audio_interface>;
  50. // ...
  51. };
  52. // ...
  53. void init() {
  54. locator::camera::set<camera_null>();
  55. locator::audio::set<audio_implementation>(params...);
  56. // ...
  57. }
  58. ```