]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/alist.h
- Remove timed wait for VSS on Win2K3 as it is not yet
[bacula/bacula] / bacula / src / lib / alist.h
1 /*
2  *   Version $Id$
3  *
4  *  Kern Sibbald, June MMIII
5  */
6 /*
7    Copyright (C) 2003-2006 Kern Sibbald
8
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License
11    version 2 as amended with additional clauses defined in the
12    file LICENSE in the main source directory.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
17    the file LICENSE for additional details.
18
19  */
20
21
22 /*
23  * There is a lot of extra casting here to work around the fact
24  * that some compilers (Sun and Visual C++) do not accept
25  * (void *) as an lvalue on the left side of an equal.
26  *
27  * Loop var through each member of list
28  */
29 #ifdef HAVE_GCC
30 #define foreach_alist(var, list) \
31         for((var)=(typeof(var))(list)->first(); (var); (var)=(typeof(var))(list)->next() )
32 #else
33 #define foreach_alist(var, list) \
34     for((*((void **)&(var))=(void*)((list)->first())); \
35          (var); \
36          (*((void **)&(var))=(void*)((list)->next())))
37 #endif
38
39
40
41 /* Second arg of init */
42 enum {
43   owned_by_alist = true,
44   not_owned_by_alist = false
45 };
46
47 /*
48  * Array list -- much like a simplified STL vector
49  *   array of pointers to inserted items
50  */
51 class alist : public SMARTALLOC {
52    void **items;
53    int num_items;
54    int max_items;
55    int num_grow;
56    int cur_item;
57    bool own_items;
58    void grow_list(void);
59 public:
60    alist(int num = 1, bool own=true);
61    ~alist();
62    void init(int num = 1, bool own=true);
63    void append(void *item);
64    void prepend(void *item);
65    void *remove(int index);
66    void *get(int index);
67    bool empty() const;
68    void *prev();
69    void *next();
70    void *first();
71    void *last();
72    void * operator [](int index) const;
73    int size() const;
74    void destroy();
75    void grow(int num);
76
77    /* Use it as a stack, pushing and poping from the end */
78    void push(void *item) { append(item); };
79    void *pop() { return remove(num_items-1); };
80 };
81
82 inline void * alist::operator [](int index) const {
83    if (index < 0 || index >= num_items) {
84       return NULL;
85    }
86    return items[index];
87 }
88
89 inline bool alist::empty() const
90 {
91    /* Check for null pointer */
92    return this ? num_items == 0 : true;
93 }
94
95 /*
96  * This allows us to do explicit initialization,
97  *   allowing us to mix C++ classes inside malloc'ed
98  *   C structures. Define before called in constructor.
99  */
100 inline void alist::init(int num, bool own) {
101    items = NULL;
102    num_items = 0;
103    max_items = 0;
104    num_grow = num;
105    own_items = own;
106 }
107
108 /* Constructor */
109 inline alist::alist(int num, bool own) {
110    init(num, own);
111 }
112
113 /* Destructor */
114 inline alist::~alist() {
115    destroy();
116 }
117
118
119
120 /* Current size of list */
121 inline int alist::size() const
122 {
123    /*
124     * Check for null pointer, which allows test
125     *  on size to succeed even if nothing put in
126     *  alist.
127     */
128    return this ? num_items : 0;
129 }
130
131 /* How much to grow by each time */
132 inline void alist::grow(int num)
133 {
134    num_grow = num;
135 }