]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/dlist.h
Use the command line utility dropdb instead of the psql command
[bacula/bacula] / bacula / src / lib / dlist.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  * 
27  *   Doubly linked list  -- dlist
28  *
29  */
30
31 /* In case you want to specifically specify the offset to the link */
32 #define OFFSET(item, link) ((char *)(link) - (char *)(item))
33
34 /*
35  * Loop var through each member of list
36  */
37 #define foreach_dlist(var, list) \
38         for((var)=NULL; (((void *)(var))=(list)->next(var)); )
39
40 struct dlink {
41    void *next;
42    void *prev;
43 };
44
45 class dlist {
46    void *head;
47    void *tail;
48    int loffset;
49    int num_items;
50 public:
51    dlist(void *item, void *link);
52    void init(void *item, void *link);
53    void prepend(void *item);
54    void append(void *item);
55    void insert_before(void *item, void *where);
56    void insert_after(void *item, void *where);
57    void remove(void *item);
58    bool empty();
59    int  size();
60    void *next(void *item);
61    void *prev(void *item);
62    void destroy();
63    void *first();
64    void *last();
65    void * operator new(size_t);
66    void operator delete(void *);
67 };
68
69 /*                            
70  * This allows us to do explicit initialization,
71  *   allowing us to mix C++ classes inside malloc'ed
72  *   C structures. Define before called in constructor.
73  */
74 inline void dlist::init(void *item, void *link) 
75 {
76    head = tail = NULL;
77    loffset = (char *)link - (char *)item;
78    num_items = 0;
79 }
80
81 /* Constructor */
82 inline dlist::dlist(void *item, void *link)
83 {
84    this->init(item, link);
85 }
86
87 inline bool dlist::empty()
88 {
89    return head == NULL;
90 }
91
92 inline int dlist::size()
93 {
94    return num_items;
95 }
96
97    
98 inline void * dlist::operator new(size_t)
99 {
100    return malloc(sizeof(dlist));
101 }
102
103 inline void dlist::operator delete(void  *item)
104 {
105    ((dlist *)item)->destroy();
106    free(item);
107 }
108  
109
110 inline void * dlist::first()
111 {
112    return head;
113 }
114
115 inline void * dlist::last()
116 {
117    return tail;
118 }