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, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef RAII_H
21#define RAII_H
22
23#include <wx/window.h>
24
25
26/*
27 * Exception-safe (and 'return' safe) scoped handlers following the "resource allocation is
28 * initialization" pattern.
29 */
30
31
32// Exception-safe method for nulling a pointer
33class NULLER
34{
35public:
36 NULLER( void*& aPtr ) :
37 m_what( aPtr )
38 {}
39
41 {
42 m_what = nullptr;
43 }
44
45private:
46 void*& m_what;
47};
48
49
50// Temporarily un-freeze a window, and then re-freeze on destruction
52{
53public:
54 WINDOW_THAWER( wxWindow* aWindow )
55 {
56 m_window = aWindow;
57 m_freezeCount = 0;
58
59 while( m_window->IsFrozen() )
60 {
61 m_window->Thaw();
63 }
64 }
65
67 {
68 while( m_freezeCount > 0 )
69 {
70 m_window->Freeze();
72 }
73 }
74
75protected:
76 wxWindow* m_window;
78};
79
80
83{
84public:
85 WINDOW_DISABLER( wxWindow* aWindow ) :
86 m_win( aWindow )
87 {
88 if( m_win )
89 m_win->Disable();
90 }
91
93 {
94 if( m_win )
95 {
96 m_win->Enable();
97 m_win->Raise(); // let's focus back on the parent window
98 }
99 }
100
102 {
103 if( m_win )
104 m_win->Enable();
105 }
106
108 {
109 if( m_win )
110 m_win->Disable();
111 }
112
113private:
114 wxWindow* m_win;
115};
116
117
118#endif // RAII_H
NULLER(void *&aPtr)
Definition raii.h:36
void *& m_what
Definition raii.h:46
~NULLER()
Definition raii.h:40
~WINDOW_DISABLER()
Definition raii.h:92
void SuspendForTrueModal()
Definition raii.h:101
wxWindow * m_win
Definition raii.h:114
void ResumeAfterTrueModal()
Definition raii.h:107
WINDOW_DISABLER(wxWindow *aWindow)
Definition raii.h:85
WINDOW_THAWER(wxWindow *aWindow)
Definition raii.h:54
wxWindow * m_window
Definition raii.h:76
int m_freezeCount
Definition raii.h:77
~WINDOW_THAWER()
Definition raii.h:66