CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
CeresEngine::CBOR::Encoder Class Referencefinal

The Encoder class is a CBOR (Concise Binary Object Representation) encoder that provides an abstraction for serializing CBOR data to an output stream. More...

#include <CeresEngine/Foundation/CBOR.hpp>

Public Member Functions

 Encoder (OutputStream outputStream)
 Creates a new CBOR encoder from an OutputStream.
 
 Encoder (IOutputStream &outputStream)
 Creates a new CBOR encoder from an IOutputStream.
 
void write (UInt64 value)
 Encodes an unsigned integer into the CBOR stream.
 
void write (Int64 value)
 Encodes a signed integer into the CBOR stream.
 
void write (float value)
 Encodes a float into the CBOR stream.
 
void write (double value)
 Encodes a double into the CBOR stream.
 
void write (bool value)
 Encodes a bool into the CBOR stream.
 
void write (StringView value)
 Encodes a StringView into the CBOR stream.
 
void write (const String &value)
 Encodes a String into the CBOR stream.
 
void write (const char *value)
 Encodes a const char* into the CBOR stream.
 
void write (Binary value)
 Encodes a Binary into the CBOR stream.
 
void write (std::nullptr_t value)
 Encodes a nullptr into the CBOR stream.
 
void write (Undefined value)
 Encodes an undefined into the CBOR stream.
 
void write (Break value)
 Encodes an Break into the CBOR stream.
 
void break_ ()
 Encodes an Break into the CBOR stream.
 
void write (Array value)
 Encodes an array header into the CBOR stream.
 
void array (const UInt64 size)
 Encodes an array header into the CBOR stream.
 
template<typename Func >
void array (const UInt64 size, Func &&func)
 Encodes an array header into the CBOR stream.
 
void array ()
 Encodes an array header into the CBOR stream.
 
template<typename Func >
void array (Func &&func)
 Encodes an array header into the CBOR stream.
 
void write (Map value)
 Encodes a map header into the CBOR stream.
 
void map (const UInt64 size)
 Encodes an map header into the CBOR stream.
 
template<typename Func >
void map (const UInt64 size, Func &&func)
 Encodes a map header into the CBOR stream.
 
void map ()
 Encodes an map header into the CBOR stream.
 
template<typename Func >
void map (Func &&func)
 Encodes a map header into the CBOR stream.
 
void write (Tag value)
 Encodes a tag header into the CBOR stream.
 
void tag (const UInt64 id)
 Encodes an tag header into the CBOR stream.
 
template<typename Func >
void tag (const UInt64 id, Func &&func)
 Encodes a tag header into the CBOR stream.
 
void binary (const void *data, size_t length)
 Encodes binary data into the CBOR stream.
 
void binary (const MemoryView< const Byte > &data)
 Encodes binary data into the CBOR stream.
 
void binary (const FunctionView< void(OutputStream &) const > &func)
 Encodes binary data into the CBOR stream.
 
void binary (UInt64 length, const FunctionView< void(OutputStream &) const > &func)
 Encodes binary data into the CBOR stream.
 
void write (Value &&value)
 Encodes a value into the CBOR stream.
 
bool flush ()
 
template<typename T >
requires std::is_enum_v<T>
void write (T value)
 Encodes an enum value into the CBOR stream.
 

Private Attributes

BufferedOutputStream mStream
 The stream to write to.
 

Detailed Description

The Encoder class is a CBOR (Concise Binary Object Representation) encoder that provides an abstraction for serializing CBOR data to an output stream.

It's designed to encode CBOR values to an instance of an OutputStream. It provides an efficient and convenient way to write and encode CBOR data streams, without having to create a representation of the entire data stream into memory.

Constructor & Destructor Documentation

◆ Encoder() [1/2]

CeresEngine::CBOR::Encoder::Encoder ( OutputStream  outputStream)
inlineexplicit

Creates a new CBOR encoder from an OutputStream.

◆ Encoder() [2/2]

CeresEngine::CBOR::Encoder::Encoder ( IOutputStream outputStream)
inlineexplicit

Creates a new CBOR encoder from an IOutputStream.

Member Function Documentation

◆ array() [1/4]

void CeresEngine::CBOR::Encoder::array ( )
inline

Encodes an array header into the CBOR stream.

The user is responsible for encoding the correct number of elements and calling break_() once done.

Note
The array will have indeterminate length and break_() must be called to finalize it.

◆ array() [2/4]

void CeresEngine::CBOR::Encoder::array ( const UInt64  size)
inline

Encodes an array header into the CBOR stream.

The user is responsible for encoding the correct number of elements.

Parameters
sizeThe number of elements in the array.

◆ array() [3/4]

template<typename Func >
void CeresEngine::CBOR::Encoder::array ( const UInt64  size,
Func &&  func 
)
inline

Encodes an array header into the CBOR stream.

Parameters
sizeThe number of elements in the array.
funcA function to be called after serializing the header.

◆ array() [4/4]

template<typename Func >
void CeresEngine::CBOR::Encoder::array ( Func &&  func)
inline

Encodes an array header into the CBOR stream.

Note
This function will automatically call break_() to finalize the array once func returns.
Parameters
funcA function to be called after serializing the header.

◆ binary() [1/4]

void CeresEngine::CBOR::Encoder::binary ( const FunctionView< void(OutputStream &) const > &  func)

Encodes binary data into the CBOR stream.

This function allows encoding of binary data directly into the CBOR stream by executing the given function with an OutputStream that the user can use to write the binary data. The function passed OutputStream is a special stream that will automatically encode the indefinite length byte string as data is written to it.

Parameters
funcA FunctionView that takes an OutputStream reference as an argument. This function is executed to encode the binary data into the CBOR stream.

◆ binary() [2/4]

