Athena IO Library
MemoryWriter.hpp
1 #ifndef MEMORYWRITER_HPP
2 #define MEMORYWRITER_HPP
3 
4 #include <string>
5 #include <memory>
6 #include <functional>
7 #include "athena/IStreamWriter.hpp"
8 
9 namespace athena
10 {
11 namespace io
12 {
13 
23 {
24 public:
25 
26  virtual ~MemoryWriter();
27 
33  explicit MemoryWriter(atUint8* data, atUint64 length, bool takeOwnership = false);
34 
40  void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
41 
42 
47  inline atUint64 position() const
48  {return m_position;}
49 
54  inline atUint64 length() const
55  {return m_length;}
56 
57  inline bool isOpen() const {return true;}
58 
64  void setData(atUint8* data, atUint64 length, bool takeOwnership = false);
65 
66 
72  atUint8* data() const;
73 
78  inline void setFilepath(const std::string& filepath)
79  {m_filepath = filepath;}
80 
84  inline std::string filepath() const
85  {return m_filepath;}
86 
87 
92  void save(const std::string& filename = "");
93 
101  void writeUBytes(const atUint8* data, atUint64 len);
102 
103 protected:
104  MemoryWriter() {}
105  atUint8* m_data;
106  atUint64 m_length;
107  atUint64 m_position;
108  bool m_bufferOwned;
109  std::string m_filepath;
110 };
111 
113 {
114 public:
115 
121  explicit MemoryCopyWriter(atUint8* data=nullptr, atUint64 length=0x10);
122 
127  MemoryCopyWriter(const std::string& filename);
128 
134  void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
135 
145  void setData(const atUint8* data, atUint64 length);
146 
153  void writeUBytes(const atUint8* data, atUint64 len);
154 
155 protected:
156  std::unique_ptr<atUint8[]> m_dataCopy;
157 private:
158  void resize(atUint64 newSize);
159 };
160 
161 }
162 }
163 
164 #ifndef MEMORYWRITER_BASE
165 #define MEMORYWRITER_BASE() \
166  private: \
167  typedef athena::io::MemoryWriter base
168 #endif // BINARYWRITER_BASE
169 
170 #ifndef MEMORYCOPYWRITER_BASE
171 #define MEMORYCOPYWRITER_BASE() \
172  private: \
173  typedef athena::io::MemoryCopyWriter base
174 #endif // BINARYWRITER_BASE
175 
176 #endif // MEMORYWRITER_HPP
A Stream class for writing data to a memory position.
std::string filepath() const
Returns the target file.
atUint8 * data() const
Returns a copy of the current buffer. Changes to the copy do not affect the buffer so it&#39;s perfectly...
atUint64 position() const
Returns the current position in the stream.
void writeUBytes(const atUint8 *data, atUint64 len)
Writes the given buffer with the specified length, buffers can be bigger than the length however it&#39;s...
atUint64 length() const
Returns the length of the stream.
void seek(atInt64 pos, SeekOrigin origin=SeekOrigin::Current)
Sets the buffers position relative to the specified position. It seeks relative to the current posit...
void setFilepath(const std::string &filepath)
Sets the target file.
std::string m_filepath
Path to the target file.
void save(const std::string &filename="")
Saves the file to the specified file.
void setData(atUint8 *data, atUint64 length, bool takeOwnership=false)
Sets the buffer to the given one, deleting the current one if it owns it.