]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/alist.h
Apply Preben 'Peppe' Guldberg <peppe@wielders.org>
[bacula/bacula] / bacula / src / lib / alist.h
1 /*
2  *   Version $Id$
3  */
4
5 /*
6    Copyright (C) 2003-2004 Kern Sibbald and John Walker
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2 of
11    the License, or (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public
19    License along with this program; if not, write to the Free
20    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21    MA 02111-1307, USA.
22
23    Kern Sibbald, June MMIII
24
25  */
26
27 /*
28  * There is a lot of extra casting here to work around the fact
29  * that some compilers (Sun and Visual C++) do not accept
30  * (void *) as an lvalue on the left side of an equal.
31  *
32  * Loop var through each member of list
33  */
34 #define foreach_alist(var, list) \
35     for((*((void **)&(var))=(void*)((list)->first())); (var); (*((void **)&(var))=(void*)((list)->next())))
36
37 #ifdef the_easy_way
38 #define foreach_alist(var, list) \
39         for((void*(var))=(list)->first(); (var); (void *(var))=(list)->next(var)); )
40 #endif
41
42
43 /* Second arg of init */
44 enum {
45   owned_by_alist = true,
46   not_owned_by_alist = false
47 };
48
49 /*
50  * Array list -- much like a simplified STL vector
51  *   array of pointers to inserted items
52  */
53 class alist : public SMARTALLOC {
54    void **items;
55    int num_items;
56    int max_items;
57    int num_grow;
58    int cur_item;
59    bool own_items;
60    void grow_list(void);
61 public:
62    alist(int num = 1, bool own=true);
63    ~alist();
64    void init(int num = 1, bool own=true);
65    void append(void *item);
66    void prepend(void *item);
67    void *remove(int index);
68    void *get(int index);
69    bool empty() const;
70    void *prev();
71    void *next();
72    void *first();
73    void *last();
74    void * operator [](int index) const;
75    int size() const;
76    void destroy();
77    void grow(int num);
78 };
79
80 inline void * alist::operator [](int index) const {
81    if (index < 0 || index >= num_items) {
82       return NULL;
83    }
84    return items[index];
85 }
86
87 inline bool alist::empty() const
88 {
89    /* Check for null pointer */
90    return this ? num_items == 0 : true;
91 }
92
93 /*
94  * This allows us to do explicit initialization,
95  *   allowing us to mix C++ classes inside malloc'ed
96  *   C structures. Define before called in constructor.
97  */
98 inline void alist::init(int num, bool own) {
99    items = NULL;
100    num_items = 0;
101    max_items = 0;
102    num_grow = num;
103    own_items = own;
104 }
105
106 /* Constructor */
107 inline alist::alist(int num, bool own) {
108    init(num, own);
109 }
110
111 /* Destructor */
112 inline alist::~alist() {
113    destroy();
114 }
115
116
117
118 /* Current size of list */
119 inline int alist::size() const
120 {
121    /*
122     * Check for null pointer, which allows test
123     *  on size to succeed even if nothing put in
124     *  alist.
125     */
126    return this ? num_items : 0;
127 }
128
129 /* How much to grow by each time */
130 inline void alist::grow(int num)
131 {
132    num_grow = num;
133 }