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