This file defines serialization and deserialization functions read
and write
, as well as the print
function for all fundamental types and core data structures.
The file also contains the definition and implementation of the core::memory_stream
class, which may be used to read/write to an in-memory buffer.
The read
, write
, and print
functions in this library follow a very regular argument structure: The first argument is the object to read/write/print. The second argument is the stream to read from/write to/print to. Most functions also require a third (optional) argument called the scribe
, which controls how the subobjects are read/written/printed. The scribe is typically passed to the read/write/print functions when operating on subobjects of the given object.
Calling read/write/print with core::default_scribe will call the same function without the third argument. This largely corresponds to the "default" behavior of those functions.
For example, write(const core::array<T>& a, Stream& out, Writer&&... writer)
will call the function write(a[i], out, writer)
for every element a[i]
in a
. This enables users to define their own scribes, and define new ways to read/write/print objects without having to re-implement the read/write/print functions for container structures such as core::array. The following example demonstrates how the behavior of the print
function for an array of integers can be altered using a custom scribe.
#include <core/io.h> using namespace core; struct my_string_scribe { const char* strings[3]; }; template<typename Stream> bool print(int i, Stream& out, const my_string_scribe& printer) { return print(printer.strings[i], out); } int main() { array<int> a = array<int>(8); a.add(1); a.add(2); a.add(0); print(a, stdout); print(' ', stdout); default_scribe def; print(a, stdout, def); print(' ', stdout); my_string_scribe printer; printer.strings[0] = "vici"; printer.strings[1] = "veni"; printer.strings[2] = "vidi"; print(a, stdout, printer); }
This example has expected output [1, 2, 0] [1, 2, 0] [veni, vidi, vici]
.
sizeof(T)
bytes from in
and writes them to the memory referenced by value
. This function does not perform endianness transformations.in | the stream given by a FILE pointer. |
T | satisfies is_fundamental. |
length
elements from in
and writes them to the native array values
. This function does not perform endianness transformations.in | the stream given by a FILE pointer. |
T | satisfies is_fundamental. |
sizeof(T)
bytes to out
from the memory referenced by value
. This function does not perform endianness transformations.out | the stream given by a FILE pointer. |
T | satisfies is_fundamental. |
length
elements to out
from the native array values
. This function does not perform endianness transformations.out | the stream given by a FILE pointer. |
T | satisfies is_fundamental. |
Prints the character value
to the stream given by the FILE pointer out
.
Prints the int value
to the stream given by the FILE pointer out
.
Prints the long value
to the stream given by the FILE pointer out
.
Prints the long long value
to the stream given by the FILE pointer out
.
Prints the unsigned int value
to the stream given by the FILE pointer out
.
Prints the unsigned long value
to the stream given by the FILE pointer out
.
Prints the unsigned long long value
to the stream given by the FILE pointer out
.
Prints the float value
to the stream given by the FILE pointer out
.
Prints the double value
to the stream given by the FILE pointer out
.
Prints the float value
to the stream given by the FILE pointer out
with the given precision
.
Prints the double value
to the stream given by the FILE pointer out
with the given precision
.
Prints the null-terminated C string value
to the stream given by the FILE pointer out
.
This type trait is true_type if and only if the function bool read(integral&, T&)
is defined where integral
is any integral type.
This type trait is true_type if and only if the function bool fwrite(const integral&, T&)
is defined where integral
is any integral type.
This type trait is true_type if and only if the function bool print(value, T&)
is defined.
Represents a stream to read/write from an in-memory buffer.
Public members | |
---|---|
unsigned int | length |
unsigned int | position |
char * | buffer |
memory_stream () | |
memory_stream (unsigned int initial_capacity) | |
memory_stream (const char * buf, unsigned int length) | |
bool | read (void * dst, unsigned int bytes) |
bool | ensure_capacity (unsigned int bytes) |
bool | write (const void * src, unsigned int bytes) |
The size of the stream.
The current position of the stream in the buffer.
The underlying buffer.
The default constructor does not initialize any fields.
Initializes the stream with memory_stream::length given by initial_capacity
and memory_stream::position set to 0
. memory_stream::buffer is allocated but not initialized to any value.
Initializes the stream with the memory_stream::buffer given by buf
, memory_stream::length given by length
, and memory_stream::position set to 0
.
Reads a number of bytes given by bytes
from the memory_stream and writes them to dst
. This function assumes dst
has sufficient capacity.
Checks whether the stream has sufficient size for an additional number of bytes given by bytes
at its current memory_stream::position. If not, this function attempts to expand the buffer to a new size computed as memory_stream::position + bytes
.
Writes a number of bytes given by bytes
from the given native array src
to the current position in this stream. memory_stream::ensure_capacity is called to ensure the underlying buffer has sufficient size.
sizeof(T)
bytes from in
and writes them to the memory referenced by value
. This function does not perform endianness transformations.in |
T | satisfies is_fundamental. |
length
elements from in
and writes them to the native array values
. This function does not perform endianness transformations.in |
T | satisfies is_fundamental. |
sizeof(T)
bytes to out
from the memory referenced by value
. This function does not perform endianness transformations.out |
T | satisfies is_fundamental. |
length
elements to out
from the native array values
. This function does not perform endianness transformations.out |
T | satisfies is_fundamental. |
n
elements, each with a size of size
bytes, from the memory_stream in
, to the memory address referenced by dst
.the number of elements read.
n
elements, each with a size of size
bytes, from the memory address referenced by src
to the memory_stream out
.either n
if the write is successful, or 0
upon failure.
0 on success; nonzero value otherwise.
0 on success; nonzero value otherwise.
format
to the memory_stream out
.the number of bytes written to the stream, or -1
upon error.
Prints the character value
to the stream given by the memory_stream out
.
Prints the int value
to the stream given by the memory_stream out
.
Prints the long value
to the stream given by the memory_stream out
.
Prints the long long value
to the stream given by the memory_stream out
.
Prints the unsigned int value
to the stream given by the memory_stream out
.
Prints the unsigned long value
to the stream given by the memory_stream out
.
Prints the unsigned long long value
to the stream given by the memory_stream out
.
Prints the float value
to the stream given by the memory_stream out
.
Prints the double value
to the stream given by the memory_stream out
.
Prints the float value
to the stream given by the memory_stream out
with the given precision
.
Prints the double value
to the stream given by the memory_stream out
with the given precision
.
Prints the null-terminated C string value
to the stream given by the memory_stream out
.
A stream wrapper for reading UTF-32 characters from an underlying multibyte stream (such as UTF-8).
Public members | |
---|---|
char32_t | fgetc32 () |
Returns the next UTF-32 character (as a char32_t
) from the stream. If there are no further bytes in the underlying stream or an error occurred, static_cast<char32_t>(-1)
is returned.
Public members | |
---|---|
char32_t | fgetc32 () |
Returns the next UTF-32 character (as a char32_t
) from the stream. If there are no further bytes in the underlying stream or an error occurred, static_cast<char32_t>(-1)
is returned.
buffered_stream< BufferSize, Stream > & | input | ) |
Returns the next UTF-32 character (as a char32_t
) from the buffered_stream input
. If there are no further bytes in the underlying stream or an error occurred, static_cast<char32_t>(-1)
is returned.
A stream wrapper for reading/writing integral types as fixed-width integral values. This is useful for cross-platform readability and writeability.
T & | value, | |
fixed_width_stream< Stream, Args... > & | in | ) |
size(K)
bytes from in
where K
is the appropriate template argument in the fixed_width_stream and writes them to the memory referenced by value
. This function does not perform endianness transformations.in |
T | satisfies is_fundamental. |
T * | values, | |
fixed_width_stream< Stream, Args... > & | in, | |
SizeType | length | ) |
length
elements from in
and writes them to the native array values
. This function does not perform endianness transformations.in |
T | satisfies is_fundamental. |
const T & | value, | |
fixed_width_stream< Stream, Args... > & | out | ) |
sizeof(K)
bytes to out
from the memory referenced by value
where K
is the appropriate template argument in the fixed_width_stream. This function does not perform endianness transformations.out |
T | satisfies is_fundamental. |
const T * | values, | |
fixed_width_stream< Stream, Args... > & | out, | |
SizeType | length | ) |
length
elements to out
from the native array values
. This function does not perform endianness transformations.out |
T | satisfies is_fundamental. |
The default scribe implementation that provides the default behavior for read/write/print functions.
Section on scribes.
read(value, in)
, dropping the default_scribe argument.Stream | satisfies is_readable. |
write(value, out)
, dropping the default_scribe argument.Stream | satisfies is_writeable. |
print(value, out)
, dropping the default_scribe argument.Stream | satisfies is_printable. |
A scribe that prints pointers by dereferencing the pointer and calling the appropriate read/write/print function.
Section on scribes.
T *& | value, | |
Stream & | in, | |
const pointer_scribe & | scribe, | |
Reader &&... | reader | ) |
value
, and then calls read(*value, in, reader)
, dropping the pointer_scribe argument. Note that since reader
is a variadic argument, it may be empty.Stream | satisfies is_readable. |
const T *const | value, | |
Stream & | out, | |
const pointer_scribe & | scribe, | |
Writer &&... | writer | ) |
write(*value, out, writer)
, dropping the pointer_scribe argument. Note that since writer
is a variadic argument, it may be empty.Stream | satisfies is_writeable. |
const T *const | value, | |
Stream & | out, | |
const pointer_scribe & | scribe, | |
Printer &&... | printer | ) |
print(*value, out, printer)
, dropping the pointer_scribe argument. Note that since printer
is a variadic argument, it may be empty.Stream | satisfies is_printable. |
The default left delimitter "[" for the array print functions.
The default right delimitter "]" for the array print functions.
The default separator between elements ", " for the array print functions.
const T * | values, | |
SizeType | length, | |
Stream & | out, | |
Printer &&... | printer | ) |
values
each of type T
, where length
is the number of elements in the array. The output stream is out
.printer | a scribe for which the function |
Stream | satisfies is_printable. |
const T(&) | values[N], | |
Stream & | out, | |
Printer &&... | printer | ) |
values
each of type T
, where N
is the number of elements in the array. The output stream is out
.printer | a scribe for which the function |
Stream | satisfies is_printable. |
T * | a, | |
Stream & | in, | |
SizeType | length, | |
Reader &&... | reader | ) |
length
elements from in
and stores the result in the given native array a
. This function assumes a
has sufficient capacity.reader | a scribe for which the function |
Stream | satisfies is_readable. |
T(&) | a[N], | |
Stream & | in, | |
Reader &&... | reader | ) |
N
elements from in
and stores the result in the given native array a
.reader | a scribe for which the function |
Stream | satisfies is_readable. |
array< T > & | a, | |
Stream & | in, | |
Reader &&... | reader | ) |
in
and stores the result in a
.a | an uninitialized core::array structure. This function initializes |
reader | a scribe for which the function |
Stream | satisfies is_readable. |
const T * | a, | |
Stream & | out, | |
SizeType | length, | |
Writer &&... | writer | ) |
a
of elements to out
, each of type T
, where the number of elements is given by length
.writer | a scribe for which the function |
Stream | satisfies is_writeable. |
const T(&) | a[N], | |
Stream & | out, | |
Writer &&... | writer | ) |
a
of elements to out
, each of type T
, where the number of elements is given by N
.writer | a scribe for which the function |
Stream | satisfies is_writeable. |
const array< T > & | a, | |
Stream & | out, | |
Writer &&... | writer | ) |
a
of elements to out
, each of type T
.writer | a scribe for which the function |
Stream | satisfies is_writeable. |
const array< T > & | a, | |
Stream && | out, | |
Printer &&... | printer | ) |
a
of elements to out
, each of type T
.printer | a scribe for which the function |
Stream | satisfies is_printable. |
hash_set< T > & | set, | |
Stream & | in, | |
alloc_keys_func | alloc_keys, | |
Reader &&... | reader | ) |
set
from in
.set | an uninitialized core::hash_set structure. This function initializes |
alloc_keys | a memory allocation function with prototype |
reader | a scribe for which the function |
Stream | satisfies is_readable. |
hash_set< T > & | set, | |
Stream & | in, | |
Reader &&... | reader | ) |
set
from in
. The keys in the hash_set are allocated using calloc.set | an uninitialized core::hash_set structure. This function initializes |
reader | a scribe for which the function |
Stream | satisfies is_readable. |
const hash_set< T > & | set, | |
Stream & | out, | |
Writer &&... | writer | ) |
set
to out
.writer | a scribe for which the function |
Stream | satisfies is_writeable. |
hash_map< K, V > & | map, | |
Stream & | in, | |
alloc_keys_func | alloc_keys, | |
KeyReader & | key_reader, | |
ValueReader & | value_reader | ) |
map
from in
.map | an uninitialized core::hash_map structure. This function initializes |
alloc_keys | a memory allocation function with prototype |
key_reader | a scribe for which the function |
value_reader | a scribe for which the function |
Stream | satisfies is_readable. |
hash_map< K, V > & | map, | |
Stream & | in, | |
KeyReader & | key_reader, | |
alloc_keys_func | alloc_keys | ) |
map
from in
.map | an uninitialized core::hash_map structure. This function initializes |
alloc_keys | a memory allocation function with prototype |
key_reader | a scribe for which the function |
Stream | satisfies is_readable. |
hash_map< K, V > & | map, | |
Stream & | in, | |
alloc_keys_func | alloc_keys | ) |
map
from in
.map | an uninitialized core::hash_map structure. This function initializes |
alloc_keys | a memory allocation function with prototype |
Stream | satisfies is_readable. |
const hash_map< K, V > & | map, | |
Stream & | out, | |
KeyWriter & | key_writer, | |
ValueWriter & | value_writer | ) |
map
to out
.key_writer | a scribe for which the function |
value_writer | a scribe for which the function |
Stream | satisfies is_writeable. |
const hash_map< K, V > & | map, | |
Stream & | out, | |
KeyWriter & | key_writer | ) |
map
to out
.key_writer | a scribe for which the function |
Stream | satisfies is_writeable. |
array_map< K, V > & | map, | |
Stream & | in, | |
KeyReader & | key_reader, | |
ValueReader & | value_reader | ) |
map
from in
.map | an uninitialized core::array_map structure. This function initializes |
key_reader | a scribe for which the function |
value_reader | a scribe for which the function |
Stream | satisfies is_readable. |
array_map< K, V > & | map, | |
Stream & | in, | |
KeyReader & | key_reader | ) |
map
from in
.map | an uninitialized core::array_map structure. This function initializes |
key_reader | a scribe for which the function |
Stream | satisfies is_readable. |
map
from in
.map | an uninitialized core::array_map structure. This function initializes |
Stream | satisfies is_readable. |
const array_map< K, V > & | map, | |
Stream & | out, | |
KeyWriter & | key_writer, | |
ValueWriter & | value_writer | ) |
map
to out
.key_writer | a scribe for which the function |
value_writer | a scribe for which the function |
Stream | satisfies is_writeable. |
const array_map< K, V > & | map, | |
Stream & | out, | |
KeyWriter & | key_writer | ) |
map
to out
.key_writer | a scribe for which the function |
Stream | satisfies is_writeable. |
p
from stream
by calling read(p.key, stream)
and read(p.value, stream)
.Stream | satisfies is_readable. |
p
to stream
by calling write(p.key, stream)
and write(p.value, stream)
.Stream | satisfies is_writeable. |