Plasma Engine  2.0
Loading...
Searching...
No Matches
DeduplicationWriteContext_inl.h
1
2#include <Foundation/IO/Stream.h>
3
4namespace plInternal
5{
6 // This internal helper is needed to differentiate between reference and pointer which is not possible with regular function overloading
7 // in this case.
8 template <typename T>
10 {
11 static const T* GetAddress(const T& obj) { return &obj; }
12 };
13
14 template <typename T>
16 {
17 static const T* GetAddress(const T* pObj) { return pObj; }
18 };
19} // namespace plInternal
20
21template <typename T>
22PL_ALWAYS_INLINE plResult plDeduplicationWriteContext::WriteObject(plStreamWriter& inout_stream, const T& obj)
23{
24 return WriteObjectInternal(inout_stream, plInternal::WriteObjectHelper<T>::GetAddress(obj));
25}
26
27template <typename T>
28PL_ALWAYS_INLINE plResult plDeduplicationWriteContext::WriteObject(plStreamWriter& inout_stream, const plSharedPtr<T>& pObject)
29{
30 return WriteObjectInternal(inout_stream, pObject.Borrow());
31}
32
33template <typename T>
34PL_ALWAYS_INLINE plResult plDeduplicationWriteContext::WriteObject(plStreamWriter& inout_stream, const plUniquePtr<T>& pObject)
35{
36 return WriteObjectInternal(inout_stream, pObject.Borrow());
37}
38
39template <typename ArrayType, typename ValueType>
41{
42 const plUInt64 uiCount = array.GetCount();
43 PL_SUCCEED_OR_RETURN(inout_stream.WriteQWordValue(&uiCount));
44
45 for (plUInt32 i = 0; i < static_cast<plUInt32>(uiCount); ++i)
46 {
47 PL_SUCCEED_OR_RETURN(WriteObject(inout_stream, array[i]));
48 }
49
50 return PL_SUCCESS;
51}
52
53template <typename KeyType, typename Comparer>
55{
56 const plUInt64 uiWriteSize = set.GetCount();
57 PL_SUCCEED_OR_RETURN(inout_stream.WriteQWordValue(&uiWriteSize));
58
59 for (const auto& item : set)
60 {
61 PL_SUCCEED_OR_RETURN(WriteObject(inout_stream, item));
62 }
63
64 return PL_SUCCESS;
65}
66
67template <typename KeyType, typename ValueType, typename Comparer>
69{
70 const plUInt64 uiWriteSize = map.GetCount();
71 PL_SUCCEED_OR_RETURN(inout_stream.WriteQWordValue(&uiWriteSize));
72
73 if (mode == WriteMapMode::DedupKey)
74 {
75 for (auto It = map.GetIterator(); It.IsValid(); ++It)
76 {
77 PL_SUCCEED_OR_RETURN(WriteObject(inout_stream, It.Key()));
78 PL_SUCCEED_OR_RETURN(plStreamWriterUtil::Serialize<ValueType>(inout_stream, It.Value()));
79 }
80 }
81 else if (mode == WriteMapMode::DedupValue)
82 {
83 for (auto It = map.GetIterator(); It.IsValid(); ++It)
84 {
85 PL_SUCCEED_OR_RETURN(plStreamWriterUtil::Serialize<KeyType>(inout_stream, It.Key()));
86 PL_SUCCEED_OR_RETURN(WriteObject(inout_stream, It.Value()));
87 }
88 }
89 else
90 {
91 for (auto It = map.GetIterator(); It.IsValid(); ++It)
92 {
93 PL_SUCCEED_OR_RETURN(WriteObject(inout_stream, It.Key()));
94 PL_SUCCEED_OR_RETURN(WriteObject(inout_stream, It.Value()));
95 }
96 }
97
98 return PL_SUCCESS;
99}
100
101template <typename T>
102plResult plDeduplicationWriteContext::WriteObjectInternal(plStreamWriter& stream, const T* pObject)
103{
104 plUInt32 uiIndex = plInvalidIndex;
105
106 if (pObject)
107 {
108 bool bIsRealObject = !m_Objects.TryGetValue(pObject, uiIndex);
109 stream << bIsRealObject;
110
111 if (bIsRealObject)
112 {
113 uiIndex = m_Objects.GetCount();
114 m_Objects.Insert(pObject, uiIndex);
115
116 return plStreamWriterUtil::Serialize<T>(stream, *pObject);
117 }
118 else
119 {
120 stream << uiIndex;
121 }
122 }
123 else
124 {
125 stream << false;
126 stream << plInvalidIndex;
127 }
128
129 return PL_SUCCESS;
130}
Base class for all array containers. Implements all the basic functionality that only requires a poin...
Definition ArrayBase.h:19
plUInt32 GetCount() const
Returns the number of active elements in the array.
Definition ArrayBase_inl.h:172
plResult WriteSet(plStreamWriter &inout_stream, const plSetBase< KeyType, Comparer > &set)
Writes a set of de-duplicated objects.
Definition DeduplicationWriteContext_inl.h:54
plResult WriteObject(plStreamWriter &inout_stream, const T &obj)
Writes a single object to the stream. Can be either a reference or a pointer to the object.
plResult WriteMap(plStreamWriter &inout_stream, const plMapBase< KeyType, ValueType, Comparer > &map, WriteMapMode mode)
Writes a map. Mode controls whether key or value or both should de-duplicated.
Definition DeduplicationWriteContext_inl.h:68
plResult WriteArray(plStreamWriter &inout_stream, const plArrayBase< ValueType, ArrayType > &array)
Writes an array of de-duplicated objects.
Definition DeduplicationWriteContext_inl.h:40
plUInt32 GetCount() const
Returns the number of active entries in the table.
Definition HashTable_inl.h:343
bool TryGetValue(const CompatibleKeyType &key, ValueType &out_value) const
Returns whether an entry with the given key was found and if found writes out the corresponding value...
bool Insert(CompatibleKeyType &&key, CompatibleValueType &&value, ValueType *out_pOldValue=nullptr)
Inserts the key value pair or replaces value if an entry with the given key already exists.
An associative container. Similar to STL::map.
Definition Map.h:193
plUInt32 GetCount() const
Returns the number of elements currently stored in the map. O(1) operation.
Definition Map_inl.h:200
Iterator GetIterator()
Returns an Iterator to the very first element.
Definition Map_inl.h:207
A set container that only stores whether an element resides in it or not. Similar to STL::set.
Definition Set.h:13
plUInt32 GetCount() const
Returns the number of elements currently stored in the set. O(1) operation.
Definition Set_inl.h:158
A Shared ptr manages a shared object and destroys that object when no one references it anymore....
Definition SharedPtr.h:10
T * Borrow() const
Borrows the managed object. The shared ptr stays unmodified.
Definition SharedPtr_inl.h:168
Interface for binary out (write) streams.
Definition Stream.h:107
plResult WriteQWordValue(const T *pQWordValue)
Helper method to write a qword value correctly (copes with potentially different endianess)
Definition Stream_inl.h:134
A Unique ptr manages an object and destroys that object when it goes out of scope....
Definition UniquePtr.h:10
T * Borrow() const
Borrows the managed object. The unique ptr stays unmodified.
Definition UniquePtr_inl.h:102
Definition DeduplicationWriteContext_inl.h:10
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54