my xfce4 dotfiles
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.

56 lines
1.7 KiB

3 years ago
  1. // Copyright 2019 Roman Perepelitsa.
  2. //
  3. // This file is part of GitStatus.
  4. //
  5. // GitStatus is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GitStatus is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with GitStatus. If not, see <https://www.gnu.org/licenses/>.
  17. #ifndef ROMKATV_GITSTATUS_SCOPE_GUARD_H_
  18. #define ROMKATV_GITSTATUS_SCOPE_GUARD_H_
  19. #include <utility>
  20. #define ON_SCOPE_EXIT(capture...) \
  21. auto GITSTATUS_INTERNAL_CAT(_gitstatus_scope_guard_, __COUNTER__) = \
  22. ::gitstatus::internal_scope_guard::ScopeGuardGenerator() = [capture]()
  23. #define GITSTATUS_INTERNAL_CAT_I(x, y) x##y
  24. #define GITSTATUS_INTERNAL_CAT(x, y) GITSTATUS_INTERNAL_CAT_I(x, y)
  25. namespace gitstatus {
  26. namespace internal_scope_guard {
  27. void Undefined();
  28. template <class F>
  29. class ScopeGuard {
  30. public:
  31. explicit ScopeGuard(F f) : f_(std::move(f)) {}
  32. ~ScopeGuard() { std::move(f_)(); }
  33. ScopeGuard(ScopeGuard&& other) : f_(std::move(other.f_)) { Undefined(); }
  34. private:
  35. F f_;
  36. };
  37. struct ScopeGuardGenerator {
  38. template <class F>
  39. ScopeGuard<F> operator=(F f) const {
  40. return ScopeGuard<F>(std::move(f));
  41. }
  42. };
  43. } // namespace internal_scope_guard
  44. } // namespace gitstatus
  45. #endif // ROMKATV_GITSTATUS_SCOPE_GUARD_H_