Mercurial > hg > gpsynth
comparison third_party/boost/process/handle.hpp @ 0:add35537fdbb tip
Initial import
author | irh <ian.r.hobson@gmail.com> |
---|---|
date | Thu, 25 Aug 2011 11:05:55 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:add35537fdbb |
---|---|
1 // | |
2 // Boost.Process | |
3 // ~~~~~~~~~~~~~ | |
4 // | |
5 // Copyright (c) 2006, 2007 Julio M. Merino Vidal | |
6 // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling | |
7 // Copyright (c) 2009 Boris Schaeling | |
8 // Copyright (c) 2010 Felipe Tanus, Boris Schaeling | |
9 // | |
10 // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
11 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
12 // | |
13 | |
14 /** | |
15 * \file boost/process/handle.hpp | |
16 * | |
17 * Includes the declaration of the handle class. | |
18 */ | |
19 | |
20 #ifndef BOOST_PROCESS_HANDLE_HPP | |
21 #define BOOST_PROCESS_HANDLE_HPP | |
22 | |
23 #include <boost/process/config.hpp> | |
24 | |
25 #if defined(BOOST_POSIX_API) | |
26 # include <unistd.h> | |
27 #elif defined(BOOST_WINDOWS_API) | |
28 # include <windows.h> | |
29 #else | |
30 # error "Unsupported platform." | |
31 #endif | |
32 | |
33 #include <boost/shared_ptr.hpp> | |
34 #include <boost/make_shared.hpp> | |
35 | |
36 namespace boost { | |
37 namespace process { | |
38 | |
39 /** | |
40 * RAII model for handles. | |
41 * | |
42 * The \a handle class is a RAII model for native handles. This class wraps | |
43 * one of such handles grabbing its ownership, and automaticaly closes it | |
44 * upon destruction. It is used to avoid leaking open handles, shall an | |
45 * unexpected execution trace occur. | |
46 */ | |
47 class handle | |
48 { | |
49 public: | |
50 #if defined(BOOST_PROCESS_DOXYGEN) | |
51 /** | |
52 * Opaque name for the native handle type. | |
53 * | |
54 * On POSIX systems \a NativeSystemHandle is an integer type while it is | |
55 * a \a HANDLE on Windows systems. | |
56 */ | |
57 typedef NativeSystemHandle native_type; | |
58 #elif defined(BOOST_POSIX_API) | |
59 typedef int native_type; | |
60 #elif defined(BOOST_WINDOWS_API) | |
61 typedef HANDLE native_type; | |
62 #endif | |
63 | |
64 /** | |
65 * Constructs an invalid handle. | |
66 * | |
67 * \see valid() | |
68 */ | |
69 handle() | |
70 { | |
71 } | |
72 | |
73 /** | |
74 * RAII settings to specify if handle should be automatically closed. | |
75 */ | |
76 enum close_type { do_close, dont_close }; | |
77 | |
78 /** | |
79 * Constructs a handle from a native handle. | |
80 * | |
81 * This constructor creates a new \a handle object that takes | |
82 * ownership of the given \a native handle. If \a close is set to | |
83 * handle::dont_close the \a native handle is not closed upon destruction. | |
84 * The user must not close \a native if it is owned by a \a handle object. | |
85 * Ownership can be reclaimed using release(). | |
86 * | |
87 * \see release() | |
88 */ | |
89 handle(native_type native, close_type close = handle::do_close) | |
90 : impl_(boost::make_shared<impl>(native, close)) | |
91 { | |
92 } | |
93 | |
94 /** | |
95 * Checks whether the handle is valid or not. | |
96 * | |
97 * \return true if the handle is valid; false otherwise. | |
98 */ | |
99 bool valid() const | |
100 { | |
101 return impl_ && impl_->valid(); | |
102 } | |
103 | |
104 /** | |
105 * Closes the handle. | |
106 * | |
107 * \post The handle is invalid. | |
108 * \post The native handle is closed. | |
109 */ | |
110 void close() | |
111 { | |
112 if (impl_) | |
113 impl_->close(); | |
114 } | |
115 | |
116 /** | |
117 * Gets the native handle. | |
118 * | |
119 * The caller can issue any operation on it except closing it. | |
120 * If closing is required, release() shall be used. | |
121 * | |
122 * \return The native handle. | |
123 */ | |
124 native_type native() const | |
125 { | |
126 return impl_ ? impl_->native() : invalid_handle(); | |
127 } | |
128 | |
129 /** | |
130 * Reclaims ownership of the native handle. | |
131 * | |
132 * The caller is responsible of closing the native handle. | |
133 * | |
134 * \post The handle is invalid. | |
135 * \return The native handle. | |
136 */ | |
137 native_type release() | |
138 { | |
139 return impl_ ? impl_->release() : invalid_handle(); | |
140 } | |
141 | |
142 private: | |
143 class impl | |
144 { | |
145 public: | |
146 typedef handle::native_type native_type; | |
147 | |
148 impl(native_type native, close_type close) | |
149 : native_(native), | |
150 close_(close) | |
151 { | |
152 } | |
153 | |
154 ~impl() | |
155 { | |
156 if (valid() && close_ == handle::do_close) | |
157 { | |
158 #if defined(BOOST_POSIX_API) | |
159 ::close(native_); | |
160 #elif defined(BOOST_WINDOWS_API) | |
161 CloseHandle(native_); | |
162 #endif | |
163 } | |
164 } | |
165 | |
166 bool valid() const | |
167 { | |
168 return native_ != handle::invalid_handle(); | |
169 } | |
170 | |
171 void close() | |
172 { | |
173 if (valid()) | |
174 { | |
175 #if defined(BOOST_POSIX_API) | |
176 ::close(native_); | |
177 #elif defined(BOOST_WINDOWS_API) | |
178 CloseHandle(native_); | |
179 #endif | |
180 native_ = handle::invalid_handle(); | |
181 } | |
182 } | |
183 | |
184 native_type native() const | |
185 { | |
186 return native_; | |
187 } | |
188 | |
189 native_type release() | |
190 { | |
191 native_type native = native_; | |
192 native_ = handle::invalid_handle(); | |
193 return native; | |
194 } | |
195 | |
196 private: | |
197 native_type native_; | |
198 close_type close_; | |
199 }; | |
200 | |
201 /** | |
202 * Implementation of handle to store native handle value. | |
203 * | |
204 * A shared pointer is used as handles represent system resources. If a | |
205 * handle is closed and becomes invalid the state of copies of the handle | |
206 * object will be updated as they all share the handle implementation. | |
207 */ | |
208 boost::shared_ptr<impl> impl_; | |
209 | |
210 /** | |
211 * Constant function representing an invalid handle value. | |
212 * | |
213 * Returns the platform-specific handle value that represents an | |
214 * invalid handle. This is a constant function rather than a regular | |
215 * constant because, in the latter case, we cannot define it under | |
216 * Windows due to the value being of a complex type. | |
217 */ | |
218 static const native_type invalid_handle() | |
219 { | |
220 #if defined(BOOST_POSIX_API) | |
221 return -1; | |
222 #elif defined(BOOST_WINDOWS_API) | |
223 return INVALID_HANDLE_VALUE; | |
224 #endif | |
225 } | |
226 }; | |
227 | |
228 } | |
229 } | |
230 | |
231 #endif |