IO.hpp
Go to the documentation of this file.
1 
4 #ifndef GOSU_IO_HPP
5 #define GOSU_IO_HPP
6 
7 #include <cstddef>
8 #include <algorithm>
9 #include <memory>
10 #include <string>
11 #include <vector>
12 #include <Gosu/Platform.hpp>
13 
14 namespace Gosu
15 {
16  class Resource;
17 
19 #ifdef __BIG_ENDIAN__
21 #else
23 #endif
24 
27  class Reader
28  {
29  const Resource* res;
30  std::size_t pos;
31 
32  public:
33  Reader(const Resource& resource, std::size_t position)
34  : res(&resource), pos(position)
35  {
36  }
37 
38  const Resource& resource() const
39  {
40  return *res;
41  }
42 
43  std::size_t position() const
44  {
45  return pos;
46  }
47 
48  void setPosition(std::size_t value)
49  {
50  // TODO: Check?
51  pos = value;
52  }
53 
54  void seek(std::ptrdiff_t offset)
55  {
56  // TODO: Check?
57  pos += offset;
58  }
59 
60  void read(void* destBuffer, std::size_t length);
61 
63  template<typename T>
64  void readPod(T& t, ByteOrder bo = boDontCare)
65  {
66  read(&t, sizeof t);
67  if (bo == otherByteOrder)
68  {
69  char* begin = reinterpret_cast<char*>(&t);
70  std::reverse(begin, begin + sizeof t);
71  }
72  }
73 
75  template<typename T>
77  {
78  T t;
79  readPod<T>(t, bo);
80  return t;
81  }
82  };
83 
86  class Writer
87  {
88  Resource* res;
89  std::size_t pos;
90 
91  public:
93  : res(&resource), pos(position)
94  {
95  }
96 
97  Resource& resource() const
98  {
99  return *res;
100  }
101 
102  std::size_t position() const
103  {
104  return pos;
105  }
106 
107  void setPosition(std::size_t value)
108  {
109  // TODO: Check?
110  pos = value;
111  }
112 
113  void seek(std::ptrdiff_t offset)
114  {
115  // TODO: Check?
116  pos += offset;
117  }
118 
119  void write(const void* sourceBuffer, std::size_t length);
120 
122  template<typename T>
123  void writePod(const T& t, ByteOrder bo = boDontCare)
124  {
125  if (bo == otherByteOrder)
126  {
127  char buf[sizeof t];
128  const char* begin = reinterpret_cast<const char*>(&t);
129  std::reverse_copy(begin, begin + sizeof t, buf);
130  write(buf, sizeof buf);
131  }
132  else
133  write(&t, sizeof t);
134  }
135  };
136 
142  class Resource
143  {
144  // Non-copyable
145 #if defined(GOSU_CPP11_ENABLED)
146  Resource(const Resource&) = delete;
147  Resource& operator=(const Resource&) = delete;
148  // and non-movable
149  Resource(Resource&&) = delete;
150  Resource& operator=(Resource&&) = delete;
151 #else
152  Resource(const Resource&);
153  Resource& operator=(const Resource&);
154 #endif
155 
156  public:
158  {
159  }
160 
161  virtual ~Resource()
162  {
163  }
164 
168  {
169  return Reader(*this, 0);
170  }
171 
175  {
176  return Writer(*this, size());
177  }
178 
179  virtual std::size_t size() const = 0;
180 
181  virtual void resize(std::size_t newSize) = 0;
182 
183  virtual void read(std::size_t offset, std::size_t length,
184  void* destBuffer) const = 0;
185 
186  virtual void write(std::size_t offset, std::size_t length,
187  const void* sourceBuffer) = 0;
188  };
189 
191  class Buffer : public Resource
192  {
193  std::vector<char> buf;
194 
195  public:
197  {
198  }
199 
200  Buffer(const Buffer& other)
201  : Resource()
202  , buf(other.buf)
203  {
204  }
205 
206  Buffer& operator=(const Buffer& other)
207  {
208  buf = other.buf;
209  return *this;
210  }
211 
212  std::size_t size() const;
213  void resize(std::size_t newSize);
214 
215  void read(std::size_t offset, std::size_t length,
216  void* destBuffer) const;
217 
218  void write(std::size_t offset, std::size_t length,
219  const void* sourceBuffer);
220 
221  const void* data() const
222  {
223  return &buf[0];
224  }
225 
226  void* data()
227  {
228  return &buf[0];
229  }
230  };
231 
232  enum FileMode
233  {
243  };
244 
246  class File : public Resource
247  {
248  struct Impl;
249  const GOSU_UNIQUE_PTR<Impl> pimpl;
250 
251  public:
252  explicit File(const std::wstring& filename, FileMode mode = fmRead);
253  ~File();
254 
255  std::size_t size() const;
256  void resize(std::size_t newSize);
257  void read(std::size_t offset, std::size_t length,
258  void* destBuffer) const;
259  void write(std::size_t offset, std::size_t length,
260  const void* sourceBuffer);
261  };
262 
264  void loadFile(Buffer& buffer, const std::wstring& filename);
266  void saveFile(const Buffer& buffer, const std::wstring& filename);
267 }
268 
269 #endif
std::size_t position() const
Definition: IO.hpp:43
FileMode
Definition: IO.hpp:232
void writePod(const T &t, ByteOrder bo=boDontCare)
Convenience function; equivalent to write(&amp;t, sizeof t).
Definition: IO.hpp:123
void write(std::size_t offset, std::size_t length, const void *sourceBuffer)
void setPosition(std::size_t value)
Definition: IO.hpp:107
virtual ~Resource()
Definition: IO.hpp:161
Writer backWriter()
Convenience: Creates a new Writer that appends data at the end of the resource.
Definition: IO.hpp:174
File(const std::wstring &filename, FileMode mode=fmRead)
virtual std::size_t size() const =0
Piece of memory with the Resource interface.
Definition: IO.hpp:191
T getPod(ByteOrder bo=boDontCare)
Similar to readPod(T&amp;), but returns the read value instead.
Definition: IO.hpp:76
virtual void resize(std::size_t newSize)=0
virtual void write(std::size_t offset, std::size_t length, const void *sourceBuffer)=0
void write(std::size_t offset, std::size_t length, const void *sourceBuffer)
Buffer(const Buffer &other)
Definition: IO.hpp:200
Utility class that points to a specific position in a resource and offers an interface for sequential...
Definition: IO.hpp:27
Writer(Resource &resource, std::size_t position)
Definition: IO.hpp:92
Opens an existing file for reading; throws an exception if the file cannot be found.
Definition: IO.hpp:236
Writes data to a file.
Definition: IO.hpp:239
Buffer & operator=(const Buffer &other)
Definition: IO.hpp:206
Base class for resources.
Definition: IO.hpp:142
void readPod(T &t, ByteOrder bo=boDontCare)
Convenience function; equivalent to read(&amp;t, sizeof t).
Definition: IO.hpp:64
Resource & resource() const
Definition: IO.hpp:97
void setPosition(std::size_t value)
Definition: IO.hpp:48
void resize(std::size_t newSize)
void resize(std::size_t newSize)
Macros and utility functions to facilitate programming on all of Gosu&#39;s supported platforms...
Utility class that points to a specific position in a resource and offers an interface for sequential...
Definition: IO.hpp:86
void * data()
Definition: IO.hpp:226
std::size_t size() const
ByteOrder
Definition: IO.hpp:18
File with the Resource interface.
Definition: IO.hpp:246
void write(const void *sourceBuffer, std::size_t length)
Reader(const Resource &resource, std::size_t position)
Definition: IO.hpp:33
void saveFile(const Buffer &buffer, const std::wstring &filename)
Creates or overwrites a file with the contents of a buffer.
void read(void *destBuffer, std::size_t length)
void read(std::size_t offset, std::size_t length, void *destBuffer) const
const ByteOrder otherByteOrder
Definition: IO.hpp:22
const void * data() const
Definition: IO.hpp:221
std::size_t size() const
void seek(std::ptrdiff_t offset)
Definition: IO.hpp:54
void loadFile(Buffer &buffer, const std::wstring &filename)
Loads a whole file into a buffer.
const ByteOrder nativeByteOrder
Definition: IO.hpp:22
virtual void read(std::size_t offset, std::size_t length, void *destBuffer) const =0
Buffer()
Definition: IO.hpp:196
Reader frontReader() const
Convenience: Creates a new Reader that reads from the start of the resource.
Definition: IO.hpp:167
Opens or creates a file with writing access, but does not clear existing contents.
Definition: IO.hpp:242
std::size_t position() const
Definition: IO.hpp:102
const Resource & resource() const
Definition: IO.hpp:38
void read(std::size_t offset, std::size_t length, void *destBuffer) const
void seek(std::ptrdiff_t offset)
Definition: IO.hpp:113