Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
TemporaryFile.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_IO_FileSystem_TemporaryFile_h_
5#define _Stroika_Foundation_IO_FileSystem_TemporaryFile_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include "Stroika/Foundation/IO/FileSystem/Common.h"
10
11/**
12 * \file
13 *
14 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
15 */
16
18
19 /**
20 * Creates an empty file with a random/unique name (based on baseName) in inFolder (or if not specified, in the WellKnownLocations::GetTemporary () directory) and returns that filename.
21 * These files are NOT automatically cleaned up by Stroika (for that see ScopedTmpFile).
22 *
23 * \note caller must assure 'inFolder' exists and is writeable'
24 *
25 * require root_path is empty - just filename and possibly extention. If extension missing, maybe added automatically
26 *
27 * \note this actually creates (an empty) file
28 */
29 filesystem::path CreateTmpFile (const String& baseName);
30 filesystem::path CreateTmpFile (const String& baseName, const filesystem::path& inFolder);
31
32 /**
33 * Create the 'basenamed' temporary directory (no worries about name conflicts) from the argument filenameBase.
34 *
35 * Directory and its contents destroyed on destruction (exceptions caught internally and DbgTraced, but otherwise ignored).
36 *
37 * note references AppTmpFileManager::sThe
38 */
40 public:
41 ScopedTmpDir (const String& fileNameBase);
42 ScopedTmpDir (const ScopedTmpDir&) = delete;
44 ScopedTmpDir& operator= (const ScopedTmpDir&) = delete;
45
46 public:
47 operator filesystem::path () const;
48
49 private:
50 filesystem::path fTmpDir_;
51 };
52
53 /**
54 * Create the 'basenamed' temporary file (no worries about name conflicts) from the argument filenameBase (including file suffix).
55 *
56 * The file is removed on destruction (exceptions caught internally and DbgTraced, but otherwise ignored).
57 *
58 * note references AppTmpFileManager::sThe
59 */
61 public:
62 ScopedTmpFile (const String& fileBaseName);
63 ScopedTmpFile (const ScopedTmpFile&) = delete;
65 ScopedTmpFile& operator= (const ScopedTmpFile&) = delete;
66
67 public:
68 operator filesystem::path () const;
69
70 private:
71 filesystem::path fTmpFile_;
72 };
73
74 /**
75 * Generally not used directly - prefer using ScopedTmpDir or ScopedTmpFile.
76 *
77 * If used directly, you can make your own, or use AppTmpFileManager::sThe.
78 *
79 * AppTmpFileManager::sThe CAN be overwritten, but best practice is to do so early in main (main thread)
80 * before any threads have been created (which might create a race).
81 */
83 public:
84 /**
85 */
86 struct Options {
87 optional<filesystem::path> fRelativePathInsideTmpDir; // defaults to EXE name (less extensions)
88 };
89
90 public:
91 /**
92 * In typical use, this won't be directly constructed - just use the default sThe
93 */
95 AppTmpFileManager (const Options& options = {});
97
98 public:
99 /**
100 * This DESTROYS any contained files if they existed in 'this' before assignment. If that destruction fails, so will this
101 * assignment.
102 *
103 * This maybe used as in main () {
104 * AppTmpFileManager::sThe = AppTmpFileManager{AppTmpFileManager::Options{... custom options}}
105 */
107
108 public:
109 /**
110 * Note - this global variable is used by ScopedTmpDir, and ScopedTmpFile.
111 * This can be overwritten (assigned to), but following C++ thread safety rules, so best to do in the main thread before any
112 * thread could have begin accessing the object.
113 */
115
116 public:
117 nonvirtual filesystem::path GetRootTmpDir () const;
118
119 public:
120 /**
121 * require root_path is empty - just filename and possibly extention. If extension missing, maybe added automatically
122 * \note this actually creates (an empty) file
123 */
124 nonvirtual filesystem::path GetTmpFile (const String& fileBaseName);
125
126 public:
127 nonvirtual filesystem::path GetTmpDir (const String& dirNameBase);
128
129 private:
130 filesystem::path fTmpDir_;
131 };
133
134}
135
136/*
137 ********************************************************************************
138 ***************************** Implementation Details ***************************
139 ********************************************************************************
140 */
141#include "TemporaryFile.inl"
142
143#endif /*_Stroika_Foundation_IO_FileSystem_TemporaryFile_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
AppTmpFileManager & operator=(AppTmpFileManager &&rhs) noexcept
nonvirtual filesystem::path GetTmpFile(const String &fileBaseName)
AppTmpFileManager(const AppTmpFileManager &)=delete
filesystem::path CreateTmpFile(const String &baseName)