universe.h 1.9 KB

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