BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdls_filedescriptorguard.h
Go to the documentation of this file.
1/// @file bdls_filedescriptorguard.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdls_filedescriptorguard.h -*-C++-*-
8#ifndef INCLUDED_BDLS_FILEDESCRIPTORGUARD
9#define INCLUDED_BDLS_FILEDESCRIPTORGUARD
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdls_filedescriptorguard bdls_filedescriptorguard
15/// @brief Provide a RAII guard class used to close files.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdls
19/// @{
20/// @addtogroup bdls_filedescriptorguard
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdls_filedescriptorguard-purpose"> Purpose</a>
25/// * <a href="#bdls_filedescriptorguard-classes"> Classes </a>
26/// * <a href="#bdls_filedescriptorguard-description"> Description </a>
27/// * <a href="#bdls_filedescriptorguard-usage"> Usage </a>
28/// * <a href="#bdls_filedescriptorguard-example-1-close-a-file-descriptor"> Example 1: Close a File Descriptor </a>
29///
30/// # Purpose {#bdls_filedescriptorguard-purpose}
31/// Provide a RAII guard class used to close files.
32///
33/// # Classes {#bdls_filedescriptorguard-classes}
34///
35/// - bdls::FileDescriptorGuard: RAII guard class used to close files
36///
37/// @see bdls_filesystemutil
38///
39/// # Description {#bdls_filedescriptorguard-description}
40/// This component defines a class, `bdls::FileDescriptorGuard`, an
41/// object of which manages an open file descriptor, and closes it when the
42/// guard goes out of scope and is destroyed. A `release` method is provided,
43/// which will release the descriptor from management by the guard. When a
44/// released guard is destroyed, nothing happens. A `closeAndRelease` method
45/// is also provided, which closes the managed file handle and puts the guard
46/// into a released state.
47///
48/// ## Usage {#bdls_filedescriptorguard-usage}
49///
50///
51/// This section illustrates intended use of this component.
52///
53/// ### Example 1: Close a File Descriptor {#bdls_filedescriptorguard-example-1-close-a-file-descriptor}
54///
55///
56/// Suppose we want to open a file and perform some I/O operations. We use an
57/// object of type `bdls::FileDescriptorGuard` to ensure this handle is closed
58/// after the operations are complete.
59///
60/// First, we create a name for our temporary file name and a few local
61/// variables.
62/// @code
63/// const bsl::string fileName = "essay.txt";
64/// int rc;
65/// @endcode
66/// Then, we open the file:
67/// @code
68/// Util::FileDescriptor fd = Util::open(fileName,
69/// Util::e_CREATE,
70/// Util::e_READ_WRITE);
71/// assert(Util::k_INVALID_FD != fd);
72/// @endcode
73/// Next, we enter a lexical scope and create a guard object to manage `fd`:
74/// @code
75/// {
76/// bdls::FileDescriptorGuard guard(fd);
77/// @endcode
78/// Then, we declare an essay we would like to write to the file:
79/// @code
80/// const char essay[] = {
81/// "If you can't annoy somebody, there is little\n"
82/// "point in writing.\n"
83/// " Kingsley Amis\n"
84/// "\n"
85/// "It takes a lifetime to build a reputation, and\n"
86/// "five minutes to lose it.\n"
87/// " Warren Buffet\n"
88/// "\n"
89/// "Originality is stubborn but not indestructible.\n"
90/// "You can't tell it what to do, and if you try too\n"
91/// "hard to steer it, you either chase it away or\n"
92/// "murder it.\n"
93/// " Salman Khan\n" };
94/// @endcode
95/// Next, we write our essay to the file:
96/// @code
97/// rc = Util::write(fd, essay, sizeof(essay));
98/// assert(sizeof(essay) == rc);
99/// @endcode
100/// Now, `guard` goes out of scope, and its destructor closes the file
101/// descriptor.
102/// @code
103/// }
104/// @endcode
105/// Finally, we observe that further attempts to access `fd` fail because the
106/// descriptor has been closed:
107/// @code
108/// Util::Offset off = Util::seek(fd,
109/// 0,
110/// Util::e_SEEK_FROM_BEGINNING);
111/// assert(-1 == off);
112/// @endcode
113/// @}
114/** @} */
115/** @} */
116
117/** @addtogroup bdl
118 * @{
119 */
120/** @addtogroup bdls
121 * @{
122 */
123/** @addtogroup bdls_filedescriptorguard
124 * @{
125 */
126
127#include <bdlscm_version.h>
128
129#include <bdls_filesystemutil.h>
130
131#include <bsls_assert.h>
132
133
134
135namespace bdls {
136 // ==========================
137 // struct FileDescriptorGuard
138 // ==========================
139
140/// This class implements a guard that conditionally closes an open file
141/// descriptor upon its destruction.
143
144 // DATA
145 FilesystemUtil::FileDescriptor d_descriptor; // handle for the
146 // merged file
147
148 private:
149 // NOT IMPLEMENTED
151 FileDescriptorGuard& operator=(const FileDescriptorGuard&);
152
153 public:
154 // CREATORS
155
156 /// Create a guard object that will manage the specified `descriptor`,
157 /// closing it upon destruction (unless either `realease` or
158 /// `closeAndRelease` has been called). It is permissible for
159 /// `descriptor` to be `FilesystemUtil::k_INVALID_FD`, in which case the
160 /// guard created will not manage anything.
161 explicit
162 FileDescriptorGuard(FilesystemUtil::FileDescriptor descriptor);
163
164 /// If this guard object manages a file, close that file.
166
167 // MANIPULATORS
168
169 /// Close the file managed by this guard and release that file from
170 /// management by this object. The behavior is undefined unless this
171 /// object is managing a file.
173
174 /// Release the file from management by this object (without closing it)
175 /// and return the formerly managed descriptor. The behavior is
176 /// undefined unless this object is managing a file.
177 FilesystemUtil::FileDescriptor release();
178
179 // ACCESSORS
180
181 /// If this guard is managing a file, return the file descriptor
182 /// referring to that file, and return `FilesystemUtil::k_INVALID_FD`
183 /// otherwise.
184 FilesystemUtil::FileDescriptor descriptor() const;
185};
186
187// ============================================================================
188// INLINE DEFINITIONS
189// ============================================================================
190
191// CREATORS
192inline
193FileDescriptorGuard::FileDescriptorGuard(
194 FilesystemUtil::FileDescriptor descriptor)
195: d_descriptor(descriptor)
196{
197}
198
199inline
206
207// MANIPULATORS
208inline
209FilesystemUtil::FileDescriptor FileDescriptorGuard::release()
210{
212
213 FilesystemUtil::FileDescriptor ret = d_descriptor;
215
216 return ret;
217}
218
219// ACCESSORS
220inline
221FilesystemUtil::FileDescriptor FileDescriptorGuard::descriptor()
222 const
223{
224 return d_descriptor;
225}
226
227} // close package namespace
228
229
230#endif
231
232// ----------------------------------------------------------------------------
233// Copyright 2015 Bloomberg Finance L.P.
234//
235// Licensed under the Apache License, Version 2.0 (the "License");
236// you may not use this file except in compliance with the License.
237// You may obtain a copy of the License at
238//
239// http://www.apache.org/licenses/LICENSE-2.0
240//
241// Unless required by applicable law or agreed to in writing, software
242// distributed under the License is distributed on an "AS IS" BASIS,
243// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
244// See the License for the specific language governing permissions and
245// limitations under the License.
246// ----------------------------- END-OF-FILE ----------------------------------
247
248/** @} */
249/** @} */
250/** @} */
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdls_fdstreambuf.h:412
Definition bdls_filedescriptorguard.h:142
FilesystemUtil::FileDescriptor release()
Definition bdls_filedescriptorguard.h:209
FilesystemUtil::FileDescriptor d_descriptor
Definition bdls_filedescriptorguard.h:145
~FileDescriptorGuard()
If this guard object manages a file, close that file.
Definition bdls_filedescriptorguard.h:200
FilesystemUtil::FileDescriptor descriptor() const
Definition bdls_filedescriptorguard.h:221
static const FileDescriptor k_INVALID_FD
Definition bdls_filesystemutil.h:482