diff options
Diffstat (limited to 'src/common/file_util.h')
| -rw-r--r-- | src/common/file_util.h | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/src/common/file_util.h b/src/common/file_util.h index 28697d527..d0987fb57 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h | |||
| @@ -8,6 +8,7 @@ | |||
| 8 | #include <cstdio> | 8 | #include <cstdio> |
| 9 | #include <fstream> | 9 | #include <fstream> |
| 10 | #include <functional> | 10 | #include <functional> |
| 11 | #include <limits> | ||
| 11 | #include <string> | 12 | #include <string> |
| 12 | #include <string_view> | 13 | #include <string_view> |
| 13 | #include <type_traits> | 14 | #include <type_traits> |
| @@ -207,39 +208,42 @@ public: | |||
| 207 | 208 | ||
| 208 | template <typename T> | 209 | template <typename T> |
| 209 | size_t ReadArray(T* data, size_t length) const { | 210 | size_t ReadArray(T* data, size_t length) const { |
| 210 | static_assert(std::is_trivially_copyable<T>(), | 211 | static_assert(std::is_trivially_copyable_v<T>, |
| 211 | "Given array does not consist of trivially copyable objects"); | 212 | "Given array does not consist of trivially copyable objects"); |
| 212 | 213 | ||
| 213 | if (!IsOpen()) | 214 | if (!IsOpen()) { |
| 214 | return -1; | 215 | return std::numeric_limits<size_t>::max(); |
| 216 | } | ||
| 215 | 217 | ||
| 216 | return std::fread(data, sizeof(T), length, m_file); | 218 | return std::fread(data, sizeof(T), length, m_file); |
| 217 | } | 219 | } |
| 218 | 220 | ||
| 219 | template <typename T> | 221 | template <typename T> |
| 220 | size_t WriteArray(const T* data, size_t length) { | 222 | size_t WriteArray(const T* data, size_t length) { |
| 221 | static_assert(std::is_trivially_copyable<T>(), | 223 | static_assert(std::is_trivially_copyable_v<T>, |
| 222 | "Given array does not consist of trivially copyable objects"); | 224 | "Given array does not consist of trivially copyable objects"); |
| 223 | if (!IsOpen()) | 225 | if (!IsOpen()) { |
| 224 | return -1; | 226 | return std::numeric_limits<size_t>::max(); |
| 227 | } | ||
| 228 | |||
| 225 | return std::fwrite(data, sizeof(T), length, m_file); | 229 | return std::fwrite(data, sizeof(T), length, m_file); |
| 226 | } | 230 | } |
| 227 | 231 | ||
| 228 | template <typename T> | 232 | template <typename T> |
| 229 | size_t ReadBytes(T* data, size_t length) const { | 233 | size_t ReadBytes(T* data, size_t length) const { |
| 230 | static_assert(std::is_trivially_copyable<T>(), "T must be trivially copyable"); | 234 | static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable"); |
| 231 | return ReadArray(reinterpret_cast<char*>(data), length); | 235 | return ReadArray(reinterpret_cast<char*>(data), length); |
| 232 | } | 236 | } |
| 233 | 237 | ||
| 234 | template <typename T> | 238 | template <typename T> |
| 235 | size_t WriteBytes(const T* data, size_t length) { | 239 | size_t WriteBytes(const T* data, size_t length) { |
| 236 | static_assert(std::is_trivially_copyable<T>(), "T must be trivially copyable"); | 240 | static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable"); |
| 237 | return WriteArray(reinterpret_cast<const char*>(data), length); | 241 | return WriteArray(reinterpret_cast<const char*>(data), length); |
| 238 | } | 242 | } |
| 239 | 243 | ||
| 240 | template <typename T> | 244 | template <typename T> |
| 241 | size_t WriteObject(const T& object) { | 245 | size_t WriteObject(const T& object) { |
| 242 | static_assert(!std::is_pointer<T>::value, "Given object is a pointer"); | 246 | static_assert(!std::is_pointer_v<T>, "WriteObject arguments must not be a pointer"); |
| 243 | return WriteArray(&object, 1); | 247 | return WriteArray(&object, 1); |
| 244 | } | 248 | } |
| 245 | 249 | ||