KiCad PCB EDA Suite
Loading...
Searching...
No Matches
raii.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#ifndef RAII_H
25#define RAII_H
26
27#include <wx/window.h>
28
29
30/*
31 * Exception-safe (and 'return' safe) scoped handlers following the "resource allocation is
32 * initialization" pattern.
33 */
34
35
36// Exception-safe method for nulling a pointer
37class NULLER
38{
39public:
40 NULLER( void*& aPtr )
41 : m_what( aPtr )
42 {}
43
45 {
46 m_what = nullptr;
47 }
48
49private:
50 void*& m_what;
51};
52
53
54// Temporarily un-freeze a window, and then re-freeze on destruction
56{
57public:
58 WINDOW_THAWER( wxWindow* aWindow )
59 {
60 m_window = aWindow;
61 m_freezeCount = 0;
62
63 while( m_window->IsFrozen() )
64 {
65 m_window->Thaw();
67 }
68 }
69
71 {
72 while( m_freezeCount > 0 )
73 {
74 m_window->Freeze();
76 }
77 }
78
79protected:
80 wxWindow* m_window;
82};
83
84
87{
88public:
89 WINDOW_DISABLER( wxWindow* aWindow ) :
90 m_win( aWindow )
91 {
92 if( m_win )
93 m_win->Disable();
94 }
95
97 {
98 if( m_win )
99 {
100 m_win->Enable();
101 m_win->Raise(); // let's focus back on the parent window
102 }
103 }
104
106 {
107 if( m_win )
108 m_win->Enable();
109 }
110
112 {
113 if( m_win )
114 m_win->Disable();
115 }
116
117private:
118 wxWindow* m_win;
119};
120
121
122#endif // RAII_H
Definition: raii.h:38
NULLER(void *&aPtr)
Definition: raii.h:40
void *& m_what
Definition: raii.h:50
~NULLER()
Definition: raii.h:44
Temporarily disable a window, and then re-enable on destruction.
Definition: raii.h:87
~WINDOW_DISABLER()
Definition: raii.h:96
void SuspendForTrueModal()
Definition: raii.h:105
wxWindow * m_win
Definition: raii.h:118
void ResumeAfterTrueModal()
Definition: raii.h:111
WINDOW_DISABLER(wxWindow *aWindow)
Definition: raii.h:89
WINDOW_THAWER(wxWindow *aWindow)
Definition: raii.h:58
wxWindow * m_window
Definition: raii.h:80
int m_freezeCount
Definition: raii.h:81
~WINDOW_THAWER()
Definition: raii.h:70