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