blob: df97ee226c75956737f94ca64c892de5a11c60f5 [file] [log] [blame]
Shawn McCarneyb6f07c92020-09-03 21:49:21 -05001/**
2 * Copyright © 2020 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
18#include <filesystem>
19#include <utility>
20
21namespace phosphor::power::regulators
22{
23
24namespace fs = std::filesystem;
25
26/**
27 * @class TemporaryFile
28 *
29 * A temporary file in the file system.
30 *
31 * The temporary file is created by the constructor. The absolute path to the
32 * file can be obtained using getPath().
33 *
34 * The temporary file can be deleted by calling remove(). Otherwise the file
35 * will be deleted by the destructor.
36 *
37 * TemporaryFile objects cannot be copied, but they can be moved. This enables
38 * them to be stored in containers like std::vector.
39 */
40class TemporaryFile
41{
42 public:
43 // Specify which compiler-generated methods we want
44 TemporaryFile(const TemporaryFile&) = delete;
45 TemporaryFile& operator=(const TemporaryFile&) = delete;
46
47 /**
48 * Constructor.
49 *
50 * Creates a temporary file in the temporary directory (normally /tmp).
51 *
52 * Throws an exception if the file cannot be created.
53 */
54 TemporaryFile();
55
56 /**
57 * Move constructor.
58 *
59 * Transfers ownership of a temporary file.
60 *
61 * @param file TemporaryFile object being moved
62 */
63 TemporaryFile(TemporaryFile&& file) : path{std::move(file.path)}
64 {
65 // Clear path in other object; after move path is in unspecified state
66 file.path.clear();
67 }
68
69 /**
70 * Move assignment operator.
71 *
72 * Deletes the temporary file owned by this object. Then transfers
73 * ownership of the temporary file owned by the other object.
74 *
75 * Throws an exception if an error occurs during the deletion.
76 *
77 * @param file TemporaryFile object being moved
78 */
79 TemporaryFile& operator=(TemporaryFile&& file);
80
81 /**
82 * Destructor.
83 *
84 * Deletes the temporary file if necessary.
85 */
86 ~TemporaryFile()
87 {
88 try
89 {
90 remove();
91 }
92 catch (...)
93 {
94 // Destructors should not throw exceptions
95 }
96 }
97
98 /**
99 * Deletes the temporary file.
100 *
101 * Does nothing if the file has already been deleted.
102 *
103 * Throws an exception if an error occurs during the deletion.
104 */
105 void remove();
106
107 /**
108 * Returns the absolute path to the temporary file.
109 *
110 * Returns an empty path if the file has been deleted.
111 *
112 * @return temporary file path
113 */
114 const fs::path& getPath() const
115 {
116 return path;
117 }
118
119 private:
120 /**
121 * Absolute path to the temporary file.
122 *
123 * Empty when file has been deleted.
124 */
125 fs::path path{};
126};
127
128} // namespace phosphor::power::regulators