blob: 0ae8e95b9a1bfce955164d01ba7a138e3a0bcb6c [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001// Copyright 2021 The Dawn & Tint Authors
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002//
Austin Engcc2516a2023-10-17 20:57:54 +00003// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
Ryan Harrisondbc13af2022-02-21 15:19:07 +00005//
Austin Engcc2516a2023-10-17 20:57:54 +00006// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
Ryan Harrisondbc13af2022-02-21 15:19:07 +00008//
Austin Engcc2516a2023-10-17 20:57:54 +00009// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// 3. Neither the name of the copyright holder nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Ryan Harrisondbc13af2022-02-21 15:19:07 +000027
dan sinclair22b4dd22023-07-21 00:40:07 +000028#ifndef SRC_TINT_UTILS_FILE_TMPFILE_H_
29#define SRC_TINT_UTILS_FILE_TMPFILE_H_
Ryan Harrisondbc13af2022-02-21 15:19:07 +000030
Ryan Harrisondbc13af2022-02-21 15:19:07 +000031#include <string>
32
dan sinclair22b4dd22023-07-21 00:40:07 +000033#include "src/tint/utils/text/string_stream.h"
dan sinclairdee884c2023-02-28 22:22:58 +000034
dan sinclairbae54e72023-07-28 15:01:54 +000035namespace tint {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000036
37/// TmpFile constructs a temporary file that can be written to, and is
38/// automatically deleted on destruction.
39class TmpFile {
dan sinclair41e4d9a2022-05-01 14:40:55 +000040 public:
41 /// Constructor.
42 /// Creates a new temporary file which can be written to.
43 /// The temporary file will be automatically deleted on destruction.
44 /// @param extension optional file extension to use with the file. The file
45 /// have no extension by default.
46 explicit TmpFile(std::string extension = "");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000047
dan sinclair41e4d9a2022-05-01 14:40:55 +000048 /// Destructor.
49 /// Deletes the temporary file.
50 ~TmpFile();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000051
dan sinclair41e4d9a2022-05-01 14:40:55 +000052 /// @return true if the temporary file was successfully created.
53 operator bool() { return !path_.empty(); }
Ryan Harrisondbc13af2022-02-21 15:19:07 +000054
dan sinclair41e4d9a2022-05-01 14:40:55 +000055 /// @return the path to the temporary file
56 std::string Path() const { return path_; }
Ryan Harrisondbc13af2022-02-21 15:19:07 +000057
dan sinclair41e4d9a2022-05-01 14:40:55 +000058 /// Opens the temporary file and appends |size| bytes from |data| to the end
59 /// of the temporary file. The temporary file is closed again before
60 /// returning, allowing other processes to open the file on operating systems
61 /// that require exclusive ownership of opened files.
62 /// @param data the data to write to the end of the file
63 /// @param size the number of bytes to write from data
64 /// @returns true on success, otherwise false
65 bool Append(const void* data, size_t size) const;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000066
dan sinclair41e4d9a2022-05-01 14:40:55 +000067 /// Appends the argument to the end of the file.
68 /// @param data the data to write to the end of the file
69 /// @return a reference to this TmpFile
70 template <typename T>
71 inline TmpFile& operator<<(T&& data) {
dan sinclairbae54e72023-07-28 15:01:54 +000072 StringStream ss;
dan sinclair41e4d9a2022-05-01 14:40:55 +000073 ss << data;
74 std::string str = ss.str();
75 Append(str.data(), str.size());
76 return *this;
77 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +000078
dan sinclair41e4d9a2022-05-01 14:40:55 +000079 private:
80 TmpFile(const TmpFile&) = delete;
81 TmpFile& operator=(const TmpFile&) = delete;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000082
dan sinclair41e4d9a2022-05-01 14:40:55 +000083 std::string path_;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000084};
85
dan sinclairbae54e72023-07-28 15:01:54 +000086} // namespace tint
Ryan Harrisondbc13af2022-02-21 15:19:07 +000087
dan sinclair22b4dd22023-07-21 00:40:07 +000088#endif // SRC_TINT_UTILS_FILE_TMPFILE_H_