]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/alist.h
Convert to pure GPL v2 license.
[bacula/bacula] / bacula / src / lib / alist.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2003-2007 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    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 License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of John Walker.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *   Version $Id$
30  *
31  *  Kern Sibbald, June MMIII
32  */
33
34
35 /*
36  * There is a lot of extra casting here to work around the fact
37  * that some compilers (Sun and Visual C++) do not accept
38  * (void *) as an lvalue on the left side of an equal.
39  *
40  * Loop var through each member of list
41  */
42 #ifdef HAVE_TYPEOF
43 #define foreach_alist(var, list) \
44         for((var)=(typeof(var))(list)->first(); (var); (var)=(typeof(var))(list)->next() )
45 #else
46 #define foreach_alist(var, list) \
47     for((*((void **)&(var))=(void*)((list)->first())); \
48          (var); \
49          (*((void **)&(var))=(void*)((list)->next())))
50 #endif
51
52
53
54 /* Second arg of init */
55 enum {
56   owned_by_alist = true,
57   not_owned_by_alist = false
58 };
59
60 /*
61  * Array list -- much like a simplified STL vector
62  *   array of pointers to inserted items
63  */
64 class alist : public SMARTALLOC {
65    void **items;
66    int num_items;
67    int max_items;
68    int num_grow;
69    int cur_item;
70    bool own_items;
71    void grow_list(void);
72 public:
73    alist(int num = 1, bool own=true);
74    ~alist();
75    void init(int num = 1, bool own=true);
76    void append(void *item);
77    void prepend(void *item);
78    void *remove(int index);
79    void *get(int index);
80    bool empty() const;
81    void *prev();
82    void *next();
83    void *first();
84    void *last();
85    void * operator [](int index) const;
86    int size() const;
87    void destroy();
88    void grow(int num);
89
90    /* Use it as a stack, pushing and poping from the end */
91    void push(void *item) { append(item); };
92    void *pop() { return remove(num_items-1); };
93 };
94
95 inline void * alist::operator [](int index) const {
96    if (index < 0 || index >= num_items) {
97       return NULL;
98    }
99    return items[index];
100 }
101
102 inline bool alist::empty() const
103 {
104    /* Check for null pointer */
105    return this ? num_items == 0 : true;
106 }
107
108 /*
109  * This allows us to do explicit initialization,
110  *   allowing us to mix C++ classes inside malloc'ed
111  *   C structures. Define before called in constructor.
112  */
113 inline void alist::init(int num, bool own) {
114    items = NULL;
115    num_items = 0;
116    max_items = 0;
117    num_grow = num;
118    own_items = own;
119 }
120
121 /* Constructor */
122 inline alist::alist(int num, bool own) {
123    init(num, own);
124 }
125
126 /* Destructor */
127 inline alist::~alist() {
128    destroy();
129 }
130
131
132
133 /* Current size of list */
134 inline int alist::size() const
135 {
136    /*
137     * Check for null pointer, which allows test
138     *  on size to succeed even if nothing put in
139     *  alist.
140     */
141    return this ? num_items : 0;
142 }
143
144 /* How much to grow by each time */
145 inline void alist::grow(int num)
146 {
147    num_grow = num;
148 }