-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerializer.h
145 lines (131 loc) · 3.85 KB
/
Serializer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**************************************************************************
* @file: Serializer.h
* @brief:
*
* Copyright (c) 2022 O-Net Communications Inc.
* All rights reserved.
*************************************************************************/
#pragma once
#include <fstream>
#include <sstream>
#include <string>
#include "Archive.h"
namespace cppbase {
class Serializer
{
public:
Serializer() = default;
virtual ~Serializer() = default;
/**
* @brief Serialize an object to an archive file.
*
* @param obj Object to be serialized
* @param archive_path Path to the archive to be created
*/
template <typename OutArchive, typename T>
static void SaveObjectToFile(const T& obj, const std::string& archive_path)
{
std::ofstream ofs(archive_path, std::ios::binary);
if (!ofs.is_open())
{
throw std::runtime_error("Failed to open file for writing: " + archive_path);
}
OutArchive arch(ofs);
arch(obj);
}
/**
* @brief Deserialize and object from an archive file
*
* @param archive_path Path to the archive to be loaded
* @return The loaded object
*/
template <typename InArchive, typename T>
static T LoadObjectFromFile(const std::string& archive_path)
{
std::ifstream ifs(archive_path, std::ios::binary);
if (!ifs.is_open())
{
throw std::runtime_error("Failed to open file for reading: " + archive_path);
}
InArchive arch(ifs);
// std::shared_ptr<T> obj;
T obj;
arch(obj);
return obj;
}
/**
* @brief Serialize an object to a string
*
* @param obj Object to be serialized
* @return std::string String representation of the object
*/
template <typename OutArchive, typename T>
static std::string SaveObjectToString(const T& obj)
{
std::stringstream ss;
OutArchive arch(ss);
arch(obj);
return ss.str();
}
/**
* @brief Deserialize an object from a string
*
* @param archive String representation of the object
* @return The loaded object
*/
template <typename InArchive, typename T>
static T LoadObjectFromString(const std::string& archive)
{
std::stringstream ss;
ss << archive;
InArchive arch(ss);
T obj;
arch(obj);
return obj;
}
/**
* @brief Serialize an object to a binary archive file
*
* @param obj Object to be serialized
* @param archive_path Path to the archive file to be created
*/
template<typename T>
static void SaveObjectToFileBinary(const T& obj, const std::string& archive_path)
{
SaveObjectToFile<BinaryOutputArchive, T>(obj, archive_path);
}
/**
* @brief Serialize an object to a binary archive string
*
* @param obj Object to be serialized
* @return std::string String that contains the archive data
*/
template<typename T>
static std::string SaveObjectToStringBinary(const T& obj)
{
return SaveObjectToString<BinaryOutputArchive, T>(obj);
}
/**
* @brief Deserialize an object from a binary archive file
*
* @param archive_path Path to the archive file to be loaded
* @return The loaded object
*/
template<typename T>
static T LoadObjectFromFileBinary(const std::string& archive_path)
{
return LoadObjectFromFile<BinaryInputArchive, T>(archive_path);
}
/**
* @brief Deserialize an object from a binary archive string
*
* @param archive String that contains the archive data
* @return The loaded object
*/
template<typename T>
static T LoadObjectFromStringBinary(const std::string& archive)
{
return LoadObjectFromString<BinaryInputArchive, T>(archive);
}
};
} // namespace cppbase