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
28 namespace 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///
45 class G::ScopeExit
46{
47 public:
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
58 public:
59 ScopeExit( const ScopeExit & ) = delete ;
60 ScopeExit( ScopeExit && ) = delete ;
61 void operator=( const ScopeExit & ) = delete ;
62 void operator=( ScopeExit && ) = delete ;
63
64 private:
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///
79 class G::ScopeExitSetFalse
80{
81 public:
82 explicit ScopeExitSetFalse( bool & bref ) noexcept ;
83 ///< Constructor.
84
85 ~ScopeExitSetFalse() noexcept ;
86 ///< Destructor, sets the bound value to false.
87
88 public:
89 ScopeExitSetFalse( const ScopeExitSetFalse & ) = delete ;
90 ScopeExitSetFalse( ScopeExitSetFalse && ) = delete ;
91 void operator=( const ScopeExitSetFalse & ) = delete ;
92 void operator=( ScopeExitSetFalse && ) = delete ;
93
94 private:
95 bool & m_bref ;
96} ;
97
98 inline
99 G::ScopeExit::ScopeExit( std::function<void()> fn ) :
100 m_fn(std::move(fn))
101{
102}
103
104 inline
105 void G::ScopeExit::release() noexcept
106{
107 m_fn = nullptr ;
108}
109
110 inline
111 G::ScopeExit::~ScopeExit()
112{
113 if( m_fn )
114 m_fn() ;
115}
116
117 inline
118 G::ScopeExitSetFalse::ScopeExitSetFalse( bool & bref ) noexcept :
119 m_bref(bref)
120{
121}
122
123 inline
124 G::ScopeExitSetFalse::~ScopeExitSetFalse() noexcept
125{
126 m_bref = false ;
127}
128
129#endif
G::ScopeExitSetFalse
A class that sets a boolean variable to false at the end of its scope.
Definition: gscope.h:80
G::ScopeExitSetFalse::~ScopeExitSetFalse
~ScopeExitSetFalse() noexcept
Destructor, sets the bound value to false.
Definition: gscope.h:124
G::ScopeExitSetFalse::ScopeExitSetFalse
ScopeExitSetFalse(bool &bref) noexcept
Constructor.
Definition: gscope.h:118
G::ScopeExit
A class that calls an exit function at the end of its scope.
Definition: gscope.h:46
G::ScopeExit::ScopeExit
ScopeExit(std::function< void()> fn)
Constructor.
Definition: gscope.h:99
G::ScopeExit::release
void release() noexcept
Deactivates the exit function.
Definition: gscope.h:105
G::ScopeExit::~ScopeExit
~ScopeExit()
Destructor.
Definition: gscope.h:111
G
Low-level classes.
Definition: galign.h:28