nlib
nn::nlib::ReallocOutputStream Class Referencefinal

The class for output streams written to memory expanded using nlib_realloc or another realloc function. More...

#include "nn/nlib/ReallocOutputStream.h"

+ Inheritance diagram for nn::nlib::ReallocOutputStream:

Public Types

typedef UniquePtr< uint8_t[], ReallocDeleter< uint8_t > > UniquePtrType
 The typedefed UniquePtr to a uint8_t array. More...
 
typedef UniquePtr< char[], ReallocDeleter< char > > CharPtrType
 The typedefed UniquePtr to a char array.
 
- Public Types inherited from nn::nlib::OutputStream
enum  BufferingMode {
  BUFFERINGMODE_BLOCKBUFFERED = 0,
  BUFFERINGMODE_LINEBUFFERED,
  BUFFERINGMODE_UNBUFFERED
}
 The buffering mode for OutputStream. More...
 

Public Member Functions

uint8_t * Data () noexcept
 Gets the pointer to the start of the data that is in the process of being written. More...
 
bool Reserve (size_t n) noexcept
 Allocates a buffer of the specified size for writing to. More...
 
void ShrinkToFit () noexcept
 Makes the buffer size fit to the current write position. More...
 
size_t Release (UniquePtrType *ptr) noexcept
 Closes a stream and moves the data that was written to the stream to ptr. More...
 
bool ReleaseAsCstring (CharPtrType *ptr) noexcept
 Closes a stream after adding 0 and moves the string that was written to the stream to ptr. More...
 
Basic Member Functions
 ReallocOutputStream () noexcept
 Instantiates the object with default parameters (default constructor). Configures the object to expand memory using the nlib_realloc function.
 
 ReallocOutputStream (ReallocFunc func) noexcept
 Specifies a realloc function and instantiates an object. More...
 
virtual ~ReallocOutputStream () noexcept override
 Destructor.
 
- Public Member Functions inherited from nn::nlib::OutputStream
size_t Pos () const noexcept
 Returns the current position in the stream. More...
 
uint64_t Pos64 () const noexcept
 Returns the current position in the stream as a 64-bit integer. More...
 
bool Write (int b) noexcept
 Writes one byte of data to the stream. More...
 
bool Write (const void *p, size_t n) noexcept
 Writes n bytes of data to the stream. More...
 
bool WriteGather (const nlib_fd_iovec *iov, int iovcnt) noexcept
 Writes data from multiple non-continuous buffers to a stream. More...
 
bool Flush () noexcept
 Flushes the stream. More...
 
bool Close () noexcept
 Closes the stream after it has been flushed. Returns true if successful. More...
 
errno_t GetErrorValue () const noexcept
 Gets the error value. More...
 
bool IsOk () const noexcept
 Checks that no error has occurred. More...
 
BufferingMode GetBufferingMode () const noexcept
 Gets the buffering mode.
 
 operator bool () const
 Returns true if no internal error has occurred.
 
constexpr OutputStream () noexcept
 Instantiates the object with default parameters (default constructor).
 
virtual ~OutputStream () noexcept
 Destructor. Does not do anything.
 

Additional Inherited Members

- Protected Member Functions inherited from nn::nlib::OutputStream
void ResetBuffer (void *p, size_t nBytes) noexcept
 Sets the buffer held by OutputStream. More...
 
void SetError (errno_t e) const noexcept
 Sets an error to OutputStream. More...
 
- Protected Attributes inherited from nn::nlib::OutputStream
BufferingMode m_BufferingMode
 Stores the buffering mode. More...
 

Detailed Description

The class for output streams written to memory expanded using nlib_realloc or another realloc function.

Description
The memory region storing the written data is expanded by 4096 bytes at a time. After data has been written, it can be obtained as either a byte string or as a C string.
// Writes data using the OutputStream interface.
....
os.Flush();
// 1) If you want to get the data as a byte string.
size_t nBytes = os.Release(&data);
// Use the data.get function to get the pointer (type uint8_t*).
// nBytes matches the amount of written data.
// os gets closed.
// 2) If you want to get the data as a C string.
bool result = os.ReleaseAsCstring(&cstr);
if (result) { Error; }
// The data is moved to cstr after '0' is written to the end of ReleaseAsCstring.
// Use the cstr.get function to get type char*.
// os gets closed.
Examples:
exi/serializer/serializer.cpp, exi/textparser/textparser.cpp, misc/usezlib/usezlib.cpp, msgpack/jsonrpc/jsonrpc.cpp, msgpack/jsonrpc/server.cpp, msgpack/msgpack1/msgpack1.cpp, msgpack/msgpack2/msgpack2.cpp, msgpack/usertype/usertype.cpp, and oss/binarypatch/binarypatch.cpp.

Definition at line 60 of file ReallocOutputStream.h.

Member Typedef Documentation

The typedefed UniquePtr to a uint8_t array.

Description
It is freed by the free function when the destructor is started.
Examples:
msgpack/jsonrpc/jsonrpc.cpp, and msgpack/jsonrpc/server.cpp.

Definition at line 64 of file ReallocOutputStream.h.

Constructor & Destructor Documentation

nn::nlib::ReallocOutputStream::ReallocOutputStream ( ReallocFunc  func)
explicitnoexcept

Specifies a realloc function and instantiates an object.

Parameters
[in]funcThe function that performs the realloc operation.

Member Function Documentation

nn::nlib::ReallocOutputStream::Data ( )
inlinenoexcept

Gets the pointer to the start of the data that is in the process of being written.

Returns
The pointer to the data.
Description
Note that this pointer might get changed by the realloc operation during writing.

Definition at line 72 of file ReallocOutputStream.h.

nn::nlib::ReallocOutputStream::Release ( UniquePtrType ptr)
noexcept

Closes a stream and moves the data that was written to the stream to ptr.

Parameters
[out]ptrPointer where the data is set.
Returns
The data size.
nn::nlib::ReallocOutputStream::ReleaseAsCstring ( CharPtrType ptr)
noexcept

Closes a stream after adding 0 and moves the string that was written to the stream to ptr.

Parameters
[out]ptrPointer where the C string is set.
Returns
Returns true if the function succeeded and 0 was successfully added.
nn::nlib::ReallocOutputStream::Reserve ( size_t  n)
inlinenoexcept

Allocates a buffer of the specified size for writing to.

Parameters
[in]nThe size of the buffer to allocate.
Returns
Returns true when allocated successfully.
Examples:
misc/usezlib/usezlib.cpp.

Definition at line 75 of file ReallocOutputStream.h.

nn::nlib::ReallocOutputStream::ShrinkToFit ( )
inlinenoexcept

Makes the buffer size fit to the current write position.

Description
This function also is called when the Release and ReleaseAsCstring functions execute.

Definition at line 76 of file ReallocOutputStream.h.


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