#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2023 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_SUPPORT_TEMPORARYFILESCOPE_H #define NUCLEX_SUPPORT_TEMPORARYFILESCOPE_H #include "Nuclex/Support/Config.h" #include // for std::string #include // for std::vector #include // for std::uint8_t namespace Nuclex { namespace Support { // ------------------------------------------------------------------------------------------- // /// Creates a temporary file that is deleted when the scope is destroyed /// /// /// This is very useful for unit tests or if you're dealing with a poorly designed /// library that can only read resources from the file system rather than providing /// an abstract IO interface. /// /// /// Usage is straight-forward: /// /// /// /// void test() { /// TemporaryFileScope tempFile(u8"xyz"); // file with custom prefix /// /// // Write something into the file. Overloads are also provided for /// // a buffer + length pair as well as for std::vector. /// tempFile.SetFileContents(u8"Hello World!"); /// /// // ...do something that requires an actual file.... /// ::poorly_designed_library_load_message(tempFile.GetPath()); /// /// // The file is deleted again upon destruction of the temporary file scope /// } /// /// /// class NUCLEX_SUPPORT_TYPE TemporaryFileScope { /// Reserves and creates a unique temporary file /// Prefix for the temporary filename public: NUCLEX_SUPPORT_API TemporaryFileScope(const std::string &namePrefix = u8"tmp"); /// Deletes the temporary file again public: NUCLEX_SUPPORT_API ~TemporaryFileScope(); /// Returns the full, absolute path to the temporary file /// The absolute path of the temporary file as an UTF-8 string public: NUCLEX_SUPPORT_API const std::string &GetPath() const { return this->path; } /// Replaces the file contents with the specified string /// String whose contents will be written into the file public: NUCLEX_SUPPORT_API void SetFileContents(const std::string &text) { SetFileContents(reinterpret_cast(text.c_str()), text.length()); } /// Replaces the file contents with the data in the specified vector /// Contents that will be written into the file public: NUCLEX_SUPPORT_API void SetFileContents(const std::vector &contents) { SetFileContents(contents.data(), contents.size()); } /// Replaces the file contents with the specified memory block /// Memory block containing the new file contents /// Number of bytes that will be written to the file public: NUCLEX_SUPPORT_API void SetFileContents( const std::uint8_t *contents, std::size_t byteCount ); /// The full path to the temporary file private: std::string path; /// Memory used to store the open file handle private: std::uint8_t privateImplementationData[sizeof(std::uintptr_t)]; }; // ------------------------------------------------------------------------------------------- // }} // namespace Nuclex::Support #endif // NUCLEX_SUPPORT_TEMPORARYFILESCOPE_H