]> git.saurik.com Git - wxWidgets.git/blob - include/wx/clntdata.h
Added a const for CW compilation
[wxWidgets.git] / include / wx / clntdata.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/clntdata.h
3 // Purpose: A mixin class for holding a wxClientData or void pointer
4 // Author: Robin Dunn
5 // Modified by:
6 // Created: 9-Oct-2001
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_CLNTDATAH__
13 #define _WX_CLNTDATAH__
14
15 #if defined(__GNUG__) && !defined(__APPLE__)
16 #pragma interface "clntdata.h"
17 #endif
18
19 #include "wx/defs.h"
20 #include "wx/string.h"
21
22 // ----------------------------------------------------------------------------
23
24 // what kind of client data do we have?
25 enum wxClientDataType
26 {
27 wxClientData_None, // we don't know yet because we don't have it at all
28 wxClientData_Object, // our client data is typed and we own it
29 wxClientData_Void // client data is untyped and we don't own it
30 };
31
32 class WXDLLEXPORT wxClientData
33 {
34 public:
35 wxClientData() { }
36 virtual ~wxClientData() { }
37 };
38
39 class WXDLLEXPORT wxStringClientData : public wxClientData
40 {
41 public:
42 wxStringClientData() : m_data() { }
43 wxStringClientData( const wxString &data ) : m_data(data) { }
44 void SetData( const wxString &data ) { m_data = data; }
45 const wxString& GetData() const { return m_data; }
46
47 private:
48 wxString m_data;
49 };
50
51
52
53 // This class is a mixin that provides storage and management of "client
54 // data." The client data stored can either be a pointer to a wxClientData
55 // object in which case it is managed by the container (i.e. it will delete
56 // the data when it's destroyed) or an untyped pointer which won't be deleted
57 // by the container - but not both of them
58 //
59 // NOTE: This functionality is currently duplicated in wxEvtHandler in order
60 // to avoid having more than one vtable in that class heirachy.
61
62 class WXDLLEXPORT wxClientDataContainer
63 {
64 public:
65 wxClientDataContainer();
66 virtual ~wxClientDataContainer();
67
68 void SetClientObject( wxClientData *data ) { DoSetClientObject(data); }
69 wxClientData *GetClientObject() const { return DoGetClientObject(); }
70
71 void SetClientData( void *data ) { DoSetClientData(data); }
72 void *GetClientData() const { return DoGetClientData(); }
73
74 protected:
75 // The user data: either an object which will be deleted by the container
76 // when it's deleted or some raw pointer which we do nothing with - only
77 // one type of data can be used with the given window (i.e. you cannot set
78 // the void data and then associate the container with wxClientData or vice
79 // versa)
80 union
81 {
82 wxClientData *m_clientObject;
83 void *m_clientData;
84 };
85
86 // client data accessors
87 virtual void DoSetClientObject( wxClientData *data );
88 virtual wxClientData *DoGetClientObject() const;
89
90 virtual void DoSetClientData( void *data );
91 virtual void *DoGetClientData() const;
92
93 // what kind of data do we have?
94 wxClientDataType m_clientDataType;
95
96 };
97
98 // not Motif-specific, but currently used only under Motif
99 #ifdef __WXMOTIF__
100
101 #include <wx/vector.h>
102
103 struct WXDLLEXPORT wxClientDataDictionaryPair
104 {
105 wxClientDataDictionaryPair( size_t idx ) : index( idx ), data( 0 ) { }
106
107 size_t index;
108 wxClientData* data;
109 };
110
111 WX_DECLARE_VECTOR(wxClientDataDictionaryPair,wxClientDataDictionaryPairVector);
112
113 // this class is used internally to maintain the association between items
114 // of (some subclasses of) wxControlWithItems and their client data
115 // NOTE: this class does not keep track if it contains
116 // wxClientData or void*, the client must ensure that
117 // it does not contain a mix of the two, and that
118 // DestroyData is called if it contains wxClientData
119 class WXDLLEXPORT wxClientDataDictionary
120 {
121 public:
122 wxClientDataDictionary() {};
123
124 // deletes all the data
125 void DestroyData()
126 {
127 for( size_t i = 0, end = m_vec.size(); i != end; ++i )
128 delete m_vec[i].data;
129 m_vec.clear();
130 }
131
132 // if data for the given index is not present, add it,
133 // if it is present, delete the old data and replace it with
134 // the new one
135 void Set( size_t index, wxClientData* data, bool doDelete )
136 {
137 size_t ptr = Find( index );
138
139 if( !data )
140 {
141 if( ptr == m_vec.size() ) return;
142 if( doDelete )
143 delete m_vec[ptr].data;
144 m_vec.erase( ptr );
145 }
146 else
147 {
148 if( ptr == m_vec.size() )
149 {
150 m_vec.push_back( wxClientDataDictionaryPair( index ) );
151 ptr = m_vec.size() - 1;
152 }
153
154 if( doDelete )
155 delete m_vec[ptr].data;
156 m_vec[ptr].data = data;
157 }
158 }
159
160 // get the data associated with the given index,
161 // return 0 if not found
162 wxClientData* Get( size_t index ) const
163 {
164 size_t it = Find( index );
165 if( it == m_vec.size() ) return 0;
166 return (wxClientData*)m_vec[it].data; // const cast
167 }
168
169 // delete the data associated with the given index
170 // it also decreases by one the indices of all the elements
171 // with an index greater than the given index
172 void Delete( size_t index, bool doDelete )
173 {
174 size_t todel = m_vec.size();
175
176 for( size_t i = 0, end = m_vec.size(); i != end; ++i )
177 {
178 if( m_vec[i].index == index )
179 todel = i;
180 else if( m_vec[i].index > index )
181 --(m_vec[i].index);
182 }
183
184 if( todel != m_vec.size() )
185 {
186 if( doDelete )
187 delete m_vec[todel].data;
188 m_vec.erase( todel );
189 }
190 }
191 private:
192 // returns MyVec.size() if not found
193 size_t Find( size_t index ) const
194 {
195 for( size_t i = 0, end = m_vec.size(); i != end; ++i )
196 {
197 if( m_vec[i].index == index )
198 return i;
199 }
200
201 return m_vec.size();
202 }
203
204 wxClientDataDictionaryPairVector m_vec;
205 };
206
207 #endif // __WXMOTIF__
208
209 // ----------------------------------------------------------------------------
210 #endif
211