forward-scope-exit.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Copyright (C) 2019-2022 Free Software Foundation, Inc.
  2. This file is part of GDB.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef COMMON_FORWARD_SCOPE_EXIT_H
  14. #define COMMON_FORWARD_SCOPE_EXIT_H
  15. #include "gdbsupport/scope-exit.h"
  16. #include <functional>
  17. /* A forward_scope_exit is like scope_exit, but instead of giving it a
  18. callable, you instead specialize it for a given cleanup function,
  19. and the generated class automatically has a constructor with the
  20. same interface as the cleanup function. forward_scope_exit
  21. captures the arguments passed to the ctor, and in turn passes those
  22. as arguments to the wrapped cleanup function, when it is called at
  23. scope exit time, from within the forward_scope_exit dtor. The
  24. forward_scope_exit class can take any number of arguments, and is
  25. cancelable if needed.
  26. This allows usage like this:
  27. void
  28. delete_longjmp_breakpoint (int arg)
  29. {
  30. // Blah, blah, blah...
  31. }
  32. using longjmp_breakpoint_cleanup
  33. = FORWARD_SCOPE_EXIT (delete_longjmp_breakpoint);
  34. This above created a new cleanup class `longjmp_breakpoint_cleanup`
  35. than can then be used like this:
  36. longjmp_breakpoint_cleanup obj (thread);
  37. // Blah, blah, blah...
  38. obj.release (); // Optional cancel if needed.
  39. forward_scope_exit is also handy when you would need to wrap a
  40. scope_exit in a gdb::optional:
  41. gdb::optional<longjmp_breakpoint_cleanup> cleanup;
  42. if (some condition)
  43. cleanup.emplace (thread);
  44. ...
  45. if (cleanup)
  46. cleanup->release ();
  47. since with scope exit, you would have to know the scope_exit's
  48. callable template type when you create the gdb::optional:
  49. gdb:optional<scope_exit<what goes here?>>
  50. The "forward" naming fits both purposes shown above -- the class
  51. "forwards" ctor arguments to the wrapped cleanup function at scope
  52. exit time, and can also be used to "forward declare"
  53. scope_exit-like objects. */
  54. namespace detail
  55. {
  56. /* Function and Signature are passed in the same type, in order to
  57. extract Function's arguments' types in the specialization below.
  58. Those are used to generate the constructor. */
  59. template<typename Function, Function *function, typename Signature>
  60. struct forward_scope_exit;
  61. template<typename Function, Function *function,
  62. typename Res, typename... Args>
  63. class forward_scope_exit<Function, function, Res (Args...)>
  64. : public scope_exit_base<forward_scope_exit<Function,
  65. function,
  66. Res (Args...)>>
  67. {
  68. /* For access to on_exit(). */
  69. friend scope_exit_base<forward_scope_exit<Function,
  70. function,
  71. Res (Args...)>>;
  72. public:
  73. explicit forward_scope_exit (Args ...args)
  74. : m_bind_function (function, args...)
  75. {
  76. /* Nothing. */
  77. }
  78. private:
  79. void on_exit ()
  80. {
  81. m_bind_function ();
  82. }
  83. /* The function and the arguments passed to the ctor, all packed in
  84. a std::bind. */
  85. decltype (std::bind (function, std::declval<Args> ()...))
  86. m_bind_function;
  87. };
  88. } /* namespace detail */
  89. /* This is the "public" entry point. It's a macro to avoid having to
  90. name FUNC more than once. */
  91. #define FORWARD_SCOPE_EXIT(FUNC) \
  92. detail::forward_scope_exit<decltype (FUNC), FUNC, decltype (FUNC)>
  93. #endif /* COMMON_FORWARD_SCOPE_EXIT_H */