OpenEnroth 73e68f7
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Attributes | Friends | List of all members
Blob Class Referencefinal

#include <Blob.h>

Public Member Functions

 Blob ()
 
 Blob (const Blob &)=delete
 
 Blob (Blob &&other)
 
 ~Blob ()=default
 
Bloboperator= (const Blob &)=delete
 
Bloboperator= (Blob &&other)
 
Blob subBlob (size_t offset, size_t size=-1) const
 
size_t size () const
 
bool empty () const
 
const void * data () const
 
bool operator! () const
 
 operator bool () const
 
std::string_view string_view () const
 
const std::string & displayPath () const
 
Blob withDisplayPath (std::string_view displayPath)
 

Static Public Member Functions

static Blob fromMalloc (const void *data, size_t size)
 
static Blob fromMalloc (std::unique_ptr< void, FreeDeleter > data, size_t size)
 
static Blob fromFile (std::string_view path)
 
static Blob fromString (std::string string)
 
static Blob copy (const void *data, size_t size)
 
static Blob copy (const Blob &other)
 
static Blob view (const void *data, size_t size)
 
static Blob view (std::string_view data)
 
static Blob read (FILE *file, size_t size)
 
static Blob read (InputStream *stream, size_t size)
 
static Blob concat (const Blob &l, const Blob &r)
 
static Blob share (const Blob &other)
 

Private Attributes

const void * _data = nullptr
 
size_t _size = 0
 
std::shared_ptr< void > _state
 
std::string _displayPath
 

Friends

void swap (Blob &l, Blob &r)
 

Detailed Description

Blob is an abstraction that couples a contiguous memory region with the knowledge of how to deallocate it.

Deallocation is type-erased (like it's done in std::shared_ptr), so you don't have to pass in deleter as a template parameter.

For debug and error reporting purposes, Blob also stores a string describing the data source.

Constructor & Destructor Documentation

◆ Blob() [1/3]

Blob::Blob ( )
inline

◆ Blob() [2/3]

Blob::Blob ( const Blob )
delete

◆ Blob() [3/3]

Blob::Blob ( Blob &&  other)
inline

◆ ~Blob()

Blob::~Blob ( )
default

Member Function Documentation

◆ concat()

Blob Blob::concat ( const Blob l,
const Blob r 
)
static
Parameters
lFirst blob.
rSecond blob.
Returns
Newly allocated blob that contains a concatenation of the data of the two provided blobs.

◆ copy() [1/2]

Blob Blob::copy ( const Blob other)
static
Parameters
otherBlob to copy.
Returns
Copy of the provided Blob in malloc-allocated memory.

◆ copy() [2/2]

Blob Blob::copy ( const void *  data,
size_t  size 
)
static
Parameters
dataMemory region pointer.
sizeMemory region size.
Returns
Blob that owns a copy of the provided memory region.

◆ data()

const void * Blob::data ( ) const
inline

◆ displayPath()

const std::string & Blob::displayPath ( ) const
inline

◆ empty()

bool Blob::empty ( ) const
inline

◆ fromFile()

Blob Blob::fromFile ( std::string_view  path)
static
Parameters
pathPath to a file.
Returns
Blob that wraps the memory mapping of the provided file, with display path set to path.
Exceptions
std::runtime_errorIf file doesn't exist or on some other OS error.

◆ fromMalloc() [1/2]

Blob Blob::fromMalloc ( const void *  data,
size_t  size 
)
static
Parameters
dataPointer to a malloc-allocated memory region.
sizeSize of the memory region.
Returns
Blob that takes ownership of the provided memory region.

◆ fromMalloc() [2/2]

Blob Blob::fromMalloc ( std::unique_ptr< void, FreeDeleter data,
size_t  size 
)
static

◆ fromString()

Blob Blob::fromString ( std::string  string)
static
Parameters
stringString to create a blob from.
Returns
Blob that wraps the provided string.

◆ operator bool()

Blob::operator bool ( ) const
inlineexplicit

◆ operator!()

bool Blob::operator! ( ) const
inline

◆ operator=() [1/2]

Blob & Blob::operator= ( Blob &&  other)
inline

◆ operator=() [2/2]

Blob & Blob::operator= ( const Blob )
delete

◆ read() [1/2]

Blob Blob::read ( FILE *  file,
size_t  size 
)
static
Parameters
fileFile to read from.
sizeNumber of bytes to read.
Returns
Blob that owns the data that was read from the provided file.
Exceptions
ExceptionIf the provided number of bytes couldn't be read.

◆ read() [2/2]

Blob Blob::read ( InputStream stream,
size_t  size 
)
static
Parameters
streamStream to read from.
sizeNumber of bytes to read.
Returns
Blob that owns the data that was read from the provided file, with display path set to the display path of the passed stream.
Exceptions
ExceptionIf the provided number of bytes couldn't be read.

◆ share()

Blob Blob::share ( const Blob other)
static
Parameters
otherBlob to share.
Returns
Blob that shares the memory ownership with other.

◆ size()

size_t Blob::size ( ) const
inline

◆ string_view()

std::string_view Blob::string_view ( ) const
inline

◆ subBlob()

Blob Blob::subBlob ( size_t  offset,
size_t  size = -1 
) const

Pretty much like substr, but for Blobs.

Important difference from substr is that the underlying memory is not copied and is instead shared between the two blobs. It will be freed when both are destroyed, so cases where a small subblob holds a 100Mb chunk of memory are possible, be careful.

Also, this function doesn't throw std::out_of_range if offset is larger than the blob's size, returning an empty blob in this case instead.

Parameters
offsetOffset in the blob.
sizeSize of the subblob.
Returns
Subblob that shares the ownership of the underlying memory with this blob.

◆ view() [1/2]

Blob Blob::view ( const void *  data,
size_t  size 
)
static
Parameters
dataMemory region pointer.
sizeMemory region size.
Returns
Non-owning blob view into the provided memory region.

◆ view() [2/2]

Blob Blob::view ( std::string_view  data)
static
Parameters
dataMemory region
Returns
Non-owning blob view into the provided memory region.

◆ withDisplayPath()

Blob Blob::withDisplayPath ( std::string_view  displayPath)
inline

Friends And Related Function Documentation

◆ swap

void swap ( Blob l,
Blob r 
)
friend

Member Data Documentation

◆ _data

const void* Blob::_data = nullptr
private

◆ _displayPath

std::string Blob::_displayPath
private

◆ _size

size_t Blob::_size = 0
private

◆ _state

std::shared_ptr<void> Blob::_state
private

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