#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2021 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_PIXELS_STORAGE_VIRTUALFILE_H #define NUCLEX_PIXELS_STORAGE_VIRTUALFILE_H #include "Nuclex/Pixels/Config.h" #include // for std::string #include // for std::unique_ptr #include // for std::uint64_t namespace Nuclex { namespace Pixels { namespace Storage { // ------------------------------------------------------------------------------------------- // /// Allows reading and writing data to an on-disk or streamed file /// /// /// If you want to read data from a source other than a file, this class is your /// means to achieve that. All codecs fully and correctly implement their underlying /// library's custom IO callbacks, so no temporary files are created and all IO /// is translated into the 3 methods exposed in this interface. /// /// /// Custom implementations of a VirtualFile are allowed to throw exceptions from /// all their methods. Such exceptions will resurface from the /// , or /// method (or their respective wrappers in the /// class) and there will be no memory leaks. /// /// class NUCLEX_PIXELS_TYPE VirtualFile { /// Opens a real file stored in the OS' file system for reading /// /// Path of the file that will be opened for reading as UTF-8 string /// /// /// Whether you promise to read from the file sequentially only /// /// The file at the specified path, opened in read-only mode /// /// /// This opens a file using the most direct/efficient method for accessing files /// provided by the current OS. /// /// /// The returned file is *not* thread-safe. This means if /// and /// calls happen from different threads, mixed-up data, spurious exceptions and /// all kinds of wrong behavior will ensue. If you want to access the same file /// from multiple threads, each should call this method to get its own instance. /// /// public: NUCLEX_PIXELS_API static std::unique_ptr OpenRealFileForReading( const std::string &path, bool promiseSequentialAccess = false ); /// Opens a real file stored in the OS' file system for writing /// /// Path of the file that will be opened for writing as UTF-8 string /// /// /// Whether you promise to write to the file sequentially only /// /// The file at the specified path, opened in write-only mode /// /// /// If the file already exists, it will be truncated to 0 bytes. This creates /// a file using the most direct/efficient method for accessing files provided by /// the current OS. /// /// /// The returned file is *not* thread-safe. This means if /// and /// calls happen from different threads, mixed-up data, spurious exceptions and /// all kinds of wrong behavior will ensue. If you want to access the same file /// from multiple threads, each should call this method to get its own instance. /// /// public: NUCLEX_PIXELS_API static std::unique_ptr OpenRealFileForWriting( const std::string &path, bool promiseSequentialAccess = false ); /// Frees all memory used by the instance public: virtual ~VirtualFile() = default; /// Determines the current size of the file in bytes /// The size of the file in bytes public: virtual std::uint64_t GetSize() const = 0; /// Reads data from the file /// Offset in the file at which to begin reading /// Number of bytes that will be read /// Buffer into which the data will be read /// /// Some file system APIs let you specify a larger number of bytes to read /// (i.e. your buffer size) and may read less than that if the end of the /// file is reached. This implementation will throw an exception if you /// attempt to read beyond the end of the file. /// public: virtual void ReadAt( std::uint64_t start, std::size_t byteCount, std::uint8_t *buffer ) const = 0; /// Writes data into the file /// Offset at which writing will begin in the file /// Number of bytes that should be written /// Buffer holding the data that should be written /// /// As a special feature of the WriteAt() method, the start index can be /// equal to the current file length (but not more). This will append data /// at the end of the file, thus increasing the file's size. /// public: virtual void WriteAt( std::uint64_t start, std::size_t byteCount, const std::uint8_t *buffer ) = 0; }; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Pixels::Storage #endif // NUCLEX_PIXELS_STORAGE_VIRTUALFILE_H