]>
git.saurik.com Git - wxWidgets.git/blob - samples/richedit/kbList.h
1 /*-*- c++ -*-********************************************************
2 * kbList.h : a double linked list *
4 * (C) 1998-1999 by Karsten Ballüder (karsten@phy.hw.ac.uk) *
8 *******************************************************************/
16 # pragma interface "kbList.h"
23 /**@name Double linked list implementation. */
26 /** kbListNode is a class used by kbList. It represents a single
27 element in the list. It is not intended for general use outside
32 /// pointer to next node or NULL
33 struct kbListNode
*next
;
34 /// pointer to previous node or NULL
35 struct kbListNode
*prev
;
36 /// pointer to the actual data
38 /** Constructor - it automatically links the node into the list, if
39 the iprev, inext parameters are given.
40 @param ielement pointer to the data for this node (i.e. the data itself)
41 @param iprev if not NULL, use this as previous element in list
42 @param inext if not NULL, use this as next element in list
44 kbListNode( void *ielement
,
45 kbListNode
*iprev
= NULL
,
46 kbListNode
*inext
= NULL
);
51 /** The main list class, handling void pointers as data.
57 /// An iterator class for kbList, just like for the STL classes.
61 /// the node to which this iterator points
66 @param n if not NULL, the node to which to point
68 iterator(kbListNode
*n
= NULL
);
69 /** Dereference operator.
70 @return the data pointer of the node belonging to this
75 /** This operator allows us to write if(i). It is <em>not</em> a
76 dereference operator and the result is always useless apart
77 from its logical value!
79 operator void*() const { return node
== NULL
? (void*)0 : (void*)(-1); }
82 /** Increment operator - prefix, goes to next node in list.
85 iterator
& operator++();
87 /** Decrement operator - prefix, goes to previous node in list.
90 iterator
& operator--();
92 /** Increment operator - prefix, goes to next node in list.
95 iterator
& operator++(int); //postfix
97 /** Decrement operator - prefix, goes to previous node in list.
100 iterator
& operator--(int); //postfix
102 /** Comparison operator.
103 @return true if not equal.
105 bool operator !=(iterator
const &) const;
107 /* Comparison operator.
108 @return true if equal
110 bool operator ==(iterator
const &) const;
112 /** Returns a pointer to the node associated with this iterator.
113 This function is not for general use and should be
114 protected. However, if protected, it cannot be called from
115 derived classes' iterators. (Is this a bug in gcc/egcs?)
116 @return the node pointer
118 inline kbListNode
* Node(void) const
123 @param ownsEntriesFlag if true, the list owns the entries and
124 will issue a delete on each of them when deleting them. If
125 false, the entries themselves will not get deleted. Do not use
126 this with array types!
128 kbList(bool ownsEntriesFlag
= true);
131 If entries are owned, they will all get deleted from here.
135 /** Tell list whether it owns objects. If owned, they can be
136 deleted by list. See the constructor for more details.
137 @param ownsflag if true, list will own entries
139 void ownsObjects(bool ownsflag
)
140 { ownsEntries
= ownsflag
; }
142 /** Query whether list owns entries.
143 @return true if list owns entries
145 bool ownsObjects(void)
146 { return ownsEntries
; }
148 // This must be protected to disallow insertion of wrong elements.
150 /** Add an entry at the end of the list.
151 @param element pointer to data
153 void push_back(void *element
);
155 /** Add an entry at the head of the list.
156 @param element pointer to data
158 void push_front(void *element
);
160 /** Insert an element into the list.
161 @param i an iterator pointing to the element, before which the new one should be inserted
162 @param element the element data
164 void insert(iterator
& i
, void *element
);
167 /** Get element from end of the list and delete it.
168 NOTE: In this case the element's data will not get deleted by
169 the list. It is the responsibility of the caller to free it.
170 @return the element data
172 void * pop_back(void);
174 /** Get element from head of the list and delete it.
175 NOTE: In this case the element's data will not get deleted by
176 the list. It is the responsibility of the caller to free it.
177 @return the element data
179 void * pop_front(void);
181 /** Remove an element from the list _without_ deleting the object.
182 @param i iterator pointing to the element to be deleted
183 @return the value of the element just removed
185 void *remove(iterator
& i
) { void *p
= *i
; doErase(i
); return p
; }
187 /** Erase an element, move iterator to following element.
188 @param i iterator pointing to the element to be deleted
190 void erase(iterator
& i
) { deleteContent(i
); doErase(i
); }
193 @return iterator pointing to head of list
195 iterator
begin(void) const;
198 @return iterator pointing after the end of the list. This is an
199 invalid iterator which cannot be dereferenced or decremented. It is
200 only of use in comparisons. NOTE: this is different from STL!
203 iterator
end(void) const;
205 /* Get last element in list.
206 @return iterator pointing to the last element in the list.
209 iterator
tail(void) const;
211 /* Get the number of elements in the list.
212 @return number of elements in the list
214 unsigned size(void) const;
216 /* Query whether list is empty.
217 @return true if list is empty
219 inline bool empty(void) const
220 { return first
== NULL
; }
223 /// if true, list owns entries
225 /// pointer to first element in list
227 /// pointer to last element in list
230 /** Erase an element, move iterator to following element.
231 @param i iterator pointing to the element to be deleted
233 void doErase(iterator
& i
);
235 /** Deletes the actual content if ownsflag is set.
238 inline void deleteContent(iterator i
)
240 iterator
*i_ptr
= &i
;
241 if(ownsEntries
) delete i_ptr
;
246 /// forbid copy construction
247 kbList(kbList
const &foo
);
248 /// forbid assignments
249 kbList
& operator=(const kbList
& foo
);
252 /// just for backward compatibility, will be removed soon
253 typedef kbList::iterator kbListIterator
;
254 /// cast an iterator to a pointer, compatibility only to be removed
255 #define kbListICast(type, iterator) ((type *)*iterator)
256 /// cast an iterator to a const pointer, compatibility only to be removed
257 #define kbListIcCast(type, iterator) ((type const *)*iterator)
259 /** Macro to define a kbList with a given name, having elements of
260 pointer to the given type. I.e. KBLIST_DEFINE(Int,int) would
261 create a kbListInt type holding int pointers.
263 #define KBLIST_DEFINE(name,type) \
264 class name : public kbList \
267 class iterator : public kbList::iterator \
270 inline iterator(kbList::iterator const & i) \
271 { node = i.Node(); } \
274 inline iterator(kbListNode *n = NULL) \
275 : kbList::iterator(n) {} \
276 inline type * operator*() \
277 /* the cast is needed for MS VC++ 5.0 */ \
278 { return (type *)((kbList::iterator *)this)->operator*() ; } \
280 inline name(bool ownsEntriesFlag = TRUE) \
281 : kbList(ownsEntriesFlag) {} \
283 inline type *pop_back(void) \
284 { return (type *) kbList::pop_back(); } \
286 inline type *pop_front(void) \
287 { return (type *) kbList::pop_front(); } \
288 inline void push_back(type *element) \
289 { kbList::push_back( (void *) element); } \
290 void push_front(type *element) \
291 { kbList::push_front( (void *) element); } \
292 void insert(iterator & i, void *element) \
293 { kbList::insert( i, (void *) element); } \
294 type *remove(iterator& i) \
295 { return (type *)kbList::remove(i); } \
296 inline void erase(iterator & i) \
297 { deleteContent(i); doErase(i); } \
299 inline iterator begin(void) const \
300 { return kbList::begin(); } \
302 inline iterator end(void) const \
303 { return kbList::end(); } \
305 inline iterator tail(void) const \
306 { return kbList::tail(); } \
310 while ( first != NULL ) \
312 next = first->next; \
314 delete (type *)first->element; \
320 inline void deleteContent(iterator i) \
321 { if(ownsEntries) delete *i; } \
325 /// define the most commonly used list type once:
326 KBLIST_DEFINE(kbStringList
, String
);