]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/alist.h
c9b041a026eb537327854d864f52baa1859984b3
[bacula/bacula] / bacula / src / lib / alist.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2016 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *  Kern Sibbald, June MMIII
21  */
22
23
24 /*
25  * There is a lot of extra casting here to work around the fact
26  * that some compilers (Sun and Visual C++) do not accept
27  * (void *) as an lvalue on the left side of an equal.
28  *
29  * Loop var through each member of list
30  */
31 #ifdef HAVE_TYPEOF
32 #define foreach_alist(var, list) \
33         for((var)=(typeof(var))(list)->first(); (var); (var)=(typeof(var))(list)->next() )
34 #else
35 #define foreach_alist(var, list) \
36     for((*((void **)&(var))=(void*)((list)->first())); \
37          (var); \
38          (*((void **)&(var))=(void*)((list)->next())))
39 #endif
40
41 #ifdef HAVE_TYPEOF
42 #define foreach_alist_index(inx, var, list) \
43         for(inx=0; ((var)=(typeof(var))(list)->get(inx)); inx++ )
44 #else
45 #define foreach_alist_index(inx, var, list) \
46     for(inx=0; ((*((void **)&(var))=(void*)((list)->get(inx)))); inx++ )
47 #endif
48
49
50
51
52 /* Second arg of init */
53 enum {
54   owned_by_alist = true,
55   not_owned_by_alist = false
56 };
57
58 /*
59  * Array list -- much like a simplified STL vector
60  *   array of pointers to inserted items
61  */
62 class alist : public SMARTALLOC {
63    void **items;
64    int num_items;
65    int max_items;
66    int num_grow;
67    int cur_item;
68    bool own_items;
69    void grow_list(void);
70 public:
71    alist(int num = 10, bool own=true);
72    ~alist();
73    void init(int num = 10, bool own=true);
74    void append(void *item);
75    void prepend(void *item);
76    void *remove(int index);
77    void *get(int index);
78    bool empty() const;
79    void *prev();
80    void *next();
81    void *first();
82    void *last();
83    void * operator [](int index) const;
84    int current() const { return cur_item; };
85    int size() const;
86    void destroy();
87    void grow(int num);
88
89    /* Use it as a stack, pushing and poping from the end */
90    void push(void *item) { append(item); };
91    void *pop() { return remove(num_items-1); };
92 };
93
94 /*
95  * Define index operator []
96  */
97 inline void * alist::operator [](int index) const {
98    if (index < 0 || index >= num_items) {
99       return NULL;
100    }
101    return items[index];
102 }
103
104 inline bool alist::empty() const
105 {
106    /* Check for null pointer */
107    return this ? num_items == 0 : true;
108 }
109
110 /*
111  * This allows us to do explicit initialization,
112  *   allowing us to mix C++ classes inside malloc'ed
113  *   C structures. Define before called in constructor.
114  */
115 inline void alist::init(int num, bool own)
116 {
117    items = NULL;
118    num_items = 0;
119    max_items = 0;
120    num_grow = num;
121    own_items = own;
122 }
123
124 /* Constructor */
125 inline alist::alist(int num, bool own)
126 {
127    init(num, own);
128 }
129
130 /* Destructor */
131 inline alist::~alist()
132 {
133    destroy();
134 }
135
136
137
138 /* Current size of list */
139 inline int alist::size() const
140 {
141    /*
142     * Check for null pointer, which allows test
143     *  on size to succeed even if nothing put in
144     *  alist.
145     */
146    return this ? num_items : 0;
147 }
148
149 /* How much to grow by each time */
150 inline void alist::grow(int num)
151 {
152    num_grow = num;
153 }