-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat: added serializers #2756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: added serializers #2756
Changes from 5 commits
0b19d86
6e1f86d
79539ae
4df4977
8b8f915
afb2b7a
a436849
447d463
33573a9
155df97
4e4089c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,163 @@ | ||||||
/** | ||||||
* @file | ||||||
* @brief A simple Serializer and Deserializer utility for fundamental data | ||||||
* types and strings. | ||||||
*/ | ||||||
realstealthninja marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
#include <cassert> // for assert | ||||||
#include <cstdint> // for std::uint32_t | ||||||
#include <fstream> // for std::ifstream std::ofstream | ||||||
#include <iostream> // for std::ios std::cout std::cerr | ||||||
#include <string> // for std::string | ||||||
#include <type_traits> // for std::is_fundamental | ||||||
|
||||||
realstealthninja marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
/** | ||||||
* @brief A utility class for serializing fundamental data types and strings to | ||||||
* a binary file. | ||||||
realstealthninja marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
*/ | ||||||
class Serializer { | ||||||
realstealthninja marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
public: | ||||||
/** | ||||||
* @brief Serializes fundamental data types to a binary file. | ||||||
* @tparam T The type of the data to be serialized. | ||||||
* @param out The output stream (std::ofstream). | ||||||
* @param data The data to be serialized. | ||||||
* | ||||||
* @note This function only works for fundamental types (primitives). | ||||||
*/ | ||||||
template <typename T> | ||||||
static void serialize(std::ofstream &out, const T &data) { | ||||||
static_assert( | ||||||
std::is_fundamental<T>::value, | ||||||
"Non-fundamental types are not allowed for this function!"); | ||||||
out.write(reinterpret_cast<const char *>(&data), sizeof(T)); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Serializes a string to a binary file. | ||||||
|
* Serializes a string to a binary file. | |
* @brief Serializes a string to a binary file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still needs to be resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* The string is serialized by first storing its length, followed by the | |
* @note The string is serialized by first storing its length, followed by the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still needs to be resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should add more details via a
@detail
tag and your name with@author
tag