0026377: Passing Handle objects as arguments to functions as non-const reference...
[occt.git] / src / Interface / Interface_EntityIterator.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #include <Interface_EntityIterator.hxx>
16 #include <Interface_IntVal.hxx>
17 #include <Standard_NoMoreObject.hxx>
18 #include <Standard_NoSuchObject.hxx>
19 #include <Standard_Transient.hxx>
20
21 //  Iterateur pour ecriture for, ou while avec Next en fin :
22 //  for (creer iterateur; iter.More(); iter.Next()) { val = iter.Value(); ... }
23 // .... Definitions initiales : en particulier celles requises pour
24 //      les outils de graphe (construction avec le graphe, avec un vertex)
25 Interface_EntityIterator::Interface_EntityIterator ()
26 {
27 //  thecurr = new Interface_IntVal;
28 //  thecurr->CValue() = 0;
29 //  thelist = new TColStd_HSequenceOfTransient();  // constructeur vide
30 //  thelist sera construit au premier Add (quelquefois, il nyena pas)
31 }
32
33     Interface_EntityIterator::Interface_EntityIterator
34   (const Handle(TColStd_HSequenceOfTransient)& list)
35 {
36   thecurr = new Interface_IntVal;
37   thecurr->CValue() = 0;
38   thelist = list;
39 }
40
41     void Interface_EntityIterator::AddList
42   (const Handle(TColStd_HSequenceOfTransient)& list)
43 {
44   if (thelist.IsNull()) thelist = new TColStd_HSequenceOfTransient();
45   if (thecurr.IsNull()) thecurr = new Interface_IntVal;
46   thecurr->CValue() = 0;
47   thelist->Append(list);
48 }
49
50     void Interface_EntityIterator::AddItem
51   (const Handle(Standard_Transient)& anentity)
52 {
53   if (anentity.IsNull()) return;
54   if (thecurr.IsNull()) thecurr = new Interface_IntVal;
55   if (thelist.IsNull()) thelist = new TColStd_HSequenceOfTransient();
56   thecurr->CValue() = 0;
57   thelist->Append(anentity);
58 }
59
60     void Interface_EntityIterator::GetOneItem
61   (const Handle(Standard_Transient)& anentity)
62       {  AddItem(anentity);  }
63
64     void Interface_EntityIterator::Reset ()
65 {
66   if (thecurr.IsNull()) thecurr = new Interface_IntVal;
67   thecurr->CValue() = 0;
68   thelist = new TColStd_HSequenceOfTransient();
69 }
70
71
72 // .... Fonctionnalites de tri prealable a l'iteration ....
73
74 //  Facon "bete" : supprimer les termes qui ne conviennent pas : lent !
75 //  Mieux vaut refaire une autre sequence a cote
76
77     void Interface_EntityIterator::SelectType
78   (const Handle(Standard_Type)& atype, const Standard_Boolean keep)
79 {
80   if (thelist.IsNull()) return;
81   Standard_Integer i, n = thelist->Length();
82   Handle(TColStd_HSequenceOfTransient) nlist = new TColStd_HSequenceOfTransient();
83   for (i = 1 ; i <= n ; i ++) {
84     if (thelist->Value(i)->IsKind(atype) == keep) nlist->Append(thelist->Value(i));
85   }
86   thelist = nlist;
87 }
88
89 //  ....  Iteration proprement dite  ....
90
91     Standard_Integer Interface_EntityIterator::NbEntities () const
92 {
93   if (thelist.IsNull()) return 0;
94   if (!thecurr.IsNull() && thecurr->Value() == 0) Start();
95   return thelist->Length();
96 }
97
98     Standard_Integer Interface_EntityIterator::NbTyped
99   (const Handle(Standard_Type)& atype) const
100 {
101   Standard_Integer res = 0;
102   if (thelist.IsNull()) return res;
103   Standard_Integer i, n = thelist->Length();
104   for (i = 1 ; i <= n ; i ++) {
105     if (thelist->Value(i)->IsKind(atype)) res ++;
106   }
107   return res;
108 }
109
110     Interface_EntityIterator  Interface_EntityIterator::Typed
111   (const Handle(Standard_Type)& atype) const
112 {
113   Interface_EntityIterator res;
114   if (thelist.IsNull()) return res;
115   Standard_Integer i, n = thelist->Length();
116   for (i = 1 ; i <= n ; i ++) {
117     if (thelist->Value(i)->IsKind(atype)) res.AddItem (thelist->Value(i));
118   }
119   return res;
120 }
121
122
123     void Interface_EntityIterator::Start () const
124       {    if (!thecurr.IsNull()) thecurr->CValue() = 1 ;  }     // peut etre redefini ...
125
126     Standard_Boolean Interface_EntityIterator::More () const
127 {
128   if (thecurr.IsNull()) return Standard_False;
129   if (thecurr->Value() == 0) Start();  // preparation de l iteration
130   if (thelist.IsNull()) return Standard_False;
131   return (thecurr->Value() <= thelist->Length());
132 }
133
134     void Interface_EntityIterator::Next () const
135       {    thecurr->CValue() ++;  }    // Next ne verifie rien : soin laisse a Value
136
137     const Handle(Standard_Transient)& Interface_EntityIterator::Value () const
138 {
139 //  NbEntity pas const (on ne sait pas comment il est implemente apres tout)
140   if (thelist.IsNull()) Standard_NoSuchObject::Raise("Interface_EntityIterator");
141   if (thecurr->Value() < 1 || thecurr->Value() > thelist->Length())
142     Standard_NoSuchObject::Raise("Interface_EntityIterator");
143   return thelist->Value(thecurr->Value());
144 }
145
146     Handle(TColStd_HSequenceOfTransient)  Interface_EntityIterator::Content () const
147 {
148   if (!thecurr.IsNull() && thecurr->Value() == 0) Start();
149   if (thelist.IsNull()) return new TColStd_HSequenceOfTransient();  // vide
150   return thelist;
151 }
152
153 void  Interface_EntityIterator::Destroy ()
154       {  thecurr.Nullify();  }  // redevient vide !
155
156 Interface_EntityIterator::~Interface_EntityIterator()
157 {
158   Destroy();
159 }
160