Plasma Engine  2.0
Loading...
Searching...
No Matches
Stream.h
1
2#pragma once
3
4#include <Foundation/Basics.h>
5#include <Foundation/Containers/ArrayBase.h>
6#include <Foundation/Containers/HashTable.h>
7#include <Foundation/Containers/Map.h>
8#include <Foundation/Containers/Set.h>
9#include <Foundation/Containers/SmallArray.h>
10#include <Foundation/Math/Math.h>
11#include <Foundation/Memory/EndianHelper.h>
12
13using plTypeVersion = plUInt16;
14
15template <plUInt16 Size, typename AllocatorWrapper>
16struct plHybridString;
17
19
21class PL_FOUNDATION_DLL plStreamReader
22{
23 PL_DISALLOW_COPY_AND_ASSIGN(plStreamReader);
24
25public:
28
30 virtual ~plStreamReader();
31
34 virtual plUInt64 ReadBytes(void* pReadBuffer, plUInt64 uiBytesToRead) = 0; // [tested]
35
37 template <typename T>
38 plResult ReadWordValue(T* pWordValue); // [tested]
39
41 template <typename T>
42 plResult ReadDWordValue(T* pDWordValue); // [tested]
43
45 template <typename T>
46 plResult ReadQWordValue(T* pQWordValue); // [tested]
47
49 template <typename ArrayType, typename ValueType>
50 plResult ReadArray(plArrayBase<ValueType, ArrayType>& inout_array); // [tested]
51
53 template <typename ValueType, plUInt16 uiSize, typename AllocatorWrapper>
55
57 template <typename ValueType, plUInt32 uiSize>
58 plResult ReadArray(ValueType (&array)[uiSize]);
59
61 template <typename KeyType, typename Comparer>
62 plResult ReadSet(plSetBase<KeyType, Comparer>& inout_set); // [tested]
63
65 template <typename KeyType, typename ValueType, typename Comparer>
66 plResult ReadMap(plMapBase<KeyType, ValueType, Comparer>& inout_map); // [tested]
67
69 template <typename KeyType, typename ValueType, typename Hasher>
70 plResult ReadHashTable(plHashTableBase<KeyType, ValueType, Hasher>& inout_hashTable); // [tested]
71
73 plResult ReadString(plStringBuilder& ref_sBuilder); // [tested]
74
76 plResult ReadString(plString& ref_sString);
77
78
80 virtual plUInt64 SkipBytes(plUInt64 uiBytesToSkip)
81 {
82 plUInt8 uiTempBuffer[1024];
83
84 plUInt64 uiBytesSkipped = 0;
85
86 while (uiBytesSkipped < uiBytesToSkip)
87 {
88 plUInt64 uiBytesToRead = plMath::Min<plUInt64>(uiBytesToSkip - uiBytesSkipped, 1024);
89
90 plUInt64 uiBytesRead = ReadBytes(uiTempBuffer, uiBytesToRead);
91
92 uiBytesSkipped += uiBytesRead;
93
94 // Terminate early if the stream didn't read as many bytes as we requested (EOF for example)
95 if (uiBytesRead < uiBytesToRead)
96 break;
97 }
98
99 return uiBytesSkipped;
100 }
101
102 PL_ALWAYS_INLINE plTypeVersion ReadVersion(plTypeVersion expectedMaxVersion);
103};
104
106class PL_FOUNDATION_DLL plStreamWriter
107{
108 PL_DISALLOW_COPY_AND_ASSIGN(plStreamWriter);
109
110public:
113
116
119 virtual plResult WriteBytes(const void* pWriteBuffer, plUInt64 uiBytesToWrite) = 0; // [tested]
120
123 virtual plResult Flush() // [tested]
124 {
125 return PL_SUCCESS;
126 }
127
129 template <typename T>
130 plResult WriteWordValue(const T* pWordValue); // [tested]
131
133 template <typename T>
134 plResult WriteDWordValue(const T* pDWordValue); // [tested]
135
137 template <typename T>
138 plResult WriteQWordValue(const T* pQWordValue); // [tested]
139
141 PL_ALWAYS_INLINE void WriteVersion(plTypeVersion version);
142
144 template <typename ArrayType, typename ValueType>
145 plResult WriteArray(const plArrayBase<ValueType, ArrayType>& array); // [tested]
146
148 template <typename ValueType, plUInt16 uiSize>
149 plResult WriteArray(const plSmallArrayBase<ValueType, uiSize>& array);
150
152 template <typename ValueType, plUInt32 uiSize>
153 plResult WriteArray(const ValueType (&array)[uiSize]);
154
156 template <typename KeyType, typename Comparer>
157 plResult WriteSet(const plSetBase<KeyType, Comparer>& set); // [tested]
158
160 template <typename KeyType, typename ValueType, typename Comparer>
161 plResult WriteMap(const plMapBase<KeyType, ValueType, Comparer>& map); // [tested]
162
164 template <typename KeyType, typename ValueType, typename Hasher>
165 plResult WriteHashTable(const plHashTableBase<KeyType, ValueType, Hasher>& hashTable); // [tested]
166
168 plResult WriteString(const plStringView sStringView); // [tested]
169};
170
171// Contains the helper methods of both interfaces
172#include <Foundation/IO/Implementation/Stream_inl.h>
173
174// Standard operators for overloads of common data types
175#include <Foundation/IO/Implementation/StreamOperations_inl.h>
176
177#include <Foundation/IO/Implementation/StreamOperationsMath_inl.h>
178
179#include <Foundation/IO/Implementation/StreamOperationsOther_inl.h>
Base class for all array containers. Implements all the basic functionality that only requires a poin...
Definition ArrayBase.h:19
Implementation of a hashtable which stores key/value pairs.
Definition HashTable.h:157
An associative container. Similar to STL::map.
Definition Map.h:193
A set container that only stores whether an element resides in it or not. Similar to STL::set.
Definition Set.h:13
Implementation of a dynamically growing array with in-place storage and small memory overhead.
Definition SmallArray.h:17
Definition SmallArray.h:219
Interface for binary in (read) streams.
Definition Stream.h:22
virtual ~plStreamReader()
Virtual destructor to ensure correct cleanup.
virtual plUInt64 SkipBytes(plUInt64 uiBytesToSkip)
Helper method to skip a number of bytes (implementations of the stream reader may implement this more...
Definition Stream.h:80
plStreamReader()
Constructor.
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
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 ...
virtual ~plStreamWriter()
Virtual destructor to ensure correct cleanup.
plStreamWriter()
Constructor.
virtual plResult Flush()
Flushes the stream, may be implemented (not necessary to implement the interface correctly) so that u...
Definition Stream.h:123
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
constexpr PL_ALWAYS_INLINE T Min(T f1, T f2)
Returns the smaller value, f1 or f2.
Definition Math_inl.h:27
Definition String.h:146
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54