]>
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 *******************************************************************/
19 /**@name Double linked list implementation. */
22 /** kbListNode is a class used by kbList. It represents a single
23 element in the list. It is not intended for general use outside
28 /// pointer to next node or NULL
29 struct kbListNode
*next
;
30 /// pointer to previous node or NULL
31 struct kbListNode
*prev
;
32 /// pointer to the actual data
34 /** Constructor - it automatically links the node into the list, if
35 the iprev, inext parameters are given.
36 @param ielement pointer to the data for this node (i.e. the data itself)
37 @param iprev if not NULL, use this as previous element in list
38 @param inext if not NULL, use this as next element in list
40 kbListNode( void *ielement
,
41 kbListNode
*iprev
= NULL
,
42 kbListNode
*inext
= NULL
);
47 /** The main list class, handling void pointers as data.
53 /// An iterator class for kbList, just like for the STL classes.
57 /// the node to which this iterator points
62 @param n if not NULL, the node to which to point
64 iterator(kbListNode
*n
= NULL
);
65 /** Dereference operator.
66 @return the data pointer of the node belonging to this
71 /** This operator allows us to write if(i). It is <em>not</em> a
72 dereference operator and the result is always useless apart
73 from its logical value!
75 operator void*() const { return node
== NULL
? (void*)0 : (void*)(-1); }
78 /** Increment operator - prefix, goes to next node in list.
81 iterator
& operator++();
83 /** Decrement operator - prefix, goes to previous node in list.
86 iterator
& operator--();
88 /** Increment operator - prefix, goes to next node in list.
91 iterator
& operator++(int); //postfix
93 /** Decrement operator - prefix, goes to previous node in list.
96 iterator
& operator--(int); //postfix
98 /** Comparison operator.
99 @return true if not equal.
101 bool operator !=(iterator
const &) const;
103 /* Comparison operator.
104 @return true if equal
106 bool operator ==(iterator
const &) const;
108 /** Returns a pointer to the node associated with this iterator.
109 This function is not for general use and should be
110 protected. However, if protected, it cannot be called from
111 derived classes' iterators. (Is this a bug in gcc/egcs?)
112 @return the node pointer
114 inline kbListNode
* Node(void) const
119 @param ownsEntriesFlag if true, the list owns the entries and
120 will issue a delete on each of them when deleting them. If
121 false, the entries themselves will not get deleted. Do not use
122 this with array types!
124 kbList(bool ownsEntriesFlag
= true);
127 If entries are owned, they will all get deleted from here.
131 /** Tell list whether it owns objects. If owned, they can be
132 deleted by list. See the constructor for more details.
133 @param ownsflag if true, list will own entries
135 void ownsObjects(bool ownsflag
)
136 { ownsEntries
= ownsflag
; }
138 /** Query whether list owns entries.
139 @return true if list owns entries
141 bool ownsObjects(void)
142 { return ownsEntries
; }
144 // This must be protected to disallow insertion of wrong elements.
146 /** Add an entry at the end of the list.
147 @param element pointer to data
149 void push_back(void *element
);
151 /** Add an entry at the head of the list.
152 @param element pointer to data
154 void push_front(void *element
);
156 /** Insert an element into the list.
157 @param i an iterator pointing to the element, before which the new one should be inserted
158 @param element the element data
160 void insert(iterator
& i
, void *element
);
163 /** Get element from end of the list and delete it.
164 NOTE: In this case the element's data will not get deleted by
165 the list. It is the responsibility of the caller to free it.
166 @return the element data
168 void * pop_back(void);
170 /** Get element from head of the list and delete it.
171 NOTE: In this case the element's data will not get deleted by
172 the list. It is the responsibility of the caller to free it.
173 @return the element data
175 void * pop_front(void);
177 /** Remove an element from the list _without_ deleting the object.
178 @param i iterator pointing to the element to be deleted
179 @return the value of the element just removed
181 void *remove(iterator
& i
) { void *p
= *i
; doErase(i
); return p
; }
183 /** Erase an element, move iterator to following element.
184 @param i iterator pointing to the element to be deleted
186 void erase(iterator
& i
) { deleteContent(i
); doErase(i
); }
189 @return iterator pointing to head of list
191 iterator
begin(void) const;
194 @return iterator pointing after the end of the list. This is an
195 invalid iterator which cannot be dereferenced or decremented. It is
196 only of use in comparisons. NOTE: this is different from STL!
199 iterator
end(void) const;
201 /* Get last element in list.
202 @return iterator pointing to the last element in the list.
205 iterator
tail(void) const;
207 /* Get the number of elements in the list.
208 @return number of elements in the list
210 unsigned size(void) const;
212 /* Query whether list is empty.
213 @return true if list is empty
215 inline bool empty(void) const
216 { return first
== NULL
; }
219 /// if true, list owns entries
221 /// pointer to first element in list
223 /// pointer to last element in list
226 /** Erase an element, move iterator to following element.
227 @param i iterator pointing to the element to be deleted
229 void doErase(iterator
& i
);
231 /** Deletes the actual content if ownsflag is set.
234 inline void deleteContent(iterator i
)
236 iterator
*i_ptr
= &i
;
237 if(ownsEntries
) delete i_ptr
;
242 /// forbid copy construction
243 kbList(kbList
const &foo
);
244 /// forbid assignments
245 kbList
& operator=(const kbList
& foo
);
248 /// just for backward compatibility, will be removed soon
249 typedef kbList::iterator kbListIterator
;
250 /// cast an iterator to a pointer, compatibility only to be removed
251 #define kbListICast(type, iterator) ((type *)*iterator)
252 /// cast an iterator to a const pointer, compatibility only to be removed
253 #define kbListIcCast(type, iterator) ((type const *)*iterator)
255 /** Macro to define a kbList with a given name, having elements of
256 pointer to the given type. I.e. KBLIST_DEFINE(Int,int) would
257 create a kbListInt type holding int pointers.
259 #define KBLIST_DEFINE(name,type) \
260 class name : public kbList \
263 class iterator : public kbList::iterator \
266 inline iterator(kbList::iterator const & i) \
267 { node = i.Node(); } \
270 inline iterator(kbListNode *n = NULL) \
271 : kbList::iterator(n) {} \
272 inline type * operator*() \
273 /* the cast is needed for MS VC++ 5.0 */ \
274 { return (type *)((kbList::iterator *)this)->operator*() ; } \
276 inline name(bool ownsEntriesFlag = true) \
277 : kbList(ownsEntriesFlag) {} \
279 inline type *pop_back(void) \
280 { return (type *) kbList::pop_back(); } \
282 inline type *pop_front(void) \
283 { return (type *) kbList::pop_front(); } \
284 inline void push_back(type *element) \
285 { kbList::push_back( (void *) element); } \
286 void push_front(type *element) \
287 { kbList::push_front( (void *) element); } \
288 void insert(iterator & i, void *element) \
289 { kbList::insert( i, (void *) element); } \
290 type *remove(iterator& i) \
291 { return (type *)kbList::remove(i); } \
292 inline void erase(iterator & i) \
293 { deleteContent(i); doErase(i); } \
295 inline iterator begin(void) const \
296 { return kbList::begin(); } \
298 inline iterator end(void) const \
299 { return kbList::end(); } \
301 inline iterator tail(void) const \
302 { return kbList::tail(); } \
306 while ( first != NULL ) \
308 next = first->next; \
310 delete (type *)first->element; \
316 inline void deleteContent(iterator i) \
317 { if(ownsEntries) delete *i; } \
321 /// define the most commonly used list type once:
322 KBLIST_DEFINE(kbStringList
, String
);