1
0
Fork 0
forked from suyu/suyu

Merge pull request #753 from lioncash/const

vfs: Minor changes
This commit is contained in:
bunnei 2018-07-21 10:44:08 -07:00 committed by GitHub
commit 3d938b8c60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 21 deletions

View file

@ -42,7 +42,7 @@ bool VfsFile::WriteByte(u8 data, size_t offset) {
return Write(&data, 1, offset) == 1; return Write(&data, 1, offset) == 1;
} }
size_t VfsFile::WriteBytes(std::vector<u8> data, size_t offset) { size_t VfsFile::WriteBytes(const std::vector<u8>& data, size_t offset) {
return Write(data.data(), data.size(), offset); return Write(data.data(), data.size(), offset);
} }

View file

@ -59,8 +59,7 @@ struct VfsFile : NonCopyable {
// Returns the number of bytes (sizeof(T)*number_elements) read successfully. // Returns the number of bytes (sizeof(T)*number_elements) read successfully.
template <typename T> template <typename T>
size_t ReadArray(T* data, size_t number_elements, size_t offset = 0) const { size_t ReadArray(T* data, size_t number_elements, size_t offset = 0) const {
static_assert(std::is_trivially_copyable<T>::value, static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
"Data type must be trivially copyable.");
return Read(reinterpret_cast<u8*>(data), number_elements * sizeof(T), offset); return Read(reinterpret_cast<u8*>(data), number_elements * sizeof(T), offset);
} }
@ -69,8 +68,7 @@ struct VfsFile : NonCopyable {
// Returns the number of bytes read successfully. // Returns the number of bytes read successfully.
template <typename T> template <typename T>
size_t ReadBytes(T* data, size_t size, size_t offset = 0) const { size_t ReadBytes(T* data, size_t size, size_t offset = 0) const {
static_assert(std::is_trivially_copyable<T>::value, static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
"Data type must be trivially copyable.");
return Read(reinterpret_cast<u8*>(data), size, offset); return Read(reinterpret_cast<u8*>(data), size, offset);
} }
@ -78,8 +76,7 @@ struct VfsFile : NonCopyable {
// Returns the number of bytes read successfully (sizeof(T)). // Returns the number of bytes read successfully (sizeof(T)).
template <typename T> template <typename T>
size_t ReadObject(T* data, size_t offset = 0) const { size_t ReadObject(T* data, size_t offset = 0) const {
static_assert(std::is_trivially_copyable<T>::value, static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
"Data type must be trivially copyable.");
return Read(reinterpret_cast<u8*>(data), sizeof(T), offset); return Read(reinterpret_cast<u8*>(data), sizeof(T), offset);
} }
@ -88,33 +85,29 @@ struct VfsFile : NonCopyable {
virtual bool WriteByte(u8 data, size_t offset = 0); virtual bool WriteByte(u8 data, size_t offset = 0);
// Writes a vector of bytes to offset in file and returns the number of bytes successfully // Writes a vector of bytes to offset in file and returns the number of bytes successfully
// written. // written.
virtual size_t WriteBytes(std::vector<u8> data, size_t offset = 0); virtual size_t WriteBytes(const std::vector<u8>& data, size_t offset = 0);
// Writes an array of type T, size number_elements to offset in file. // Writes an array of type T, size number_elements to offset in file.
// Returns the number of bytes (sizeof(T)*number_elements) written successfully. // Returns the number of bytes (sizeof(T)*number_elements) written successfully.
template <typename T> template <typename T>
size_t WriteArray(T* data, size_t number_elements, size_t offset = 0) { size_t WriteArray(const T* data, size_t number_elements, size_t offset = 0) {
static_assert(std::is_trivially_copyable<T>::value, static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
"Data type must be trivially copyable.");
return Write(data, number_elements * sizeof(T), offset); return Write(data, number_elements * sizeof(T), offset);
} }
// Writes size bytes starting at memory location data to offset in file. // Writes size bytes starting at memory location data to offset in file.
// Returns the number of bytes written successfully. // Returns the number of bytes written successfully.
template <typename T> template <typename T>
size_t WriteBytes(T* data, size_t size, size_t offset = 0) { size_t WriteBytes(const T* data, size_t size, size_t offset = 0) {
static_assert(std::is_trivially_copyable<T>::value, static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
"Data type must be trivially copyable."); return Write(reinterpret_cast<const u8*>(data), size, offset);
return Write(reinterpret_cast<u8*>(data), size, offset);
} }
// Writes one object of type T to offset in file. // Writes one object of type T to offset in file.
// Returns the number of bytes written successfully (sizeof(T)). // Returns the number of bytes written successfully (sizeof(T)).
template <typename T> template <typename T>
size_t WriteObject(const T& data, size_t offset = 0) { size_t WriteObject(const T& data, size_t offset = 0) {
static_assert(std::is_trivially_copyable<T>::value, static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
"Data type must be trivially copyable.");
return Write(&data, sizeof(T), offset); return Write(&data, sizeof(T), offset);
} }

View file

@ -2,6 +2,7 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <algorithm>
#include <utility> #include <utility>
#include "core/file_sys/vfs_offset.h" #include "core/file_sys/vfs_offset.h"
@ -75,7 +76,7 @@ bool OffsetVfsFile::WriteByte(u8 data, size_t r_offset) {
return false; return false;
} }
size_t OffsetVfsFile::WriteBytes(std::vector<u8> data, size_t r_offset) { size_t OffsetVfsFile::WriteBytes(const std::vector<u8>& data, size_t r_offset) {
return file->Write(data.data(), TrimToFit(data.size(), r_offset), offset + r_offset); return file->Write(data.data(), TrimToFit(data.size(), r_offset), offset + r_offset);
} }
@ -88,7 +89,7 @@ size_t OffsetVfsFile::GetOffset() const {
} }
size_t OffsetVfsFile::TrimToFit(size_t r_size, size_t r_offset) const { size_t OffsetVfsFile::TrimToFit(size_t r_size, size_t r_offset) const {
return std::max<size_t>(std::min<size_t>(size - r_offset, r_size), 0); return std::clamp(r_size, size_t{0}, size - r_offset);
} }
} // namespace FileSys } // namespace FileSys

View file

@ -28,7 +28,7 @@ struct OffsetVfsFile : public VfsFile {
std::vector<u8> ReadBytes(size_t size, size_t offset) const override; std::vector<u8> ReadBytes(size_t size, size_t offset) const override;
std::vector<u8> ReadAllBytes() const override; std::vector<u8> ReadAllBytes() const override;
bool WriteByte(u8 data, size_t offset) override; bool WriteByte(u8 data, size_t offset) override;
size_t WriteBytes(std::vector<u8> data, size_t offset) override; size_t WriteBytes(const std::vector<u8>& data, size_t offset) override;
bool Rename(const std::string& name) override; bool Rename(const std::string& name) override;