E-MailRelay
gscope.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 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 template <typename T,T Value> class ScopeExitSet ;
32 using ScopeExitSetFalse = ScopeExitSet<bool,false> ;
33}
34
35//| \class G::ScopeExit
36/// A class that calls an exit function at the end of its scope.
37/// Eg:
38/// \code
39/// {
40/// int fd = open( ... ) ;
41/// ScopeExit closer( [&](){close(fd);} ) ;
42/// int nread = read( fd , ... ) ;
43/// }
44/// \endcode
45///
47{
48public:
49 explicit ScopeExit( std::function<void()> fn ) ;
50 ///< Constructor.
51
52 ~ScopeExit() ;
53 ///< Destructor. Calls the exit function unless
54 ///< release()d.
55
56 void release() noexcept ;
57 ///< Deactivates the exit function.
58
59public:
60 ScopeExit( const ScopeExit & ) = delete ;
61 ScopeExit( ScopeExit && ) = delete ;
62 ScopeExit & operator=( const ScopeExit & ) = delete ;
63 ScopeExit & operator=( ScopeExit && ) = delete ;
64
65private:
66 std::function<void()> m_fn ;
67} ;
68
69//| \class G::ScopeExitSet
70/// A class that sets a simple variable to a particular value
71/// at the end of its scope.
72/// Eg:
73/// \code
74/// {
75/// ScopeExitSet<bool,false> _( m_busy = true ) ;
76/// ...
77/// }
78/// \endcode
79///
80template <typename T,T Value>
82{
83public:
84 explicit ScopeExitSet( T & ref ) noexcept ;
85 ///< Constructor.
86
87 ~ScopeExitSet() noexcept ;
88 ///< Destructor, sets the bound value.
89
90 void release() noexcept ;
91 ///< Deactivates the exit function.
92
93public:
94 ScopeExitSet( const ScopeExitSet & ) = delete ;
95 ScopeExitSet( ScopeExitSet && ) = delete ;
96 ScopeExitSet & operator=( const ScopeExitSet & ) = delete ;
97 ScopeExitSet & operator=( ScopeExitSet && ) = delete ;
98
99private:
100 T * m_ptr ;
101} ;
102
103inline
104G::ScopeExit::ScopeExit( std::function<void()> fn ) :
105 m_fn(std::move(fn))
106{
107}
108
109inline
111{
112 m_fn = nullptr ;
113}
114
115inline
117{
118 try
119 {
120 if( m_fn )
121 m_fn() ;
122 }
123 catch(...) // dtor
124 {
125 }
126}
127
128template <typename T,T Value>
130 m_ptr(&ref)
131{
132}
133
134template <typename T,T Value>
136{
137 m_ptr = nullptr ;
138}
139
140namespace G
141{
142 template <typename T,T Value>
144 {
145 if( m_ptr )
146 *m_ptr = Value ;
147 }
148}
149
150#endif
A class that sets a simple variable to a particular value at the end of its scope.
Definition: gscope.h:82
void release() noexcept
Deactivates the exit function.
Definition: gscope.h:135
~ScopeExitSet() noexcept
Destructor, sets the bound value.
Definition: gscope.h:143
ScopeExitSet(T &ref) noexcept
Constructor.
Definition: gscope.h:129
A class that calls an exit function at the end of its scope.
Definition: gscope.h:47
ScopeExit(std::function< void()> fn)
Constructor.
Definition: gscope.h:104
void release() noexcept
Deactivates the exit function.
Definition: gscope.h:110
~ScopeExit()
Destructor.
Definition: gscope.h:116
Low-level classes.
Definition: garg.h:36
STL namespace.