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 on destruction
33class NULLER
34{
35public:
36 NULLER( void*& aPtr ) :
37 m_what( aPtr )
38 {}
39
41 {
42 m_what = nullptr;
43 }
44
45 // No copying or it can lead to double-nulling of the pointer.
46 NULLER( const NULLER& ) = delete;
47 NULLER& operator=( const NULLER& ) = delete;
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 m_window( aWindow )
60 {
61 while( m_window->IsFrozen() )
62 {
63 m_window->Thaw();
64 m_freezeCount++;
65 }
66 }
67
69 {
70 while( m_freezeCount > 0 )
71 {
72 m_window->Freeze();
74 }
75 }
76
77 // No copying or it can lead to double-re-freezing of the window.
78 WINDOW_THAWER( const WINDOW_THAWER& ) = delete;
80
81protected:
82 wxWindow* m_window;
84};
85
86
89{
90public:
91 WINDOW_DISABLER( wxWindow* aWindow ) :
92 m_win( aWindow )
93 {
94 if( m_win )
95 m_win->Disable();
96 }
97
99 {
100 if( m_win )
101 {
102 m_win->Enable();
103 m_win->Raise(); // let's focus back on the parent window
104 }
105 }
106
107 // No copying or it can lead to double-re-enabling of the window.
110
112 {
113 if( m_win )
114 m_win->Enable();
115 }
116
118 {
119 if( m_win )
120 m_win->Disable();
121 }
122
123private:
124 wxWindow* m_win;
125};
126
127
128#endif // RAII_H
NULLER(void *&aPtr)
Definition raii.h:36
NULLER & operator=(const NULLER &)=delete
NULLER(const NULLER &)=delete
void *& m_what
Definition raii.h:50
~NULLER()
Definition raii.h:40
WINDOW_DISABLER(const WINDOW_DISABLER &)=delete
~WINDOW_DISABLER()
Definition raii.h:98
void SuspendForTrueModal()
Definition raii.h:111
wxWindow * m_win
Definition raii.h:124
WINDOW_DISABLER & operator=(const WINDOW_DISABLER &)=delete
void ResumeAfterTrueModal()
Definition raii.h:117
WINDOW_DISABLER(wxWindow *aWindow)
Definition raii.h:91
WINDOW_THAWER(wxWindow *aWindow)
Definition raii.h:58
wxWindow * m_window
Definition raii.h:82
int m_freezeCount
Definition raii.h:83
WINDOW_THAWER(const WINDOW_THAWER &)=delete
~WINDOW_THAWER()
Definition raii.h:68
WINDOW_THAWER & operator=(const WINDOW_THAWER &)=delete