StevEngine
StevEngine is a customizable C++ game engine.
Loading...
Searching...
No Matches
Stream.hpp
1#pragma once
2#include "main/ResourceManager.hpp"
3
4#include <fstream>
5#include <sstream>
6
7namespace StevEngine::Utilities {
11 enum StreamType {
12 Text,
13 Binary
14 };
20 class Stream {
21 public:
22 const StreamType type;
23
28 Stream(StreamType type) : type(type) {};
29
35 template <typename T> T Read();
36
42 template <typename T> void Write(const T& data);
43
44 //Stream operators
50 template <typename T> Stream& operator << (const T& data) {
51 Write<T>(data);
52 return *this;
53 }
54
59 template <typename T> Stream& operator >> (T& out) {
60 out = Read<T>();
61 return *this;
62 }
63
70 void WriteToFile(const char* path) const;
71
76 void ReadFromFile(const Resources::Resource& file);
77
82 void ReadFromFile(std::ifstream& file);
83
88 std::stringstream& GetStream() { return stream; }
89
94 const std::stringstream& GetStream() const { return stream; }
95
100 Stream(const Stream& stream) : type(stream.type) {
101 *this << stream;
102 }
103 protected:
104 std::stringstream stream;
105 };
106}
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:59
void WriteToFile(const char *path) const
Write full stream content to a file.
Definition Stream.cpp:8
Stream(StreamType type)
Create new stream of type.
Definition Stream.hpp:28
void ReadFromFile(const Resources::Resource &file)
Read content from a file to stream.
Definition Stream.cpp:15
std::stringstream & GetStream()
Get underlying std::stringstream object.
Definition Stream.hpp:88
const StreamType type
Type of serialization used by stream.
Definition Stream.hpp:22
Stream(const Stream &stream)
make a copy of a stream
Definition Stream.hpp:100
const std::stringstream & GetStream() const
Get underlying std::stringstream object.
Definition Stream.hpp:94
Stream & operator<<(const T &data)
Write data of specified type to stream.
Definition Stream.hpp:50