]> git.saurik.com Git - wxWidgets.git/blob - include/wx/windowid.h
added wxWeakRef<T> (slightly modified patch 1860953)
[wxWidgets.git] / include / wx / windowid.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/windowid.h
3 // Purpose: wxWindowID class - a class for managing window ids
4 // Author: Brian Vanderburg II
5 // Created: 2007-09-21
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 Brian Vanderburg II
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_WINDOWID_H_
12 #define _WX_WINDOWID_H_
13
14 // NB: do not include defs.h as we are included from it
15
16 typedef int wxWindowID;
17
18 // ----------------------------------------------------------------------------
19 // wxWindowIDRef: reference counted id value
20 // ----------------------------------------------------------------------------
21
22 // A wxWindowIDRef object wraps an id value and marks it as (un)used as
23 // necessary. All ids returned from wxWindow::NewControlId() should be assigned
24 // to an instance of this class to ensure that the id is marked as being in
25 // use.
26 //
27 // This class is always defined but it is trivial if wxUSE_AUTOID_MANAGEMENT is
28 // off.
29 class WXDLLIMPEXP_CORE wxWindowIDRef
30 {
31 public:
32 // default ctor
33 wxWindowIDRef()
34 {
35 m_id = wxID_NONE;
36 }
37
38 // ctor taking id values
39 wxWindowIDRef(int id)
40 {
41 Init(id);
42 }
43
44 wxWindowIDRef(long id)
45 {
46 Init(id);
47 }
48
49 wxWindowIDRef(const wxWindowIDRef& id)
50 {
51 Init(id.m_id);
52 }
53
54 // dtor
55 ~wxWindowIDRef()
56 {
57 Assign(wxID_NONE);
58 }
59
60 // assignment
61 wxWindowIDRef& operator=(int id)
62 {
63 Assign(id);
64 return *this;
65 }
66
67 wxWindowIDRef& operator=(long id)
68 {
69 Assign(id);
70 return *this;
71 }
72
73 wxWindowIDRef& operator=(const wxWindowIDRef& id)
74 {
75 Assign(id.m_id);
76 return *this;
77 }
78
79 // access to the stored id value
80 wxWindowID GetValue() const
81 {
82 return m_id;
83 }
84
85 operator wxWindowID() const
86 {
87 return m_id;
88 }
89
90 private:
91 #if wxUSE_AUTOID_MANAGEMENT
92 // common part of all ctors: call Assign() for our new id
93 void Init(wxWindowID id)
94 {
95 // m_id must be initialized before calling Assign()
96 m_id = wxID_NONE;
97 Assign(id);
98 }
99
100 // increase reference count of id, decrease the one of m_id
101 void Assign(wxWindowID id);
102 #else // !wxUSE_AUTOID_MANAGEMENT
103 // trivial stubs for the functions above
104 void Init(wxWindowID id)
105 {
106 m_id = id;
107 }
108
109 void Assign(wxWindowID id)
110 {
111 m_id = id;
112 }
113 #endif // wxUSE_AUTOID_MANAGEMENT/!wxUSE_AUTOID_MANAGEMENT
114
115
116 wxWindowID m_id;
117 };
118
119 // comparison operators
120 inline bool operator==(const wxWindowIDRef& lhs, const wxWindowIDRef& rhs)
121 {
122 return lhs.GetValue() == rhs.GetValue();
123 }
124
125 inline bool operator==(const wxWindowIDRef& lhs, int rhs)
126 {
127 return lhs.GetValue() == rhs;
128 }
129
130 inline bool operator==(const wxWindowIDRef& lhs, long rhs)
131 {
132 return lhs.GetValue() == rhs;
133 }
134
135 inline bool operator==(int lhs, const wxWindowIDRef& rhs)
136 {
137 return rhs == lhs;
138 }
139
140 inline bool operator==(long lhs, const wxWindowIDRef& rhs)
141 {
142 return rhs == lhs;
143 }
144
145 inline bool operator!=(const wxWindowIDRef& lhs, const wxWindowIDRef& rhs)
146 {
147 return !(lhs == rhs);
148 }
149
150 inline bool operator!=(const wxWindowIDRef& lhs, int rhs)
151 {
152 return !(lhs == rhs);
153 }
154
155 inline bool operator!=(const wxWindowIDRef& lhs, long rhs)
156 {
157 return !(lhs == rhs);
158 }
159
160 inline bool operator!=(int lhs, const wxWindowIDRef& rhs)
161 {
162 return !(lhs == rhs);
163 }
164
165 inline bool operator!=(long lhs, const wxWindowIDRef& rhs)
166 {
167 return !(lhs == rhs);
168 }
169
170 // ----------------------------------------------------------------------------
171 // wxIdManager
172 // ----------------------------------------------------------------------------
173
174 class WXDLLIMPEXP_CORE wxIdManager
175 {
176 public:
177 // This returns an id value and not an wxWindowIDRef. The returned value
178 // should be assigned a.s.a.p to a wxWindowIDRef. The IDs are marked as
179 // reserved so that another call to ReserveId before assigning the id to a
180 // wxWindowIDRef will not use the same ID
181 static wxWindowID ReserveId(int count = 1);
182
183 // This will release an unused reserved ID. This should only be called
184 // if the ID returned by ReserveId was NOT assigned to a wxWindowIDRef
185 // for some purpose, maybe an early return from a function
186 static void UnreserveId(wxWindowID id, int count = 1);
187 };
188
189 #endif // _WX_WINDOWID_H_