Where Online Learning is simpler!
The C and C++ Include Header Files
/usr/include/c++/13/experimental/scope
$ cat -n /usr/include/c++/13/experimental/scope 1 //
-*- C++ -*- 2 3 // Copyright The GNU Toolchain Authors. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 //
. 24 25 /** @file experimental/scope 26 * This is a TS C++ Library header. 27 * @ingroup libfund-ts 28 */ 29 30 #ifndef _GLIBCXX_EXPERIMENTAL_SCOPE 31 #define _GLIBCXX_EXPERIMENTAL_SCOPE 1 32 33 #pragma GCC system_header 34 35 #include
// experimental is currently omitted 36 37 #if __cplusplus >= 202002L 38 39 #include
40 #include
// uncaught_exceptions 41 #include
42 43 namespace std _GLIBCXX_VISIBILITY(default) 44 { 45 _GLIBCXX_BEGIN_NAMESPACE_VERSION 46 namespace experimental::inline fundamentals_v3 47 { 48 #define __cpp_lib_experimental_scope 201902 49 50 template
51 concept __not_same_as = !same_as<_Tp, _Up>; 52 53 template
54 concept __not_lvalue_ref = !is_lvalue_reference_v<_Tp>; 55 56 template
57 class [[nodiscard]] scope_exit 58 { 59 public: 60 template
61 requires __not_same_as
, scope_exit> 62 && constructible_from<_Ef, _Efp> 63 [[nodiscard]] explicit 64 scope_exit(_Efp&& __f) noexcept(is_nothrow_constructible_v<_Ef, _Efp&>) 65 #ifdef __cpp_exceptions 66 try 67 #endif 68 : _M_exit_function(__f) 69 { } 70 #ifdef __cpp_exceptions 71 catch (...) { __f(); } 72 #endif 73 74 template
75 requires __not_same_as
, scope_exit> 76 && constructible_from<_Ef, _Efp> 77 && __not_lvalue_ref<_Efp> 78 && is_nothrow_constructible_v<_Ef, _Efp> 79 explicit 80 scope_exit(_Efp&& __f) noexcept 81 : _M_exit_function(std::forward<_Efp>(__f)) 82 { } 83 84 scope_exit(scope_exit&& __rhs) noexcept 85 requires is_nothrow_move_constructible_v<_Ef> 86 : _M_exit_function(std::forward<_Ef>(__rhs._M_exit_function)) 87 { __rhs.release(); } 88 89 scope_exit(scope_exit&& __rhs) 90 noexcept(is_nothrow_copy_constructible_v<_Ef>) 91 requires (!is_nothrow_move_constructible_v<_Ef>) 92 && is_copy_constructible_v<_Ef> 93 : _M_exit_function(__rhs._M_exit_function) 94 { __rhs.release(); } 95 96 scope_exit(const scope_exit&) = delete; 97 scope_exit& operator=(const scope_exit&) = delete; 98 scope_exit& operator=(scope_exit&&) = delete; 99 100 ~scope_exit() noexcept 101 { 102 if (_M_execute_on_destruction) 103 _M_exit_function(); 104 } 105 106 void release() noexcept { _M_execute_on_destruction = false; } 107 108 private: 109 [[no_unique_address]] _Ef _M_exit_function; 110 bool _M_execute_on_destruction = true; 111 }; 112 113 template
114 scope_exit(_Ef) -> scope_exit<_Ef>; 115 116 template
117 class [[nodiscard]] scope_fail 118 { 119 public: 120 template
121 requires __not_same_as
, scope_fail> 122 && constructible_from<_Ef, _Efp> 123 explicit 124 scope_fail(_Efp&& __f) noexcept(is_nothrow_constructible_v<_Ef, _Efp&>) 125 #ifdef __cpp_exceptions 126 try 127 #endif 128 : _M_exit_function(__f) 129 { } 130 #ifdef __cpp_exceptions 131 catch (...) { __f(); } 132 #endif 133 134 template
135 requires __not_same_as
, scope_fail> 136 && constructible_from<_Ef, _Efp> 137 && __not_lvalue_ref<_Efp> 138 && is_nothrow_constructible_v<_Ef, _Efp> 139 explicit 140 scope_fail(_Efp&& __f) noexcept 141 : _M_exit_function(std::forward<_Efp>(__f)) 142 { } 143 144 scope_fail(scope_fail&& __rhs) noexcept 145 requires is_nothrow_move_constructible_v<_Ef> 146 : _M_exit_function(std::forward<_Ef>(__rhs._M_exit_function)) 147 { __rhs.release(); } 148 149 scope_fail(scope_fail&& __rhs) 150 noexcept(is_nothrow_copy_constructible_v<_Ef>) 151 requires (!is_nothrow_move_constructible_v<_Ef>) 152 && is_copy_constructible_v<_Ef> 153 : _M_exit_function(__rhs._M_exit_function) 154 { __rhs.release(); } 155 156 scope_fail(const scope_fail&) = delete; 157 scope_fail& operator=(const scope_fail&) = delete; 158 scope_fail& operator=(scope_fail&&) = delete; 159 160 ~scope_fail() noexcept 161 { 162 if (std::uncaught_exceptions() > _M_uncaught_init) 163 _M_exit_function(); 164 } 165 166 void release() noexcept { _M_uncaught_init = __INT_MAX__; } 167 168 private: 169 [[no_unique_address]] _Ef _M_exit_function; 170 int _M_uncaught_init = std::uncaught_exceptions(); 171 }; 172 173 template
174 scope_fail(_Ef) -> scope_fail<_Ef>; 175 176 template
177 class [[nodiscard]] scope_success 178 { 179 public: 180 template
181 requires __not_same_as
, scope_success> 182 && constructible_from<_Ef, _Efp> 183 explicit 184 scope_success(_Efp&& __f) noexcept(is_nothrow_constructible_v<_Ef, _Efp&>) 185 : _M_exit_function(__f) 186 { } 187 188 template
189 requires __not_same_as
, scope_success> 190 && constructible_from<_Ef, _Efp> 191 && __not_lvalue_ref<_Efp> 192 && is_nothrow_constructible_v<_Ef, _Efp> 193 explicit 194 scope_success(_Efp&& __f) noexcept 195 : _M_exit_function(std::forward<_Efp>(__f)) 196 { } 197 198 scope_success(scope_success&& __rhs) noexcept 199 requires is_nothrow_move_constructible_v<_Ef> 200 : _M_exit_function(std::forward<_Ef>(__rhs._M_exit_function)) 201 { __rhs.release(); } 202 203 scope_success(scope_success&& __rhs) 204 noexcept(is_nothrow_copy_constructible_v<_Ef>) 205 requires (!is_nothrow_move_constructible_v<_Ef>) 206 && is_copy_constructible_v<_Ef> 207 : _M_exit_function(__rhs._M_exit_function) 208 { __rhs.release(); } 209 210 scope_success(const scope_success&) = delete; 211 scope_success& operator=(const scope_success&) = delete; 212 scope_success& operator=(scope_success&&) = delete; 213 214 ~scope_success() noexcept(noexcept(this->_M_exit_function())) 215 { 216 if (std::uncaught_exceptions() <= _M_uncaught_init) 217 _M_exit_function(); 218 } 219 220 void release() noexcept { _M_uncaught_init = -__INT_MAX__; } 221 222 private: 223 [[no_unique_address]] _Ef _M_exit_function; 224 int _M_uncaught_init = std::uncaught_exceptions(); 225 }; 226 227 template
228 scope_success(_Ef) -> scope_success<_Ef>; 229 230 template
231 class [[nodiscard]] unique_resource 232 { 233 static_assert(!is_rvalue_reference_v<_Resrc>); 234 static_assert(!is_reference_v<_Del>); 235 236 struct _Dummy { constexpr void release() { } }; 237 238 template
239 struct _Wrap 240 { 241 template
242 requires is_constructible_v<_Tp, _Up> 243 _Wrap(_Up&&) 244 noexcept(is_nothrow_constructible_v<_Tp, _Up>); 245 246 template
247 requires is_constructible_v<_Tp, _Up> 248 _Wrap(_Up&& __r, _Del2&& __d) 249 noexcept(is_nothrow_constructible_v<_Tp, _Up>) 250 : _M_t(std::forward<_Up>(__r)) 251 { __d.release(); } 252 253 _Wrap() = default; 254 255 _Wrap(_Wrap&&) = default; 256 257 _Wrap(_Wrap&& __rhs) noexcept(is_nothrow_constructible_v<_Tp, _Tp&>) 258 requires (!is_nothrow_move_constructible_v<_Tp>) 259 : _M_t(__rhs._M_t) 260 { } 261 262 _Wrap& operator=(const _Wrap&) = default; 263 264 _Wrap& operator=(_Wrap&&) = default; 265 266 constexpr _Tp& get() noexcept { return _M_t; } 267 constexpr const _Tp& get() const noexcept { return _M_t; } 268 269 [[no_unique_address]] _Tp _M_t{}; 270 }; 271 272 template
273 struct _Wrap<_Tp&> 274 { 275 template
276 requires is_constructible_v
, _Up> 277 _Wrap(_Up&&) 278 noexcept(is_nothrow_constructible_v
, _Up>); 279 280 template
281 _Wrap(_Up&& __r, _Del2&& __d) 282 noexcept(is_nothrow_constructible_v
, _Up>) 283 : _M_p(__builtin_addressof(static_cast<_Tp&>(__r))) 284 { __d.release(); } 285 286 _Wrap() = delete; 287 288 _Wrap(const _Wrap&) = default; 289 290 _Wrap& operator=(const _Wrap&) = default; 291 292 _Tp& get() noexcept { return *_M_p; } 293 const _Tp& get() const noexcept { return *_M_p; } 294 295 _Tp* _M_p = nullptr; 296 }; 297 298 using _Res1 = _Wrap<_Resrc>; 299 300 template
301 requires is_constructible_v<_Tp, _Up> 302 && (is_nothrow_constructible_v<_Tp, _Up> 303 || is_constructible_v<_Tp, _Up&>) 304 using _Fwd_t 305 = __conditional_t
, _Up, _Up&>; 306 307 template
308 static constexpr _Fwd_t<_Tp, _Up> 309 _S_fwd(_Up& __u) 310 { return static_cast<_Fwd_t<_Tp, _Up>&&>(__u); } 311 312 template
313 static constexpr auto 314 _S_guard(_Del2& __d, _Res2& __r) 315 { 316 if constexpr (is_nothrow_constructible_v<_Tp, _Up>) 317 return _Dummy{}; 318 else 319 return scope_fail{[&] { __d(__r); }}; 320 } 321 322 public: 323 unique_resource() = default; 324 325 template
326 requires requires { 327 typename _Fwd_t<_Res1, _Res2>; 328 typename _Fwd_t<_Del, _Del2>; 329 } 330 unique_resource(_Res2&& __r, _Del2&& __d) 331 noexcept((is_nothrow_constructible_v<_Res1, _Res2> 332 || is_nothrow_constructible_v<_Res1, _Res2&>) 333 && 334 (is_nothrow_constructible_v<_Del, _Del2> 335 || is_nothrow_constructible_v<_Del, _Del2&>)) 336 : _M_res(_S_fwd<_Res1, _Res2>(__r), 337 _S_guard<_Res1, _Res2>(__d, __r)), 338 _M_del(_S_fwd<_Del, _Del2>(__d), 339 _S_guard<_Del, _Del2>(__d, _M_res.get())), 340 _M_exec_on_reset(true) 341 { } 342 343 unique_resource(unique_resource&& __rhs) noexcept 344 requires is_nothrow_move_constructible_v<_Res1> 345 && is_nothrow_move_constructible_v<_Del> 346 : _M_res(std::move(__rhs._M_res)), 347 _M_del(std::move(__rhs._M_del)), 348 _M_exec_on_reset(std::__exchange(__rhs._M_exec_on_reset, false)) 349 { } 350 351 unique_resource(unique_resource&& __rhs) 352 requires is_nothrow_move_constructible_v<_Res1> 353 && (!is_nothrow_move_constructible_v<_Del>) 354 : _M_res(std::move(__rhs._M_res)), 355 _M_del(_S_fwd<_Del, _Del>(__rhs._M_del.get()), 356 scope_fail([&]{ 357 if (__rhs._M_exec_on_reset) 358 { 359 __rhs._M_del.get()(_M_res.get()); 360 __rhs.release(); 361 } 362 })), 363 _M_exec_on_reset(std::__exchange(__rhs._M_exec_on_reset, false)) 364 { } 365 366 unique_resource(unique_resource&& __rhs) 367 requires (!is_nothrow_move_constructible_v<_Res1>) 368 : unique_resource(__rhs._M_res.get(), __rhs._M_del.get(), _Dummy{}) 369 { 370 if (__rhs._M_exec_on_reset) 371 { 372 _M_exec_on_reset = true; 373 __rhs._M_exec_on_reset = false; 374 } 375 } 376 377 // 3.3.3.3, Destructor 378 ~unique_resource() { reset(); } 379 380 // 3.3.3.4, Assignment 381 unique_resource& 382 operator=(unique_resource&& __rhs) 383 noexcept(is_nothrow_move_assignable_v<_Res1> 384 && is_nothrow_move_assignable_v<_Del>) 385 { 386 reset(); 387 if constexpr (is_nothrow_move_assignable_v<_Res1>) 388 { 389 if constexpr (is_nothrow_move_assignable_v<_Del>) 390 { 391 _M_res = std::move(__rhs._M_res); 392 _M_del = std::move(__rhs._M_del); 393 } 394 else 395 { 396 _M_del = __rhs._M_del; 397 _M_res = std::move(__rhs._M_res); 398 } 399 } 400 else 401 { 402 if constexpr (is_nothrow_move_assignable_v<_Del>) 403 { 404 _M_res = __rhs._M_res; 405 _M_del = std::move(__rhs._M_del); 406 } 407 else 408 { 409 _M_res = __rhs._M_res; 410 _M_del = __rhs._M_del; 411 } 412 } 413 _M_exec_on_reset = std::__exchange(__rhs._M_exec_on_reset, false); 414 return *this; 415 } 416 417 // 3.3.3.5, Other member functions 418 void 419 reset() noexcept 420 { 421 if (_M_exec_on_reset) 422 { 423 _M_exec_on_reset = false; 424 _M_del.get()(_M_res.get()); 425 } 426 } 427 428 template
429 void 430 reset(_Res2&& __r) 431 { 432 reset(); 433 if constexpr (is_nothrow_assignable_v<_Res1&, _Res2>) 434 _M_res.get() = std::forward<_Res2>(__r); 435 else 436 _M_res.get() = const_cast
&>(__r); 437 _M_exec_on_reset = true; 438 } 439 440 void 441 release() noexcept 442 { _M_exec_on_reset = false; } 443 444 const _Resrc& 445 get() const noexcept 446 { return _M_res.get(); } 447 448 add_lvalue_reference_t
> 449 operator*() const noexcept 450 requires is_pointer_v<_Resrc> && (!is_void_v
>) 451 { return *get(); } 452 453 _Resrc operator->() const noexcept 454 requires is_pointer_v<_Resrc> 455 { return _M_res.get(); } 456 457 const _Del& 458 get_deleter() const noexcept 459 { return _M_del.get(); } 460 461 private: 462 [[no_unique_address]] _Res1 _M_res{}; 463 [[no_unique_address]] _Wrap<_Del> _M_del{}; 464 bool _M_exec_on_reset = false; 465 466 template
467 friend unique_resource
, decay_t<_Del2>> 468 make_unique_resource_checked(_Res2&&, const _St&, _Del2&&) 469 noexcept(is_nothrow_constructible_v
, _Res2> 470 && is_nothrow_constructible_v
, _Del2>); 471 472 template
473 unique_resource(_Res2&& __r, _Del2&& __d, _Dummy __noop) 474 noexcept(is_nothrow_constructible_v<_Resrc, _Res2> 475 && is_nothrow_constructible_v<_Del, _Del2>) 476 : _M_res(std::forward<_Res2>(__r), __noop), 477 _M_del(std::forward<_Del>(__d), __noop) 478 { } 479 }; 480 481 template
482 unique_resource(_Resrc, _Del) -> unique_resource<_Resrc, _Del>; 483 484 template
> 485 unique_resource
, decay_t<_Del>> 486 make_unique_resource_checked(_Resrc&& __r, const _St& __invalid, _Del&& __d) 487 noexcept(is_nothrow_constructible_v
, _Resrc> 488 && is_nothrow_constructible_v
, _Del>) 489 { 490 if (__r == __invalid) 491 return { std::forward<_Resrc>(__r), std::forward<_Del>(__d), {} }; 492 return { std::forward<_Resrc>(__r), std::forward<_Del>(__d) }; 493 } 494 495 } // namespace experimental::fundamentals_v3 496 _GLIBCXX_END_NAMESPACE_VERSION 497 } // namespace std 498 #endif // C++20 499 #endif // _GLIBCXX_EXPERIMENTAL_SCOPE
Contact us
|
About us
|
Term of use
|
Copyright © 2000-2025 MyWebUniversity.com ™