Class Serializer

Page Contents

Class Documentation

class Serializer

Serialization helper class.

This class can be used to store and retrieve a great variety of C++ data types using a compact binary file format. The intended purpose is to quickly save and restore the complete state of an application, e.g. to facilitate debugging sessions. This class supports all core C++ types, NanoGUI widgets, sparse and dense Eigen matrices, as well as OpenGL shaders and buffer objects.

Note that this header file just provides the basics; the files nanogui/serializer/opengl.h, and nanogui/serializer/sparse.h must be included to serialize the respective data types.

Public Functions

Serializer(const std::string &filename, bool write)

Create a new serialized file for reading or writing.

~Serializer()

Release all resources.

size_t size()

Return the current size of the output file.

void push(const std::string &name)

Push a name prefix onto the stack (use this to isolate identically-named data fields)

void pop()

Pop a name prefix from the stack.

std::vector<std::string> keys() const

Return all field names under the current name prefix.

void setCompatibility(bool compatibility)

Enable/disable compatibility mode.

When enabled, missing attributes cause a warning to be printed, but get() does not throw an exception.

bool compatibility()

Return whether compatibility mode is enabled.

template<typename T>
void set(const std::string &name, const T &value)

Store a field in the serialized file (when opened with write=true)

template<typename T>
bool get(const std::string &name, T &value)

Retrieve a field from the serialized file (when opened with write=false)

Public Static Functions

static bool isSerializedFile(const std::string &filename)

Check whether a file contains serialized data.

Protected Functions

void set_base(const std::string &name, const std::string &type_id)
bool get_base(const std::string &name, const std::string &type_id)
void writeTOC()
void readTOC()
void read(void *p, size_t size)
void write(const void *p, size_t size)
void seek(size_t pos)