]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/cxx/lib/tl/include/hlist
update
[l4.git] / l4 / pkg / cxx / lib / tl / include / hlist
1 // vi:ft=cpp
2 /*
3  * (c) 2011 Alexander Warg <warg@os.inf.tu-dresden.de>
4  *     economic rights: Technische Universität Dresden (Germany)
5  *
6  * This file is part of TUD:OS and distributed under the terms of the
7  * GNU General Public License 2.
8  * Please see the COPYING-GPL-2 file for details.
9  *
10  * As a special exception, you may use this file as part of a free software
11  * library without restriction.  Specifically, if other files instantiate
12  * templates or use macros or inline functions from this file, or you compile
13  * this file and link it with other files to produce an executable, this
14  * file does not by itself cause the resulting executable to be covered by
15  * the GNU General Public License.  This exception does not however
16  * invalidate any other reasons why the executable file might be covered by
17  * the GNU General Public License.
18  */
19
20 #pragma once
21
22 namespace cxx {
23
24 template< typename T > class H_list;
25
26 template< typename T >
27 class H_list_item
28 {
29 private:
30   friend class H_list<T>;
31
32   H_list_item *_n, **_pn;
33
34 public:
35   H_list_item() : _n(0), _pn(0) {}
36
37   T *l_next() const { return static_cast<T*>(_n); }
38   void l_remove()
39   {
40     if (!_pn)
41       return;
42
43     *_pn = _n;
44     if (_n) 
45       _n->_pn = _pn;
46
47     _pn = 0;
48   }
49
50   bool l_in_list() const { return _pn; }
51
52   ~H_list_item() { l_remove(); }
53 };
54
55 template< typename T >
56 class H_list
57 {
58 private:
59   H_list_item<T> *_f;
60
61 public:
62   typedef T Item;
63
64   H_list() : _f(0) {}
65
66   T *first() const { return static_cast<T*>(_f); }
67   static T *next(T *c) { return c->l_next(); }
68   void add(T *e)
69   {
70     if (_f)
71       _f->_pn = &e->_n;
72     e->_n = _f;
73     e->_pn = &_f;
74     _f = e;
75   }
76 };
77
78 }