Athena IO Library
MemoryReader.hpp
1 #ifndef MEMORYREADER_HPP
2 #define MEMORYREADER_HPP
3 
4 #include <string>
5 #include <memory>
6 #include <functional>
7 #include "athena/IStreamReader.hpp"
8 
9 namespace athena
10 {
11 namespace io
12 {
22 {
23 protected:
24  MemoryReader() = default;
25 public:
26  virtual ~MemoryReader();
27 
34  MemoryReader(const void* data, atUint64 length, bool takeOwnership=false, bool globalErr=true);
35 
41  void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
42 
47  inline atUint64 position() const
48  {return m_position;}
49 
54  inline atUint64 length() const
55  {return m_length;}
56 
57 
68  void setData(const atUint8* data, atUint64 length, bool takeOwnership=false);
69 
70 
78  atUint8* data() const;
79 
85  atUint64 readUBytesToBuf(void* buf, atUint64 len);
86 
87 protected:
88  const void* m_data = nullptr;
89  atUint64 m_length = 0;
90  atUint64 m_position = 0;
91  bool m_owns = false;
92  bool m_globalErr = true;
93 };
94 
96 {
97 public:
103  MemoryCopyReader(const void* data, atUint64 length);
104 
109  MemoryCopyReader(const std::string& filename)
110  : m_filepath(filename)
111  {loadData();}
112 
113  void setData(const atUint8* data, atUint64 length);
114 
115 protected:
116  void loadData();
117  std::unique_ptr<atUint8[]> m_dataCopy;
118  std::string m_filepath;
119 };
120 
121 } // io
122 } // Athena
123 
124 #ifndef MEMORYREADER_BASE
125 #define MEMORYREADER_BASE() \
126 private: \
127  typedef athena::io::MemoryReader base
128 
129 #endif // MEMORYREADER_BASE
130 
131 #ifndef MEMORYCOPYREADER_BASE
132 #define MEMORYCOPYREADER_BASE() \
133 private: \
134  typedef athena::io::MemoryCopyReader base
135 
136 #endif // MEMORYCOPYREADER_BASE
137 
138 #endif // MEMORYREADER_HPP
void setData(const atUint8 *data, atUint64 length, bool takeOwnership=false)
Sets the buffer to the given one, deleting the current one. BEWARE: As this deletes the current buff...
atUint64 readUBytesToBuf(void *buf, atUint64 len)
Reads a specified number of bytes to user-allocated buffer.
The IStreamReader class defines a basic API for reading from streams, Implementors are provided with ...
void seek(atInt64 pos, SeekOrigin origin=SeekOrigin::Current)
Sets the buffers position relative to the specified position. It seeks relative to the current posit...
std::string m_filepath
Path to the target file.
atUint64 position() const
Returns the current position in the stream.
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 length() const
Returns whether or not the stream is at the end.
A Stream class for reading data from a memory position.
MemoryCopyReader(const std::string &filename)
This constructor creates an instance from a file on disk.