universe.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 rng A random number generator's underlying implementation. This
  31. * exists for dependency injection's sake. If rng is null, then random_ will
  32. * be constructed with the default random device.
  33. */
  34. Universe(std::shared_ptr<Mailbox> mailbox, env::Config const & cfg,
  35. std::shared_ptr<detail::random_impl> rng);
  36. ~Universe();
  37. protected:
  38. template <typename T>
  39. void load(std::string const & name, std::string const & fallback = "");
  40. /**
  41. * @deprecated This exists solely for the purpose of needing to initialize
  42. * the singleton.
  43. */
  44. Universe();
  45. Universe(Universe &&) = default;
  46. Universe & operator=(Universe &&) = default;
  47. };
  48. }