| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_IO_READ_FILEDATA_HPP |
| 4 |
#define MPT_IO_READ_FILEDATA_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#if !defined(MPT_CONFIGURATION_IO_READ_FILEDATA_NO_64BIT) |
| 9 |
#include "mpt/base/integer.hpp" |
| 10 |
#endif // !MPT_CONFIGURATION_IO_READ_FILEDATA_NO_64BIT |
| 11 |
#include "mpt/base/memory.hpp" |
| 12 |
#include "mpt/base/namespace.hpp" |
| 13 |
|
| 14 |
#include <algorithm> |
| 15 |
|
| 16 |
#if defined(MPT_CONFIGURATION_IO_READ_FILEDATA_NO_64BIT) |
| 17 |
#include <cstddef> |
| 18 |
#endif // MPT_CONFIGURATION_IO_READ_FILEDATA_NO_64BIT |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace mpt { |
| 23 |
inline namespace MPT_INLINE_NS { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
namespace IO { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class IFileData { |
| 32 |
public: |
| 33 |
#if !defined(MPT_CONFIGURATION_IO_READ_FILEDATA_NO_64BIT) |
| 34 |
using pos_type = uint64; |
| 35 |
#else // MPT_CONFIGURATION_IO_READ_FILEDATA_NO_64BIT |
| 36 |
using pos_type = std::size_t; |
| 37 |
#endif // MPT_CONFIGURATION_IO_READ_FILEDATA_NO_64BIT |
| 38 |
|
| 39 |
protected: |
| 40 |
IFileData() = default; |
| 41 |
|
| 42 |
public: |
| 43 |
IFileData(const IFileData &) = default; |
| 44 |
IFileData & operator=(const IFileData &) = default; |
| 45 |
virtual ~IFileData() = default; |
| 46 |
|
| 47 |
public: |
| 48 |
virtual bool IsValid() const = 0; |
| 49 |
virtual bool HasFastGetLength() const = 0; |
| 50 |
virtual bool HasPinnedView() const = 0; |
| 51 |
virtual const std::byte * GetRawData() const = 0; |
| 52 |
virtual pos_type GetLength() const = 0; |
| 53 |
virtual mpt::byte_span Read(pos_type pos, mpt::byte_span dst) const = 0; |
| 54 |
|
| 55 |
virtual bool CanRead(pos_type pos, pos_type length) const { |
| 56 |
pos_type dataLength = GetLength(); |
| 57 |
if ((pos == dataLength) && (length == 0)) { |
| 58 |
return true; |
| 59 |
} |
| 60 |
if (pos >= dataLength) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
return length <= dataLength - pos; |
| 64 |
} |
| 65 |
|
| 66 |
virtual pos_type GetReadableLength(pos_type pos, pos_type length) const { |
| 67 |
pos_type dataLength = GetLength(); |
| 68 |
if (pos >= dataLength) { |
| 69 |
return 0; |
| 70 |
} |
| 71 |
return std::min(length, dataLength - pos); |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
} // namespace IO |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
} // namespace MPT_INLINE_NS |
| 82 |
} // namespace mpt |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
#endif // MPT_IO_READ_FILEDATA_HPP |