]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/alist.h
15Jan06
[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 #define foreach_alist(var, list) \
30     for((*((void **)&(var))=(void*)((list)->first())); \
31          (var); \
32          (*((void **)&(var))=(void*)((list)->next())))
33
34 #ifdef the_easy_way
35 #define foreach_alist(var, list) \
36         for(((void*)(var))=(list)->first(); (var); ((void*)(var))=(list)->next()); )
37 #endif
38
39
40 /* Second arg of init */
41 enum {
42   owned_by_alist = true,
43   not_owned_by_alist = false
44 };
45
46 /*
47  * Array list -- much like a simplified STL vector
48  *   array of pointers to inserted items
49  */
50 class alist : public SMARTALLOC {
51    void **items;
52    int num_items;
53    int max_items;
54    int num_grow;
55    int cur_item;
56    bool own_items;
57    void grow_list(void);
58 public:
59    alist(int num = 1, bool own=true);
60    ~alist();
61    void init(int num = 1, bool own=true);
62    void append(void *item);
63    void prepend(void *item);
64    void *remove(int index);
65    void *get(int index);
66    bool empty() const;
67    void *prev();
68    void *next();
69    void *first();
70    void *last();
71    void * operator [](int index) const;
72    int size() const;
73    void destroy();
74    void grow(int num);
75
76    /* Use it as a stack, pushing and poping from the end */
77    void push(void *item) { append(item); };
78    void *pop() { return remove(num_items-1); };
79 };
80
81 inline void * alist::operator [](int index) const {
82    if (index < 0 || index >= num_items) {
83       return NULL;
84    }
85    return items[index];
86 }
87
88 inline bool alist::empty() const
89 {
90    /* Check for null pointer */
91    return this ? num_items == 0 : true;
92 }
93
94 /*
95  * This allows us to do explicit initialization,
96  *   allowing us to mix C++ classes inside malloc'ed
97  *   C structures. Define before called in constructor.
98  */
99 inline void alist::init(int num, bool own) {
100    items = NULL;
101    num_items = 0;
102    max_items = 0;
103    num_grow = num;
104    own_items = own;
105 }
106
107 /* Constructor */
108 inline alist::alist(int num, bool own) {
109    init(num, own);
110 }
111
112 /* Destructor */
113 inline alist::~alist() {
114    destroy();
115 }
116
117
118
119 /* Current size of list */
120 inline int alist::size() const
121 {
122    /*
123     * Check for null pointer, which allows test
124     *  on size to succeed even if nothing put in
125     *  alist.
126     */
127    return this ? num_items : 0;
128 }
129
130 /* How much to grow by each time */
131 inline void alist::grow(int num)
132 {
133    num_grow = num;
134 }