31
31
#include < string> // for std::string
32
32
#include < type_traits> // for std::is_fundamental
33
33
34
+ /* * \namespace ciphers
35
+ * \brief Classes for binary Serialization and Deserialization
36
+ */
37
+ namespace Serialization {
34
38
/* *
35
39
* @class Serializer
36
40
* @brief A utility class for serializing fundamental data types and strings to
@@ -68,8 +72,7 @@ class Serializer {
68
72
// Check if the string exceeds the size limit of 1MB.
69
73
const std::uint32_t max_size = 1024 * 1024 ;
70
74
if (length > max_size) {
71
- throw std::runtime_error (
72
- " String exceeds the maximum size of 1MB." );
75
+ throw std::runtime_error (" String exceeds the maximum size of 1MB." );
73
76
}
74
77
75
78
serialize (out, length); // Serialize the length of the string.
@@ -129,6 +132,8 @@ class Deserializer {
129
132
}
130
133
}
131
134
};
135
+ } // namespace Serialization
136
+
132
137
/* *
133
138
* @brief self test implementation
134
139
* @return void
@@ -146,10 +151,10 @@ void tests() {
146
151
std::string testString = " Testing String Serialization!" ;
147
152
148
153
// Serialize the data
149
- Serializer::serialize (outFile, testInt);
150
- Serializer::serialize (outFile, testDouble);
151
- Serializer::serialize (outFile, testChar);
152
- Serializer::serialize (outFile, testString);
154
+ Serialization:: Serializer::serialize (outFile, testInt);
155
+ Serialization:: Serializer::serialize (outFile, testDouble);
156
+ Serialization:: Serializer::serialize (outFile, testChar);
157
+ Serialization:: Serializer::serialize (outFile, testString);
153
158
154
159
outFile.close ();
155
160
@@ -165,10 +170,10 @@ void tests() {
165
170
std::string stringResult;
166
171
167
172
// Deserialize the data
168
- Deserializer::deserialize (inFile, intResult);
169
- Deserializer::deserialize (inFile, doubleResult);
170
- Deserializer::deserialize (inFile, charResult);
171
- Deserializer::deserialize (inFile, stringResult);
173
+ Serialization:: Deserializer::deserialize (inFile, intResult);
174
+ Serialization:: Deserializer::deserialize (inFile, doubleResult);
175
+ Serialization:: Deserializer::deserialize (inFile, charResult);
176
+ Serialization:: Deserializer::deserialize (inFile, stringResult);
172
177
173
178
inFile.close ();
174
179
@@ -185,7 +190,9 @@ void tests() {
185
190
std::ofstream largeOutFile (" large_string.bin" , std::ios::binary);
186
191
// Create a string larger than 1MB
187
192
std::string largeString (1024 * 1024 + 1 , ' A' );
188
- Serializer::serialize (largeOutFile, largeString); // Should throw an error
193
+ Serialization::Serializer::serialize (
194
+ largeOutFile,
195
+ largeString); // Should throw an error
189
196
largeOutFile.close ();
190
197
assert (false ); // If we reach here, the test has failed.
191
198
} catch (const std::runtime_error &e) {
@@ -195,20 +202,26 @@ void tests() {
195
202
}
196
203
// Test for missing delimiter in string serialization
197
204
try {
198
- std::ofstream missingDelimiterOutFile (" missing_delimiter.bin" ,std::ios::binary);
205
+ std::ofstream missingDelimiterOutFile (" missing_delimiter.bin" ,
206
+ std::ios::binary);
199
207
200
208
std::string incompleteString = " Incomplete string test" ;
201
209
std::uint32_t length = incompleteString.size ();
202
210
203
211
// Serialize string length and content without the delimiter
204
- missingDelimiterOutFile.write (reinterpret_cast <const char *>(&length),sizeof (length));
205
- missingDelimiterOutFile.write (incompleteString.c_str (), length); // No delimiter '|'
212
+ missingDelimiterOutFile.write (reinterpret_cast <const char *>(&length),
213
+ sizeof (length));
214
+ missingDelimiterOutFile.write (incompleteString.c_str (),
215
+ length); // No delimiter '|'
206
216
missingDelimiterOutFile.close ();
207
217
208
- std::ifstream missingDelimiterInFile (" missing_delimiter.bin" ,std::ios::binary);
218
+ std::ifstream missingDelimiterInFile (" missing_delimiter.bin" ,
219
+ std::ios::binary);
209
220
210
221
std::string deserializedString;
211
- Deserializer::deserialize (missingDelimiterInFile,deserializedString); // Should throw an error
222
+ Serialization::Deserializer::deserialize (
223
+ missingDelimiterInFile,
224
+ deserializedString); // Should throw an error
212
225
missingDelimiterInFile.close ();
213
226
214
227
assert (false );
@@ -230,6 +243,6 @@ void tests() {
230
243
* @returns 0 on successful exit
231
244
*/
232
245
int main () {
233
- tests (); // run self test implementations
246
+ tests (); // run self test implementations
234
247
return 0 ;
235
248
}
0 commit comments