3#if PL_ENABLED(PL_PLATFORM_BIG_ENDIAN)
8 static_assert(
sizeof(T) ==
sizeof(plUInt16));
12 const plUInt32 uiRead =
ReadBytes(
reinterpret_cast<plUInt8*
>(&uiTemp),
sizeof(T));
16 return (uiRead ==
sizeof(T)) ? PL_SUCCESS : PL_FAILURE;
22 static_assert(
sizeof(T) ==
sizeof(plUInt32));
26 const plUInt32 uiRead =
ReadBytes(
reinterpret_cast<plUInt8*
>(&uiTemp),
sizeof(T));
30 return (uiRead ==
sizeof(T)) ? PL_SUCCESS : PL_FAILURE;
36 static_assert(
sizeof(T) ==
sizeof(plUInt64));
40 const plUInt32 uiRead =
ReadBytes(
reinterpret_cast<plUInt8*
>(&uiTemp),
sizeof(T));
44 return (uiRead ==
sizeof(T)) ? PL_SUCCESS : PL_FAILURE;
52 static_assert(
sizeof(T) ==
sizeof(plUInt16));
54 plUInt16 uiTemp = *
reinterpret_cast<const plUInt16*
>(pWordValue);
57 return WriteBytes(
reinterpret_cast<plUInt8*
>(&uiTemp),
sizeof(T));
63 static_assert(
sizeof(T) ==
sizeof(plUInt32));
65 plUInt32 uiTemp = *
reinterpret_cast<const plUInt32*
>(pDWordValue);
68 return WriteBytes(
reinterpret_cast<plUInt8*
>(&uiTemp),
sizeof(T));
74 static_assert(
sizeof(T) ==
sizeof(plUInt64));
76 plUInt64 uiTemp = *
reinterpret_cast<const plUInt64*
>(pQWordValue);
79 return WriteBytes(
reinterpret_cast<plUInt8*
>(&uiTemp),
sizeof(T));
87 static_assert(
sizeof(T) ==
sizeof(plUInt16));
89 if (
ReadBytes(
reinterpret_cast<plUInt8*
>(pWordValue),
sizeof(T)) !=
sizeof(T))
98 static_assert(
sizeof(T) ==
sizeof(plUInt32));
100 if (
ReadBytes(
reinterpret_cast<plUInt8*
>(pDWordValue),
sizeof(T)) !=
sizeof(T))
109 static_assert(
sizeof(T) ==
sizeof(plUInt64));
111 if (
ReadBytes(
reinterpret_cast<plUInt8*
>(pQWordValue),
sizeof(T)) !=
sizeof(T))
120 static_assert(
sizeof(T) ==
sizeof(plUInt16));
122 return WriteBytes(
reinterpret_cast<const plUInt8*
>(pWordValue),
sizeof(T));
128 static_assert(
sizeof(T) ==
sizeof(plUInt32));
130 return WriteBytes(
reinterpret_cast<const plUInt8*
>(pDWordValue),
sizeof(T));
136 static_assert(
sizeof(T) ==
sizeof(plUInt64));
138 return WriteBytes(
reinterpret_cast<const plUInt8*
>(pQWordValue),
sizeof(T));
143plTypeVersion plStreamReader::ReadVersion(plTypeVersion expectedMaxVersion)
148 PL_ASSERT_ALWAYS(v <= expectedMaxVersion,
"Read version ({0}) is larger than expected max version ({1}).", v, expectedMaxVersion);
149 PL_ASSERT_ALWAYS(v > 0,
"Invalid version.");
156 PL_ASSERT_ALWAYS(version > 0,
"Version cannot be zero.");
162namespace plStreamWriterUtil
167 PL_ALWAYS_INLINE
auto SerializeImpl(
plStreamWriter& inout_stream,
const T& obj,
int) ->
decltype(inout_stream << obj,
plResult(PL_SUCCESS))
175 PL_ALWAYS_INLINE
auto SerializeImpl(
plStreamWriter& inout_stream,
const T& obj,
long) ->
decltype(obj.Serialize(inout_stream).IgnoreResult(),
plResult(PL_SUCCESS))
177 return plToResult(obj.Serialize(inout_stream));
181 PL_ALWAYS_INLINE
auto SerializeImpl(
plStreamWriter& inout_stream,
const T& obj,
float) ->
decltype(obj.serialize(inout_stream).IgnoreResult(),
plResult(PL_SUCCESS))
183 return plToResult(obj.serialize(inout_stream));
187 PL_ALWAYS_INLINE
auto Serialize(
plStreamWriter& inout_stream,
const T& obj) ->
decltype(SerializeImpl(inout_stream, obj, 0).IgnoreResult(),
plResult(PL_SUCCESS))
189 return SerializeImpl(inout_stream, obj, 0);
194#if PL_DISABLED(PL_PLATFORM_WINDOWS_UWP)
196 PL_ALWAYS_INLINE
auto SerializeArrayImpl(
plStreamWriter& inout_stream,
const T* pArray, plUInt64 uiCount,
int) ->
decltype(SerializeArray(inout_stream, pArray, uiCount),
plResult(PL_SUCCESS))
198 return SerializeArray(inout_stream, pArray, uiCount);
205 for (plUInt64 i = 0; i < uiCount; ++i)
207 PL_SUCCEED_OR_RETURN(plStreamWriterUtil::Serialize<T>(inout_stream, pArray[i]));
216 return SerializeArrayImpl(inout_stream, pArray, uiCount, 0);
220template <
typename ArrayType,
typename ValueType>
223 const plUInt64 uiCount = array.
GetCount();
226 return plStreamWriterUtil::SerializeArray<ValueType>(*
this, array.
GetData(), array.
GetCount());
229template <
typename ValueType, plUInt16 uiSize>
232 const plUInt32 uiCount = array.
GetCount();
235 return plStreamWriterUtil::SerializeArray<ValueType>(*
this, array.
GetData(), array.
GetCount());
238template <
typename ValueType, plUInt32 uiSize>
241 const plUInt64 uiWriteSize = uiSize;
244 return plStreamWriterUtil::SerializeArray<ValueType>(*
this, array, uiSize);
247template <
typename KeyType,
typename Comparer>
250 const plUInt64 uiWriteSize = set.
GetCount();
253 for (
const auto& item : set)
255 PL_SUCCEED_OR_RETURN(plStreamWriterUtil::Serialize<KeyType>(*
this, item));
261template <
typename KeyType,
typename ValueType,
typename Comparer>
264 const plUInt64 uiWriteSize = map.
GetCount();
267 for (
auto It = map.
GetIterator(); It.IsValid(); ++It)
269 PL_SUCCEED_OR_RETURN(plStreamWriterUtil::Serialize<KeyType>(*
this, It.Key()));
270 PL_SUCCEED_OR_RETURN(plStreamWriterUtil::Serialize<ValueType>(*
this, It.Value()));
276template <
typename KeyType,
typename ValueType,
typename Hasher>
279 const plUInt64 uiWriteSize = hashTable.
GetCount();
282 for (
auto It = hashTable.
GetIterator(); It.IsValid(); ++It)
284 PL_SUCCEED_OR_RETURN(plStreamWriterUtil::Serialize<KeyType>(*
this, It.Key()));
285 PL_SUCCEED_OR_RETURN(plStreamWriterUtil::Serialize<ValueType>(*
this, It.Value()));
291namespace plStreamReaderUtil
294 PL_ALWAYS_INLINE
auto DeserializeImpl(
plStreamReader& inout_stream, T& ref_obj,
int) ->
decltype(inout_stream >> ref_obj,
plResult(PL_SUCCESS))
296 inout_stream >> ref_obj;
302 PL_ALWAYS_INLINE
auto DeserializeImpl(
plStreamReader& inout_stream, T& inout_obj,
long) ->
decltype(inout_obj.Deserialize(inout_stream).IgnoreResult(),
plResult(PL_SUCCESS))
304 return plToResult(inout_obj.Deserialize(inout_stream));
308 PL_ALWAYS_INLINE
auto DeserializeImpl(
plStreamReader& inout_stream, T& inout_obj,
float) ->
decltype(inout_obj.deserialize(inout_stream).IgnoreResult(),
plResult(PL_SUCCESS))
310 return plToResult(inout_obj.deserialize(inout_stream));
314 PL_ALWAYS_INLINE
auto Deserialize(
plStreamReader& inout_stream, T& inout_obj) ->
decltype(DeserializeImpl(inout_stream, inout_obj, 0).IgnoreResult(),
plResult(PL_SUCCESS))
316 return DeserializeImpl(inout_stream, inout_obj, 0);
321#if PL_DISABLED(PL_PLATFORM_WINDOWS_UWP)
323 PL_ALWAYS_INLINE
auto DeserializeArrayImpl(
plStreamReader& inout_stream, T* pArray, plUInt64 uiCount,
int) ->
decltype(DeserializeArray(inout_stream, pArray, uiCount),
plResult(PL_SUCCESS))
325 return DeserializeArray(inout_stream, pArray, uiCount);
332 for (plUInt64 i = 0; i < uiCount; ++i)
334 PL_SUCCEED_OR_RETURN(plStreamReaderUtil::Deserialize<T>(inout_stream, pArray[i]));
343 return DeserializeArrayImpl(inout_stream, pArray, uiCount, 0);
348template <
typename ArrayType,
typename ValueType>
351 plUInt64 uiCount = 0;
360 static_cast<ArrayType&
>(inout_array).SetCount(
static_cast<plUInt32
>(uiCount));
362 PL_SUCCEED_OR_RETURN(plStreamReaderUtil::DeserializeArray<ValueType>(*
this, inout_array.
GetData(), uiCount));
374template <
typename ValueType, plUInt16 uiSize,
typename AllocatorWrapper>
377 plUInt32 uiCount = 0;
386 ref_array.SetCount(
static_cast<plUInt16
>(uiCount));
388 PL_SUCCEED_OR_RETURN(plStreamReaderUtil::DeserializeArray<ValueType>(*
this, ref_array.
GetData(), uiCount));
400template <
typename ValueType, plUInt32 uiSize>
403 plUInt64 uiCount = 0;
406 if (
static_cast<plUInt32
>(uiCount) != uiSize)
411 PL_SUCCEED_OR_RETURN(plStreamReaderUtil::DeserializeArray<ValueType>(*
this, array, uiCount));
420template <
typename KeyType,
typename Comparer>
423 plUInt64 uiCount = 0;
430 for (plUInt32 i = 0; i < static_cast<plUInt32>(uiCount); ++i)
433 PL_SUCCEED_OR_RETURN(plStreamReaderUtil::Deserialize(*
this, Item));
435 inout_set.
Insert(std::move(Item));
447template <
typename KeyType,
typename ValueType,
typename Comparer>
450 plUInt64 uiCount = 0;
457 for (plUInt32 i = 0; i < static_cast<plUInt32>(uiCount); ++i)
461 PL_SUCCEED_OR_RETURN(plStreamReaderUtil::Deserialize(*
this, Key));
462 PL_SUCCEED_OR_RETURN(plStreamReaderUtil::Deserialize(*
this, Value));
464 inout_map.
Insert(std::move(Key), std::move(Value));
476template <
typename KeyType,
typename ValueType,
typename Hasher>
479 plUInt64 uiCount = 0;
484 inout_hashTable.
Clear();
485 inout_hashTable.
Reserve(
static_cast<plUInt32
>(uiCount));
487 for (plUInt32 i = 0; i < static_cast<plUInt32>(uiCount); ++i)
491 PL_SUCCEED_OR_RETURN(plStreamReaderUtil::Deserialize(*
this, Key));
492 PL_SUCCEED_OR_RETURN(plStreamReaderUtil::Deserialize(*
this, Value));
494 inout_hashTable.
Insert(std::move(Key), std::move(Value));
Base class for all array containers. Implements all the basic functionality that only requires a poin...
Definition ArrayBase.h:19
T * GetData()
Returns a pointer to the array data, or nullptr if the array is empty.
Definition ArrayBase_inl.h:423
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
Implementation of a hashtable which stores key/value pairs.
Definition HashTable.h:157
Iterator GetIterator()
Returns an Iterator to the very first element.
Definition HashTable_inl.h:625
plUInt32 GetCount() const
Returns the number of active entries in the table.
Definition HashTable_inl.h:343
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.
void Clear()
Clears the table.
Definition HashTable_inl.h:355
void Reserve(plUInt32 uiCapacity)
Expands the hashtable by over-allocating the internal storage so that the load factor is lower or equ...
Definition HashTable_inl.h:307
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
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
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
plUInt32 GetCount() const
Returns the number of elements currently stored in the set. O(1) operation.
Definition Set_inl.h:158
Implementation of a dynamically growing array with in-place storage and small memory overhead.
Definition SmallArray.h:17
void Clear()
Clears the array.
Definition SmallArray_inl.h:222
T * GetData()
Returns a pointer to the array data, or nullptr if the array is empty.
Definition SmallArray_inl.h:444
plUInt32 GetCount() const
Returns the number of active elements in the array.
Definition SmallArray_inl.h:210
Definition SmallArray.h:219
Interface for binary in (read) streams.
Definition Stream.h:22
plResult ReadArray(plArrayBase< ValueType, ArrayType > &inout_array)
Reads an array of elements from the stream.
Definition Stream_inl.h:349
plResult ReadWordValue(T *pWordValue)
Helper method to read a word value correctly (copes with potentially different endianess)
Definition Stream_inl.h:85
plResult ReadHashTable(plHashTableBase< KeyType, ValueType, Hasher > &inout_hashTable)
Read a hash table (note that the entry order is not stable)
Definition Stream_inl.h:477
plResult ReadQWordValue(T *pQWordValue)
Helper method to read a qword value correctly (copes with potentially different endianess)
Definition Stream_inl.h:107
plResult ReadMap(plMapBase< KeyType, ValueType, Comparer > &inout_map)
Reads a map.
Definition Stream_inl.h:448
plResult ReadSet(plSetBase< KeyType, Comparer > &inout_set)
Reads a set.
Definition Stream_inl.h:421
plResult ReadDWordValue(T *pDWordValue)
Helper method to read a dword value correctly (copes with potentially different endianess)
Definition Stream_inl.h:96
virtual plUInt64 ReadBytes(void *pReadBuffer, plUInt64 uiBytesToRead)=0
Reads a raw number of bytes into the read buffer, this is the only method which has to be implemented...
Interface for binary out (write) streams.
Definition Stream.h:107
plResult WriteSet(const plSetBase< KeyType, Comparer > &set)
Writes a set.
Definition Stream_inl.h:248
virtual plResult WriteBytes(const void *pWriteBuffer, plUInt64 uiBytesToWrite)=0
Writes a raw number of bytes from the buffer, this is the only method which has to be implemented to ...
plResult WriteDWordValue(const T *pDWordValue)
Helper method to write a dword value correctly (copes with potentially different endianess)
Definition Stream_inl.h:126
plResult WriteArray(const plArrayBase< ValueType, ArrayType > &array)
Writes an array of elements to the stream.
Definition Stream_inl.h:221
PL_ALWAYS_INLINE void WriteVersion(plTypeVersion version)
Writes a type version to the stream.
Definition Stream_inl.h:154
plResult WriteMap(const plMapBase< KeyType, ValueType, Comparer > &map)
Writes a map.
Definition Stream_inl.h:262
plResult WriteWordValue(const T *pWordValue)
Helper method to write a word value correctly (copes with potentially different endianess)
Definition Stream_inl.h:118
plResult WriteHashTable(const plHashTableBase< KeyType, ValueType, Hasher > &hashTable)
Writes a hash table (note that the entry order might change on read)
Definition Stream_inl.h:277
plResult WriteQWordValue(const T *pQWordValue)
Helper method to write a qword value correctly (copes with potentially different endianess)
Definition Stream_inl.h:134
constexpr TYPE MaxValue()
Returns the largest possible positive value (that is not infinity).
static PL_ALWAYS_INLINE plUInt16 Switch(plUInt16 uiWord)
Returns a single switched word (16 bit value).
Definition EndianHelper.h:45
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
PL_ALWAYS_INLINE void IgnoreResult()
Used to silence compiler warnings, when success or failure doesn't matter.
Definition Types.h:69