4 * Kern Sibbald, June MMIII
7 Copyright (C) 2003-2006 Kern Sibbald
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 version 2 as amended with additional clauses defined in the
12 file LICENSE in the main source directory.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 the file LICENSE for additional details.
23 * There is a lot of extra casting here to work around the fact
24 * that some compilers (Sun and Visual C++) do not accept
25 * (void *) as an lvalue on the left side of an equal.
27 * Loop var through each member of list
30 #define foreach_alist(var, list) \
31 for((var)=(typeof(var))(list)->first(); (var); (var)=(typeof(var))(list)->next() )
33 #define foreach_alist(var, list) \
34 for((*((void **)&(var))=(void*)((list)->first())); \
36 (*((void **)&(var))=(void*)((list)->next())))
41 /* Second arg of init */
43 owned_by_alist = true,
44 not_owned_by_alist = false
48 * Array list -- much like a simplified STL vector
49 * array of pointers to inserted items
51 class alist : public SMARTALLOC {
60 alist(int num = 1, bool own=true);
62 void init(int num = 1, bool own=true);
63 void append(void *item);
64 void prepend(void *item);
65 void *remove(int index);
72 void * operator [](int index) const;
77 /* Use it as a stack, pushing and poping from the end */
78 void push(void *item) { append(item); };
79 void *pop() { return remove(num_items-1); };
82 inline void * alist::operator [](int index) const {
83 if (index < 0 || index >= num_items) {
89 inline bool alist::empty() const
91 /* Check for null pointer */
92 return this ? num_items == 0 : true;
96 * This allows us to do explicit initialization,
97 * allowing us to mix C++ classes inside malloc'ed
98 * C structures. Define before called in constructor.
100 inline void alist::init(int num, bool own) {
109 inline alist::alist(int num, bool own) {
114 inline alist::~alist() {
120 /* Current size of list */
121 inline int alist::size() const
124 * Check for null pointer, which allows test
125 * on size to succeed even if nothing put in
128 return this ? num_items : 0;
131 /* How much to grow by each time */
132 inline void alist::grow(int num)