InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SimpleLink.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Steve Pellegrin
6 //
7 // $Author$
8 //
9 // $DateTime$
10 //
11 // $Revision$
12 //
13 // $Change$
14 //
15 // Copyright 1997-2010 Adobe Systems Incorporated. All rights reserved.
16 //
17 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance
18 // with the terms of the Adobe license agreement accompanying it. If you have received
19 // this file from a source other than Adobe, then your use, modification, or
20 // distribution of it requires the prior written permission of Adobe.
21 //
22 //========================================================================================
23 
24 #if !defined(__SimpleLink__)
25 #define __SimpleLink__
26 
27 // ----- Includes -----
28 
29 #include "K2Debugging.h"
30 
31 //========================================================================================
32 // Class SimpleLink
33 // Used to construct bi-directional queues that can be used as lists.
34 //========================================================================================
35 
36 template<class T>
38 {
39 public:
40  typedef object_type data_type;
41 
42 public:
43  SimpleLink()
44  : fParent(nil)
45  {Empty();}
46  SimpleLink(const SimpleLink<T> &other)
47  {Copy(other);}
48 
49  ~SimpleLink()
50  {}
51 
52  void LinkBefore(SimpleLink<T> *next);
53  void LinkAfter(SimpleLink<T> *prev);
54  void Unlink();
55 
56  void SetParent(T *parent)
57  {fParent = parent;}
58  T *GetParent()
59  {return fParent;}
60  const T *GetParent() const
61  {return fParent;}
62 
63  SimpleLink<T> *Previous() const
64  {return fPrev;}
65  SimpleLink<T> *Next() const
66  {return fNext;}
67 
68  void Empty()
69  {fNext = fPrev = this;}
70  bool IsEmpty() const
71  {return (fNext == this);}
72 
73  SimpleLink<T> &operator=(const SimpleLink<T> &other)
74  {Copy(other); return *this;}
75 
76 public:
77  // Class methods
78  static uint32 ListSize(SimpleLink<T> *head);
79  static SimpleLink<T> *FindPrevious(SimpleLink<T> *startLink, uint32 count);
80  static void Detach(SimpleLink<T> *first, SimpleLink<T> *last);
81  static void AttachAfter(SimpleLink<T> *head, SimpleLink<T> *first, SimpleLink<T> *last);
82 
83 private:
84  void Copy(const SimpleLink<T> &other);
85 
86 private:
87  SimpleLink<T> * fPrev;
88  SimpleLink<T> * fNext;
89  T * fParent;
90 };
91 
92 
93 //========================================================================================
94 // LinkBefore
95 //========================================================================================
96 template<class T>
98 (
99  SimpleLink<T> * next
100 )
101 {
102  ASSERT_MSG(IsEmpty(), "SimpleLink::LinkBefore() - Already linked!");
103 
104  fNext = next;
105  fPrev = next->fPrev;
106  fNext->fPrev = fPrev->fNext = this;
107 }
108 
109 
110 //========================================================================================
111 // LinkAfter
112 //========================================================================================
113 template<class T>
115 (
116  SimpleLink<T> * prev
117 )
118 {
119  ASSERT_MSG(IsEmpty(), "SimpleLink::LinkAfter() - Already linked!");
120 
121  fPrev = prev;
122  fNext = prev->fNext;
123  fNext->fPrev = fPrev->fNext = this;
124 }
125 
126 
127 //========================================================================================
128 // Unlink
129 //========================================================================================
130 template<class T>
132 {
133  fNext->fPrev = fPrev;
134  fPrev->fNext = fNext;
135  Empty();
136 }
137 
138 
139 //========================================================================================
140 // Copy
141 //========================================================================================
142 template<class T>
144 (
145  const SimpleLink<T> & other
146 )
147 {
148  // If the other link is empty, make this one empty too.
149  // Otherwise, use the other link's pointers.
150  if (other.IsEmpty())
151  {
152  this->Empty();
153  }
154  else
155  {
156  // Hmmm... I wonder whether this is a good thing?
157  fNext = other.fNext;
158  fPrev = other.fPrev;
159  }
160  fParent = other.fParent;
161 }
162 
163 
164 //========================================================================================
165 // ListSize (Class method)
166 //========================================================================================
167 template<class T>
169 (
170  SimpleLink<T> * head
171 )
172 {
173  uint32 count = 0;
174  SimpleLink<T> *link = head->Next();
175  while (link != head)
176  {
177  count++;
178  link = link->Next();
179  }
180 
181  return count;
182 }
183 
184 
185 //========================================================================================
186 // FindPrevious (Class method)
187 //========================================================================================
188 template<class T>
190 (
191  SimpleLink<T> * startLink,
192  uint32 count
193 )
194 {
195  SimpleLink<T> *endLink = startLink;
196  while (count--)
197  {
198  endLink = endLink->Previous();
199  }
200 
201  return endLink;
202 }
203 
204 
205 //========================================================================================
206 // Detach (Class method)
207 //========================================================================================
208 template<class T>
210 (
211  SimpleLink<T> * first,
212  SimpleLink<T> * last
213 )
214 {
215  first->fPrev->fNext = last->fNext;
216  last->fNext->fPrev = first->fPrev;
217 }
218 
219 
220 //========================================================================================
221 // AttachAfter (Class method)
222 //========================================================================================
223 template<class T>
225 (
226  SimpleLink<T> * head,
227  SimpleLink<T> * first,
228  SimpleLink<T> * last
229 )
230 {
231  last->fNext = head->fNext;
232  last->fNext->fPrev = last;
233  first->fPrev = head;
234  head->fNext = first;
235 }
236 
237 #endif // __SimpleLink__