1 // File: LH3D_DeclareSequence.hxx
2 // Created: 29.01.01 15:36:46
3 // Author: Alexander GRIGORIEV
4 // Copyright: OpenCascade 2001
6 #ifndef _Sequence_Declare_HeaderFile
7 #define _Sequence_Declare_HeaderFile
9 #ifndef _Standard_Macro_HeaderFile
10 #include <Standard_Macro.hxx>
13 // Declaration of Sequence (numbered list) class.
14 // Remarks on the current implementation:
16 // 1. Methods First() and Last() added
17 // 2. The method InsertAt(anIndex) replaces InsertBefore and InsertAfter.
18 // This method never throws exception "OutOfRange". Its behaviour:
19 // anIndex <= 1 => equivalent to Prepend()
20 // anIndex > Length() => equivalent to Append()
21 // else => equivalent to InsertBefore.
23 // *******************************************************************
24 // use the following somewhere in a header file;
25 // ClassName - name of the list class to create
26 // Type - type of members of the list
27 // *******************************************************************
29 #define DECLARE_SEQUENCE( ClassName, Type ) \
33 inline ClassName (); \
34 inline ClassName (const ClassName& anOther); \
35 inline ClassName& operator= (const ClassName& anOther); \
36 inline Standard_Integer Length () const; \
37 inline const Type& First () const; \
38 inline const Type& Last () const; \
39 inline const Type& Value (const Standard_Integer)const;\
40 inline Type& ChangeValue (const Standard_Integer); \
41 inline const Type& operator () (const Standard_Integer)const;\
42 inline Type& operator () (const Standard_Integer); \
44 Standard_EXPORT virtual ~ClassName (); \
45 Standard_EXPORT void Append (const Type& aVal); \
46 Standard_EXPORT void Prepend (const Type& aVal); \
47 Standard_EXPORT void InsertAt (const Standard_Integer, \
49 Standard_EXPORT void Clear (); \
50 Standard_EXPORT void Remove (const Standard_Integer); \
59 Node (const Type& aValue, Node * aPrv, Node * aNxt) \
60 : myValue (aValue), myPrev (aPrv), myNext (aNxt) {} \
61 const Type& Value () const { return myValue; } \
62 Type& ChangeValue () { return myValue; } \
63 friend class ClassName; \
66 Standard_EXPORT const void * FindItem (const Standard_Integer)const;\
67 Standard_EXPORT void Assign (const ClassName& anOther); \
72 Standard_Integer myICur; \
73 Standard_Integer myLength; \
76 inline ClassName::ClassName () : \
83 inline ClassName::ClassName (const ClassName& anOther) : \
89 inline ClassName& ClassName::operator= (const ClassName& anOther) \
95 inline Standard_Integer ClassName::Length () const{ \
99 inline const Type& ClassName::First () const \
101 return myFirst -> Value (); /* exception if out of range */ \
104 inline const Type& ClassName::Last () const \
106 return myLast -> Value(); /* exception if out of range */ \
109 inline const Type& ClassName::Value (const Standard_Integer anI) const \
111 const Node * anItem = (const Node *) FindItem (anI); \
112 return anItem -> Value (); /* exception if out of range */ \
115 inline Type& ClassName::ChangeValue (const Standard_Integer anI) \
117 Node * anItem = (Node *) FindItem (anI); \
118 return anItem -> ChangeValue (); /* exception if out of range */ \
121 inline const Type& ClassName::operator() (const Standard_Integer anI) const \
123 return Value (anI); \
126 inline Type& ClassName::operator() (const Standard_Integer anI) \
128 return ChangeValue (anI); \
131 // *******************************************************************
132 // use the following in a translation unit (*.cxx);
134 // *******************************************************************
135 #define IMPLEMENT_SEQUENCE( ClassName, Type ) \
136 const void * ClassName::FindItem (const Standard_Integer anI) const \
138 if (anI < 1 || anI > myLength) return NULL; \
139 Standard_Integer aCounter; \
140 Node * aCurrent = (Node *) myCurrent; \
141 Standard_Boolean aDir (Standard_False); \
142 if (aCurrent == NULL) { \
143 aCurrent = myFirst; \
144 aCounter = anI - 1; \
145 aDir = Standard_True; \
147 aCounter = Abs (anI - myICur); \
148 if (anI <= aCounter) { \
149 aCurrent = myFirst; \
150 aCounter = anI - 1; \
151 aDir = Standard_True; \
152 }else if (myLength - anI < aCounter) { \
154 aCounter = myLength - anI; \
155 }else if (anI > myICur) \
156 aDir = Standard_True; \
159 while (aCounter--) aCurrent = aCurrent -> myNext; \
161 while (aCounter--) aCurrent = aCurrent -> myPrev; \
162 (Standard_Integer&) myICur = anI; \
163 (Node *&) myCurrent = aCurrent; \
167 ClassName::~ClassName () \
172 void ClassName::Append (const Type& aVal) \
174 Node * anItem = new Node (aVal, myLast, NULL); \
178 myLast -> myNext = anItem; \
183 void ClassName::Prepend (const Type& aVal) \
185 Node * anItem = new Node (aVal, NULL, myFirst); \
189 myFirst -> myPrev = anItem; \
192 if (myICur > 0) myICur++; \
195 void ClassName::InsertAt (const Standard_Integer anI, const Type& aVal) \
197 if (anI <= 1) Prepend (aVal); \
198 else if (anI > myLength) Append (aVal); \
199 else if (FindItem(anI)) { \
200 Node * anItem = new Node (aVal, myCurrent -> myPrev, myCurrent); \
201 myCurrent -> myPrev = anItem; \
202 if (anItem -> myPrev) anItem -> myPrev -> myNext = anItem; \
208 void ClassName::Clear () \
211 Node * aCurr = myFirst -> myNext; \
215 myFirst = myLast = myCurrent = NULL; \
220 void ClassName::Remove (const Standard_Integer anI) \
222 Node * anItem = (Node *) FindItem (anI); \
224 if (myCurrent -> myPrev) { \
225 myCurrent -> myPrev -> myNext = myCurrent -> myNext; \
227 if (myCurrent -> myNext) { \
228 myCurrent -> myNext -> myPrev = myCurrent -> myPrev; \
229 myCurrent = myCurrent -> myNext; \
231 myCurrent = myCurrent -> myPrev; \
234 if (myFirst == anItem) myFirst = myFirst -> myNext; \
235 if (myLast == anItem) myLast = myLast -> myPrev; \
241 void ClassName::Assign (const ClassName& anOther) \
244 if (anOther.Length () == 0) return; \
245 myFirst = new Node (anOther.First(), NULL, NULL); \
246 Node * aPrevious = myFirst; \
248 while (myLength < anOther.Length()) { \
250 Node * aCurrent = new Node (anOther.Value(myLength), aPrevious, NULL); \
251 aPrevious = aPrevious -> myNext = aCurrent; \
253 myLast = aPrevious; \