]>
git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/kbList.h
818b2fe728b8b1073e9f717b1f370a1fe473231c
1 /*-*- c++ -*-********************************************************
2 * kbList.h : a double linked list *
4 * (C) 1998 by Karsten Ballüder (Ballueder@usa.net) *
8 *******************************************************************/
14 # pragma interface "kbList.h"
21 /**@name Double linked list implementation. */
24 /** kbListNode is a class used by kbList. It represents a single
25 element in the list. It is not intended for general use outside
30 /// pointer to next node or NULL
31 struct kbListNode
*next
;
32 /// pointer to previous node or NULL
33 struct kbListNode
*prev
;
34 /// pointer to the actual data
36 /** Constructor - it automatically links the node into the list, if
37 the iprev, inext parameters are given.
38 @param ielement pointer to the data for this node (i.e. the data itself)
39 @param iprev if not NULL, use this as previous element in list
40 @param inext if not NULL, use this as next element in list
42 kbListNode( void *ielement
,
43 kbListNode
*iprev
= NULL
,
44 kbListNode
*inext
= NULL
);
49 /** The main list class, handling void pointers as data.
55 /// An iterator class for kbList, just like for the STL classes.
59 /// the node to which this iterator points
64 @param n if not NULL, the node to which to point
66 iterator(kbListNode
*n
= NULL
);
67 /** Dereference operator.
68 @return the data pointer of the node belonging to this
73 /** Increment operator - prefix, goes to next node in list.
76 iterator
& operator++();
78 /** Decrement operator - prefix, goes to previous node in list.
81 iterator
& operator--();
83 /** Increment operator - prefix, goes to next node in list.
86 iterator
& operator++(int); //postfix
88 /** Decrement operator - prefix, goes to previous node in list.
91 iterator
& operator--(int); //postfix
93 /** Comparison operator.
94 @return true if not equal.
96 bool operator !=(iterator
const &) const;
98 /* Comparison operator.
101 bool operator ==(iterator
const &) const;
103 /** Returns a pointer to the node associated with this iterator.
104 This function is not for general use and should be
105 protected. However, if protected, it cannot be called from
106 derived classes' iterators. (Is this a bug in gcc/egcs?)
107 @return the node pointer
109 inline kbListNode
* Node(void) const
114 @param ownsEntriesFlag if true, the list owns the entries and
115 will issue a delete on each of them when deleting them. If
116 false, the entries themselves will not get deleted. Do not use
117 this with array types!
119 kbList(bool ownsEntriesFlag
= true);
122 If entries are owned, they will all get deleted from here.
126 /** Tell list whether it owns objects. If owned, they can be
127 deleted by list. See the constructor for more details.
128 @param ownsflag if true, list will own entries
130 void ownsObjects(bool ownsflag
= true)
131 { ownsEntries
= ownsflag
; }
133 /** Query whether list owns entries.
134 @return true if list owns entries
136 bool ownsObjects(void)
137 { return ownsEntries
; }
139 /** Add an entry at the end of the list.
140 @param element pointer to data
142 void push_back(void *element
);
144 /** Add an entry at the head of the list.
145 @param element pointer to data
147 void push_front(void *element
);
149 /** Get element from end of the list and delete it.
150 NOTE: In this case the element's data will not get deleted by
151 the list. It is the responsibility of the caller to free it.
152 @return the element data
154 void *pop_back(void);
156 /** Get element from head of the list and delete it.
157 NOTE: In this case the element's data will not get deleted by
158 the list. It is the responsibility of the caller to free it.
159 @return the element data
161 void *pop_front(void);
163 /** Insert an element into the list.
164 @param i an iterator pointing to the element, before which the new one should be inserted
165 @param element the element data
167 void insert(iterator
& i
, void *element
);
169 /** Erase an element, move iterator to following element.
170 @param i iterator pointing to the element to be deleted
172 void erase(iterator
& i
);
175 @return iterator pointing to head of list
177 iterator
begin(void) const;
180 @return iterator pointing after the end of the list. This is an
181 invalid iterator which cannot be dereferenced or decremented. It is
182 only of use in comparisons. NOTE: this is different from STL!
185 iterator
end(void) const;
187 /* Get last element in list.
188 @return iterator pointing to the last element in the list.
191 iterator
tail(void) const;
193 /* Get the number of elements in the list.
194 @return number of elements in the list
196 unsigned size(void) const;
198 /* Query whether list is empty.
199 @return true if list is empty
201 bool empty(void) const
202 { return first
== NULL
; }
205 /// if true, list owns entries
207 /// pointer to first element in list
209 /// pointer to last element in list
213 /// forbid copy construction
214 kbList(kbList
const &foo
);
215 /// forbid assignments
216 kbList
& operator=(const kbList
& foo
);
219 /// just for backward compatibility, will be removed soon
220 typedef kbList::iterator kbListIterator
;
221 /// cast an iterator to a pointer, compatibility only to be removed
222 #define kbListICast(type, iterator) ((type *)*iterator)
223 /// cast an iterator to a const pointer, compatibility only to be removed
224 #define kbListIcCast(type, iterator) ((type const *)*iterator)
226 /** Macro to define a kbList with a given name, having elements of
227 pointer to the given type. I.e. KBLIST_DEFINE(Int,int) would
228 create a kbListInt type holding int pointers.
230 #define KBLIST_DEFINE(name,type) \
231 class name : public kbList \
234 class iterator : public kbList::iterator \
237 inline iterator(kbList::iterator const & i) \
238 { node = i.Node(); } \
241 inline iterator(kbListNode *n = NULL) \
242 : kbList::iterator(n) {} \
243 inline type * operator*() \
244 /* the cast is needed for MS VC++ 5.0 */ \
245 { return (type *)((kbList::iterator *)this)->operator*() ; } \
247 inline name(bool ownsEntriesFlag = FALSE) \
248 : kbList(ownsEntriesFlag) {} \
250 inline void push_back(type *element) \
251 { kbList::push_back((void *)element); } \
253 inline void push_front(type *element) \
254 { kbList::push_front((void *)element); } \
256 inline type *pop_back(void) \
257 { return (type *) kbList::pop_back(); } \
259 inline type *pop_front(void) \
260 { return (type *) kbList::pop_front(); } \
262 inline void insert(iterator & i, type *element) \
263 { kbList::insert(i, (void *) element); } \
265 inline void erase(iterator & i) \
266 { kbList::erase(i); } \
268 inline iterator begin(void) const \
269 { return kbList::begin(); } \
271 inline iterator end(void) const \
272 { return kbList::end(); } \
274 inline iterator tail(void) const \
275 { return kbList::tail(); } \
279 while ( first != NULL ) \
281 next = first->next; \
283 delete typecast(first->element); \
289 inline type * typecast(void *ptr) \
290 { return (type *) ptr; } \
294 /// define the most commonly used list type once:
295 KBLIST_DEFINE(kbStringList
, String
);