Beatmup
Beatmup::ChunkCollection Class Referenceabstract

A key-value pair set storing pieces of arbitrary data (chunks) under string keys. More...

#include <chunkfile.h>

Inheritance diagram for Beatmup::ChunkCollection:
Beatmup::Object Beatmup::ChunkStream Beatmup::Python::WritableChunkCollection Beatmup::Android::ChunkAsset Beatmup::ChunkFile

Public Member Functions

virtual void open ()=0
 Opens the collection to read chunks from it. More...
 
virtual void close ()=0
 Closes the collection after a reading session. More...
 
virtual size_t size () const =0
 Returns the number of chunks available in the collection after it is opened. More...
 
virtual bool chunkExists (const std::string &id) const =0
 Check if a specific chunk exists. More...
 
virtual chunksize_t chunkSize (const std::string &id) const =0
 Retrieves size of a specific chunk. More...
 
virtual chunksize_t fetch (const std::string &id, void *data, const chunksize_t limit)=0
 Reads a chunk. More...
 
virtual void save (const std::string &filename, bool append=false)=0
 Saves the collection to a file. More...
 
template<typename datatype >
datatype read (const std::string &id)
 Reads a chunk and casts it into a given type. More...
 
template<typename datatype >
std::vector< datatype > readVector (const std::string &id)
 Reads a chunk into a vector of a specific type. More...
 
template<>
std::string read (const std::string &id)
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Detailed Description

A key-value pair set storing pieces of arbitrary data (chunks) under string keys.

A chunk is a header and a piece of data packed in memory like this: (idLength[4], id[idLength], size[sizeof(chunksize_t)], data[size]) ChunkCollection defines an interface to retrieve chunks by their ids.

Definition at line 36 of file chunkfile.h.

Member Function Documentation

◆ open()

virtual void Beatmup::ChunkCollection::open ( )
pure virtual

Opens the collection to read chunks from it.

Implemented in Beatmup::Python::WritableChunkCollection, Beatmup::ChunkFile, and Beatmup::Android::ChunkAsset.

◆ close()

virtual void Beatmup::ChunkCollection::close ( )
pure virtual

Closes the collection after a reading session.

Implemented in Beatmup::Python::WritableChunkCollection, Beatmup::ChunkFile, and Beatmup::Android::ChunkAsset.

◆ size()

virtual size_t Beatmup::ChunkCollection::size ( ) const
pure virtual

Returns the number of chunks available in the collection after it is opened.

Implemented in Beatmup::Python::WritableChunkCollection, and Beatmup::ChunkStream.

◆ chunkExists()

virtual bool Beatmup::ChunkCollection::chunkExists ( const std::string &  id) const
pure virtual

Check if a specific chunk exists.

Parameters
[in]idThe chunk id
Returns
true if the chunk exists in the collection.

Implemented in Beatmup::Python::WritableChunkCollection, and Beatmup::ChunkStream.

◆ chunkSize()

virtual chunksize_t Beatmup::ChunkCollection::chunkSize ( const std::string &  id) const
pure virtual

Retrieves size of a specific chunk.

Parameters
[in]idThe chunk id
Returns
size of the chunk in bytes, 0 if not found.

Implemented in Beatmup::Python::WritableChunkCollection, and Beatmup::ChunkStream.

◆ fetch()

virtual chunksize_t Beatmup::ChunkCollection::fetch ( const std::string &  id,
void *  data,
const chunksize_t  limit 
)
pure virtual

Reads a chunk.

The collection is expected to be opened.

Parameters
[in]idWanted chunk id.
[out]dataA buffer to write out the wanted chunk content.
[in]limitThe buffer capacity in bytes.
Returns
number of bytes written out to the buffer: if the chunk is found fits the buffer, the chunk size is returned; if the chunk is found and too big, limit is returned (number of bytes actually written); if no chunk found, 0 is returned.

Implemented in Beatmup::Python::WritableChunkCollection, and Beatmup::ChunkStream.

◆ save()

virtual void Beatmup::ChunkCollection::save ( const std::string &  filename,
bool  append = false 
)
pure virtual

Saves the collection to a file.

Parameters
[in]filenameThe name of the file to write chunks to
[in]appendIf true, writing to the end of the file (keeping the existing content). Rewriting the file otherwise.

Implemented in Beatmup::Python::WritableChunkCollection, and Beatmup::ChunkStream.

◆ read() [1/2]

template<typename datatype >
datatype Beatmup::ChunkCollection::read ( const std::string &  id)
inline

Reads a chunk and casts it into a given type.

Parameters
[in]idThe searched chunk id.

Definition at line 91 of file chunkfile.h.

91  {
92  datatype result;
93 #ifdef BEATMUP_DEBUG
94  DebugAssertion::check(sizeof(result) == chunkSize(id),
95  "Cannot fit chunk " + id + " of " + std::to_string(chunkSize(id)) + " bytes into " + std::to_string(sizeof(result)) + " bytes");
96 #endif
97  fetch(id, &result, sizeof(result));
98  return result;
99  }
virtual chunksize_t chunkSize(const std::string &id) const =0
Retrieves size of a specific chunk.
virtual chunksize_t fetch(const std::string &id, void *data, const chunksize_t limit)=0
Reads a chunk.
std::string to_string(Beatmup::NNets::ActivationFunction function)
Beatmup::IntPoint result

◆ readVector()

template<typename datatype >
std::vector<datatype> Beatmup::ChunkCollection::readVector ( const std::string &  id)
inline

Reads a chunk into a vector of a specific type.

Parameters
[in]idThe searched chunk id.

Definition at line 106 of file chunkfile.h.

106  {
107 #ifdef BEATMUP_DEBUG
108  DebugAssertion::check(chunkSize(id) % sizeof(datatype) == 0,
109  "Cannot read chunk " + id + " of " + std::to_string(chunkSize(id)) + " bytes in a vector of elements"
110  " of " + std::to_string(sizeof(datatype)) + " bytes each");
111 #endif
112  std::vector<datatype> result;
113  result.resize(chunkSize(id) / sizeof(datatype));
114  fetch(id, static_cast<void*>(result.data()), result.size() * sizeof(datatype));
115  return result;
116  }

◆ read() [2/2]

template<>
std::string Beatmup::ChunkCollection::read ( const std::string &  id)
inline

Definition at line 121 of file chunkfile.h.

121  {
122  std::string result;
123  result.resize(chunkSize(id));
124  fetch(id, const_cast<char*>(result.data()), result.size());
125  return result;
126  }

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