creativity  v1.3.0
Agent-based model of creativity and piracy
Classes | Public Types | Public Member Functions | Protected Member Functions | List of all members
creativity::state::FileStorage Class Reference

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 Stateload (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...
 
- Public Member Functions inherited from creativity::state::StorageBackend
 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

- Protected Attributes inherited from creativity::state::StorageBackend
CreativitySettingssettings_
 CreativitySettings reference. More...
 

Detailed Description

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.

Member Enumeration Documentation

§ TYPE

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.

Constructor & Destructor Documentation

§ FileStorage() [1/3]

creativity::state::FileStorage::FileStorage ( CreativitySettings settings)
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.

Parameters
settingsthe CreativitySettings reference which is read when creating a new file and updated when reading an existing file.

§ FileStorage() [2/3]

template<typename... Args>
creativity::state::FileStorage::FileStorage ( CreativitySettings settings,
const std::string &  filename,
Mode  mode,
Args &&...  args 
)
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().

See also
eris::Serializer::open()
Exceptions
variousexceptions if the file does not exist, cannot be read, is empty, or contains invalid data.

§ FileStorage() [3/3]

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.

Member Function Documentation

§ storage_flush()

virtual void creativity::state::FileStorage::storage_flush ( )
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).

See also
creativity::state::StorageBackend::flush
std::basic_ostream::flush

Reimplemented from creativity::state::StorageBackend.

§ throwParseError()

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.

Inheritance diagram for creativity::state::FileStorage:
[legend]
Collaboration diagram for creativity::state::FileStorage:
[legend]

The documentation for this class was generated from the following file: