StevEngine
StevEngine is a customizable C++ game engine.
Loading...
Searching...
No Matches
Stream.hpp
1#pragma once
2#include <fstream>
3#include <sstream>
4
5namespace StevEngine::Resources {
6 class Resource;
7}
8
9namespace StevEngine::Utilities {
13 enum StreamType {
14 Text,
15 Binary
16 };
22 class Stream {
23 public:
24 const StreamType type;
25
30 Stream(StreamType type = Binary) : type(type) {};
31
37 template <typename T> Stream(const T& data, StreamType type = Binary) : type(type) {
38 *this << data;
39 };
40
41
47 template <typename T> T Read();
48
54 template <typename T> void Write(const T& data);
55
56 //Stream operators
62 template <typename T> Stream& operator << (const T& data) {
63 Write<T>(data);
64 return *this;
65 }
66
71 template <typename T> Stream& operator >> (T& out) {
72 out = Read<T>();
73 return *this;
74 }
75
82 void WriteToFile(const char* path) const;
83
88 bool ReadFromFile(const Resources::Resource& file);
89
94 bool ReadFromFile(std::ifstream& file);
95
100 std::stringstream& GetStream() { return stream; }
101 operator std::stringstream&() { return stream; }
102
107 const std::stringstream& GetStream() const { return stream; }
108 operator const std::stringstream&() const { return stream; }
109
114 Stream(const Stream& stream) : type(stream.type) {
115 *this << stream;
116 }
117 protected:
118 std::stringstream stream;
119 };
120}
Container for loaded resource data.
Definition ResourceManager.hpp:11
T Read()
Read data of specified type from stream.
void Write(const T &data)
Write data of specified type to stream.
Stream & operator>>(T &out)
Read data of specified type from stream.
Definition Stream.hpp:71
bool ReadFromFile(const Resources::Resource &file)
Read content from a file to stream.
Definition Stream.cpp:17
void WriteToFile(const char *path) const
Write full stream content to a file.
Definition Stream.cpp:10
Stream(StreamType type=Binary)
Create new stream of type.
Definition Stream.hpp:30
std::stringstream & GetStream()
Get underlying std::stringstream object.
Definition Stream.hpp:100
const StreamType type
Type of serialization used by stream.
Definition Stream.hpp:24
Stream(const Stream &stream)
make a copy of a stream
Definition Stream.hpp:114
const std::stringstream & GetStream() const
Get underlying std::stringstream object.
Definition Stream.hpp:107
Stream(const T &data, StreamType type=Binary)
Create new stream of type, with data already added.
Definition Stream.hpp:37
Stream & operator<<(const T &data)
Write data of specified type to stream.
Definition Stream.hpp:62