You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

scope_success.h 2.2KB

3 jaren geleden
7 jaren geleden
7 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // MIT License
  2. //
  3. // Copyright (c) 2017-2021 offa
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. // SOFTWARE.
  22. #pragma once
  23. #include "detail/scope_guard_base.h"
  24. #include <exception>
  25. namespace sr
  26. {
  27. namespace detail
  28. {
  29. struct scope_success_strategy
  30. {
  31. bool should_execute() const noexcept
  32. {
  33. return std::uncaught_exceptions() <= uncaught_on_creation;
  34. }
  35. int uncaught_on_creation = std::uncaught_exceptions();
  36. };
  37. template<class F>
  38. struct is_noexcept_dtor<F, scope_success_strategy>
  39. : public std::conditional_t<noexcept(std::declval<F>()()), std::true_type, std::false_type>
  40. {
  41. };
  42. }
  43. template<class EF>
  44. class scope_success : public detail::scope_guard_base<EF, detail::scope_success_strategy>
  45. {
  46. using ScopeGuardBase = std::enable_if_t<!std::is_same_v<detail::remove_cvref_t<EF>, scope_success<EF>>,
  47. detail::scope_guard_base<EF, detail::scope_success_strategy>
  48. >;
  49. public:
  50. using ScopeGuardBase::ScopeGuardBase;
  51. };
  52. template<class EF>
  53. scope_success(EF) -> scope_success<EF>;
  54. }