BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdls_tempdirectoryguard.h
Go to the documentation of this file.
1/// @file bdls_tempdirectoryguard.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdls_tempdirectoryguard.h -*-C++-*-
8#ifndef INCLUDED_BDLS_TEMPDIRECTORYGUARD
9#define INCLUDED_BDLS_TEMPDIRECTORYGUARD
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdls_tempdirectoryguard bdls_tempdirectoryguard
15/// @brief Provide a scoped guard that creates a unique temporary directory.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdls
19/// @{
20/// @addtogroup bdls_tempdirectoryguard
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdls_tempdirectoryguard-purpose"> Purpose</a>
25/// * <a href="#bdls_tempdirectoryguard-classes"> Classes </a>
26/// * <a href="#bdls_tempdirectoryguard-description"> Description </a>
27/// * <a href="#bdls_tempdirectoryguard-usage"> Usage </a>
28/// * <a href="#bdls_tempdirectoryguard-example-1-basic-usage"> Example 1: Basic Usage </a>
29///
30/// # Purpose {#bdls_tempdirectoryguard-purpose}
31/// Provide a scoped guard that creates a unique temporary directory.
32///
33/// # Classes {#bdls_tempdirectoryguard-classes}
34///
35/// - bdls::TempDirectoryGuard: guard for creating and removing a temp directory
36///
37/// # Description {#bdls_tempdirectoryguard-description}
38/// This component provides a scoped guard
39/// `bdls::TempDirectoryGuard`, intended primarily for testing, which creates a
40/// uniquely named temporary directory. If possible, this is located in the
41/// system temp directory, otherwise it is created within the current working
42/// directory.
43///
44/// As this class is primarily intended for testing, any failures to build the
45/// directory name or create the temporary directory will be fatal.
46///
47/// ## Usage {#bdls_tempdirectoryguard-usage}
48///
49///
50/// This section illustrates intended use of this component.
51///
52/// ### Example 1: Basic Usage {#bdls_tempdirectoryguard-example-1-basic-usage}
53///
54///
55/// Suppose an algorithm requires writing data to a temporary file on disk
56/// during processing:
57/// @code
58/// /// Do "algorithm" using the specified `testFileName` for intermidiate state
59/// /// storage.
60/// void testAlgorithm(const bsl::string &testFileName);
61/// @endcode
62/// A function looking to use this algorithm can obtain a directory in which to
63/// put this file, guaranteed to not be used by other processes and to be
64/// cleaned up on normal exit, using an instance of `bdls::TempDirectoryGuard`:
65/// @code
66/// void usesTestAlgorithm()
67/// {
68/// bdls::TempDirectoryGuard tempDirGuard("my_algo_");
69///
70/// bsl::string tmpFileName(tempDirGuard.getTempDirName());
71/// bdls::PathUtil::appendRaw(&tmpFileName,"algorithm.scratch");
72///
73/// testAlgorithm(tmpFileName);
74/// }
75/// @endcode
76/// After exiting, the scratch file (named "algorithm.scratch") and the
77/// temporary directory (with an unspecified name starting with "my_algo_"),
78/// possibly in the system temp directory or the current working directory, will
79/// be removed.
80/// @}
81/** @} */
82/** @} */
83
84/** @addtogroup bdl
85 * @{
86 */
87/** @addtogroup bdls
88 * @{
89 */
90/** @addtogroup bdls_tempdirectoryguard
91 * @{
92 */
93
94#include <bslma_allocator.h>
96
98
99#include <bsl_string.h>
100
101
102namespace bdls {
103
104 // ========================
105 // class TempDirectoryGuard
106 // ========================
107
108/// This class implements a scoped temporary directory guard. The guard tries
109/// to create a temporary directory in the system-wide temp directory and falls
110/// back to the current directory.
111///
112/// See @ref bdls_tempdirectoryguard
114
115 // DATA
116 bsl::string d_dirName; // path to the created directory
117 bslma::Allocator *d_allocator_p; // memory allocator (held, not owned)
118
119 // NOT IMPLEMENTED
121 TempDirectoryGuard& operator=(const TempDirectoryGuard&);
122
123 public:
124 // TRAITS
127
128 // CREATORS
129
130 /// Create temporary directory with the specified `prefix` in the
131 /// system-wide temp or current directory. Optionally specify a
132 /// `basicAllocator` used to supply memory. If `basicAllocator` is 0, the
133 /// currently installed default allocator is used.
134 explicit TempDirectoryGuard(const bsl::string& prefix,
135 bslma::Allocator *basicAllocator = 0);
136
137 /// Destroy this object and remove the temporary directory (recursively)
138 /// created at construction.
140
141 // ACCESSORS
142
143 /// Return a `const` reference to the name of the created temporary
144 /// directory.
146};
147
148} // close package namespace
149
150#endif
151
152// ----------------------------------------------------------------------------
153// Copyright 2019 Bloomberg Finance L.P.
154//
155// Licensed under the Apache License, Version 2.0 (the "License");
156// you may not use this file except in compliance with the License.
157// You may obtain a copy of the License at
158//
159// http://www.apache.org/licenses/LICENSE-2.0
160//
161// Unless required by applicable law or agreed to in writing, software
162// distributed under the License is distributed on an "AS IS" BASIS,
163// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
164// See the License for the specific language governing permissions and
165// limitations under the License.
166// ----------------------------- END-OF-FILE ----------------------------------
167
168/** @} */
169/** @} */
170/** @} */
Definition bdls_tempdirectoryguard.h:113
const bsl::string & getTempDirName() const
BSLMF_NESTED_TRAIT_DECLARATION(TempDirectoryGuard, bslma::UsesBslmaAllocator)
TempDirectoryGuard(const bsl::string &prefix, bslma::Allocator *basicAllocator=0)
Definition bslstl_string.h:1281
Definition bslma_allocator.h:457
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdls_fdstreambuf.h:412
Definition bslma_usesbslmaallocator.h:343