Kùzu C++ API
Loading...
Searching...
No Matches
serializer.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <memory>
5#include <string>
6#include <unordered_map>
7#include <unordered_set>
8#include <vector>
9
10#include "writer.h"
11
12namespace kuzu {
13namespace common {
14
16public:
17 explicit Serializer(std::shared_ptr<Writer> writer) : writer(std::move(writer)) {}
18
19 Writer& getWriter() const { return *writer; }
20
21 template<typename T>
22 requires std::is_trivially_destructible<T>::value || std::is_same<std::string, T>::value
23 void serializeValue(const T& value) {
24 writer->write((uint8_t*)&value, sizeof(T));
25 }
26
27 // Alias for serializeValue
28 template<typename T>
29 void write(const T& value) {
30 serializeValue(value);
31 }
32
33 void writeDebuggingInfo(const std::string& value);
34
35 void write(const uint8_t* value, uint64_t len) { writer->write(value, len); }
36
37 template<typename T>
38 void serializeOptionalValue(const std::unique_ptr<T>& value) {
39 serializeValue(value == nullptr);
40 if (value != nullptr) {
41 value->serialize(*this);
42 }
43 }
44
45 template<typename T1, typename T2>
46 void serializeUnorderedMap(const std::unordered_map<T1, std::unique_ptr<T2>>& values) {
47 uint64_t mapSize = values.size();
48 serializeValue(mapSize);
49 for (auto& value : values) {
50 serializeValue(value.first);
51 value.second->serialize(*this);
52 }
53 }
54
55 template<typename T>
56 void serializeVector(const std::vector<T>& values) {
57 uint64_t vectorSize = values.size();
58 serializeValue<uint64_t>(vectorSize);
59 for (auto& value : values) {
60 if constexpr (requires(Serializer& ser) { value.serialize(ser); }) {
61 value.serialize(*this);
62 } else {
63 serializeValue<T>(value);
64 }
65 }
66 }
67
68 template<typename T, uint64_t ARRAY_SIZE>
69 void serializeArray(const std::array<T, ARRAY_SIZE>& values) {
70 for (auto& value : values) {
71 if constexpr (requires(Serializer& ser) { value.serialize(ser); }) {
72 value.serialize(*this);
73 } else {
74 serializeValue<T>(value);
75 }
76 }
77 }
78
79 template<typename T>
80 void serializeVectorOfPtrs(const std::vector<std::unique_ptr<T>>& values) {
81 uint64_t vectorSize = values.size();
82 serializeValue<uint64_t>(vectorSize);
83 for (auto& value : values) {
84 value->serialize(*this);
85 }
86 }
87
88 template<typename T>
89 void serializeUnorderedSet(const std::unordered_set<T>& values) {
90 uint64_t setSize = values.size();
91 serializeValue(setSize);
92 for (const auto& value : values) {
93 serializeValue(value);
94 }
95 }
96
97private:
98 std::shared_ptr<Writer> writer;
99};
100
101template<>
102void Serializer::serializeValue(const std::string& value);
103
104} // namespace common
105} // namespace kuzu
Definition serializer.h:15
void writeDebuggingInfo(const std::string &value)
void serializeValue(const T &value)
Definition serializer.h:23
void serializeVectorOfPtrs(const std::vector< std::unique_ptr< T > > &values)
Definition serializer.h:80
void write(const T &value)
Definition serializer.h:29
void write(const uint8_t *value, uint64_t len)
Definition serializer.h:35
void serializeUnorderedSet(const std::unordered_set< T > &values)
Definition serializer.h:89
void serializeUnorderedMap(const std::unordered_map< T1, std::unique_ptr< T2 > > &values)
Definition serializer.h:46
void serializeArray(const std::array< T, ARRAY_SIZE > &values)
Definition serializer.h:69
void serializeOptionalValue(const std::unique_ptr< T > &value)
Definition serializer.h:38
Writer & getWriter() const
Definition serializer.h:19
void serializeVector(const std::vector< T > &values)
Definition serializer.h:56
Serializer(std::shared_ptr< Writer > writer)
Definition serializer.h:17
Definition writer.h:8
Definition alter_type.h:5