E-MailRelay
gscope.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gscope.h
19///
20
21#ifndef G_SCOPE_H
22#define G_SCOPE_H
23
24#include "gdef.h"
25#include <functional>
26#include <utility>
27
28namespace G
29{
30 class ScopeExit ;
31 class ScopeExitSetFalse ;
32}
33
34//| \class G::ScopeExit
35/// A class that calls an exit function at the end of its scope.
36/// Eg:
37/// \code
38/// {
39/// int fd = open( ... ) ;
40/// ScopeExit closer( [&](){close(fd);} ) ;
41/// int nread = read( fd , ... ) ;
42/// }
43/// \endcode
44///
46{
47public:
48 explicit ScopeExit( std::function<void()> fn ) ;
49 ///< Constructor.
50
51 ~ScopeExit() ;
52 ///< Destructor. Calls the exit function unless
53 ///< release()d.
54
55 void release() noexcept ;
56 ///< Deactivates the exit function.
57
58public:
59 ScopeExit( const ScopeExit & ) = delete ;
60 ScopeExit( ScopeExit && ) = delete ;
61 ScopeExit & operator=( const ScopeExit & ) = delete ;
62 ScopeExit & operator=( ScopeExit && ) = delete ;
63
64private:
65 std::function<void()> m_fn ;
66} ;
67
68//| \class G::ScopeExitSetFalse
69/// A class that sets a boolean variable to false at the
70/// end of its scope.
71/// Eg:
72/// \code
73/// {
74/// ScopeExitSetFalse _( m_busy = true ) ;
75/// ...
76/// }
77/// \endcode
78///
80{
81public:
82 explicit ScopeExitSetFalse( bool & bref ) noexcept ;
83 ///< Constructor.
84
85 ~ScopeExitSetFalse() noexcept ;
86 ///< Destructor, sets the bound value to false.
87
88 void release() noexcept ;
89 ///< Deactivates the exit function.
90
91public:
92 ScopeExitSetFalse( const ScopeExitSetFalse & ) = delete ;
94 ScopeExitSetFalse & operator=( const ScopeExitSetFalse & ) = delete ;
95 ScopeExitSetFalse & operator=( ScopeExitSetFalse && ) = delete ;
96
97private:
98 bool * m_ptr ;
99} ;
100
101inline
102G::ScopeExit::ScopeExit( std::function<void()> fn ) :
103 m_fn(std::move(fn))
104{
105}
106
107inline
109{
110 m_fn = nullptr ;
111}
112
113inline
115{
116 try
117 {
118 if( m_fn )
119 m_fn() ;
120 }
121 catch(...) // dtor
122 {
123 }
124}
125
126inline
128 m_ptr(&bref)
129{
130}
131
132inline
134{
135 m_ptr = nullptr ;
136}
137
138inline
140{
141 if( m_ptr )
142 *m_ptr = false ;
143}
144
145#endif
A class that sets a boolean variable to false at the end of its scope.
Definition: gscope.h:80
~ScopeExitSetFalse() noexcept
Destructor, sets the bound value to false.
Definition: gscope.h:139
void release() noexcept
Deactivates the exit function.
Definition: gscope.h:133
ScopeExitSetFalse(bool &bref) noexcept
Constructor.
Definition: gscope.h:127
A class that calls an exit function at the end of its scope.
Definition: gscope.h:46
ScopeExit(std::function< void()> fn)
Constructor.
Definition: gscope.h:102
void release() noexcept
Deactivates the exit function.
Definition: gscope.h:108
~ScopeExit()
Destructor.
Definition: gscope.h:114
Low-level classes.
Definition: garg.h:30
STL namespace.