nlib
nn::nlib::BinaryWriter Class Referencefinal

The class for writing binary to streams (to OutputStream). More...

#include "nn/nlib/BinaryWriter.h"

Public Types

enum  EndianSetting {
  DEFAULT = 0,
  ENDIAN_LITTLE,
  ENDIAN_BIG
}
 Specifies the endianness. More...
 

Public Member Functions

bool Init (OutputStream *stream, EndianSetting endian=DEFAULT) noexcept
 Initializes a binary writer for the specified stream and specified endianness. More...
 
template<class T >
bool Write (T x) noexcept
 Calls binary_writer::Write().
 
template<class T >
bool WriteArray (const T *x, size_t n) noexcept
 Calls binary_writer::WriteArray.
 
template<class T , size_t N>
bool WriteArray (const T(&a)[N]) noexcept
 Writes a numeric-type array to a stream. More...
 
bool Flush () noexcept
 Flushes the binary writer and the base stream. More...
 
bool Close () noexcept
 Closes the binary writer. More...
 
void SetError (errno_t e) noexcept
 Sets an error. If an error has been set already, this function does not set an error. More...
 
errno_t GetErrorValue () const noexcept
 This function can get the cause of the error when writing has failed. More...
 
OutputStreamGetStream () noexcept
 Gets the stream for the text writer to write to. More...
 
 operator bool () const
 Returns true if no internal error has occurred.
 
Basic Member Functions
 BinaryWriter () noexcept
 Instantiates the object with default parameters (default constructor).
 
 ~BinaryWriter () noexcept
 Destructor. This function does not close the stream.
 
Writing Basic Data Types
bool Write (char x) noexcept
 Writes char-type data as binary to a stream. Returns true if successful.
 
bool Write (signed char x) noexcept
 Writes signed char-type data as binary to a stream. Returns true if successful.
 
bool Write (unsigned char x) noexcept
 Writes unsigned char-type data as binary to a stream. Returns true if successful.
 
bool Write (short x) noexcept
 Writes short-type data as binary to a stream. Returns true if successful.
 
bool Write (unsigned short x) noexcept
 Writes unsigned short-type data as binary to a stream. Returns true if successful.
 
bool Write (int x) noexcept
 Writes int-type data as binary to a stream. Returns true if successful.
 
bool Write (unsigned int x) noexcept
 Writes unsigned int-type data as binary to a stream. Returns true if successful.
 
bool Write (long x) noexcept
 Writes long-type data as binary to a stream. Returns true if successful.
 
bool Write (unsigned long x) noexcept
 Writes unsigned long-type data as binary to a stream. Returns true if successful.
 
bool Write (long long x) noexcept
 Writes long long-type data as binary to a stream. Returns true if successful.
 
bool Write (unsigned long long x) noexcept
 Writes unsigned long long-type data as binary to a stream. Returns true if successful.
 
bool Write (float x) noexcept
 Writes float-type data as binary to a stream. Returns true if successful.
 
bool Write (double x) noexcept
 Writes double-type data as binary to a stream. Returns true if successful.
 
Writing Basic Data Type Arrays
bool WriteArray (const unsigned char *x, size_t n) noexcept
 Writes an unsigned char-type array as binary to a stream. Returns true if successful. If n is 0, it will also successfully complete.
 
bool WriteArray (const unsigned short *x, size_t n) noexcept
 Writes an unsigned short-type array as binary to a stream. Returns true if successful. If n is 0, it will also successfully complete.
 
bool WriteArray (const unsigned int *x, size_t n) noexcept
 Writes an unsigned int-type array as binary to a stream. Returns true if successful. If n is 0, it will also successfully complete.
 
bool WriteArray (const unsigned long long *x, size_t n) noexcept
 Writes an unsigned long long-type array as binary to a stream. Returns true if successful. If n is 0, it will also successfully complete.
 
bool WriteArray (const unsigned long *x, size_t n) noexcept
 Writes an unsigned long-type array as binary to a stream. Returns true if successful. If n is 0, it will also successfully complete.
 
bool WriteArray (const float *x, size_t n) noexcept
 Writes a float-type array as binary to a stream. Returns true if successful. If n is 0, it will also successfully complete.
 
