Data

Wrapper for data located in external memory, to prevent faux references. Represents a slice of data, which may or may not be in unmanaged memory. Data in unmanaged memory is bound to a DataWrapper class instance.

All operations on this class should be safe, except for accessing contents directly. All operations on contents must be accompanied by a live reference to the Data object, to keep a GC anchor towards the unmanaged data.

Concatenations and appends to Data contents will cause reallocations on the heap, consider using Data instead.

Be sure not to lose Data references while using their contents! For example, avoid code like this:

fun(cast(string)transformSomeData(someData).contents);

The Data return value may be unreachable once .contents is evaluated. Use .toHeap instead of .contents in such cases to get a safe heap copy.

Constructors

this
this(const(void)[] data, bool forceReallocation)
this(void[] data, bool forceReallocation)

Create new instance wrapping the given data.

this
this(size_t size, size_t capacity)

Create a new instance with given size/capacity. Capacity defaults to size.

this
this(DataWrapper wrapper, bool mutable)

Create a new instance slicing all of the given wrapper's contents.

Destructor

~this
~this()
Undocumented in source.

Postblit

this(this)
this(this)
Undocumented in source.

Members

Aliases

opDollar
alias opDollar = length
Undocumented in source.

Functions

append
Data append(const(void)[] data)

Append data to this Data. Unlike concatenation (a ~ b), appending (a ~= b) will preallocate.

clear
void clear()

Unreference contents, freeing it if this was the last reference.

concat
Data concat(const(void)[] data)

Create a new Data containing the concatenation of this and data. Does not preallocate for successive appends.

deleteContents
void deleteContents()

This used to be an unsafe method which deleted the wrapped data. Now that Data is refcounted, this simply calls clear() and additionally asserts that this Data is the only Data holding a reference to the wrapper.

opCast
bool opCast()
Undocumented in source. Be warned that the author may not have intended to support it.
opSlice
Data opSlice()
Data opSlice(size_t x, size_t y)

Returns a Data pointing at a slice of this Data's contents.

popFront
Data popFront(size_t size)

Return a new Data for the first size bytes, and slice this instance from size to end.

prepend
Data prepend(const(void)[] data)

Create a new Data containing the concatenation of data and this. Does not preallocate for successive appends.

Properties

capacity
size_t capacity [@property getter]

Return maximum value that can be set to length without causing a reallocation

contents
const(void)[] contents [@property getter]

Get contents

dup
Data dup [@property getter]

Create a copy of the data

empty
bool empty [@property getter]

True if contents is unset

length
size_t length [@property getter]

Size in bytes of contents

length
size_t length [@property setter]

Resize contents

mcontents
void[] mcontents [@property getter]

Get mutable contents

mptr
void* mptr [@property getter]

Get pointer to mutable contents

ptr
const(void)* ptr [@property getter]

Get pointer to contents

toHeap
void[] toHeap [@property getter]

Put a copy of the data on D's managed heap, and return it.

Templates

opBinary
template opBinary(string op)

Create a new Data containing the concatenation of this and data. Does not preallocate for successive appends.

opBinaryRight
template opBinaryRight(string op)

Create a new Data containing the concatenation of data and this. Does not preallocate for successive appends.

opOpAssign
template opOpAssign(string op)

Append data to this Data. Unlike concatenation (a ~ b), appending (a ~= b) will preallocate.

Meta