]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/alist.h
Removed old sd plugins which doesn't work anymore.
[bacula/bacula] / bacula / src / lib / alist.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2003-2011 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 three of the GNU Affero 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 Affero 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(R) is a registered trademark of Kern Sibbald.
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  *  Kern Sibbald, June MMIII
30  */
31
32
33 /*
34  * There is a lot of extra casting here to work around the fact
35  * that some compilers (Sun and Visual C++) do not accept
36  * (void *) as an lvalue on the left side of an equal.
37  *
38  * Loop var through each member of list
39  */
40 #ifdef HAVE_TYPEOF
41 #define foreach_alist(var, list) \
42         for((var)=(typeof(var))(list)->first(); (var); (var)=(typeof(var))(list)->next() )
43 #else
44 #define foreach_alist(var, list) \
45     for((*((void **)&(var))=(void*)((list)->first())); \
46          (var); \
47          (*((void **)&(var))=(void*)((list)->next())))
48 #endif
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 = 1, bool own=true);
72    ~alist();
73    void init(int num = 1, 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 size() const;
85    void destroy();
86    void grow(int num);
87
88    /* Use it as a stack, pushing and poping from the end */
89    void push(void *item) { append(item); };
90    void *pop() { return remove(num_items-1); };
91 };
92
93 /*
94  * Define index operator []
95  */
96 inline void * alist::operator [](int index) const {
97    if (index < 0 || index >= num_items) {
98       return NULL;
99    }
100    return items[index];
101 }
102
103 inline bool alist::empty() const
104 {
105    /* Check for null pointer */
106    return this ? num_items == 0 : true;
107 }
108
109 /*
110  * This allows us to do explicit initialization,
111  *   allowing us to mix C++ classes inside malloc'ed
112  *   C structures. Define before called in constructor.
113  */
114 inline void alist::init(int num, bool own)
115 {
116    items = NULL;
117    num_items = 0;
118    max_items = 0;
119    num_grow = num;
120    own_items = own;
121 }
122
123 /* Constructor */
124 inline alist::alist(int num, bool own)
125 {
126    init(num, own);
127 }
128
129 /* Destructor */
130 inline alist::~alist()
131 {
132    destroy();
133 }
134
135
136
137 /* Current size of list */
138 inline int alist::size() const
139 {
140    /*
141     * Check for null pointer, which allows test
142     *  on size to succeed even if nothing put in
143     *  alist.
144     */
145    return this ? num_items : 0;
146 }
147
148 /* How much to grow by each time */
149 inline void alist::grow(int num)
150 {
151    num_grow = num;
152 }