void CeresEngine::CBOR::Encoder::binary ( const MemoryView< const Byte > &  data)
inline

Encodes binary data into the CBOR stream.

Parameters
dataA pointer to the binary data.
lengthThe length of the binary data.

◆ binary() [3/4]

void CeresEngine::CBOR::Encoder::binary ( const void data,
size_t  length 
)

Encodes binary data into the CBOR stream.

Parameters
dataA pointer to the binary data.
lengthThe length of the binary data.

◆ binary() [4/4]

void CeresEngine::CBOR::Encoder::binary ( UInt64  length,
const FunctionView< void(OutputStream &) const > &  func 
)

Encodes binary data into the CBOR stream.

This function allows encoding of binary data directly into the CBOR stream by executing the given function with an OutputStream that the user can use to write the binary data. The function passed OutputStream is a special stream that will ensure that only the right amount of bytes are written to it.

Parameters
lengthThe number of bytes to encode into the CBOR byte string. The caller must ensure that it writes exactly this many bytes into the given stream.
funcA FunctionView that takes an OutputStream reference as an argument. This function is executed to encode the binary data into the CBOR stream.

◆ break_()

void CeresEngine::CBOR::Encoder::break_ ( )
inline

Encodes an Break into the CBOR stream.

◆ flush()

bool CeresEngine::CBOR::Encoder::flush ( )
inline

◆ map() [1/4]

void CeresEngine::CBOR::Encoder::map ( )
inline

Encodes an map header into the CBOR stream.

The user is responsible for encoding the correct number of key-value pairs and calling break_() once done.

Note
The map will have indeterminate length and break_() must be called to finalize it.

◆ map() [2/4]

void CeresEngine::CBOR::Encoder::map ( const UInt64  size)
inline

Encodes an map header into the CBOR stream.

The user is responsible for encoding the correct number of key-value pairs.

Parameters
sizeThe number of key-value pairs in the map.

◆ map() [3/4]

template<typename Func >
void CeresEngine::CBOR::Encoder::map ( const UInt64  size,
Func &&  func 
)
inline

Encodes a map header into the CBOR stream.

Parameters
sizeThe number of key-value pairs in the map.
funcA function to be called after serializing the header.

◆ map() [4/4]

template<typename Func >
void CeresEngine::CBOR::Encoder::map ( Func &&  func)
inline

Encodes a map header into the CBOR stream.

Note
This function will automatically call break_() to finalize the array once func returns.
Parameters
funcA function to be called after serializing the header.

◆ tag() [1/2]

void CeresEngine::CBOR::Encoder::tag ( const UInt64  id)
inline

Encodes an tag header into the CBOR stream.

The user is responsible for encoding the tag contents.

Parameters
idThe tag unique identifier.

◆ tag() [2/2]

template<typename Func >
void CeresEngine::CBOR::Encoder::tag ( const UInt64  id,
Func &&  func 
)
inline

Encodes a tag header into the CBOR stream.

Parameters
idThe tag unique identifier.
funcA function to be called after serializing the header.

◆ write() [1/17]

void CeresEngine::CBOR::Encoder::write ( Array  value)

Encodes an array header into the CBOR stream.

◆ write() [2/17]

void CeresEngine::CBOR::Encoder::write ( Binary  value)

Encodes a Binary into the CBOR stream.

◆ write() [3/17]

void CeresEngine::CBOR::Encoder::write ( bool  value)

Encodes a bool into the CBOR stream.

◆ write() [4/17]

void CeresEngine::CBOR::Encoder::write ( Break  value)

Encodes an Break into the CBOR stream.

◆ write() [5/17]

void CeresEngine::CBOR::Encoder::write ( const char value)
inline

Encodes a const char* into the CBOR stream.

◆ write() [6/17]

void CeresEngine::CBOR::Encoder::write ( const String value)
inline

Encodes a String into the CBOR stream.

◆ write() [7/17]

void CeresEngine::CBOR::Encoder::write ( double  value)

Encodes a double into the CBOR stream.

◆ write() [8/17]

void CeresEngine::CBOR::Encoder::write ( float  value)

Encodes a float into the CBOR stream.

◆ write() [9/17]

void CeresEngine::CBOR::Encoder::write ( Int64  value)

Encodes a signed integer into the CBOR stream.

◆ write() [10/17]

void CeresEngine::CBOR::Encoder::write ( Map  value)

Encodes a map header into the CBOR stream.

◆ write() [11/17]

void CeresEngine::CBOR::Encoder::write ( std::nullptr_t  value)

Encodes a nullptr into the CBOR stream.

◆ write() [12/17]

void CeresEngine::CBOR::Encoder::write ( StringView  value)

Encodes a StringView into the CBOR stream.

◆ write() [13/17]

template<typename T >
requires std::is_enum_v<T>
void CeresEngine::CBOR::Encoder::write ( T  value)
inline

Encodes an enum value into the CBOR stream.

◆ write() [14/17]

void CeresEngine::CBOR::Encoder::write ( Tag  value)

Encodes a tag header into the CBOR stream.

◆ write() [15/17]

void CeresEngine::CBOR::Encoder::write ( UInt64  value)

Encodes an unsigned integer into the CBOR stream.

◆ write() [16/17]

void CeresEngine::CBOR::Encoder::write ( Undefined  value)

Encodes an undefined into the CBOR stream.

◆ write() [17/17]

void CeresEngine::CBOR::Encoder::write ( Value &&  value)

Encodes a value into the CBOR stream.

Member Data Documentation

◆ mStream

BufferedOutputStream CeresEngine::CBOR::Encoder::mStream
private

The stream to write to.

This is a buffered OutputStream and it's very important that this is referenced by value (not through the OutputStream wrapper), as it allows to reduce the number of virtual calls and significantly increase the performance.


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