0024057: Eliminate compiler warning C4100 in MSVC++ with warning level 4
[occt.git] / src / Interface / Interface_Graph.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18 #include <Interface_Graph.ixx> 
19 #include <Interface_GeneralModule.hxx>
20 #include <Interface_ReportEntity.hxx>
21 #include <Standard_DomainError.hxx>
22 #include <TColStd_Array1OfInteger.hxx>
23 #include <Interface_ShareTool.hxx>
24 #include <TColStd_HSequenceOfTransient.hxx>
25 #include <TColStd_ListIteratorOfListOfInteger.hxx>
26 #include <Interface_GTool.hxx>
27
28 // Flags : 0 = Presence, 1 = Sharing Error
29 #define Graph_Present 0
30 #define Graph_ShareError 1
31
32
33 //  ###########################################################################
34
35 //  ....                           CONSTRUCTEURS                           ....
36
37 //  ....       Construction a partir de la connaissance des Entites        ....
38
39
40 Interface_Graph::Interface_Graph
41 (const Handle(Interface_InterfaceModel)& amodel,
42  const Interface_GeneralLib& /*lib*/,
43  Standard_Boolean theModeStat)
44  : themodel   (amodel), thepresents ("") 
45 {
46   if(theModeStat)
47     InitStats();
48   Evaluate();
49 }
50
51 Interface_Graph::Interface_Graph
52 (const Handle(Interface_InterfaceModel)& amodel,
53  const Handle(Interface_Protocol)& /*protocol*/,
54  Standard_Boolean theModeStat)
55  : themodel   (amodel) , thepresents ("")    
56
57 {
58  if(theModeStat)
59     InitStats();  
60   Evaluate();
61 }
62
63 Interface_Graph::Interface_Graph
64 (const Handle(Interface_InterfaceModel)& amodel,
65  const Handle(Interface_GTool)& /*gtool*/,
66  Standard_Boolean theModeStat)
67  : themodel   (amodel) , thepresents ("") 
68 {
69   if(theModeStat)
70     InitStats();
71   Evaluate();
72 }
73
74 Interface_Graph::Interface_Graph
75 (const Handle(Interface_InterfaceModel)& amodel,
76  Standard_Boolean theModeStat)
77 : themodel   (amodel) , thepresents ("")  
78 {
79   if(theModeStat)
80     InitStats();
81   Evaluate ();
82 }
83
84 //  ....                Construction depuis un autre Graph                ....
85
86 Interface_Graph::Interface_Graph
87 (const Interface_Graph& agraph, const Standard_Boolean /*copied*/)
88 : themodel   (agraph.Model()), thepresents ("") 
89 {
90   thesharings = agraph.SharingTable();
91   Standard_Integer nb = agraph.NbStatuses();
92   if(!nb)
93     return;
94   if(thestats.IsNull())
95     thestats = new TColStd_HArray1OfInteger(1,nb);
96   for (Standard_Integer i = 1; i <= nb; i ++)
97     thestats->SetValue (i,agraph.Status(i));
98   theflags.Initialize(agraph.BitMap(),Standard_True);
99 }
100
101 void Interface_Graph::InitStats()
102 {
103   thestats = new TColStd_HArray1OfInteger(1,themodel->NbEntities()) , 
104     theflags.Initialize(themodel->NbEntities(),2);
105   theflags.AddFlag ("ShareError");
106 }
107
108 Standard_Integer Interface_Graph::NbStatuses() const
109 {
110   return (thestats.IsNull() ? 0 : thestats->Length());
111 }
112
113 const Handle(TColStd_HArray1OfListOfInteger)& Interface_Graph::SharingTable () const
114 {  return thesharings;  }
115
116 void Interface_Graph::Evaluate()
117 {
118   //  Evaluation d un Graphe de dependances : sur chaque Entite, on prend sa
119   //  liste "Shared". On en deduit les "Sharing"  directement
120   Standard_Integer n = Size();
121   thesharings = new TColStd_HArray1OfListOfInteger(1,n);//TColStd_HArray1OfTransient(1,n);//Clear();
122   if(themodel->GTool().IsNull())
123     return;
124   
125
126   Standard_Integer i; // svv Jan11 2000 : porting on DEC
127   for (i = 1; i <= n; i ++) {
128      //    ATTENTION : Si Entite non chargee donc illisible, basculer sur son
129     //    "Contenu" equivalent
130     Handle(Standard_Transient) ent = themodel->Value(i);
131   
132
133     //    Resultat obtenu via GeneralLib
134     Interface_EntityIterator iter = GetShareds(ent);
135
136     //    Mise en forme : liste d entiers
137     for (iter.Start(); iter.More(); iter.Next()) {
138       //    num = 0 -> on sort du Model de depart, le noter "Error" et passer
139       Handle(Standard_Transient) entshare = iter.Value();
140       if(entshare == ent)
141         continue;
142
143       Standard_Integer num = EntityNumber(entshare);
144
145       if (!num )  
146       {
147         if(!thestats.IsNull())
148           theflags.SetTrue (i,Graph_ShareError);
149         continue;
150       }
151       thesharings->ChangeValue(num).Append(i);
152     }
153   }
154 }
155
156 //  ....                Construction depuis un autre Graph                ....
157
158
159 //  ###########################################################################
160
161 //  ....                ACCES UNITAIRES AUX DONNEES DE BASE                ....
162
163 void  Interface_Graph::Reset ()
164 {
165   if(!thestats.IsNull())
166   {
167     thestats.Nullify();
168     theflags.Init (Standard_False, Graph_Present);
169   }
170
171 }
172
173 void  Interface_Graph::ResetStatus ()
174
175   if(!thestats.IsNull())
176   {
177     thestats->Init(0);  
178     theflags.Init (Standard_False, Graph_Present);
179   }
180 }
181
182
183 Standard_Integer  Interface_Graph::Size () const 
184 {  return themodel->NbEntities(); }//thestats.Upper();  }
185
186 Standard_Integer  Interface_Graph::EntityNumber
187 (const Handle(Standard_Transient)& ent) const 
188 {  return themodel->Number(ent);  }
189
190 Standard_Boolean  Interface_Graph::IsPresent
191 (const Standard_Integer num) const 
192 {
193   if (num <= 0 || num > Size()) 
194     return Standard_False;
195   return (!thestats.IsNull() ? theflags.Value (num,Graph_Present) : Standard_False);
196 }
197
198 Standard_Boolean  Interface_Graph::IsPresent
199 (const Handle(Standard_Transient)& ent) const 
200 {  return IsPresent(EntityNumber(ent));  }
201
202 const Handle(Standard_Transient)&  Interface_Graph::Entity
203 (const Standard_Integer num) const 
204 {  
205   return themodel->Value(num);  
206 }
207
208
209 Standard_Integer  Interface_Graph::Status (const Standard_Integer num) const
210 {  
211   return (!thestats.IsNull() ? thestats->Value(num) : 0);  
212 }
213
214 void  Interface_Graph::SetStatus
215 (const Standard_Integer num, const Standard_Integer stat)
216 {  
217   if(!thestats.IsNull())
218     thestats->SetValue(num,stat);  
219 }
220
221 void  Interface_Graph::RemoveItem(const Standard_Integer num)
222 {
223   if(!thestats.IsNull())
224   {
225     thestats->SetValue(num,0);  
226     theflags.SetFalse (num,Graph_Present);
227   }
228 }
229
230 void  Interface_Graph::ChangeStatus
231 (const Standard_Integer oldstat, const Standard_Integer newstat)
232 {
233   if(thestats.IsNull())
234     return;
235   Standard_Integer nb = thestats->Upper();
236   for (Standard_Integer i = 1; i <= nb; i ++) {
237     if (thestats->Value(i) == oldstat) 
238       thestats->SetValue(i,newstat);
239   }
240 }
241
242 void  Interface_Graph::RemoveStatus(const Standard_Integer stat)
243 {
244   if(thestats.IsNull())
245     return;
246   Standard_Integer nb = thestats->Upper();
247   for (Standard_Integer i = 1; i <= nb; i ++) {
248     if (thestats->Value(i) == stat) RemoveItem(i);
249   }
250 }
251
252 const Interface_BitMap&  Interface_Graph::BitMap () const
253 {  return theflags;  }
254
255 Interface_BitMap&  Interface_Graph::CBitMap ()
256 {  return theflags;  }
257
258 //  ###########################################################################
259
260 //  ....      Chargements Elementaires avec Propagation de "Share"      .... //
261
262 const Handle(Interface_InterfaceModel)&  Interface_Graph::Model() const 
263 {  return themodel;  }
264
265 void  Interface_Graph::GetFromModel ()
266 {
267   if (themodel.IsNull() || thestats.IsNull()) 
268     return;    // no model ... (-> on n ira pas loin)
269   theflags.Init (Standard_True,Graph_Present);
270   thestats->Init (0);
271 }
272
273 void  Interface_Graph::GetFromEntity
274 (const Handle(Standard_Transient)& ent, const Standard_Boolean shared,
275  const Standard_Integer newstat)
276 {
277   if(thestats.IsNull())
278     return;
279   Standard_Integer num = EntityNumber(ent);
280   if (!num ) 
281     return;
282   if (theflags.CTrue(num,Graph_Present)) return;  // deja pris : on passe
283   thestats->SetValue(num,newstat);
284   if (!shared) return;
285   //  Attention a la redefinition !
286   Interface_EntityIterator aIter = GetShareds(ent);
287
288   for ( ; aIter.More() ; aIter.Next())    
289     GetFromEntity(aIter.Value(),Standard_True,newstat);
290 }
291
292 void  Interface_Graph::GetFromEntity
293 (const Handle(Standard_Transient)& ent, const Standard_Boolean shared,
294  const Standard_Integer newstat, const Standard_Integer overlapstat,
295  const Standard_Boolean cumul)
296 {
297   if(thestats.IsNull())
298     return;
299   Standard_Integer num   = EntityNumber(ent);
300   if (!num ) return;
301   Standard_Boolean pasla = !theflags.CTrue (num,Graph_Present);
302   Standard_Integer stat  = thestats->Value(num); 
303
304   if (pasla) {
305     ///    theflags.SetTrue (num, Graph_Present);   // nouveau : noter avec newstat
306     thestats->SetValue(num,newstat);
307   } else {
308     Standard_Integer overstat = stat;
309     if (stat != newstat) {                   // deja pris, meme statut : passer
310       if (cumul) overstat += overlapstat;    // nouveau statut : avec cumul ...
311       else       overstat  = overlapstat;    // ... ou sans (statut force)
312       if (stat != overstat)                  // si repasse deja faite, passer
313         thestats->SetValue(num,overstat);
314     }
315   }
316   if (!shared) return;
317   //  Attention a la redefinition !
318   Interface_EntityIterator aIter = GetShareds(ent);
319
320   for ( ; aIter.More() ; aIter.Next())    
321     GetFromEntity(aIter.Value(),Standard_True,newstat);
322 }
323
324 void  Interface_Graph::GetFromIter
325 (const Interface_EntityIterator& iter, const Standard_Integer newstat)
326 {
327    if(thestats.IsNull())
328     return;
329   for (iter.Start(); iter.More(); iter.Next()) {
330     Handle(Standard_Transient) ent = iter.Value();
331     Standard_Integer num = EntityNumber(ent);
332     if (!num) 
333       continue;
334     if (theflags.CTrue(num,Graph_Present)) 
335       continue;
336     thestats->SetValue(num,newstat);
337   }
338 }
339
340
341 void  Interface_Graph::GetFromIter
342 (const Interface_EntityIterator& iter,
343  const Standard_Integer newstat, const Standard_Integer overlapstat,
344  const Standard_Boolean cumul)
345 {
346   if(thestats.IsNull())
347     return;
348   for (iter.Start(); iter.More(); iter.Next()) {
349     Handle(Standard_Transient) ent = iter.Value();
350     Standard_Integer num   = EntityNumber(ent);
351     if (!num) 
352       continue;
353     /*Standard_Boolean pasla = !*/theflags.Value(num,Graph_Present);
354     /*Standard_Integer stat  = */thestats->Value(num); 
355     GetFromEntity (ent,Standard_False,newstat,overlapstat,cumul);
356   }
357 }
358
359
360 void Interface_Graph::GetFromGraph (const Interface_Graph& agraph)
361 {
362   if (Model() != agraph.Model()) Standard_DomainError::Raise
363     ("Graph from Interface : GetFromGraph");
364   Standard_Integer nb = Size();
365   for (Standard_Integer i = 1; i <= nb; i ++) {
366     if (agraph.IsPresent(i))
367       GetFromEntity (agraph.Entity(i),Standard_False,agraph.Status(i));
368   }
369 }
370
371 void Interface_Graph::GetFromGraph
372 (const Interface_Graph& agraph, const Standard_Integer stat)
373 {
374   if (Model() != agraph.Model()) Standard_DomainError::Raise
375     ("Graph from Interface : GetFromGraph");
376   Standard_Integer nb = Size();
377   for (Standard_Integer i = 1; i <= nb; i ++) {
378     if (agraph.IsPresent(i) && agraph.Status(i) == stat)
379       GetFromEntity (agraph.Entity(i),Standard_False,stat);
380   }
381 }
382
383 //  #####################################################################
384
385 //  ....                Listage des Entites Partagees                ....
386
387 Standard_Boolean Interface_Graph::HasShareErrors
388 (const Handle(Standard_Transient)& ent) const
389 {
390   if(thestats.IsNull())
391     return Standard_False;
392   Standard_Integer num   = EntityNumber(ent);
393   if (num == 0) return Standard_True;
394   return theflags.Value (num,Graph_ShareError);
395 }
396
397 Interface_EntityIterator Interface_Graph::Shareds
398 (const Handle(Standard_Transient)& ent) const
399 {
400   Interface_EntityIterator iter;
401   Standard_Integer num   = EntityNumber(ent);
402   if(!num)
403     return iter;
404
405   Handle(Standard_Transient) aCurEnt =  ent;
406   if (themodel->IsRedefinedContent(num)) 
407      aCurEnt = themodel->ReportEntity(num)->Content();
408
409   //if (num == 0)  Standard_DomainError::Raise ("Interface : Shareds");
410   Handle(Interface_GeneralModule) module;
411   Standard_Integer CN;
412   if (themodel->GTool()->Select(aCurEnt,module,CN))  
413     module->FillShared(themodel,CN,aCurEnt,iter);
414   return iter;
415 }
416
417 Handle(TColStd_HSequenceOfTransient) Interface_Graph::GetShareds(const Handle(Standard_Transient)& ent) const
418 {
419   Handle(TColStd_HSequenceOfTransient) aseq = new TColStd_HSequenceOfTransient;
420   Interface_EntityIterator iter = Shareds(ent);
421   for( ; iter.More(); iter.Next())
422     aseq->Append(iter.Value());
423   return aseq;
424 }
425
426 Handle(TColStd_HSequenceOfTransient) Interface_Graph::GetSharings(const Handle(Standard_Transient)& ent) const
427 {
428   Standard_Integer num   = EntityNumber(ent);
429   if(!num)
430     return 0;
431   //return Handle(TColStd_HSequenceOfTransient)::DownCast(thesharings->Value(num));
432   const TColStd_ListOfInteger& alist = thesharings->Value(num);
433   Handle(TColStd_HSequenceOfTransient) aSharings = new TColStd_HSequenceOfTransient;
434   TColStd_ListIteratorOfListOfInteger aIt(alist);
435   for( ; aIt.More() ; aIt.Next())
436     aSharings->Append(Entity(aIt.Value()));
437   return aSharings;
438 }
439
440 Interface_EntityIterator Interface_Graph::Sharings
441 (const Handle(Standard_Transient)& ent) const
442 {
443   Interface_EntityIterator iter;
444   iter.AddList(GetSharings(ent));
445   return iter;
446
447 }
448
449 static void AddTypedSharings
450 (const Handle(Standard_Transient)& ent, const Handle(Standard_Type)& type,
451  Interface_EntityIterator& iter, const Standard_Integer n,
452  const Interface_Graph& G)
453 {
454   if (ent.IsNull()) return;
455   if (ent->IsKind(type))  {  iter.AddItem (ent);  return;  }
456   if (iter.NbEntities() > n) return;
457
458   Handle(TColStd_HSequenceOfTransient) list = G.GetSharings(ent);
459   if(list.IsNull())
460     return;
461
462   Standard_Integer nb = list->Length();
463   for (Standard_Integer i = 1; i <= nb; i ++)
464     AddTypedSharings (list->Value(i) ,type,iter,nb,G);
465 }
466
467 Interface_EntityIterator Interface_Graph::TypedSharings
468 (const Handle(Standard_Transient)& ent, const Handle(Standard_Type)& type) const
469 {
470   Interface_EntityIterator iter;
471   Standard_Integer n = Size();
472   AddTypedSharings (ent,type,iter,n,*this);
473   return iter;
474 }
475
476
477 Interface_EntityIterator Interface_Graph::RootEntities () const
478 {
479   Interface_EntityIterator iter;
480   Standard_Integer nb = thesharings->Length();
481   for (Standard_Integer i = 1; i <= nb; i ++) {
482     if(!thesharings->Value(i).IsEmpty())
483       continue;
484     iter.AddItem(Entity(i));
485   }
486   return iter;
487 }
488
489 Handle(TCollection_HAsciiString)  Interface_Graph::Name(const Handle(Standard_Transient)& ent) const
490 {
491   Handle(TCollection_HAsciiString) str;
492   if (themodel.IsNull()) return str;
493   if (themodel->Number(ent)) return str;
494
495   Handle(Interface_GTool) gtool = themodel->GTool();
496   if (gtool.IsNull()) return str;
497
498   Handle(Interface_GeneralModule) module;
499   Standard_Integer CN;
500   if (!gtool->Select(ent,module,CN)) return str;
501
502   Interface_ShareTool sht (*this);
503   return module->Name (CN,ent,sht);
504 }
505
506 Standard_Boolean Interface_Graph::ModeStat() const
507 {
508   return (!thestats.IsNull());
509 }