E-MailRelay
gscope.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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 void operator=( const ScopeExit & ) = delete ;
62 void 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
88public:
89 ScopeExitSetFalse( const ScopeExitSetFalse & ) = delete ;
91 void operator=( const ScopeExitSetFalse & ) = delete ;
92 void operator=( ScopeExitSetFalse && ) = delete ;
93
94private:
95 bool & m_bref ;
96} ;
97
98inline
99G::ScopeExit::ScopeExit( std::function<void()> fn ) :
100 m_fn(std::move(fn))
101{
102}
103
104inline
106{
107 m_fn = nullptr ;
108}
109
110inline
112{
113 if( m_fn )
114 m_fn() ;
115}
116
117inline
119 m_bref(bref)
120{
121}
122
123inline
125{
126 m_bref = false ;
127}
128
129#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:124
ScopeExitSetFalse(bool &bref) noexcept
Constructor.
Definition: gscope.h:118
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:99
void release() noexcept
Deactivates the exit function.
Definition: gscope.h:105
~ScopeExit()
Destructor.
Definition: gscope.h:111
Low-level classes.
Definition: galign.h:28