blob: da938a090a22ffe5a99cf014d3c04279c061a872 [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001// Copyright (C) 2004-2005 Open Source Telecom Corporation.
2// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3//
4// This program is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation; either version 2 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17//
18// As a special exception, you may use this file as part of a free software
19// library without restriction. Specifically, if other files instantiate
20// templates or use macros or inline functions from this file, or you compile
21// this file and link it with other files to produce an executable, this
22// file does not by itself cause the resulting executable to be covered by
23// the GNU General Public License. This exception does not however
24// invalidate any other reasons why the executable file might be covered by
25// the GNU General Public License.
26//
27// This exception applies only to the code released under the name GNU
28// Common C++. If you copy code from other releases into a copy of GNU
29// Common C++, as the General Public License permits, the exception does
30// not apply to the code that you add in this way. To avoid misleading
31// anyone as to the status of such modified files, you must delete
32// this exception notice from them.
33//
34// If you write modifications of your own for GNU Common C++, it is your choice
35// whether to permit this exception to apply to your modifications.
36// If you do not wish that, delete this exception notice.
37//
38
39#include <cc++/config.h>
40#include <cc++/export.h>
41#include <cc++/thread.h>
42#include <cc++/object.h>
43#include "private.h"
44
45#ifdef CCXX_NAMESPACES
46namespace ost {
47#endif
48
49LinkedSingle::~LinkedSingle() {}
50
51LinkedSingle *LinkedSingle::getFirst(void)
52{
53 return this;
54}
55
56LinkedSingle *LinkedSingle::getLast(void)
57{
58 LinkedSingle *obj = this;
59
60 while(obj->nextObject)
61 obj = obj->nextObject;
62
63 return obj;
64}
65
66void LinkedSingle::insert(LinkedSingle& obj)
67{
68 obj.nextObject = nextObject;
69 nextObject = &obj;
70}
71
72LinkedSingle &LinkedSingle::operator+=(LinkedSingle &obj)
73{
74 insert(obj);
75 return *this;
76}
77
78LinkedDouble::~LinkedDouble()
79{
80 detach();
81}
82
83void LinkedDouble::enterLock() {}
84
85void LinkedDouble::leaveLock() {}
86
87LinkedDouble *LinkedDouble::firstObject()
88{
89 LinkedDouble *node = this;
90
91 while(node->prevObject)
92 node = node->prevObject;
93
94 return node;
95}
96
97LinkedDouble *LinkedDouble::lastObject()
98{
99 LinkedDouble *node = this;
100
101 while(node->nextObject)
102 node = node->nextObject;
103
104 return node;
105}
106
107LinkedDouble *LinkedDouble::getFirst(void)
108{
109 LinkedDouble *node;
110
111 enterLock();
112 node = firstObject();
113 leaveLock();
114
115 return node;
116}
117
118LinkedDouble *LinkedDouble::getLast(void)
119{
120 LinkedDouble *node;
121
122 enterLock();
123 node = lastObject();
124 leaveLock();
125
126 return node;
127}
128
129LinkedDouble *LinkedDouble::getInsert(void)
130{
131 return getLast();
132}
133
134void LinkedDouble::insert(LinkedDouble& obj, InsertMode position)
135{
136 LinkedDouble *node;
137
138 enterLock();
139 obj.detach();
140
141 switch ( position ) {
142 case modeAtFirst:
143 node = firstObject();
144 obj.nextObject = node;
145 node->prevObject = &obj;
146 break;
147
148 case modeBefore:
149 obj.nextObject = this;
150 obj.prevObject = this->prevObject;
151 this->prevObject = &obj;
152 if (obj.prevObject)
153 obj.prevObject->nextObject = &obj;
154 break;
155
156 case modeAfter:
157 obj.nextObject = this->nextObject;
158 obj.prevObject = this;
159 this->nextObject = &obj;
160 if (obj.nextObject)
161 obj.nextObject->prevObject = &obj;
162 break;
163
164 case modeAtLast:
165 default:
166 node = lastObject();
167 obj.nextObject = node->nextObject;
168 obj.prevObject = node;
169 node->nextObject = &obj;
170 if(obj.nextObject)
171 obj.nextObject->prevObject = &obj;
172 break;
173 }
174 leaveLock();
175}
176
177void LinkedDouble::detach(void)
178{
179 enterLock();
180
181 if(prevObject)
182 prevObject->nextObject = nextObject;
183
184 if(nextObject)
185 nextObject->prevObject = prevObject;
186
187 prevObject = NULL;
188 nextObject = NULL;
189
190 leaveLock();
191}
192
193LinkedDouble &LinkedDouble::operator+=(LinkedDouble &obj)
194{
195 insert(obj);
196 return *this;
197}
198
199LinkedDouble &LinkedDouble::operator--()
200{
201 detach();
202 return *this;
203}
204
205#ifdef CCXX_NAMESPACES
206}
207#endif
208
209/** EMACS **
210 * Local variables:
211 * mode: c++
212 * c-basic-offset: 4
213 * End:
214 */