]>
git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/kbList.h
1 /*-*- c++ -*-********************************************************
2 * kbList.h : a double linked list *
4 * (C) 1998 by Karsten Ballüder (Ballueder@usa.net) *
8 * Revision 1.1 1998/06/29 12:44:36 KB
9 * Added my wxWindows based layout engine to the repository.
10 * It arranges text and graphics for display on a wxDC.
11 * This code is licensed under the LGPL.
13 * Revision 1.6 1998/06/27 20:06:10 KB
14 * Added my layout code.
16 *******************************************************************/
22 # pragma interface "kbList.h"
29 /**@name Double linked list implementation. */
32 /** kbListNode is a class used by kbList. It represents a single
33 element in the list. It is not intended for general use outside
38 /// pointer to next node or NULL
39 struct kbListNode
*next
;
40 /// pointer to previous node or NULL
41 struct kbListNode
*prev
;
42 /// pointer to the actual data
44 /** Constructor - it automatically links the node into the list, if
45 the iprev, inext parameters are given.
46 @param ielement pointer to the data for this node (i.e. the data itself)
47 @param iprev if not NULL, use this as previous element in list
48 @param inext if not NULL, use this as next element in list
50 kbListNode( void *ielement
,
51 kbListNode
*iprev
= NULL
,
52 kbListNode
*inext
= NULL
);
57 /** The main list class, handling void pointers as data.
63 /// An iterator class for kbList, just like for the STL classes.
67 /// the node to which this iterator points
72 @param n if not NULL, the node to which to point
74 iterator(kbListNode
*n
= NULL
);
75 /** Dereference operator.
76 @return the data pointer of the node belonging to this
81 /** Increment operator - prefix, goes to next node in list.
84 iterator
& operator++();
86 /** Decrement operator - prefix, goes to previous node in list.
89 iterator
& operator--();
91 /** Increment operator - prefix, goes to next node in list.
94 iterator
& operator++(int); //postfix
96 /** Decrement operator - prefix, goes to previous node in list.
99 iterator
& operator--(int); //postfix
101 /** Comparison operator.
102 @return true if not equal.
104 bool operator !=(iterator
const &) const;
106 /* Comparison operator.
107 @return true if equal
109 bool operator ==(iterator
const &) const;
111 /** Returns a pointer to the node associated with this iterator.
112 This function is not for general use and should be
113 protected. However, if protected, it cannot be called from
114 derived classes' iterators. (Is this a bug in gcc/egcs?)
115 @return the node pointer
117 inline kbListNode
* Node(void) const
122 @param ownsEntriesFlag if true, the list owns the entries and
123 will issue a delete on each of them when deleting them. If
124 false, the entries themselves will not get deleted. Do not use
125 this with array types!
127 kbList(bool ownsEntriesFlag
= true);
130 If entries are owned, they will all get deleted from here.
134 /** Tell list whether it owns objects. If owned, they can be
135 deleted by list. See the constructor for more details.
136 @param ownsflag if true, list will own entries
138 void ownsObjects(bool ownsflag
= true)
139 { ownsEntries
= ownsflag
; }
141 /** Query whether list owns entries.
142 @return true if list owns entries
144 bool ownsObjects(void)
145 { return ownsEntries
; }
147 /** Add an entry at the end of the list.
148 @param element pointer to data
150 void push_back(void *element
);
152 /** Add an entry at the head of the list.
153 @param element pointer to data
155 void push_front(void *element
);
157 /** Get element from end of the list and delete it.
158 NOTE: In this case the element's data will not get deleted by
159 the list. It is the responsibility of the caller to free it.
160 @return the element data
162 void *pop_back(void);
164 /** Get element from head of the list and delete it.
165 NOTE: In this case the element's data will not get deleted by
166 the list. It is the responsibility of the caller to free it.
167 @return the element data
169 void *pop_front(void);
171 /** Insert an element into the list.
172 @param i an iterator pointing to the element, before which the new one should be inserted
173 @param element the element data
175 void insert(iterator
& i
, void *element
);
177 /** Erase an element, move iterator to following element.
178 @param i iterator pointing to the element to be deleted
180 void erase(iterator
& i
);
183 @return iterator pointing to head of list
185 iterator
begin(void) const;
188 @return iterator pointing after the end of the list. This is an
189 invalid iterator which cannot be dereferenced or decremented. It is
190 only of use in comparisons. NOTE: this is different from STL!
193 iterator
end(void) const;
195 /* Get last element in list.
196 @return iterator pointing to the last element in the list.
199 iterator
tail(void) const;
201 /* Get the number of elements in the list.
202 @return number of elements in the list
204 unsigned size(void) const;
206 /* Query whether list is empty.
207 @return true if list is empty
209 bool empty(void) const
210 { return first
== NULL
; }
213 /// if true, list owns entries
215 /// pointer to first element in list
217 /// pointer to last element in list
220 /// forbid copy construction
221 kbList(kbList
const &foo
);
222 /// forbid assignments
223 kbList
& operator=(const kbList
& foo
);
226 /// just for backward compatibility, will be removed soon
227 typedef kbList::iterator kbListIterator
;
228 /// cast an iterator to a pointer, compatibility only to be removed
229 #define kbListICast(type, iterator) ((type *)*iterator)
230 /// cast an iterator to a const pointer, compatibility only to be removed
231 #define kbListIcCast(type, iterator) ((type const *)*iterator)
233 /** Macro to define a kbList with a given name, having elements of
234 pointer to the given type. I.e. KBLIST_DEFINE(Int,int) would
235 create a kbListInt type holding int pointers.
237 #define KBLIST_DEFINE(name,type) \
238 class name : public kbList \
241 class iterator : public kbList::iterator \
244 inline iterator(kbList::iterator const & i) \
245 { node = i.Node(); } \
248 inline iterator(kbListNode *n = NULL) \
249 : kbList::iterator(n) {} \
250 inline type * operator*() \
251 /* the cast is needed for MS VC++ 5.0 */ \
252 { return (type *)((kbList::iterator *)this)->operator*() ; } \
254 inline name(bool ownsEntriesFlag = true) \
255 : kbList(ownsEntriesFlag) {} \
257 inline void push_back(type *element) \
258 { kbList::push_back((void *)element); } \
260 inline void push_front(type *element) \
261 { kbList::push_front((void *)element); } \
263 inline type *pop_back(void) \
264 { return (type *) kbList::pop_back(); } \
266 inline type *pop_front(void) \
267 { return (type *) kbList::pop_front(); } \
269 inline void insert(iterator & i, type *element) \
270 { kbList::insert(i, (void *) element); } \
272 void erase(iterator & i) \
273 { kbList::erase(i); } \
275 inline iterator begin(void) const \
276 { return kbList::begin(); } \
278 inline iterator end(void) const \
279 { return kbList::end(); } \
281 inline iterator tail(void) const \
282 { return kbList::tail(); } \
286 /// define the most commonly used list type once:
287 KBLIST_DEFINE(kbStringList
, String
);