creativity  v1.3.0
Agent-based model of creativity and piracy
Storage.hpp
1 #pragma once
2 #include <boost/iterator/iterator_facade.hpp>
3 #include "creativity/state/StorageBackend.hpp"
4 #include <eris/types.hpp>
5 #include <algorithm>
6 #include <cstddef>
7 #include <memory>
8 #include <type_traits>
9 #include <vector>
10 
11 namespace boost { namespace iterators { struct random_access_traversal_tag; } }
12 
13 namespace creativity { struct CreativitySettings; }
14 
15 namespace creativity { namespace state {
16 
27 class Storage final {
28  public:
29  Storage() = delete;
30 
38  template<class SB, class... Args>
39  static typename std::enable_if<std::is_base_of<StorageBackend, SB>::value, std::shared_ptr<Storage>>::type
40  create(CreativitySettings &settings, Args&&... args) {
41  return std::shared_ptr<Storage>(new Storage(settings, new SB(settings, std::forward<Args>(args)...)));
42  }
43 
55  std::shared_ptr<const State> operator[](eris::eris_time_t t) const;
56 
58  size_t size() const;
59 
63  void reserve(size_t capacity);
64 
66  bool empty() const;
67 
73  void push_back(std::shared_ptr<const State> s);
74 
82  void updateSettings();
83 
87 
89  template<class... Args>
90  void emplace_back(Args&&... args);
91 
105  void flush(bool flush_buffers = true);
106 
123  bool flush_for(long milliseconds, bool flush_buffers = true);
124 
126  StorageBackend& backend();
127 
131  bool flush_on_destroy = true;
132 
134  ~Storage();
135 
137  class state_iterator : public boost::iterator_facade<state_iterator, const std::shared_ptr<const State>, boost::random_access_traversal_tag> {
138  private:
139  const Storage *storage;
140  size_t i = 0;
141  std::shared_ptr<const State> curr;
142 
143  friend class Storage;
144  friend class boost::iterator_core_access;
145 
146  state_iterator(const Storage &st, size_t at);
147 
148  reference dereference() const;
149  void increment();
150  void decrement();
151  void advance(difference_type n);
152  difference_type distance_to(const state_iterator &it) const;
153  bool equal(const state_iterator &other) const;
154  };
155 
157  state_iterator begin() const;
160  state_iterator end() const;
161 
162  private:
171  Storage(CreativitySettings &settings, StorageBackend *sb) : settings(settings), settings_(settings), backend_(sb)
172  {
173  // Track the number of states using the size of the cache_
174  cache_.resize(backend_->size());
175  }
176 
180  CreativitySettings &settings_;
181 
184  mutable std::vector<std::weak_ptr<const State>> cache_;
185 
192  mutable std::shared_ptr<const State> cache_last_;
193 
195  std::unique_ptr<StorageBackend> backend_;
196 
200  bool need_settings_updated_ = true;
201 };
202 
203 template <class... Args>
204 void Storage::emplace_back(Args&&... args) {
205  push_back(std::make_shared<State>(std::forward<Args>(args)...));
206 }
207 
208 }}
Definition: CLI.hpp:5
Primary namespace for all Creativity library code.
Definition: config.hpp:4
static std::enable_if< std::is_base_of< StorageBackend, SB >::value, std::shared_ptr< Storage > >::type create(CreativitySettings &settings, Args &&... args)
Storage constructor, which can construct a StorageBackend on the fly.
Definition: Storage.hpp:40
const CreativitySettings & settings
Read-only access to the settings object referenced by this object.
Definition: Storage.hpp:86
Class for state storage access which accesses State values.
Definition: Storage.hpp:27
Random access iterator class for iterating through states.
Definition: Storage.hpp:137
Simulation parameters that are used to configure the simulation when calling setup().
Definition: CreativitySettings.hpp:10
Base class for state storage which accesses State values.
Definition: StorageBackend.hpp:26