Plasma Engine  2.0
Loading...
Searching...
No Matches
DeduplicationReadContext_inl.h
1
2#include <Foundation/IO/Stream.h>
3
4template <typename T>
5PL_ALWAYS_INLINE plResult plDeduplicationReadContext::ReadObjectInplace(plStreamReader& inout_stream, T& inout_obj)
6{
7 return ReadObject(inout_stream, inout_obj, nullptr);
8}
9
10template <typename T>
12{
13 bool bIsRealObject;
14 inout_stream >> bIsRealObject;
15
16 PL_ASSERT_DEV(bIsRealObject, "Reading an object inplace only works for the first occurrence");
17
18 PL_SUCCEED_OR_RETURN(plStreamReaderUtil::Deserialize<T>(inout_stream, obj));
19
20 m_Objects.PushBack(&obj);
21
22 return PL_SUCCESS;
23}
24
25template <typename T>
27{
28 bool bIsRealObject;
29 inout_stream >> bIsRealObject;
30
31 if (bIsRealObject)
32 {
33 ref_pObject = PL_NEW(pAllocator, T);
34 PL_SUCCEED_OR_RETURN(plStreamReaderUtil::Deserialize<T>(inout_stream, *ref_pObject));
35
36 m_Objects.PushBack(ref_pObject);
37 }
38 else
39 {
40 plUInt32 uiIndex;
41 inout_stream >> uiIndex;
42
43 if (uiIndex < m_Objects.GetCount())
44 {
45 ref_pObject = static_cast<T*>(m_Objects[uiIndex]);
46 }
47 else if (uiIndex == plInvalidIndex)
48 {
49 ref_pObject = nullptr;
50 }
51 else
52 {
53 return PL_FAILURE;
54 }
55 }
56
57 return PL_SUCCESS;
58}
59
60template <typename T>
62{
63 T* ptr = nullptr;
64 if (ReadObject(inout_stream, ptr, pAllocator).Succeeded())
65 {
66 ref_pObject = plSharedPtr<T>(ptr, pAllocator);
67 return PL_SUCCESS;
68 }
69 return PL_FAILURE;
70}
71
72template <typename T>
74{
75 T* ptr = nullptr;
76 if (ReadObject(inout_stream, ptr, pAllocator).Succeeded())
77 {
78 ref_pObject = std::move(plUniquePtr<T>(ptr, pAllocator));
79 return PL_SUCCESS;
80 }
81 return PL_FAILURE;
82}
83
84template <typename ArrayType, typename ValueType>
86{
87 plUInt64 uiCount = 0;
88 PL_SUCCEED_OR_RETURN(inout_stream.ReadQWordValue(&uiCount));
89
90 PL_ASSERT_DEV(uiCount < std::numeric_limits<plUInt32>::max(), "Containers currently use 32 bit for counts internally. Value from file is too large.");
91
92 ref_array.Clear();
93
94 if (uiCount > 0)
95 {
96 static_cast<ArrayType&>(ref_array).Reserve(static_cast<plUInt32>(uiCount));
97
98 for (plUInt32 i = 0; i < static_cast<plUInt32>(uiCount); ++i)
99 {
100 PL_SUCCEED_OR_RETURN(ReadObject(inout_stream, ref_array.ExpandAndGetRef(), pAllocator));
101 }
102 }
103
104 return PL_SUCCESS;
105}
106
107template <typename KeyType, typename Comparer>
109{
110 plUInt64 uiCount = 0;
111 PL_SUCCEED_OR_RETURN(inout_stream.ReadQWordValue(&uiCount));
112
113 PL_ASSERT_DEV(uiCount < std::numeric_limits<plUInt32>::max(), "Containers currently use 32 bit for counts internally. Value from file is too large.");
114
115 ref_set.Clear();
116
117 for (plUInt32 i = 0; i < static_cast<plUInt32>(uiCount); ++i)
118 {
119 KeyType key;
120 PL_SUCCEED_OR_RETURN(ReadObject(inout_stream, key, pAllocator));
121
122 ref_set.Insert(std::move(key));
123 }
124
125 return PL_SUCCESS;
126}
127
128namespace plInternal
129{
130 // Internal helper to prevent the compiler from trying to find a de-serialization method for pointer types or other types which don't have
131 // one.
133 {
134 template <typename T>
135 static auto Deserialize(plStreamReader& inout_stream, T& ref_obj, int) -> decltype(plStreamReaderUtil::Deserialize(inout_stream, ref_obj))
136 {
137 return plStreamReaderUtil::Deserialize(inout_stream, ref_obj);
138 }
139
140 template <typename T>
141 static plResult Deserialize(plStreamReader& inout_stream, T& ref_obj, float)
142 {
143 PL_REPORT_FAILURE("No deserialize method available");
144 return PL_FAILURE;
145 }
146 };
147} // namespace plInternal
148
149template <typename KeyType, typename ValueType, typename Comparer>
150plResult plDeduplicationReadContext::ReadMap(plStreamReader& inout_stream, plMapBase<KeyType, ValueType, Comparer>& ref_map, ReadMapMode mode, plAllocator* pKeyAllocator, plAllocator* pValueAllocator)
151{
152 plUInt64 uiCount = 0;
153 PL_SUCCEED_OR_RETURN(inout_stream.ReadQWordValue(&uiCount));
154
155 PL_ASSERT_DEV(uiCount < std::numeric_limits<plUInt32>::max(), "Containers currently use 32 bit for counts internally. Value from file is too large.");
156
157 ref_map.Clear();
158
159 if (mode == ReadMapMode::DedupKey)
160 {
161 for (plUInt32 i = 0; i < static_cast<plUInt32>(uiCount); ++i)
162 {
163 KeyType key;
164 ValueType value;
165 PL_SUCCEED_OR_RETURN(ReadObject(inout_stream, key, pKeyAllocator));
166 PL_SUCCEED_OR_RETURN(plInternal::DeserializeHelper::Deserialize<ValueType>(inout_stream, value, 0));
167
168 ref_map.Insert(std::move(key), std::move(value));
169 }
170 }
171 else if (mode == ReadMapMode::DedupValue)
172 {
173 for (plUInt32 i = 0; i < static_cast<plUInt32>(uiCount); ++i)
174 {
175 KeyType key;
176 ValueType value;
177 PL_SUCCEED_OR_RETURN(plInternal::DeserializeHelper::Deserialize<KeyType>(inout_stream, key, 0));
178 PL_SUCCEED_OR_RETURN(ReadObject(inout_stream, value, pValueAllocator));
179
180 ref_map.Insert(std::move(key), std::move(value));
181 }
182 }
183 else
184 {
185 for (plUInt32 i = 0; i < static_cast<plUInt32>(uiCount); ++i)
186 {
187 KeyType key;
188 ValueType value;
189 PL_SUCCEED_OR_RETURN(ReadObject(inout_stream, key, pKeyAllocator));
190 PL_SUCCEED_OR_RETURN(ReadObject(inout_stream, value, pValueAllocator));
191
192 ref_map.Insert(std::move(key), std::move(value));
193 }
194 }
195
196 return PL_SUCCESS;
197}
Base class for all memory allocators.
Definition Allocator.h:23
Base class for all array containers. Implements all the basic functionality that only requires a poin...
Definition ArrayBase.h:19
T & ExpandAndGetRef()
Grows the array by one element and returns a reference to the newly created element.
Definition ArrayBase_inl.h:310
void PushBack(const T &value)
Pushes value at the end of the array.
Definition ArrayBase_inl.h:333
void Clear()
Clears the array.
Definition ArrayBase_inl.h:184
plUInt32 GetCount() const
Returns the number of active elements in the array.
Definition ArrayBase_inl.h:172
plResult ReadObjectInplace(plStreamReader &inout_stream, T &ref_obj)
Reads a single object inplace.
plResult ReadMap(plStreamReader &inout_stream, plMapBase< KeyType, ValueType, Comparer > &ref_map, ReadMapMode mode, plAllocator *pKeyAllocator=plFoundation::GetDefaultAllocator(), plAllocator *pValueAllocator=plFoundation::GetDefaultAllocator())
Reads a map. Mode controls whether key or value or both should de-duplicated.
Definition DeduplicationReadContext_inl.h:150
plResult ReadObject(plStreamReader &inout_stream, T *&ref_pObject, plAllocator *pAllocator=plFoundation::GetDefaultAllocator())
Reads a single object and sets the pointer to it. The given allocator is used to create the object if...
Definition DeduplicationReadContext_inl.h:26
plResult ReadSet(plStreamReader &inout_stream, plSetBase< KeyType, Comparer > &ref_set, plAllocator *pAllocator=plFoundation::GetDefaultAllocator())
Reads a set of de-duplicated objects.
Definition DeduplicationReadContext_inl.h:108
plResult ReadArray(plStreamReader &inout_stream, plArrayBase< ValueType, ArrayType > &ref_array, plAllocator *pAllocator=plFoundation::GetDefaultAllocator())
Reads an array of de-duplicated objects.
Definition DeduplicationReadContext_inl.h:85
An associative container. Similar to STL::map.
Definition Map.h:193
void Clear()
Destroys all elements in the map and resets its size to zero.
Definition Map_inl.h:175
Iterator Insert(CompatibleKeyType &&key, CompatibleValueType &&value)
Inserts the key/value pair into the tree and returns an Iterator to it. O(log n) operation.
Definition Map_inl.h:535
A set container that only stores whether an element resides in it or not. Similar to STL::set.
Definition Set.h:13
void Clear()
Destroys all elements in the set and resets its size to zero.
Definition Set_inl.h:133
Iterator Insert(CompatibleKeyType &&key)
Inserts the key into the tree and returns an Iterator to it. O(log n) operation.
Definition Set_inl.h:351
A Shared ptr manages a shared object and destroys that object when no one references it anymore....
Definition SharedPtr.h:10
Interface for binary in (read) streams.
Definition Stream.h:22
plResult ReadQWordValue(T *pQWordValue)
Helper method to read a qword value correctly (copes with potentially different endianess)
Definition Stream_inl.h:107
A Unique ptr manages an object and destroys that object when it goes out of scope....
Definition UniquePtr.h:10
Definition DeduplicationReadContext_inl.h:133
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54