diff options
Diffstat (limited to 'src/common/file_util.h')
| -rw-r--r-- | src/common/file_util.h | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/src/common/file_util.h b/src/common/file_util.h index a85121aa6..c6a8694ce 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h | |||
| @@ -7,13 +7,17 @@ | |||
| 7 | #include <array> | 7 | #include <array> |
| 8 | #include <fstream> | 8 | #include <fstream> |
| 9 | #include <functional> | 9 | #include <functional> |
| 10 | #include <cstddef> | ||
| 11 | #include <cstdio> | 10 | #include <cstdio> |
| 12 | #include <string> | 11 | #include <string> |
| 12 | #include <type_traits> | ||
| 13 | #include <vector> | 13 | #include <vector> |
| 14 | 14 | ||
| 15 | #include "common/common_types.h" | 15 | #include "common/common_types.h" |
| 16 | 16 | ||
| 17 | #ifdef _MSC_VER | ||
| 18 | #include "common/string_util.h" | ||
| 19 | #endif | ||
| 20 | |||
| 17 | // User directory indices for GetUserPath | 21 | // User directory indices for GetUserPath |
| 18 | enum { | 22 | enum { |
| 19 | D_USER_IDX, | 23 | D_USER_IDX, |
| @@ -172,7 +176,6 @@ class IOFile : public NonCopyable | |||
| 172 | { | 176 | { |
| 173 | public: | 177 | public: |
| 174 | IOFile(); | 178 | IOFile(); |
| 175 | IOFile(std::FILE* file); | ||
| 176 | IOFile(const std::string& filename, const char openmode[]); | 179 | IOFile(const std::string& filename, const char openmode[]); |
| 177 | 180 | ||
| 178 | ~IOFile(); | 181 | ~IOFile(); |
| @@ -188,6 +191,11 @@ public: | |||
| 188 | template <typename T> | 191 | template <typename T> |
| 189 | size_t ReadArray(T* data, size_t length) | 192 | size_t ReadArray(T* data, size_t length) |
| 190 | { | 193 | { |
| 194 | static_assert(std::is_standard_layout<T>(), "Given array does not consist of standard layout objects"); | ||
| 195 | #if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER) | ||
| 196 | static_assert(std::is_trivially_copyable<T>(), "Given array does not consist of trivially copyable objects"); | ||
| 197 | #endif | ||
| 198 | |||
| 191 | if (!IsOpen()) { | 199 | if (!IsOpen()) { |
| 192 | m_good = false; | 200 | m_good = false; |
| 193 | return -1; | 201 | return -1; |
| @@ -203,9 +211,10 @@ public: | |||
| 203 | template <typename T> | 211 | template <typename T> |
| 204 | size_t WriteArray(const T* data, size_t length) | 212 | size_t WriteArray(const T* data, size_t length) |
| 205 | { | 213 | { |
| 206 | static_assert(std::is_standard_layout<T>::value, "Given array does not consist of standard layout objects"); | 214 | static_assert(std::is_standard_layout<T>(), "Given array does not consist of standard layout objects"); |
| 207 | // TODO: gcc 4.8 does not support is_trivially_copyable, but we really should check for it here. | 215 | #if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER) |
| 208 | //static_assert(std::is_trivially_copyable<T>::value, "Given array does not consist of trivially copyable objects"); | 216 | static_assert(std::is_trivially_copyable<T>(), "Given array does not consist of trivially copyable objects"); |
| 217 | #endif | ||
| 209 | 218 | ||
| 210 | if (!IsOpen()) { | 219 | if (!IsOpen()) { |
| 211 | m_good = false; | 220 | m_good = false; |
| @@ -235,32 +244,24 @@ public: | |||
| 235 | return WriteArray(&object, 1); | 244 | return WriteArray(&object, 1); |
| 236 | } | 245 | } |
| 237 | 246 | ||
| 238 | bool IsOpen() { return nullptr != m_file; } | 247 | bool IsOpen() const { return nullptr != m_file; } |
| 239 | 248 | ||
| 240 | // m_good is set to false when a read, write or other function fails | 249 | // m_good is set to false when a read, write or other function fails |
| 241 | bool IsGood() { return m_good; } | 250 | bool IsGood() const { return m_good; } |
| 242 | operator void*() { return m_good ? m_file : nullptr; } | 251 | explicit operator bool() const { return IsGood(); } |
| 243 | |||
| 244 | std::FILE* ReleaseHandle(); | ||
| 245 | |||
| 246 | std::FILE* GetHandle() { return m_file; } | ||
| 247 | |||
| 248 | void SetHandle(std::FILE* file); | ||
| 249 | 252 | ||
| 250 | bool Seek(s64 off, int origin); | 253 | bool Seek(s64 off, int origin); |
| 251 | u64 Tell(); | 254 | u64 Tell() const; |
| 252 | u64 GetSize(); | 255 | u64 GetSize() const; |
| 253 | bool Resize(u64 size); | 256 | bool Resize(u64 size); |
| 254 | bool Flush(); | 257 | bool Flush(); |
| 255 | 258 | ||
| 256 | // clear error state | 259 | // clear error state |
| 257 | void Clear() { m_good = true; std::clearerr(m_file); } | 260 | void Clear() { m_good = true; std::clearerr(m_file); } |
| 258 | 261 | ||
| 259 | std::FILE* m_file; | ||
| 260 | bool m_good; | ||
| 261 | private: | 262 | private: |
| 262 | IOFile(IOFile&); | 263 | std::FILE* m_file = nullptr; |
| 263 | IOFile& operator=(IOFile& other); | 264 | bool m_good = true; |
| 264 | }; | 265 | }; |
| 265 | 266 | ||
| 266 | } // namespace | 267 | } // namespace |