Class Serializer¶
Defined in File serialize.h
Inheritance Relationships¶
Derived Type¶
public portal::BinarySerializer(Class BinarySerializer)
Class Documentation¶
-
class Serializer¶
Base class for sequential binary serialization (stream-based).
Performance-focused alternative to ArchiveObject. Writes values directly to a stream in order without intermediate property trees.
Use for: Network packets, cache files, performance-critical paths, fixed schemas Avoid for: Config files, save games, human-readable formats (use ArchiveObject instead)
Supported types: Scalars, strings, GLM vectors, std::vector, std::map, enums, custom types
BinarySerializer serializer(output_stream); serializer.add_value(42); serializer.add_value("hello"); serializer.add_value(glm::vec3(1, 2, 3));
See also
Deserializer for reading data back
Subclassed by portal::BinarySerializer
Public Functions
-
virtual ~Serializer() = default¶
-
template<typename T>
inline void add_value(const T &t)¶ Serializes a scalar value (integral or floating-point).
Writes fundamental numeric types directly to the stream with type metadata. Supports all integral types (int8_t through int64_t, unsigned variants) and floating-point types (float, double).
- Template Parameters:
T – Scalar type (integral or floating-point, excluding bool which has its own overload)
- Parameters:
t – The value to serialize
-
template<typename T>
inline void add_value(const T &t) Serializes a 128-bit unsigned integer.
Special handling for uint128_t which requires explicit PropertyType::integer128.
- Parameters:
t – The 128-bit integer to serialize
-
template<reflection::Vector T>
inline void add_value(const T &t)¶ Serializes a std::vector of complex elements. (not fundamental)
Writes the vector size followed by each element. Each element’s serialize() method is called to write its data sequentially.
- Template Parameters:
T – Vector type with complex elements
- Parameters:
t – The vector to serialize
-
template<reflection::Vector T>
inline void add_value(const T &t) Serializes a std::vector of basic fundamental (ints, floats, etc…)
Writes the vector’s data contiguously without size prefix. For vectors of Serializable types, see the overload that writes size followed by elements.
- Template Parameters:
T – Vector type with non-Serializable elements
- Parameters:
t – The vector to serialize
-
template<reflection::String T>
inline void add_value(const T &t)¶ Serializes a string value.
Writes the string with null terminator for safe deserialization. Handles std::string and other types satisfying the reflection::String concept.
- Template Parameters:
T – String type (std::string, etc.)
- Parameters:
t – The string to serialize
-
inline void add_value(const std::string_view string_view)¶
Serializes a std::string_view.
Writes the string_view with null terminator.
- Parameters:
string_view – The string view to serialize
-
template<reflection::GlmVec1 T>
inline void add_value(const T &t)¶ Serializes a GLM vec1 (single-component vector).
- Template Parameters:
T – GLM vec1 type (glm::vec1, glm::ivec1, etc.)
- Parameters:
t – The vector to serialize
-
template<reflection::GlmVec2 T>
inline void add_value(const T &t)¶ Serializes a GLM vec2 (two-component vector).
- Template Parameters:
T – GLM vec2 type (glm::vec2, glm::ivec2, glm::dvec2, etc.)
- Parameters:
t – The vector to serialize
-
template<reflection::GlmVec3 T>
inline void add_value(const T &t)¶ Serializes a GLM vec3 (three-component vector).
- Template Parameters:
T – GLM vec3 type (glm::vec3, glm::ivec3, glm::dvec3, etc.)
- Parameters:
t – The vector to serialize
-
template<reflection::GlmVec4 T>
inline void add_value(const T &t)¶ Serializes a GLM vec4 (four-component vector).
- Template Parameters:
T – GLM vec4 type (glm::vec4, glm::ivec4, glm::dvec4, etc.)
- Parameters:
t – The vector to serialize
-
template<reflection::Map T>
inline void add_value(const T &t)¶ Serializes a map (std::map, std::unordered_map, etc.).
Writes the map size followed by key-value pairs in sequence. Both keys and values are serialized using their respective add_value overloads.
- Template Parameters:
T – Map type
- Parameters:
t – The map to serialize
-
template<typename T>
inline void add_value(const T &t) Serializes an enum as its underlying integer type.
Converts the enum to its underlying type (e.g., int, uint8_t) and serializes that value. More compact than the string-based enum serialization used by ArchiveObject.
- Template Parameters:
T – Enum type
- Parameters:
t – The enum value to serialize
-
template<SerializableConcept T>
inline void add_value(const T &t)¶ Serializes a custom Serializable type.
Calls the type’s serialize() method, which should write its members sequentially to this Serializer.
- Template Parameters:
T – Type satisfying the Serializable concept
- Parameters:
t – The object to serialize
-
template<ExternalSerializable T>
inline void add_value(const T &t)¶ Serializes a type with a SerializableType<T> specialization.
Calls SerializableType<T>::serialize() to write the object’s data sequentially. This enables non-intrusive serialization for types you don’t control.
- Template Parameters:
T – Type satisfying the ExternalSerializable concept
- Parameters:
t – The object to serialize
-
template<typename T>
inline void add_value(const T &t) Serializes types with glaze reflection metadata.
Fallback overload for types with glaze compile-time reflection. Automatically serializes all reflected fields in order. Use this for types that don’t implement serialize() but have glaze metadata.
- Template Parameters:
T – Type with glaze reflection metadata (glz::reflect)
- Parameters:
t – The object to serialize
-
inline void add_value(const char *t)¶
Serializes a C-string.
Writes the string with null terminator. Calculates length using strlen().
- Parameters:
t – Null-terminated C-string
-
inline void add_value(void *t, const size_t length)¶
Serializes a raw binary data block.
Writes arbitrary binary data of the specified length without interpretation.
- Parameters:
t – Pointer to binary data
length – Size of the binary data in bytes
-
inline void add_value(const StringId &id)¶
Serializes a string id.
A specialization of
add_valueto serialize string id, serializes only the id part, without the string to save on space,- Parameters:
id – The string id to serialize
-
template<typename T>
inline ReservedSlot<T> reserve()¶ Reserve space for a value to be written later.
This is useful for writing values that depend on data written after them, such as list sizes in lazy evaluation scenarios where the count isn’t known upfront.
auto size_slot = serializer.reserve<size_t>(); size_t count = 0; for (const auto& item : lazy_list) { serializer.add_value(item); count++; } size_slot.write(count);
- Template Parameters:
T – The type of value to reserve space for (must be integral or floating-point)
- Returns:
A ReservedSlot handle to write the value later
Protected Functions
-
virtual size_t reserve_slot(reflection::Property property) = 0¶
Reserve space for a value and return the position where the value data will be written.
Implementations should write placeholder metadata and value bytes based on the property, then return the position where the actual value data starts (after any metadata).
- Parameters:
property – The property describing the reserved value (buffer will be empty/null)
- Returns:
The byte position where the value data starts
-
virtual void write_at(size_t position, const void *data, size_t size) = 0¶
Write data at a specific position without changing the current write position.
Used by ReservedSlot to fill in reserved values after subsequent data has been written.
- Parameters:
position – The byte position to write at
data – Pointer to the data to write
size – Number of bytes to write
Friends
- friend class ReservedSlot
-
virtual ~Serializer() = default¶