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