bool WriteArray (const double *x, size_t n) noexcept
 Writes a double-type array as binary to a stream. Returns true if successful. If n is 0, it will also successfully complete.
 
bool WriteArray (const signed char *x, size_t n) noexcept
 Writes a signed char-type array as binary to a stream. Returns true if successful. If n is 0, it will also successfully complete.
 
bool WriteArray (const char *x, size_t n) noexcept
 Writes a char-type array as binary to a stream. Returns true if successful. If n is 0, it will also successfully complete.
 
bool WriteArray (const short *x, size_t n) noexcept
 Writes a short-type array as binary to a stream. Returns true if successful. If n is 0, it will also successfully complete.
 
bool WriteArray (const int *x, size_t n) noexcept
 Writes an int-type array as binary to a stream. Returns true if successful. If n is 0, it will also successfully complete.
 
bool WriteArray (const long *x, size_t n) noexcept
 Writes a long-type array as binary to a stream. Returns true if successful. If n is 0, it will also successfully complete.
 
bool WriteArray (const long long *x, size_t n) noexcept
 Writes a long long-type array as binary to a stream. Returns true if successful. If n is 0, it will also successfully complete.
 

Detailed Description

The class for writing binary to streams (to OutputStream).

Description
The endianness of the stream data can be specified, in which case the writing to the stream is performed accordingly.
OutputStream* stream = ....;
BinaryWriter writer;
// Writes in little-endian.
if (!writer.Init(stream, BinaryWriter::ENDIAN_LITTLE)) { Error; }
....
writer.Write(a value of various numeric types); // Writes a single numerical value.
....
writer.WriteArray(pointer to an array of various numeric-types, number of elements); // Writes an array.
writer.Flush();
if (!writer) { Error; }
writer.Close();

Definition at line 13 of file BinaryWriter.h.

Member Enumeration Documentation

Specifies the endianness.

Enumerator
DEFAULT 

Writes data in the same endianness as the machine the program is running on.

ENDIAN_LITTLE 

Writes in little-endian.

ENDIAN_BIG 

Writes in big-endian.

Definition at line 15 of file BinaryWriter.h.

Member Function Documentation

nn::nlib::BinaryWriter::Close ( )
noexcept

Closes the binary writer.

Returns
Returns true when successful.
Description
Closes the binary writer after flushing the base stream and then detaches the base stream. The base stream is not closed by this operation.
nn::nlib::BinaryWriter::Flush ( )
noexcept

Flushes the binary writer and the base stream.

Returns
Returns true when successful.
nn::nlib::BinaryWriter::GetErrorValue ( ) const
inlinenoexcept

This function can get the cause of the error when writing has failed.

Return values
0No error occurred.
EINVALInvalid argument.
EEXISTInitialized redundantly.
EBADFNo stream to write to.
EIOWriting to the stream has failed for some reason.

Definition at line 94 of file BinaryWriter.h.

nn::nlib::BinaryWriter::GetStream ( )
inlinenoexcept

Gets the stream for the text writer to write to.

Returns
The pointer to the stream.

Definition at line 95 of file BinaryWriter.h.

nn::nlib::BinaryWriter::Init ( OutputStream stream,
EndianSetting  endian = DEFAULT 
)
noexcept

Initializes a binary writer for the specified stream and specified endianness.

Parameters
[in]streamA stream.
[in]endianSpecifies the endian.
Returns
Returns true when successful.
Description
The function returns false if it is already initialized or if stream is NULL.
nn::nlib::BinaryWriter::SetError ( errno_t  e)
inlinenoexcept

Sets an error. If an error has been set already, this function does not set an error.

Parameters
[in]eAn error value.
Description
If you have user code that writes user-defined types and you want to generate errors, you can use this function for that purpose.

Definition at line 91 of file BinaryWriter.h.

template<class T , size_t N>
nn::nlib::BinaryWriter::WriteArray ( const T(&)  a[N])
inlinenoexcept

Writes a numeric-type array to a stream.

Template Parameters
TThe numeric type (such as int32_t or float).
Parameters
[in]aThe array to write.
Returns
Returns true when successful.

Definition at line 140 of file BinaryWriter.h.


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