CPP06 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
main.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* main.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/07/22 14:24:36 by kamitsui #+# #+# */
9/* Updated: 2025/07/22 14:42:36 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
18#include "Data.hpp"
19#include "Serializer.hpp"
20#include <iostream>
21
28int main() {
29 // 1. Create and initialize a Data object
30 Data *originalPtr = new Data;
31 originalPtr->id = 42;
32 originalPtr->type = 'S';
33 originalPtr->content = "Hello, Serialization!";
34
35 std::cout << "--- Original Data ---" << std::endl;
36 std::cout << "Address: " << originalPtr << std::endl;
37 std::cout << "ID: " << originalPtr->id << std::endl;
38 std::cout << "Type: " << originalPtr->type << std::endl;
39 std::cout << "Content: " << originalPtr->content << std::endl;
41
42 // 2. Serialize the pointer to uintptr_t
43 uintptr_t raw = Serializer::serialize(originalPtr);
44
45 std::cout << "--- Serialization ---" << std::endl;
46 std::cout << "Serialized value (uintptr_t): " << raw << std::endl;
48
49 // 3. Deserialize the value back to a pointer
50 Data *deserializedPtr = Serializer::deserialize(raw);
51
52 std::cout << "--- Deserialized Data ---" << std::endl;
53 std::cout << "Address: " << deserializedPtr << std::endl;
54 std::cout << "ID: " << deserializedPtr->id << std::endl;
55 std::cout << "Type: " << deserializedPtr->type << std::endl;
56 std::cout << "Content: " << deserializedPtr->content << std::endl;
58
59 // 4. Verify that the pointers are identical
60 std::cout << "--- Verification ---" << std::endl;
61 if (originalPtr == deserializedPtr) {
62 std::cout << "Success: Original and deserialized pointers are the same." << std::endl;
63 } else {
64 std::cout << "Failure: Pointers do NOT match!" << std::endl;
65 }
66
67 // Free the allocated memory
68 delete originalPtr;
69
70 return 0;
71}
Contains the definition of the Data struct.
Contains the declaration of the Serializer static class.
static uintptr_t serialize(Data *ptr)
Converts a Data pointer to a uintptr_t integer.
static Data * deserialize(uintptr_t raw)
Converts a uintptr_t integer back to a Data pointer.
T endl(T... args)
int main()
Main function to test the Serializer.
Definition main.cpp:28
A simple data structure for serialization testing.
Definition Data.hpp:29
std::string content
Definition Data.hpp:32
int id
Definition Data.hpp:30
char type
Definition Data.hpp:31