blob: 19e56bb7c45d31339c569148608cf6a5378c109b [file] [log] [blame]
Alexandre Lisionddd731e2014-01-31 11:50:08 -05001// 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 <ucommon-config.h>
40#include <commoncpp/config.h>
41#include <commoncpp/export.h>
42#include <commoncpp/thread.h>
43#include <commoncpp/object.h>
44
45using namespace COMMONCPP_NAMESPACE;
46
47LinkedSingle::~LinkedSingle() {}
48
49LinkedSingle *LinkedSingle::getFirst(void)
50{
51 return this;
52}
53
54LinkedSingle *LinkedSingle::getLast(void)
55{
56 LinkedSingle *obj = this;
57
58 while(obj->nextObject)
59 obj = obj->nextObject;
60
61 return obj;
62}
63
64void LinkedSingle::insert(LinkedSingle& obj)
65{
66 obj.nextObject = nextObject;
67 nextObject = &obj;
68}
69
70LinkedSingle &LinkedSingle::operator+=(LinkedSingle &obj)
71{
72 insert(obj);
73 return *this;
74}
75
76LinkedDouble::~LinkedDouble()
77{
78 detach();
79}
80
81void LinkedDouble::enterLock() {}
82
83void LinkedDouble::leaveLock() {}
84
85LinkedDouble *LinkedDouble::firstObject()
86{
87 LinkedDouble *node = this;
88
89 while(node->prevObject)
90 node = node->prevObject;
91
92 return node;
93}
94
95LinkedDouble *LinkedDouble::lastObject()
96{
97 LinkedDouble *node = this;
98
99 while(node->nextObject)
100 node = node->nextObject;
101
102 return node;
103}
104
105LinkedDouble *LinkedDouble::getFirst(void)
106{
107 LinkedDouble *node;
108
109 enterLock();
110 node = firstObject();
111 leaveLock();
112
113 return node;
114}
115
116LinkedDouble *LinkedDouble::getLast(void)
117{
118 LinkedDouble *node;
119
120 enterLock();
121 node = lastObject();
122 leaveLock();
123
124 return node;
125}
126
127LinkedDouble *LinkedDouble::getInsert(void)
128{
129 return getLast();
130}
131
132void LinkedDouble::insert(LinkedDouble& obj, InsertMode position)
133{
134 LinkedDouble *node;
135
136 enterLock();
137 obj.detach();
138
139 switch ( position ) {
140 case modeAtFirst:
141 node = firstObject();
142 obj.nextObject = node;
143 node->prevObject = &obj;
144 break;
145
146 case modeBefore:
147 obj.nextObject = this;
148 obj.prevObject = this->prevObject;
149 this->prevObject = &obj;
150 if (obj.prevObject)
151 obj.prevObject->nextObject = &obj;
152 break;
153
154 case modeAfter:
155 obj.nextObject = this->nextObject;
156 obj.prevObject = this;
157 this->nextObject = &obj;
158 if (obj.nextObject)
159 obj.nextObject->prevObject = &obj;
160 break;
161
162 case modeAtLast:
163 default:
164 node = lastObject();
165 obj.nextObject = node->nextObject;
166 obj.prevObject = node;
167 node->nextObject = &obj;
168 if(obj.nextObject)
169 obj.nextObject->prevObject = &obj;
170 break;
171 }
172 leaveLock();
173}
174
175void LinkedDouble::detach(void)
176{
177 enterLock();
178
179 if(prevObject)
180 prevObject->nextObject = nextObject;
181
182 if(nextObject)
183 nextObject->prevObject = prevObject;
184
185 prevObject = NULL;
186 nextObject = NULL;
187
188 leaveLock();
189}
190
191LinkedDouble &LinkedDouble::operator+=(LinkedDouble &obj)
192{
193 insert(obj);
194 return *this;
195}
196
197LinkedDouble &LinkedDouble::operator--()
198{
199 detach();
200 return *this;
201}
202