ezEngine  Milestone 9
Stream.h
1 
2 #pragma once
3 
4 #include <Foundation/Basics.h>
5 #include <Foundation/Math/Math.h>
6 #include <Foundation/Memory/EndianHelper.h>
7 #include <Foundation/Containers/ArrayBase.h>
8 #include <Foundation/Containers/Set.h>
9 #include <Foundation/Containers/Map.h>
10 
11 typedef ezUInt16 ezTypeVersion;
12 
14 class EZ_FOUNDATION_DLL ezStreamReader
15 {
16 
17 public:
20 
22  virtual ~ezStreamReader();
23 
26  virtual ezUInt64 ReadBytes(void* pReadBuffer, ezUInt64 uiBytesToRead) = 0; // [tested]
27 
29  template <typename T>
30  ezResult ReadWordValue(T* pWordValue); // [tested]
31 
33  template <typename T>
34  ezResult ReadDWordValue(T* pDWordValue); // [tested]
35 
37  template <typename T>
38  ezResult ReadQWordValue(T* pQWordValue); // [tested]
39 
41  template <typename ArrayType, typename ValueType>
42  ezResult ReadArray(ezArrayBase<ValueType, ArrayType>& Array); // [tested]
43 
45  template <typename ValueType, ezUInt32 uiSize>
46  ezResult ReadArray(ValueType (&Array)[uiSize]);
47 
49  template <typename KeyType, typename Comparer>
50  ezResult ReadSet(ezSetBase<KeyType, Comparer>& Set); // [tested]
51 
53  template <typename KeyType, typename ValueType, typename Comparer>
54  ezResult ReadMap(ezMapBase<KeyType, ValueType, Comparer>& Map); // [tested]
55 
57  ezResult ReadString(ezStringBuilder& builder); // [tested]
58 
60  virtual ezUInt64 SkipBytes(ezUInt64 uiBytesToSkip)
61  {
62  ezUInt8 uiTempBuffer[1024];
63 
64  ezUInt64 uiBytesSkipped = 0;
65 
66  while (uiBytesSkipped < uiBytesToSkip)
67  {
68  ezUInt64 uiBytesToRead = ezMath::Min<ezUInt64>(uiBytesToSkip - uiBytesSkipped, 1024);
69 
70  ezUInt64 uiBytesRead = ReadBytes(uiTempBuffer, uiBytesToRead);
71 
72  uiBytesSkipped += uiBytesRead;
73 
74  // Terminate early if the stream didn't read as many bytes as we requested (EOF for example)
75  if (uiBytesRead < uiBytesToRead)
76  break;
77  }
78 
79  return uiBytesSkipped;
80  }
81 
82  EZ_ALWAYS_INLINE ezTypeVersion ReadVersion(ezTypeVersion uiExpectedMaxVersion);
83 };
84 
86 class EZ_FOUNDATION_DLL ezStreamWriter
87 {
88 
89 public:
92 
94  virtual ~ezStreamWriter();
95 
98  virtual ezResult WriteBytes(const void* pWriteBuffer, ezUInt64 uiBytesToWrite) = 0; // [tested]
99 
102  virtual ezResult Flush() // [tested]
103  {
104  return EZ_SUCCESS;
105  }
106 
108  template <typename T>
109  ezResult WriteWordValue(const T* pWordValue); // [tested]
110 
112  template <typename T>
113  ezResult WriteDWordValue(const T* pDWordValue); // [tested]
114 
116  template <typename T>
117  ezResult WriteQWordValue(const T* pQWordValue); // [tested]
118 
120  EZ_ALWAYS_INLINE void WriteVersion(ezTypeVersion uiVersion);
121 
123  template <typename ArrayType, typename ValueType>
124  ezResult WriteArray(const ezArrayBase<ValueType, ArrayType>& Array); // [tested]
125 
127  template <typename ValueType, ezUInt32 uiSize>
128  ezResult WriteArray(const ValueType (&Array)[uiSize]);
129 
131  template <typename KeyType, typename Comparer>
132  ezResult WriteSet(const ezSetBase<KeyType, Comparer>& Set); // [tested]
133 
135  template <typename KeyType, typename ValueType, typename Comparer>
136  ezResult WriteMap(const ezMapBase<KeyType, ValueType, Comparer>& Map); // [tested]
137 
139  ezResult WriteString(const ezStringView szStringView); // [tested]
140 };
141 
142 // Contains the helper methods of both interfaces
143 #include <Foundation/IO/Implementation/Stream_inl.h>
144 
145 // Standard operators for overloads of common data types
146 #include <Foundation/IO/Implementation/StreamOperations_inl.h>
147 
148 #include <Foundation/IO/Implementation/StreamOperationsMath_inl.h>
149 
150 #include <Foundation/IO/Implementation/StreamOperationsOther_inl.h>
151 
Interface for binary in (read) streams.
Definition: Stream.h:14
virtual ezResult Flush()
Flushes the stream, may be implemented (not necessary to implement the interface correctly) so that u...
Definition: Stream.h:102
ezStringBuilder is a class that is meant for creating and modifying strings.
Definition: StringBuilder.h:34
Interface for binary out (write) streams.
Definition: Stream.h:86
Base class for all array containers. Implements all the basic functionality that only requires a poin...
Definition: ArrayBase.h:12
ezStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition: StringView.h:18
A set container that only stores whether an element resides in it or not. Similar to STL::set...
Definition: Set.h:12
virtual ezUInt64 SkipBytes(ezUInt64 uiBytesToSkip)
Helper method to skip a number of bytes (implementations of the stream reader may implement this more...
Definition: Stream.h:60
An associative container. Similar to STL::map.
Definition: Map.h:17
Default enum for returning failure or success, instead of using a bool.
Definition: Types.h:51