]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/alist.h
03ab5e3e91e0b160f1bcedc9bf3615836b2e75ca
[bacula/bacula] / bacula / src / lib / alist.h
1 /*
2  *   Version $Id$
3  */
4
5 /*
6    Copyright (C) 2000-2003 Kern Sibbald and John Walker
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2 of
11    the License, or (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but 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 General Public
19    License along with this program; if not, write to the Free
20    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21    MA 02111-1307, USA.
22
23  */
24
25 /* 
26  * Array list -- much like a simplified STL vector
27  *   array of pointers to inserted items
28  */
29 class alist {
30    void **items;
31    int num_items;
32    int max_items;
33    int num_grow;
34    bool own_items;
35 public:
36    alist(int num = 1, bool own=true);
37    void init(int num = 1, bool own=true);
38    void append(void *item);
39    void *get(int index);
40    void * operator [](int index) const;
41    int size();
42    void destroy();
43    void grow(int num);
44    void * operator new(size_t);
45    void operator delete(void *);
46 };
47
48 inline void * alist::operator [](int index) const {
49    if (index < 0 || index >= num_items) {
50       return NULL;
51    }
52    return items[index];
53 }
54
55 /*                            
56  * This allows us to do explicit initialization,
57  *   allowing us to mix C++ classes inside malloc'ed
58  *   C structures. Define before called in constructor.
59  */
60 inline void alist::init(int num, bool own) {
61    items = NULL;
62    num_items = 0;
63    max_items = 0;
64    num_grow = num;
65    own_items = own;
66 }
67
68 /* Constructor */
69 inline alist::alist(int num, bool own) {
70    this->init(num, own);
71 }
72    
73
74
75 /* Current size of list */
76 inline int alist::size()
77 {
78    return num_items;
79 }
80
81 /* How much to grow by each time */
82 inline void alist::grow(int num) 
83 {
84    num_grow = num;
85 }
86
87 inline void * alist::operator new(size_t)
88 {
89    return malloc(sizeof(alist));
90 }
91
92 inline void alist::operator delete(void  *item)
93 {
94    ((alist *)item)->destroy();
95    free(item);
96 }