]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/alist.h
This commit was manufactured by cvs2svn to create tag
[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    Kern Sibbald, June MMIII
24
25  */
26
27 /* 
28  * Array list -- much like a simplified STL vector
29  *   array of pointers to inserted items
30  */
31 class alist {
32    void **items;
33    int num_items;
34    int max_items;
35    int num_grow;
36    bool own_items;
37 public:
38    alist(int num = 1, bool own=true);
39    void init(int num = 1, bool own=true);
40    void append(void *item);
41    void *get(int index);
42    void * operator [](int index) const;
43    int size();
44    void destroy();
45    void grow(int num);
46    void * operator new(size_t);
47    void operator delete(void *);
48 };
49
50 inline void * alist::operator [](int index) const {
51    if (index < 0 || index >= num_items) {
52       return NULL;
53    }
54    return items[index];
55 }
56
57 /*                            
58  * This allows us to do explicit initialization,
59  *   allowing us to mix C++ classes inside malloc'ed
60  *   C structures. Define before called in constructor.
61  */
62 inline void alist::init(int num, bool own) {
63    items = NULL;
64    num_items = 0;
65    max_items = 0;
66    num_grow = num;
67    own_items = own;
68 }
69
70 /* Constructor */
71 inline alist::alist(int num, bool own) {
72    this->init(num, own);
73 }
74    
75
76
77 /* Current size of list */
78 inline int alist::size()
79 {
80    return num_items;
81 }
82
83 /* How much to grow by each time */
84 inline void alist::grow(int num) 
85 {
86    num_grow = num;
87 }
88
89 inline void * alist::operator new(size_t)
90 {
91    return malloc(sizeof(alist));
92 }
93
94 inline void alist::operator delete(void  *item)
95 {
96    ((alist *)item)->destroy();
97    free(item);
98 }