]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/alist.h
- Add Version, ConfigDir, and WorkingDir as Python attributes
[bacula/bacula] / bacula / src / lib / alist.h
1 /*
2  *   Version $Id$
3  */
4
5 /*
6    Copyright (C) 2003-2005 Kern Sibbald
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())); \
36          (var); \
37          (*((void **)&(var))=(void*)((list)->next())))
38
39 #ifdef the_easy_way
40 #define foreach_alist(var, list) \
41         for((void*(var))=(list)->first(); (var); (void *(var))=(list)->next(var)); )
42 #endif
43
44
45 /* Second arg of init */
46 enum {
47   owned_by_alist = true,
48   not_owned_by_alist = false
49 };
50
51 /*
52  * Array list -- much like a simplified STL vector
53  *   array of pointers to inserted items
54  */
55 class alist : public SMARTALLOC {
56    void **items;
57    int num_items;
58    int max_items;
59    int num_grow;
60    int cur_item;
61    bool own_items;
62    void grow_list(void);
63 public:
64    alist(int num = 1, bool own=true);
65    ~alist();
66    void init(int num = 1, bool own=true);
67    void append(void *item);
68    void prepend(void *item);
69    void *remove(int index);
70    void *get(int index);
71    bool empty() const;
72    void *prev();
73    void *next();
74    void *first();
75    void *last();
76    void * operator [](int index) const;
77    int size() const;
78    void destroy();
79    void grow(int num);
80
81    /* Use it as a stack, pushing and poping from the end */
82    void push(void *item) { append(item); };
83    void pop() { num_items?NULL:remove(num_items-1); };
84 };
85
86 inline void * alist::operator [](int index) const {
87    if (index < 0 || index >= num_items) {
88       return NULL;
89    }
90    return items[index];
91 }
92
93 inline bool alist::empty() const
94 {
95    /* Check for null pointer */
96    return this ? num_items == 0 : true;
97 }
98
99 /*
100  * This allows us to do explicit initialization,
101  *   allowing us to mix C++ classes inside malloc'ed
102  *   C structures. Define before called in constructor.
103  */
104 inline void alist::init(int num, bool own) {
105    items = NULL;
106    num_items = 0;
107    max_items = 0;
108    num_grow = num;
109    own_items = own;
110 }
111
112 /* Constructor */
113 inline alist::alist(int num, bool own) {
114    init(num, own);
115 }
116
117 /* Destructor */
118 inline alist::~alist() {
119    destroy();
120 }
121
122
123
124 /* Current size of list */
125 inline int alist::size() const
126 {
127    /*
128     * Check for null pointer, which allows test
129     *  on size to succeed even if nothing put in
130     *  alist.
131     */
132    return this ? num_items : 0;
133 }
134
135 /* How much to grow by each time */
136 inline void alist::grow(int num)
137 {
138    num_grow = num;
139 }