Faro Engine 0.0.0.b519570 (main)
Loading...
Searching...
No Matches
Stream.hpp
Go to the documentation of this file.
1#pragma once
2#include <Primitives.hpp>
3#include <Memory/Object.hpp>
6
7namespace Faro
8{
9 class DataStreamCopy;
10
11 /// @brief Origin point of a seek operation.
13 {
14 SSO_Start, ///< Seek relative to the beginning of a stream.
15 SSO_End, ///< Seek relative to the end of a stream.
16 SSO_Current ///< Seek relative to the current position.
17 };
18
19 /// @brief Interface that reads and writes data to and from a stream.
20 class DataStream : public IObject
21 {
22 public:
23 /**
24 * @brief Read data from the stream.
25 * @param destination Destination memory. This must be a valid pointer.
26 * @param elementSize Size of a single element
27 * @param elementCount Amount of elements to read
28 * @return uint32 Amount of bytes read
29 */
30 virtual uint32 Read(void* destination, uint16 elementSize, uint32 elementCount) = 0;
31
32 /**
33 * @brief Write data to the stream.
34 * @param source Source memory
35 * @param elementSize Size of a single element
36 * @param elementCount Amount of elements to write
37 * @return uint32 Amount of bytes written
38 */
39 virtual uint32 Write(void* source, uint16 elementSize, uint32 elementCount) = 0;
40
41 /**
42 * @brief Get the byte-size of this stream.
43 * @return uint32 Amount of bytes in this stream
44 */
45 virtual uint32 Size() = 0;
46
47 /**
48 * @brief Get the current position of the stream.
49 * @return uint32 Current position
50 */
51 virtual uint32 Tell() = 0;
52
53 /**
54 * @brief Move the position relative to the specified origin.
55 * @param origin Start point of the seeking operation
56 * @param offset Amount of bytes to seek relative to the origin
57 */
58 virtual void Seek(EStreamSeekOrigin origin, int32 offset) = 0;
59
60 /**
61 * @brief Read data from the stream.
62 * @tparam T Element type to read
63 * @param destination Destination memory. This must be a valid pointer.
64 * @param elementCount Amount of elements to read
65 * @return uint32 Amount of elements read
66 */
67 template<typename T>
68 uint32 Read(T* destination, uint32 elementCount)
69 {
70 return Read(destination, sizeof(T), elementCount) / sizeof(T);
71 }
72
73 /**
74 * @brief Read en element from the stream.
75 * @tparam T Element type to read
76 * @return T Element that was read
77 */
78 template<typename T>
79 T Read()
80 {
81 T returnValue;
83 return returnValue;
84 }
85
86 /**
87 * @brief Read the data to an array of elements.
88 * @tparam T Element type to read
89 * @return Array<T> Array of elements read from the stream
90 */
91 template<typename T>
93 {
94 uint32 elements = Size() / sizeof(T);
95 Array<T> data;
96 data.Resize(elements);
97 Read<T>(data.Data(), elements);
98 return data;
99 }
100
101 /**
102 * @brief Read the data to a stream
103 * @return String String read from the stream
104 */
106
107 /**
108 * @brief Write data to the stream.
109 * @tparam T Element type to write
110 * @param source Source memory
111 * @param elementCount Amount of elements to write
112 * @return uint32 Amount of elements written
113 */
114 template<typename T>
116 {
117 return Write(source, sizeof(T), elementCount) / sizeof(T);
118 }
119
120 /**
121 * @brief Write an element to the stream
122 * @tparam T Element type to write
123 * @param value Element to write
124 */
125 template<typename T>
127 {
128 Write<T>(&value, 1);
129 }
130
131 /// @brief Close the stream. This also releases it from memory.
132 void Close();
133
135
136 protected:
137 void Destroy() override;
139
141
143 };
144
146 {
147 public:
150 uint32 Size() override;
151 uint32 Tell() override;
152 void Seek(EStreamSeekOrigin origin, int32 offset) override;
153
154 void Init(DataStream* stream);
155
156 protected:
157 void Init() override;
158 void Destroy() override;
159
160 private:
161 DataStream* stream = nullptr;
162 uint32 cursor = 0;
163 };
164}
Definition Array.hpp:11
T * Data()
Definition Array.hpp:131
void Resize(uint16 size)
Definition Array.hpp:79
Definition Stream.hpp:146
uint32 Write(void *source, uint16 elementSize, uint32 elementCount) override
Write data to the stream.
Definition Stream.cpp:50
uint32 Tell() override
Get the current position of the stream.
Definition Stream.cpp:63
void Destroy() override
Run the destruction logic of this class. This also frees its memory.
Definition Stream.cpp:89
void Seek(EStreamSeekOrigin origin, int32 offset) override
Move the position relative to the specified origin.
Definition Stream.cpp:68
void Init() override
Run the initialization logic of this object. Should be overriden by deriving classes.
Definition Stream.cpp:83
uint32 Size() override
Get the byte-size of this stream.
Definition Stream.cpp:58
Interface that reads and writes data to and from a stream.
Definition Stream.hpp:21
virtual uint32 Write(void *source, uint16 elementSize, uint32 elementCount)=0
Write data to the stream.
virtual uint32 Tell()=0
Get the current position of the stream.
void Destroy() override
Run the destruction logic of this class. This also frees its memory.
Definition Stream.cpp:26
DataStreamCopy * OpenCopy()
Definition Stream.cpp:19
void AddCopy(DataStream *otherStream)
Definition Stream.cpp:31
uint32 Read(T *destination, uint32 elementCount)
Read data from the stream.
Definition Stream.hpp:68
void Close()
Close the stream. This also releases it from memory.
Definition Stream.cpp:13
Array< T > ReadToArray()
Read the data to an array of elements.
Definition Stream.hpp:92
virtual uint32 Size()=0
Get the byte-size of this stream.
virtual uint32 Read(void *destination, uint16 elementSize, uint32 elementCount)=0
Read data from the stream.
T Read()
Read en element from the stream.
Definition Stream.hpp:79
virtual void Seek(EStreamSeekOrigin origin, int32 offset)=0
Move the position relative to the specified origin.
String ReadToString()
Read the data to a stream.
Definition Stream.cpp:7
void RemoveCopy(DataStream *otherStream)
Definition Stream.cpp:36
uint32 copies
Definition Stream.hpp:138
uint32 Write(T *source, uint32 elementCount)
Write data to the stream.
Definition Stream.hpp:115
void Write(T value)
Write an element to the stream.
Definition Stream.hpp:126
Base class for dynamically allocated objects.
Definition Object.hpp:7
Definition String.hpp:12
Definition Array.hpp:8
int32_t int32
Definition Primitives.hpp:13
uint16_t uint16
Definition Primitives.hpp:9
uint32_t uint32
Definition Primitives.hpp:12
EStreamSeekOrigin
Origin point of a seek operation.
Definition Stream.hpp:13
@ SSO_Start
Seek relative to the beginning of a stream.
Definition Stream.hpp:14
@ SSO_End
Seek relative to the end of a stream.
Definition Stream.hpp:15
@ SSO_Current
Seek relative to the current position.
Definition Stream.hpp:16