JKQTPlotter trunk/v5.0.0
an extensive Qt5+Qt6 Plotter framework (including a feature-richt plotter widget, a speed-optimized, but limited variant and a LaTeX equation renderer!), written fully in C/C++ and without external dependencies
Loading...
Searching...
No Matches
jkqtpcodestructuring.h
1/*
2 Copyright (c) 2008-2024 Jan W. Krieger (<jan@jkrieger.de>)
3
4
5
6 This software is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program 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 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20
21
22#ifndef JKQTPCODESTRUCTURING_H_INCLUDED
23#define JKQTPCODESTRUCTURING_H_INCLUDED
24#include <functional>
25
26
27/** \brief C++11 finally construct (executes a callable-object when the edestructor is executed)
28 * \ingroup jkqtptools_codestructuring
29 *
30 * Typical usage:
31 * \code
32 * {
33 * // the instruction 'painter.restore()' will be executed at the end
34 * // of the block, when __finalpaint is destroyed (see (*) below)
35 * JKQTPFinalAct __finalpaint([&painter]() { painter.restore(); });
36 *
37 * // ...
38 * // do something ...
39 * // ...
40 *
41 * } // (*) 'painter.restore()' is executed before the end of this block!
42 * \endcode
43 *
44 * \see JKQTPFinally()
45 */
46template <class F>
48{
49public:
50 explicit JKQTPFinalAct(F f) noexcept
51 : f_(std::move(f)), invoke_(true) {}
52
53 JKQTPFinalAct(JKQTPFinalAct&& other) noexcept
54 : f_(std::move(other.f_)),
55 invoke_(other.invoke_)
56 {
57 other.invoke_ = false;
58 }
59
60 JKQTPFinalAct(const JKQTPFinalAct&) = delete;
62
63 ~JKQTPFinalAct() noexcept
64 {
65 if (invoke_) f_();
66 }
67
68private:
69 F f_;
70 bool invoke_;
71};
72
73/** \brief C++11 finally construct (executes a callable-object at the end of a scope)
74 * \ingroup jkqtptools_codestructuring
75 *
76 * Typical usage:
77 * \code
78 * {
79 * // the instruction 'painter.restore()' will be executed at the end
80 * // of the block, when __finalpaint is destroyed (see (*) below)
81 * auto __finalpaint=JKQTPFinally([&painter]() { painter.restore(); });
82 *
83 * // ...
84 * // do something ...
85 * // ...
86 *
87 * } // (*) 'painter.restore()' is executed before the end of this block!
88 * \endcode
89 *
90 * \see JKQTPFinalAct
91 */
92template <class F>
93inline JKQTPFinalAct<F> JKQTPFinally(const F& f) noexcept
94{
95 return JKQTPFinalAct<F>(f);
96}
97
98/** \brief C++11 finally construct (executes a callable-object at the end of a scope)
99 * \ingroup jkqtptools_codestructuring
100 *
101 * Typical usage:
102 * \code
103 * {
104 * // the instruction 'painter.restore()' will be executed at the end
105 * // of the block, when __finalpaint is destroyed (see (*) below)
106 * auto __finalpaint=JKQTPFinally([&painter]() { painter.restore(); });
107 *
108 * // ...
109 * // do something ...
110 * // ...
111 *
112 * } // (*) 'painter.restore()' is executed before the end of this block!
113 * \endcode
114 *
115 * \see JKQTPFinalAct
116 */
117template <class F>
118inline JKQTPFinalAct<F> JKQTPFinally(F&& f) noexcept
119{
120 return JKQTPFinalAct<F>(std::forward<F>(f));
121}
122
123
124#endif // JKQTPCODESTRUCTURING_H_INCLUDED
C++11 finally construct (executes a callable-object when the edestructor is executed)
Definition jkqtpcodestructuring.h:48
~JKQTPFinalAct() noexcept
Definition jkqtpcodestructuring.h:63
JKQTPFinalAct(JKQTPFinalAct &&other) noexcept
Definition jkqtpcodestructuring.h:53
F f_
Definition jkqtpcodestructuring.h:69
JKQTPFinalAct & operator=(const JKQTPFinalAct &)=delete
bool invoke_
Definition jkqtpcodestructuring.h:70
JKQTPFinalAct(const JKQTPFinalAct &)=delete
JKQTPFinalAct(F f) noexcept
Definition jkqtpcodestructuring.h:50
JKQTPFinalAct< F > JKQTPFinally(const F &f) noexcept
C++11 finally construct (executes a callable-object at the end of a scope)
Definition jkqtpcodestructuring.h:93