Gamedev Framework (gf)  0.9.0
A C++14 framework for 2D games
BinaryFile.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_BINARY_FILE_H
22 #define GF_BINARY_FILE_H
23 
24 #include <cstdio>
25 
26 #include "ArrayRef.h"
27 #include "BufferRef.h"
28 #include "Path.h"
29 #include "Portability.h"
30 
31 namespace gf {
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 inline namespace v1 {
34 #endif
35 
41  class GF_API BinaryFile {
42  public:
46  enum class Mode {
47  Read,
48  Write,
49  Append,
50  };
51 
58  BinaryFile(const Path& filename, Mode mode);
59 
63  BinaryFile(const BinaryFile&) = delete;
64 
68  BinaryFile& operator=(const BinaryFile&) = delete;
69 
73  BinaryFile(BinaryFile&& other);
74 
78  BinaryFile& operator=(BinaryFile&& other);
79 
85  ~BinaryFile();
86 
92  operator bool() const {
93  return m_file != nullptr;
94  }
95 
102  std::size_t write(ArrayRef<uint8_t> buffer) const;
103 
110  std::size_t write(uint8_t byte) const;
111 
118  std::size_t read(BufferRef<uint8_t> buffer) const;
119 
126  std::size_t read(uint8_t& byte) const;
127 
133  bool isEof() const;
134 
135  private:
136  std::FILE *m_file;
137  };
138 
139 #ifndef DOXYGEN_SHOULD_SKIP_THIS
140 }
141 #endif
142 }
143 
144 #endif // GF_BINARY_FILE_H
A reference to a modifiable buffer and its size.
Definition: BufferRef.h:43
The namespace for gf classes.
Definition: Action.h:34
A constant reference to an array and its size.
Definition: ArrayRef.h:42
Mode
Open mode for the file.
Definition: BinaryFile.h:46
A binary file that can be read or written.
Definition: BinaryFile.h:41
boost::filesystem::path Path
A path in the filesystem.
Definition: Path.h:41