DNA Calib 1.1
Project brief
ArchiveOffset.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#ifdef _MSC_VER
6 #pragma warning(push)
7 #pragma warning(disable : 4365 4987)
8#endif
9#include <cstddef>
10#include <cstdint>
11#include <memory>
12#ifdef _MSC_VER
13 #pragma warning(pop)
14#endif
15
16namespace terse {
17
18// ArchiveOffset is a type that stores an absolute stream offset (both in-memory and writes that value into the stream as well).
19// It has an accompanying Proxy type, which is a virtual, utility type, and they work together to achieve the function
20// of ArchiveOffset.
21// When ArchiveOffset is encountered during the serialization process, at first an empty offset (zero) value will be written
22// into the stream and the position of that offset value will be captured in an in-memory variable. Later, when its associated
23// ArchiveOffset<T>::Proxy is encountered, the current stream position will be captured, and it will update the in-memory value
24// of its ArchiveOffset with it. Then, it will seek the stream to the earlier captured position of the offset value (where zeros
25// were written initially), and it will write the offset value captured by the proxy into the stream. Lastly, it will seek back
26// to the position of the stream before the proxy was encountered, and resume serialization.
27template<typename TOffset>
29 using ValueType = TOffset;
30
31 struct Proxy {
33
34 explicit Proxy(ArchiveOffset& ptr) : target{std::addressof(ptr)} {
35 target->proxy = this;
36 }
37
39 if (target != nullptr) {
40 target->proxy = nullptr;
41 }
42 }
43
44 Proxy(const Proxy&) = delete;
45 Proxy& operator=(const Proxy&) = delete;
46
47 Proxy(Proxy&& rhs) : target{nullptr} {
48 std::swap(target, rhs.target);
49 target->proxy = this;
50 }
51
53 std::swap(target, rhs.target);
54 target->proxy = this;
55 return *this;
56 }
57
58 };
59
60 // The position of the marker itself in the stream (this is a runtime-only value
61 // which is not written to the file, needed only for the serializer to know where
62 // to seek within the stream when the marker's actual value needs to be written)
63 mutable std::size_t position;
64 // The position in the stream where the marker wants to point (this is the actual
65 // value that is written to the file)
67 // When offset is moved, it's associated proxy must be updated about the new address
69
70 ArchiveOffset() : position{}, value{}, proxy{nullptr} {
71 }
72
73 ~ArchiveOffset() = default;
74
75 ArchiveOffset(const ArchiveOffset&) = delete;
77
79 std::swap(position, rhs.position);
80 std::swap(value, rhs.value);
81 std::swap(proxy, rhs.proxy);
82 // Update proxy with new address
83 if (proxy != nullptr) {
84 proxy->target = this;
85 }
86 }
87
89 std::swap(position, rhs.position);
90 std::swap(value, rhs.value);
91 std::swap(proxy, rhs.proxy);
92 // Update proxy with new address
93 if (proxy != nullptr) {
94 proxy->target = this;
95 }
96 return *this;
97 }
98
99};
100
101template<typename TOffset>
103 return typename ArchiveOffset<TOffset>::Proxy{offset};
104}
105
106} // namespace terse
Definition: Archive.h:14
ArchiveOffset< TOffset >::Proxy proxy(ArchiveOffset< TOffset > &offset)
Definition: ArchiveOffset.h:102
Definition: ArchiveOffset.h:31
Proxy & operator=(Proxy &&rhs)
Definition: ArchiveOffset.h:52
Proxy(const Proxy &)=delete
Proxy & operator=(const Proxy &)=delete
ArchiveOffset * target
Definition: ArchiveOffset.h:32
~Proxy()
Definition: ArchiveOffset.h:38
Proxy(Proxy &&rhs)
Definition: ArchiveOffset.h:47
Proxy(ArchiveOffset &ptr)
Definition: ArchiveOffset.h:34
Definition: ArchiveOffset.h:28
ValueType value
Definition: ArchiveOffset.h:66
ArchiveOffset(const ArchiveOffset &)=delete
ArchiveOffset & operator=(const ArchiveOffset &)=delete
ArchiveOffset()
Definition: ArchiveOffset.h:70
~ArchiveOffset()=default
TOffset ValueType
Definition: ArchiveOffset.h:29
ArchiveOffset(ArchiveOffset &&rhs)
Definition: ArchiveOffset.h:78
Proxy * proxy
Definition: ArchiveOffset.h:68
std::size_t position
Definition: ArchiveOffset.h:63
ArchiveOffset & operator=(ArchiveOffset &&rhs)
Definition: ArchiveOffset.h:88