]>
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
= true)
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
)
239 { if(ownsEntries
) delete *i
; }
243 /// forbid copy construction
244 kbList(kbList
const &foo
);
245 /// forbid assignments
246 kbList
& operator=(const kbList
& foo
);
249 /// just for backward compatibility, will be removed soon
250 typedef kbList::iterator kbListIterator
;
251 /// cast an iterator to a pointer, compatibility only to be removed
252 #define kbListICast(type, iterator) ((type *)*iterator)
253 /// cast an iterator to a const pointer, compatibility only to be removed
254 #define kbListIcCast(type, iterator) ((type const *)*iterator)
256 /** Macro to define a kbList with a given name, having elements of
257 pointer to the given type. I.e. KBLIST_DEFINE(Int,int) would
258 create a kbListInt type holding int pointers.
260 #define KBLIST_DEFINE(name,type) \
261 class name : public kbList \
264 class iterator : public kbList::iterator \
267 inline iterator(kbList::iterator const & i) \
268 { node = i.Node(); } \
271 inline iterator(kbListNode *n = NULL) \
272 : kbList::iterator(n) {} \
273 inline type * operator*() \
274 /* the cast is needed for MS VC++ 5.0 */ \
275 { return (type *)((kbList::iterator *)this)->operator*() ; } \
277 inline name(bool ownsEntriesFlag = TRUE) \
278 : kbList(ownsEntriesFlag) {} \
280 inline type *pop_back(void) \
281 { return (type *) kbList::pop_back(); } \
283 inline type *pop_front(void) \
284 { return (type *) kbList::pop_front(); } \
285 inline void push_back(type *element) \
286 { kbList::push_back( (void *) element); } \
287 void push_front(type *element) \
288 { kbList::push_front( (void *) element); } \
289 void insert(iterator & i, void *element) \
290 { kbList::insert( i, (void *) element); } \
291 type *remove(iterator& i) \
292 { return (type *)kbList::remove(i); } \
293 inline void erase(iterator & i) \
294 { deleteContent(i); doErase(i); } \
296 inline iterator begin(void) const \
297 { return kbList::begin(); } \
299 inline iterator end(void) const \
300 { return kbList::end(); } \
302 inline iterator tail(void) const \
303 { return kbList::tail(); } \
307 while ( first != NULL ) \
309 next = first->next; \
311 delete (type *)first->element; \
317 inline void deleteContent(iterator i) \
318 { if(ownsEntries) delete *i; } \
322 /// define the most commonly used list type once:
323 KBLIST_DEFINE(kbStringList
, String
);