universe.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //
  2. // universe.h
  3. // engine_base
  4. //
  5. // Created by Sam Jaffe on 3/15/23.
  6. //
  7. #pragma once
  8. #include <memory>
  9. #include <string>
  10. #include <engine/forwards.h>
  11. namespace engine {
  12. class Universe {
  13. private:
  14. std::shared_ptr<Mailbox> mailbox_;
  15. // The universe is not a shared object, so by convention we use unique_ptr
  16. // to store everything that does not need to be shared.
  17. std::unique_ptr<env::Config> config_;
  18. std::unique_ptr<random_number_generator> random_;
  19. // The cache is, by definition, a shared object and so uses shared_ptr
  20. std::shared_ptr<serializer::SharedCache> serialcache_;
  21. std::unique_ptr<serializer::Jsonizer> jsonizer_;
  22. public:
  23. Mailbox & mailbox() const { return *mailbox_; }
  24. env::Config const & config() const { return *config_; }
  25. serializer::Jsonizer & jsonizer() const { return *jsonizer_; }
  26. random_number_generator const & random() const { return *random_; }
  27. /**
  28. * @brief Construct a universe with all necessary components
  29. * @param cfg The config object generated from reading a config file or JSON
  30. * @param cache A serializer cache
  31. * @param rng A random number generator's underlying implementation. This
  32. * exists for dependency injection's sake. If rng is null, then random_ will
  33. * be constructed with the default random device.
  34. */
  35. Universe(std::shared_ptr<Mailbox> mailbox, env::Config const & cfg,
  36. std::shared_ptr<serializer::SharedCache> cache,
  37. std::shared_ptr<detail::random_impl> rng);
  38. ~Universe();
  39. protected:
  40. template <typename T>
  41. void load(std::string const & name, std::string const & fallback = "");
  42. /**
  43. * @deprecated This exists solely for the purpose of needing to initialize
  44. * the singleton.
  45. */
  46. [[deprecated]] Universe();
  47. Universe(Universe &&) = default;
  48. Universe & operator=(Universe &&) = default;
  49. };
  50. }