Gosu
IO.hpp
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include <Gosu/Platform.hpp>
7 #include <algorithm>
8 #include <cstddef>
9 #include <memory>
10 #include <string>
11 #include <vector>
12 
13 namespace Gosu
14 {
15  class Resource;
16 
18 #ifdef __BIG_ENDIAN__
20 #else
21  const ByteOrder BO_NATIVE = BO_LITTLE, BO_OTHER = BO_BIG;
22 #endif
23 
26  class Reader
27  {
28  const Resource* res;
29  std::size_t pos;
30 
31  public:
32  Reader(const Resource& resource, std::size_t position)
33  : res(&resource), pos(position)
34  {
35  }
36 
37  const Resource& resource() const
38  {
39  return *res;
40  }
41 
42  std::size_t position() const
43  {
44  return pos;
45  }
46 
47  void set_position(std::size_t value)
48  {
49  // TODO: Check?
50  pos = value;
51  }
52 
53  void seek(std::ptrdiff_t offset)
54  {
55  // TODO: Check?
56  pos += offset;
57  }
58 
59  void read(void* dest_buffer, std::size_t length);
60 
62  template<typename T>
63  void read_pod(T& t, ByteOrder bo = BO_DONT_CARE)
64  {
65  read(&t, sizeof t);
66  if (bo == BO_OTHER) {
67  char* begin = reinterpret_cast<char*>(&t);
68  std::reverse(begin, begin + sizeof t);
69  }
70  }
71 
73  template<typename T>
74  T get_pod(ByteOrder bo = BO_DONT_CARE)
75  {
76  T t;
77  read_pod<T>(t, bo);
78  return t;
79  }
80  };
81 
84  class Writer
85  {
86  Resource* res;
87  std::size_t pos;
88 
89  public:
91  : res(&resource), pos(position)
92  {
93  }
94 
95  Resource& resource() const
96  {
97  return *res;
98  }
99 
100  std::size_t position() const
101  {
102  return pos;
103  }
104 
105  void set_position(std::size_t value)
106  {
107  // TODO: Check?
108  pos = value;
109  }
110 
111  void seek(std::ptrdiff_t offset)
112  {
113  // TODO: Check?
114  pos += offset;
115  }
116 
117  void write(const void* source_buffer, std::size_t length);
118 
120  template<typename T>
121  void write_pod(const T& t, ByteOrder bo = BO_DONT_CARE)
122  {
123  if (bo == BO_OTHER) {
124  char buf[sizeof t];
125  const char* begin = reinterpret_cast<const char*>(&t);
126  std::reverse_copy(begin, begin + sizeof t, buf);
127  write(buf, sizeof buf);
128  }
129  else {
130  write(&t, sizeof t);
131  }
132  }
133  };
134 
140  class Resource
141  {
142  // Non-copyable and non-movable to avoid slicing.
143  Resource(const Resource&) = delete;
144  Resource& operator=(const Resource&) = delete;
145  Resource(Resource&&) = delete;
146  Resource& operator=(Resource&&) = delete;
147 
148  public:
150  {
151  }
152 
153  virtual ~Resource()
154  {
155  }
156 
160  {
161  return Reader(*this, 0);
162  }
163 
167  {
168  return Writer(*this, size());
169  }
170 
171  virtual std::size_t size() const = 0;
172 
173  virtual void resize(std::size_t new_size) = 0;
174 
175  virtual void read(std::size_t offset, std::size_t length, void* dest_buffer) const = 0;
176 
177  virtual void write(std::size_t offset, std::size_t length, const void* source_buffer) = 0;
178  };
179 
181  class Buffer : public Resource
182  {
183  std::vector<char> buf;
184 
185  public:
187  {
188  }
189 
190  Buffer(const Buffer& other)
191  : Resource()
192  , buf(other.buf)
193  {
194  }
195 
196  Buffer& operator=(const Buffer& other)
197  {
198  buf = other.buf;
199  return *this;
200  }
201 
202  std::size_t size() const override;
203 
204  void resize(std::size_t new_size) override;
205 
206  void read(std::size_t offset, std::size_t length, void* dest_buffer) const override;
207 
208  void write(std::size_t offset, std::size_t length, const void* source_buffer) override;
209 
210  const void* data() const
211  {
212  return &buf[0];
213  }
214 
215  void* data()
216  {
217  return &buf[0];
218  }
219  };
220 
221  enum FileMode
222  {
232  };
233 
235  class File : public Resource
236  {
237  struct Impl;
238  const std::unique_ptr<Impl> pimpl;
239 
240  public:
241  explicit File(const std::string& filename, FileMode mode = FM_READ);
242  ~File();
243 
244  std::size_t size() const override;
245 
246  void resize(std::size_t new_size) override;
247 
248  void read(std::size_t offset, std::size_t length, void* dest_buffer) const override;
249 
250  void write(std::size_t offset, std::size_t length, const void* source_buffer) override;
251  };
252 
254  void load_file(Buffer& buffer, const std::string& filename);
256  void save_file(const Buffer& buffer, const std::string& filename);
257 }
FileMode
Definition: IO.hpp:221
Reader front_reader() const
Convenience: Creates a new Reader that reads from the start of the resource.
Definition: IO.hpp:159
Definition: Audio.hpp:12
virtual ~Resource()
Definition: IO.hpp:153
void write_pod(const T &t, ByteOrder bo=BO_DONT_CARE)
Convenience function; equivalent to write(&t, sizeof t).
Definition: IO.hpp:121
T get_pod(ByteOrder bo=BO_DONT_CARE)
Similar to read_pod(T&), but returns the read value instead.
Definition: IO.hpp:74
const void * data() const
Definition: IO.hpp:210
Piece of memory with the Resource interface.
Definition: IO.hpp:181
std::size_t position() const
Definition: IO.hpp:100
void load_file(Buffer &buffer, const std::string &filename)
Loads a whole file into a buffer.
Resource & resource() const
Definition: IO.hpp:95
const ByteOrder BO_OTHER
Definition: IO.hpp:21
Buffer(const Buffer &other)
Definition: IO.hpp:190
Utility class that points to a specific position in a resource and offers an interface for sequential...
Definition: IO.hpp:26
Writer(Resource &resource, std::size_t position)
Definition: IO.hpp:90
Buffer & operator=(const Buffer &other)
Definition: IO.hpp:196
Base class for resources.
Definition: IO.hpp:140
const ByteOrder BO_NATIVE
Definition: IO.hpp:21
Opens an existing file for reading; throws an exception if the file cannot be found.
Definition: IO.hpp:225
std::size_t position() const
Definition: IO.hpp:42
void save_file(const Buffer &buffer, const std::string &filename)
Creates or overwrites a file with the contents of a buffer.
void read_pod(T &t, ByteOrder bo=BO_DONT_CARE)
Convenience function; equivalent to read(&t, sizeof t).
Definition: IO.hpp:63
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:84
void set_position(std::size_t value)
Definition: IO.hpp:105
void * data()
Definition: IO.hpp:215
ByteOrder
Definition: IO.hpp:17
File with the Resource interface.
Definition: IO.hpp:235
Opens or creates a file with writing access, but does not clear existing contents.
Definition: IO.hpp:231
const Resource & resource() const
Definition: IO.hpp:37
Reader(const Resource &resource, std::size_t position)
Definition: IO.hpp:32
void set_position(std::size_t value)
Definition: IO.hpp:47
Writes data to a file.
Definition: IO.hpp:228
void seek(std::ptrdiff_t offset)
Definition: IO.hpp:53
void read(void *dest_buffer, std::size_t length)
Buffer()
Definition: IO.hpp:186
void seek(std::ptrdiff_t offset)
Definition: IO.hpp:111
Writer back_writer()
Convenience: Creates a new Writer that appends data at the end of the resource.
Definition: IO.hpp:166