1
- #include < fstream>
2
- #include < iostream>
3
- #include < string>
4
- #include < cassert>
5
-
6
1
/* *
7
2
* @file
8
3
* @brief A simple Serializer and Deserializer utility for fundamental data
9
4
* types and strings.
10
5
*/
6
+ #include < cassert> // for assert
7
+ #include < cstdint> // for std::uint32_t
8
+ #include < fstream> // for std::ifstream std::ofstream
9
+ #include < iostream> // for std::ios std::cout std::cerr
10
+ #include < string> // for std::string
11
+ #include < type_traits> // for std::is_fundamental
11
12
13
+ /* *
14
+ * @brief A utility class for serializing fundamental data types and strings to
15
+ * a binary file.
16
+ */
12
17
class Serializer {
13
18
public:
14
19
/* *
15
- * @brief Serializes fundamental data types (like int, float, double, etc.)
16
- * to a binary file.
20
+ * @brief Serializes fundamental data types to a binary file.
17
21
* @tparam T The type of the data to be serialized.
18
22
* @param out The output stream (std::ofstream).
19
23
* @param data The data to be serialized.
@@ -22,7 +26,9 @@ class Serializer {
22
26
*/
23
27
template <typename T>
24
28
static void serialize (std::ofstream &out, const T &data) {
25
- static_assert (std::is_fundamental<T>::value, " Non-fundamental types are not allowed for this function!" );
29
+ static_assert (
30
+ std::is_fundamental<T>::value,
31
+ " Non-fundamental types are not allowed for this function!" );
26
32
out.write (reinterpret_cast <const char *>(&data), sizeof (T));
27
33
}
28
34
@@ -43,8 +49,8 @@ class Serializer {
43
49
};
44
50
45
51
/* *
46
- * @ class Deserializer
47
- * A utility class for deserializing data from a binary file.
52
+ * A utility class for deserializing fundamental data types and strings to a
53
+ * binary file
48
54
*/
49
55
class Deserializer {
50
56
public:
@@ -93,55 +99,11 @@ class Deserializer {
93
99
}
94
100
}
95
101
};
96
-
97
- void runTests ();
98
-
99
102
/* *
100
- * Demonstrates the use of Serializer and Deserializer for fundamental types and
101
- * strings.
103
+ * @brief self test implementation
104
+ * @return void
102
105
*/
103
- int main () {
104
- std::ofstream outFile (" output.bin" , std::ios::binary);
105
- std::ifstream inFile (" output.bin" , std::ios::binary);
106
-
107
- if (!outFile || !inFile) {
108
- std::cerr << " Error opening files.\n " ;
109
- return 1 ;
110
- }
111
-
112
- // Data to be serialized.
113
- int num = 42 ;
114
- float pi = 3 .14159f ;
115
- std::string message = " Hello, Sharon!" ;
116
-
117
- Serializer::serialize (outFile, num);
118
- Serializer::serialize (outFile, pi);
119
- Serializer::serialize (outFile, message);
120
- outFile.close ();
121
-
122
- int numRead;
123
- float piRead;
124
- std::string messageRead;
125
-
126
- // Deserialize the data.
127
- Deserializer::deserialize (inFile, numRead);
128
- Deserializer::deserialize (inFile, piRead);
129
- Deserializer::deserialize (inFile, messageRead);
130
- inFile.close ();
131
-
132
- std::cout << " Deserialized int: " << numRead << " \n " ;
133
- std::cout << " Deserialized float: " << piRead << " \n " ;
134
- std::cout << " Deserialized string: " << messageRead << " \n " ;
135
-
136
- return 0 ;
137
- }
138
-
139
- /* *
140
- * @brief A test suite to perform extensive testing on the Serializer and
141
- * Deserializer.
142
- */
143
-
144
- void runTests () {
106
+ void tests () {
145
107
std::ofstream outFile (" test_output.bin" , std::ios::binary);
146
108
if (!outFile) {
147
109
std::cerr << " Error opening file for output.\n " ;
@@ -188,3 +150,14 @@ void runTests() {
188
150
189
151
std::cout << " All tests passed!\n " ;
190
152
}
153
+
154
+ int main () {
155
+ tests ();
156
+
157
+ return 0 ;
158
+ }
159
+
160
+ /* *
161
+ * @brief A test suite to perform extensive testing on the Serializer and
162
+ * Deserializer.
163
+ */
0 commit comments