creativity
v1.3.0
Agent-based model of creativity and piracy
|
Class for file-based storage. More...
#include <creativity/state/FileStorage.hpp>
Public Types | |
enum | TYPE : uint8_t { TYPE_DONE = 0, TYPE_READERS = 1, TYPE_BOOKS = 2, TYPE_PUBLIC_TRACKER = 3 } |
The supported record types within a state. More... | |
Public Member Functions | |
FileStorage (CreativitySettings &settings) | |
Constructs a FileStorage that stores file content in an in-memory buffer. More... | |
template<typename... Args> | |
FileStorage (CreativitySettings &settings, const std::string &filename, Mode mode, Args &&... args) | |
Constructs and returns a FileStorage object that uses the given file for reading and (optionally) writing state data. More... | |
FileStorage (CreativitySettings &settings, std::unique_ptr< std::stringstream > &&s, Mode mode=Mode::READONLY) | |
Constructs and returns a FileStorage object that uses the given stringstream for reading and (optionally) writing state data. More... | |
void | throwParseError (const std::string &message) const |
Default move constructor. More... | |
virtual std::shared_ptr< const State > | load (eris::eris_time_t t) override |
Loads the requested state data from the open file into a State object and returns it (wrapped in a shared_ptr). | |
virtual size_t | size () override |
Returns the number of states currently stored in the file. | |
virtual void | storage_flush () override |
Flushes the file stream. More... | |
![]() | |
StorageBackend (CreativitySettings &settings) | |
Constructor: takes a CreativitySettings reference. More... | |
bool | empty () |
Returns true if the container is empty; exactly equivalent to size() == 0 | |
virtual void | reserve (size_t T) |
Optional method that subclasses can override if knowing the number of states that will be stored in advance is useful. More... | |
void | flush (bool flush_buffers=true) |
Flushes changes. More... | |
bool | flush_for (long milliseconds, bool flush_buffers=true) |
Attempts to flush changes, waiting at most the given number of milliseconds for the flush to complete. More... | |
virtual void | enqueue (std::shared_ptr< const State > &&s) |
Adds a state to this storage container. More... | |
virtual size_t | pending () const |
Returns the number of states still pending in the queue. More... | |
virtual | ~StorageBackend () |
Virtual destructor that tells the thread (if created) to quit and waits for it. | |
Protected Member Functions | |
void | configureHeaderFields () override |
Registers the various creativity parameters as header fields. | |
void | writeSettings () override |
Calls updateHeaderFields() to rewrite settings. | |
void | writeExtraHeader () override |
Adds a state pointer block to the end of the header. | |
void | readExtraHeader () override |
Stores the location of the state pointer block at the end of the head. | |
Additional Inherited Members | |
![]() | |
CreativitySettings & | settings_ |
CreativitySettings reference. More... | |
Class for file-based storage.
Note that most of the methods of this class will throw errors when the underlying file stream encounters an error. You should not attempt to use the FileStorage object after such an exception is thrown as the result is unpredictable: the object could simply stop writing new data to the file or the file could become corrupted.
Writing new states is done by a background thread which is spawned the first time enqueue() is called with a new state.
enum creativity::state::FileStorage::TYPE : uint8_t |
The supported record types within a state.
Enumerator | |
---|---|
TYPE_DONE | Psuedo-type indicating the end of the record list. |
TYPE_READERS | An array of readers. |
TYPE_BOOKS | An array of books. |
TYPE_PUBLIC_TRACKER | A PublicTracker state. |
|
inline |
Constructs a FileStorage that stores file content in an in-memory buffer.
Beware: this usage requires considerably more memory for running a simulation, and the results are not automatically stored.
settings | the CreativitySettings reference which is read when creating a new file and updated when reading an existing file. |
|
inline |
Constructs and returns a FileStorage object that uses the given file for reading and (optionally) writing state data.
The file is read or created immediately. All arguments (except the first) are forwarded to eris::Serializer::open().
various | exceptions if the file does not exist, cannot be read, is empty, or contains invalid data. |
creativity::state::FileStorage::FileStorage | ( | CreativitySettings & | settings, |
std::unique_ptr< std::stringstream > && | s, | ||
Mode | mode = Mode::READONLY |
||
) |
Constructs and returns a FileStorage object that uses the given stringstream for reading and (optionally) writing state data.
Takes the open mode, which defaults to Mode::READONLY.
|
overridevirtual |
Flushes the file stream.
This calls flush()
on the underlying file object. This is normally not required: any changes will be automatically flushed when the FileStorage object is destroyed; calling this manually primarily guards against unflushed data being unwritten if the process is terminated abnormally (i.e. via a signal or a segfault).
Reimplemented from creativity::state::StorageBackend.
void creativity::state::FileStorage::throwParseError | ( | const std::string & | message | ) | const |
Default move constructor.
Default move assignment operator. Throws a eris::serialize::Serializer::parse_error exception. The given message is prefixed with Parsing file failed [pos=123]:
, where 123
is the current file position.