Beatmup
Beatmup::Chunk Class Reference

Simply a piece of binary data of a specific size. More...

#include <chunkfile.h>

Public Member Functions

 Chunk ()
 Makes an empty chunk. More...
 
 Chunk (size_t size)
 Allocates a chunk of a given size. More...
 
 Chunk (ChunkCollection &collection, const std::string &id)
 Reads a chunk from a collection. More...
 
 ~Chunk ()
 
 Chunk (Chunk &&chunk)
 
Chunkoperator= (Chunk &&chunk)
 
void writeTo (ChunkFileWriter &file, const std::string &id) const
 Writes a chunk out to a file. More...
 
size_t size () const
 
void * operator() ()
 
const void * operator() () const
 
 operator bool () const
 
template<typename datatype >
datatype * ptr (size_t offset=0)
 
template<typename datatype >
const datatype * ptr (size_t offset=0) const
 
template<typename datatype >
datatype at (size_t offset) const
 
template<typename datatype >
datatype & at (size_t offset)
 

Private Attributes

size_t chunkSize
 
void * data
 

Detailed Description

Simply a piece of binary data of a specific size.

A brick in ChunkCollection-based data sets. No information is stored on the nature of the data: it is up to the application to interpret the chunk content.

Definition at line 210 of file chunkfile.h.

Constructor & Destructor Documentation

◆ Chunk() [1/4]

Beatmup::Chunk::Chunk ( )
inline

Makes an empty chunk.

Definition at line 218 of file chunkfile.h.

218 : chunkSize(0), data(nullptr) {}
size_t chunkSize
Definition: chunkfile.h:212

◆ Chunk() [2/4]

Chunk::Chunk ( size_t  size)

Allocates a chunk of a given size.

Parameters
[in]sizeChunk size in bytes

Definition at line 153 of file chunkfile.cpp.

153  : chunkSize(size) {
154  data = malloc(size);
155 }
size_t size() const
Definition: chunkfile.h:257

◆ Chunk() [3/4]

Chunk::Chunk ( ChunkCollection collection,
const std::string &  id 
)

Reads a chunk from a collection.

Parameters
[in]collectionThe collection to read from
[in]idThe chunk id to find in the collection

Definition at line 158 of file chunkfile.cpp.

158  :
159  Chunk(collection.chunkSize(id))
160 {
161 #ifdef BEATMUP_DEBUG
162  DebugAssertion::check(collection.chunkExists(id), "Chunk not found: " + id);
163 #endif
164  collection.fetch(id, data, chunkSize);
165 }
virtual bool chunkExists(const std::string &id) const =0
Check if a specific chunk exists.
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.
Chunk()
Makes an empty chunk.
Definition: chunkfile.h:218

◆ ~Chunk()

Chunk::~Chunk ( )

Definition at line 173 of file chunkfile.cpp.

173  {
174  free(data);
175 }

◆ Chunk() [4/4]

Beatmup::Chunk::Chunk ( Chunk &&  chunk)
inline

Definition at line 235 of file chunkfile.h.

235  : chunkSize(chunk.chunkSize), data(chunk.data) {
236  chunk.data = nullptr;
237  chunk.chunkSize = 0;
238  }

Member Function Documentation

◆ operator=()

Chunk& Beatmup::Chunk::operator= ( Chunk &&  chunk)
inline

Definition at line 240 of file chunkfile.h.

240  {
241  if (data)
242  free(data);
243  chunkSize = chunk.chunkSize;
244  data = chunk.data;
245  chunk.data = nullptr;
246  chunk.chunkSize = 0;
247  return *this;
248  }

◆ writeTo()

void Chunk::writeTo ( ChunkFileWriter file,
const std::string &  id 
) const

Writes a chunk out to a file.

Parameters
[in]fileThe file to write to
[in]idThe chunk id

Definition at line 168 of file chunkfile.cpp.

168  {
169  file(id, data, chunkSize);
170 }

◆ size()

size_t Beatmup::Chunk::size ( ) const
inline

Definition at line 257 of file chunkfile.h.

257 { return chunkSize; }

◆ operator()() [1/2]

void* Beatmup::Chunk::operator() ( )
inline

Definition at line 259 of file chunkfile.h.

259 { return data; }

◆ operator()() [2/2]

const void* Beatmup::Chunk::operator() ( ) const
inline

Definition at line 260 of file chunkfile.h.

260 { return data; }

◆ operator bool()

Beatmup::Chunk::operator bool ( ) const
inline

Definition at line 262 of file chunkfile.h.

262 { return data != nullptr; }

◆ ptr() [1/2]

template<typename datatype >
datatype* Beatmup::Chunk::ptr ( size_t  offset = 0)
inline

Definition at line 264 of file chunkfile.h.

264  {
265  return (datatype*)(data) + offset;
266  }

◆ ptr() [2/2]

template<typename datatype >
const datatype* Beatmup::Chunk::ptr ( size_t  offset = 0) const
inline

Definition at line 267 of file chunkfile.h.

267  {
268  return (datatype*)(data) + offset;
269  }

◆ at() [1/2]

template<typename datatype >
datatype Beatmup::Chunk::at ( size_t  offset) const
inline

Definition at line 271 of file chunkfile.h.

271  {
272  return ((datatype*)data)[offset];
273  }

◆ at() [2/2]

template<typename datatype >
datatype& Beatmup::Chunk::at ( size_t  offset)
inline

Definition at line 275 of file chunkfile.h.

275  {
276  return ((datatype*)data)[offset];
277  }

Member Data Documentation

◆ chunkSize

size_t Beatmup::Chunk::chunkSize
private

Definition at line 212 of file chunkfile.h.

◆ data

void* Beatmup::Chunk::data
private

Definition at line 213 of file chunkfile.h.


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