Skip to content

Commit 4df4977

Browse files
committed
fix: made requested changes.
1 parent 79539ae commit 4df4977

File tree

1 file changed

+30
-57
lines changed

1 file changed

+30
-57
lines changed

others/serialization.cpp

Lines changed: 30 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
#include <fstream>
2-
#include <iostream>
3-
#include <string>
4-
#include <cassert>
5-
61
/**
72
* @file
83
* @brief A simple Serializer and Deserializer utility for fundamental data
94
* types and strings.
105
*/
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
1112

13+
/**
14+
* @brief A utility class for serializing fundamental data types and strings to
15+
* a binary file.
16+
*/
1217
class Serializer {
1318
public:
1419
/**
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.
1721
* @tparam T The type of the data to be serialized.
1822
* @param out The output stream (std::ofstream).
1923
* @param data The data to be serialized.
@@ -22,7 +26,9 @@ class Serializer {
2226
*/
2327
template <typename T>
2428
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!");
2632
out.write(reinterpret_cast<const char *>(&data), sizeof(T));
2733
}
2834

@@ -43,8 +49,8 @@ class Serializer {
4349
};
4450

4551
/**
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
4854
*/
4955
class Deserializer {
5056
public:
@@ -93,55 +99,11 @@ class Deserializer {
9399
}
94100
}
95101
};
96-
97-
void runTests();
98-
99102
/**
100-
* Demonstrates the use of Serializer and Deserializer for fundamental types and
101-
* strings.
103+
* @brief self test implementation
104+
* @return void
102105
*/
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() {
145107
std::ofstream outFile("test_output.bin", std::ios::binary);
146108
if (!outFile) {
147109
std::cerr << "Error opening file for output.\n";
@@ -188,3 +150,14 @@ void runTests() {
188150

189151
std::cout << "All tests passed!\n";
190152
}
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

Comments
 (0)