|
0
|
1 #ifndef CYGONCE_KERNEL_LLISTT_HXX |
|
|
2 #define CYGONCE_KERNEL_LLISTT_HXX |
|
|
3 |
|
|
4 //========================================================================== |
|
|
5 // |
|
2
|
6 // llistt.hxx |
|
0
|
7 // |
|
2
|
8 // Llistt linked list template class declarations |
|
0
|
9 // |
|
|
10 //========================================================================== |
|
|
11 //####COPYRIGHTBEGIN#### |
|
|
12 // |
|
|
13 // ------------------------------------------- |
|
|
14 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
15 // Version 1.0 (the "License"); you may not use this file except in |
|
|
16 // compliance with the License. You may obtain a copy of the License at |
|
|
17 // http://sourceware.cygnus.com/ecos |
|
|
18 // |
|
|
19 // Software distributed under the License is distributed on an "AS IS" |
|
|
20 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
21 // License for the specific language governing rights and limitations under |
|
|
22 // the License. |
|
|
23 // |
|
|
24 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
25 // September 30, 1998. |
|
|
26 // |
|
|
27 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
28 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
29 // ------------------------------------------- |
|
|
30 // |
|
|
31 //####COPYRIGHTEND#### |
|
|
32 //========================================================================== |
|
|
33 //#####DESCRIPTIONBEGIN#### |
|
|
34 // |
|
2
|
35 // Author(s): hmt |
|
|
36 // Contributors: hmt |
|
|
37 // Date: 1998-02-10 |
|
|
38 // Purpose: Define Llistt template class |
|
|
39 // Description: The classes defined here provide the APIs for llistts. |
|
|
40 // Usage: #include <cyg/kernel/llistt.hxx> |
|
|
41 // |
|
0
|
42 // |
|
|
43 //####DESCRIPTIONEND#### |
|
|
44 // |
|
|
45 //========================================================================== |
|
|
46 |
|
|
47 #include <cyg/kernel/ktypes.h> |
|
|
48 #include <cyg/infra/cyg_ass.h> // assertion macros |
|
|
49 #include <cyg/kernel/thread.hxx> |
|
|
50 |
|
|
51 // ------------------------------------------------------------------------- |
|
|
52 // A simple linked list template; each item also contains a pointer of type |
|
|
53 // T, and you can search for a particular T* in a list. |
|
|
54 // |
|
|
55 // It is intended that this list be amenable to the trick of using the |
|
|
56 // address of the pointer that is the list head, cast to an item pointer, |
|
|
57 // as the "zeroth" element of the list; prev of the first item is the |
|
|
58 // address of the head pointer, and inserting before the first item works |
|
|
59 // correctly. For this reason, a "getprev" is not provided; iteration may |
|
|
60 // only be forwards, until a NULL is found. |
|
|
61 // |
|
|
62 // It is expected that derived classes will be used to hold other |
|
|
63 // information than just the T* but that is beyond our discussion here; |
|
|
64 // only the T* can be searched for using code provided here. |
|
|
65 // |
|
|
66 // This module is NOT thread-safe; it is expected that all clients will be |
|
|
67 // seeing that that themselves. |
|
|
68 |
|
|
69 template <class T> |
|
|
70 class Cyg_Llistt |
|
|
71 { |
|
|
72 private: |
|
|
73 Cyg_Llistt<T> *next, *prev; |
|
|
74 T *tptr; |
|
|
75 |
|
|
76 private: |
|
|
77 // make initialisation _without_ a T* impossible. |
|
|
78 Cyg_Llistt<T> &operator=(Cyg_Llistt<T> &); |
|
|
79 Cyg_Llistt(Cyg_Llistt<T> &); |
|
|
80 Cyg_Llistt(); |
|
|
81 |
|
|
82 public: |
|
|
83 |
|
|
84 CYGDBG_DEFINE_CHECK_THIS |
|
|
85 |
|
|
86 Cyg_Llistt( T *tvalue ) // Constructor |
|
|
87 { |
|
|
88 tptr = tvalue; |
|
|
89 next = prev = NULL; |
|
|
90 } |
|
|
91 |
|
|
92 ~Cyg_Llistt() // Destructor |
|
|
93 { |
|
|
94 CYG_ASSERT( NULL == next, "bad item next - still in list" ); |
|
|
95 CYG_ASSERT( NULL == prev, "bad item prev - still in list" ); |
|
|
96 } |
|
|
97 |
|
|
98 // iterator, basically. |
|
|
99 Cyg_Llistt<T> * getnext() { return next; } |
|
|
100 |
|
|
101 // get the value |
|
|
102 T * getitem() { return tptr; } |
|
|
103 |
|
|
104 // look up a particular T value in the llist |
|
|
105 static Cyg_Llistt<T> * |
|
|
106 find( Cyg_Llistt<T> *list, T *tvalue ) |
|
|
107 { |
|
|
108 for ( ; list ; list = list->next ) { |
|
|
109 if ( list->tptr == tvalue ) |
|
|
110 break; |
|
|
111 } |
|
|
112 return list; |
|
|
113 } |
|
|
114 |
|
|
115 // unlink an item from the list |
|
|
116 void |
|
|
117 unlink() |
|
|
118 { |
|
|
119 CYG_ASSERT( prev, "not in a list" ); |
|
|
120 prev->next = next; |
|
|
121 if ( next ) { |
|
|
122 next->prev = prev; |
|
|
123 } |
|
|
124 next = prev = NULL; |
|
|
125 } |
|
|
126 |
|
|
127 // insert a new item in the list after "this" |
|
|
128 void |
|
|
129 insertafter( Cyg_Llistt<T> *item ) |
|
|
130 { |
|
|
131 CYG_ASSERT( item, "null item" ); |
|
|
132 CYG_ASSERT( NULL == item->next, "bad item next - already linked" ); |
|
|
133 CYG_ASSERT( NULL == item->prev, "bad item prev - already linked" ); |
|
|
134 item->next = next; |
|
|
135 item->prev = this; |
|
|
136 if ( next ) |
|
|
137 next->prev = item; |
|
|
138 next = item; |
|
|
139 } |
|
|
140 |
|
|
141 // insert a new item in the list before "this" |
|
|
142 void |
|
|
143 insertbefore( Cyg_Llistt<T> *item ) |
|
|
144 { |
|
|
145 CYG_ASSERT( prev, "this not in a list" ); |
|
|
146 CYG_ASSERT( item, "null item" ); |
|
|
147 CYG_ASSERT( NULL == item->next, "bad item next - already linked" ); |
|
|
148 CYG_ASSERT( NULL == item->prev, "bad item prev - already linked" ); |
|
|
149 item->prev = prev; |
|
|
150 item->next = this; |
|
|
151 prev->next = item; |
|
|
152 prev = item; |
|
|
153 } |
|
|
154 }; |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 // ------------------------------------------------------------------------- |
|
|
159 #endif // ifndef CYGONCE_KERNEL_LLISTT_HXX |
|
|
160 // EOF llistt.hxx |