0027349: XtControl_Reader is not thread-safe
[occt.git] / src / Interface / Interface_Category.cxx
CommitLineData
973c2be1 1// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
b311480e 4//
d5f74e42 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
973c2be1 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.
b311480e 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
b311480e 13
42cf5bc1 14
15#include <Interface_Category.hxx>
16#include <Interface_GeneralModule.hxx>
42cf5bc1 17#include <Interface_InterfaceModel.hxx>
18#include <Interface_Protocol.hxx>
19#include <Interface_ShareTool.hxx>
20#include <Standard_Transient.hxx>
7f56eba8 21#include <Standard_Mutex.hxx>
7fd59977 22#include <TCollection_AsciiString.hxx>
23#include <TColStd_SequenceOfAsciiString.hxx>
7f56eba8 24#include <NCollection_Vector.hxx>
7fd59977 25
26static int init = 0;
27static Standard_CString unspec = "unspecified";
28
7f56eba8 29static Standard_Mutex gMapTypesMutex;
30static volatile Standard_Boolean gMapTypesInit = Standard_False;
31
32static NCollection_Vector<TCollection_AsciiString>& theCats()
7fd59977 33{
7f56eba8 34 static NCollection_Vector<TCollection_AsciiString> aCat;
35 return aCat;
7fd59977 36}
37
7f56eba8 38Standard_Integer Interface_Category::CatNum
39 (const Handle(Standard_Transient)& theEnt,
40 const Interface_ShareTool& theShares)
7fd59977 41{
7f56eba8 42 if (theEnt.IsNull()) return 0;
7fd59977 43 Standard_Integer CN;
7f56eba8 44 Handle(Interface_GeneralModule) aModule;
45 if (!myGTool->Select (theEnt,aModule,CN)) return 0;
46 return aModule->CategoryNumber (CN,theEnt,theShares);
7fd59977 47}
48
7f56eba8 49void Interface_Category::Compute
50 (const Handle(Interface_InterfaceModel)& theModel,
51 const Interface_ShareTool& theShares)
7fd59977 52{
53 ClearNums();
7f56eba8 54 if (theModel.IsNull()) return;
55 Standard_Integer CN, i, nb = theModel->NbEntities();
56 myGTool->Reservate (nb);
7fd59977 57 if (nb == 0) return;
7f56eba8 58 myNum = new TColStd_HArray1OfInteger (1,nb); myNum->Init(0);
7fd59977 59 for (i = 1; i <= nb; i ++) {
7f56eba8 60 Handle(Standard_Transient) anEnt = theModel->Value(i);
61 if (anEnt.IsNull()) continue;
62 Handle(Interface_GeneralModule) aModule;
63 if (!myGTool->Select (anEnt,aModule,CN)) continue;
64 myNum->SetValue (i,aModule->CategoryNumber (CN,anEnt,theShares));
7fd59977 65 }
66}
67
7f56eba8 68Standard_Integer Interface_Category::Num (const Standard_Integer theNumEnt) const
7fd59977 69{
7f56eba8 70 if (myNum.IsNull()) return 0;
71 if (theNumEnt < 1 || theNumEnt > myNum->Length()) return 0;
72 return myNum->Value(theNumEnt);
7fd59977 73}
74
7f56eba8 75// List of Categories
7fd59977 76
7f56eba8 77Standard_Integer Interface_Category::AddCategory (const Standard_CString theName)
7fd59977 78{
7f56eba8 79 Standard_Integer aNum = Interface_Category::Number (theName);
80 if (aNum > 0) return aNum;
81 theCats().Append (TCollection_AsciiString(theName));
82 return theCats().Length()+1;
7fd59977 83}
84
7f56eba8 85Standard_Integer Interface_Category::NbCategories()
7fd59977 86{
7f56eba8 87 return theCats().Length();
7fd59977 88}
89
7f56eba8 90Standard_CString Interface_Category::Name (const Standard_Integer theNum)
91{
92 if (theNum < 0) return "";
93 if (theNum < 1 || theNum > theCats().Length()) return unspec;
94 return theCats().ChangeValue(theNum).ToCString();
95}
7fd59977 96
7f56eba8 97Standard_Integer Interface_Category::Number (const Standard_CString theName)
7fd59977 98{
7f56eba8 99 Standard_Integer i, nb = theCats().Length();
7fd59977 100 for (i = 1; i <= nb; i ++) {
7f56eba8 101 if (theCats().ChangeValue(i).IsEqual(theName)) return i;
7fd59977 102 }
103 return 0;
104}
105
7f56eba8 106void Interface_Category::Init ()
7fd59977 107{
7f56eba8 108 // On first call, initialize static map
109 if ( !gMapTypesInit )
110 {
111 gMapTypesMutex.Lock();
112 if ( !gMapTypesInit )
113 {
114 if (init) return; init = 1;
115 init = Interface_Category::AddCategory ("Shape");
116 init = Interface_Category::AddCategory ("Drawing");
117 init = Interface_Category::AddCategory ("Structure");
118 init = Interface_Category::AddCategory ("Description");
119 init = Interface_Category::AddCategory ("Auxiliary");
120 init = Interface_Category::AddCategory ("Professional");
121 init = Interface_Category::AddCategory ("FEA");
122 init = Interface_Category::AddCategory ("Kinematics");
123 init = Interface_Category::AddCategory ("Piping");
124
125 gMapTypesInit = Standard_True;
126 }
127 gMapTypesMutex.Unlock();
128 }
7fd59977 129}