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
28
int
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
;
40
std::cout
<<
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
;
47
std::cout
<<
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
;
57
std::cout
<<
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
}
Data.hpp
Contains the definition of the Data struct.
Serializer.hpp
Contains the declaration of the Serializer static class.
Serializer::serialize
static uintptr_t serialize(Data *ptr)
Converts a Data pointer to a uintptr_t integer.
Definition
Serializer.cpp:36
Serializer::deserialize
static Data * deserialize(uintptr_t raw)
Converts a uintptr_t integer back to a Data pointer.
Definition
Serializer.cpp:45
std::cout
std::endl
T endl(T... args)
main
int main()
Main function to test the Serializer.
Definition
main.cpp:28
iostream
Data
A simple data structure for serialization testing.
Definition
Data.hpp:29
Data::content
std::string content
Definition
Data.hpp:32
Data::id
int id
Definition
Data.hpp:30
Data::type
char type
Definition
Data.hpp:31
ex01
main.cpp
構築:
1.9.8