瀏覽代碼

refactor: reduce the obtuseness of Universe, since its mostly stateless

Sam Jaffe 2 年之前
父節點
當前提交
0fa049e796
共有 2 個文件被更改,包括 12 次插入31 次删除
  1. 9 23
      include/engine/universe.h
  2. 3 8
      src/universe.cxx

+ 9 - 23
include/engine/universe.h

@@ -12,29 +12,26 @@
 
 #include <engine/forwards.h>
 #include <random/random.h>
+#include <serializer/jsonizer.h>
 
 namespace engine {
 class Universe {
 private:
   std::shared_ptr<Mailbox> mailbox_;
-  // The universe is not a shared object, so by convention we use unique_ptr
-  // to store everything that does not need to be shared.
-  std::unique_ptr<env::Config> config_;
-  std::unique_ptr<random::Random> random_;
-  // The cache is, by definition, a shared object and so uses shared_ptr
-  std::shared_ptr<serializer::SharedCache> serialcache_;
-  std::unique_ptr<serializer::Jsonizer> jsonizer_;
+  std::shared_ptr<env::Config const> config_;
+  random::Random random_;
+  serializer::Jsonizer jsonizer_;
 
 public:
   Mailbox & mailbox() const { return *mailbox_; }
   env::Config const & config() const { return *config_; }
-  serializer::Jsonizer & jsonizer() const { return *jsonizer_; }
-  random::Random const & random() const { return *random_; }
+  serializer::Jsonizer const & jsonizer() const { return jsonizer_; }
+  random::Random const & random() const { return random_; }
 
   /**
    * @brief Construct a universe with all necessary components
    * @param cfg The config object generated from reading a config file or JSON
-   * @param cache A serializer cache
+   * @param cache A serializer cache. If null, then we construct a default cache
    * @param rng A random number generator's underlying implementation. This
    * exists for dependency injection's sake. If rng is null, then random_ will
    * be constructed with the default random device.
@@ -42,22 +39,11 @@ public:
   Universe(std::shared_ptr<Mailbox> mailbox, env::Config const & cfg,
            std::shared_ptr<serializer::SharedCache> cache,
            std::shared_ptr<random::Device> rng);
-  ~Universe();
+
+  void set_random(random::Random const &random) { random_ = random; }
 
 protected:
   template <typename T>
   void load(std::string const & name, std::string const & fallback = "");
-
-  [[nodiscard]] auto record(std::shared_ptr<random::Tape> tape) const {
-    return random_->record(tape);
-  }
-
-  /**
-   * @deprecated This exists solely for the purpose of needing to initialize
-   * the singleton.
-   */
-  [[deprecated]] Universe();
-  Universe(Universe &&) = default;
-  Universe & operator=(Universe &&) = default;
 };
 }

+ 3 - 8
src/universe.cxx

@@ -18,13 +18,8 @@ namespace engine {
 Universe::Universe(std::shared_ptr<Mailbox> mailbox, env::Config const & cfg,
                    std::shared_ptr<serializer::SharedCache> cache,
                    std::shared_ptr<random::Device> rng)
-    : mailbox_(mailbox), config_(std::make_unique<env::Config>(cfg)),
-      random_(rng ? std::make_unique<random::Random>(rng)
-                  : std::make_unique<random::Random>()),
-      serialcache_(cache ?: std::make_shared<serializer::SharedCache>()),
-      jsonizer_(std::make_unique<serializer::Jsonizer>(serialcache_)) {}
-
-Universe::Universe() {}
-Universe::~Universe() {}
+    : mailbox_(mailbox), config_(std::make_shared<env::Config>(cfg)),
+      random_(rng ? random::Random(rng) : random::Random()),
+      jsonizer_(cache ?: std::make_shared<serializer::SharedCache>()) {}
 
 }