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
28 namespace 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///
46 class G::ScopeExit
47{
48 public:
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
59 public:
60 ScopeExit( const ScopeExit & ) = delete ;
61 ScopeExit( ScopeExit && ) = delete ;
62 ScopeExit & operator=( const ScopeExit & ) = delete ;
63 ScopeExit & operator=( ScopeExit && ) = delete ;
64
65 private:
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///
80 template <typename T,T Value>
81 class G::ScopeExitSet
82{
83 public:
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
93 public:
94 ScopeExitSet( const ScopeExitSet & ) = delete ;
95 ScopeExitSet( ScopeExitSet && ) = delete ;
96 ScopeExitSet & operator=( const ScopeExitSet & ) = delete ;
97 ScopeExitSet & operator=( ScopeExitSet && ) = delete ;
98
99 private:
100 T * m_ptr ;
101} ;
102
103 inline
104 G::ScopeExit::ScopeExit( std::function<void()> fn ) :
105 m_fn(std::move(fn))
106{
107}
108
109 inline
110 void G::ScopeExit::release() noexcept
111{
112 m_fn = nullptr ;
113}
114
115 inline
116 G::ScopeExit::~ScopeExit()
117{
118 try
119 {
120 if( m_fn )
121 m_fn() ;
122 }
123 catch(...) // dtor
124 {
125 }
126}
127
128 template <typename T,T Value>
129 G::ScopeExitSet<T,Value>::ScopeExitSet( T & ref ) noexcept :
130 m_ptr(&ref)
131{
132}
133
134 template <typename T,T Value>
135 void G::ScopeExitSet<T,Value>::release() noexcept
136{
137 m_ptr = nullptr ;
138}
139
140 namespace G
141{
142 template <typename T,T Value>
143 ScopeExitSet<T,Value>::~ScopeExitSet() noexcept
144 {
145 if( m_ptr )
146 *m_ptr = Value ;
147 }
148}
149
150#endif
G::ScopeExitSet
A class that sets a simple variable to a particular value at the end of its scope.
Definition: gscope.h:82
G::ScopeExitSet::release
void release() noexcept
Deactivates the exit function.
Definition: gscope.h:135
G::ScopeExitSet::~ScopeExitSet
~ScopeExitSet() noexcept
Destructor, sets the bound value.
Definition: gscope.h:143
G::ScopeExitSet::ScopeExitSet
ScopeExitSet(T &ref) noexcept
Constructor.
Definition: gscope.h:129
G::ScopeExit
A class that calls an exit function at the end of its scope.
Definition: gscope.h:47
G::ScopeExit::ScopeExit
ScopeExit(std::function< void()> fn)
Constructor.
Definition: gscope.h:104
G::ScopeExit::release
void release() noexcept
Deactivates the exit function.
Definition: gscope.h:110
G::ScopeExit::~ScopeExit
~ScopeExit()
Destructor.
Definition: gscope.h:116
G
Low-level classes.
Definition: garg.h:36
std
STL namespace.