b311480e |
1 | // Created on: 1999-06-11 |
2 | // Created by: Vladislav ROMASHKO |
3 | // Copyright (c) 1999-1999 Matra Datavision |
973c2be1 |
4 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
5 | // |
973c2be1 |
6 | // This file is part of Open CASCADE Technology software library. |
b311480e |
7 | // |
d5f74e42 |
8 | // This library is free software; you can redistribute it and/or modify it under |
9 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
10 | // by the Free Software Foundation, with special exception defined in the file |
11 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
12 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
13 | // |
973c2be1 |
14 | // Alternatively, this file may be used under the terms of Open CASCADE |
15 | // commercial license or contractual agreement. |
7fd59977 |
16 | |
17 | #include <TFunction_DriverTable.ixx> |
18 | |
19 | #include <TFunction_DataMapOfGUIDDriver.hxx> |
20 | #include <TFunction_DataMapIteratorOfDataMapOfGUIDDriver.hxx> |
21 | #include <TFunction_Driver.hxx> |
22 | #include <TCollection_ExtendedString.hxx> |
23 | #include <TDF.hxx> |
24 | |
25 | |
26 | static Handle(TFunction_DriverTable) DT; |
27 | |
28 | //======================================================================= |
29 | //function : Get |
30 | //purpose : |
31 | //======================================================================= |
32 | |
33 | Handle(TFunction_DriverTable) TFunction_DriverTable::Get() |
34 | { |
35 | if (DT.IsNull()) DT = new TFunction_DriverTable; |
36 | return DT; |
37 | } |
38 | |
39 | //======================================================================= |
40 | //function : TFunction_DriverTable |
41 | //purpose : Constructor |
42 | //======================================================================= |
43 | |
44 | TFunction_DriverTable::TFunction_DriverTable() |
45 | {} |
46 | |
47 | //======================================================================= |
48 | //function : AddDriver |
49 | //purpose : Adds a driver to the DriverTable |
50 | //======================================================================= |
51 | |
52 | Standard_Boolean TFunction_DriverTable::AddDriver(const Standard_GUID& guid, |
53 | const Handle(TFunction_Driver)& driver, |
54 | const Standard_Integer thread) |
55 | { |
56 | if (thread == 0) |
57 | return myDrivers.Bind(guid,driver); |
58 | else if (thread > 0) |
59 | { |
60 | if (myThreadDrivers.IsNull()) |
61 | { |
62 | // Create a new table for thread-drivers. |
63 | myThreadDrivers = new TFunction_HArray1OfDataMapOfGUIDDriver(1, thread); |
64 | } |
65 | else if (myThreadDrivers->Upper() < thread) |
66 | { |
67 | // Create a bigger table for thread-drivers. |
68 | Handle(TFunction_HArray1OfDataMapOfGUIDDriver) new_dt = new TFunction_HArray1OfDataMapOfGUIDDriver(1, thread); |
69 | // Copy old table to the expanded (new) one. |
70 | Standard_Integer i = 1, old_upper = myThreadDrivers->Upper(); |
71 | for (; i <= old_upper; i++) |
72 | { |
73 | const TFunction_DataMapOfGUIDDriver& t = myThreadDrivers->Value(i); |
74 | TFunction_DataMapIteratorOfDataMapOfGUIDDriver itrt(t); |
75 | for (; itrt.More(); itrt.Next()) |
76 | { |
77 | new_dt->ChangeValue(i).Bind(itrt.Key(), itrt.Value()); |
78 | } |
79 | }//for... |
80 | myThreadDrivers = new_dt; |
81 | }//else... |
82 | return myThreadDrivers->ChangeValue(thread).Bind(guid, driver); |
83 | } |
84 | return Standard_False; |
85 | } |
86 | |
87 | //======================================================================= |
88 | //function : HasDriver |
89 | //purpose : |
90 | //======================================================================= |
91 | |
92 | Standard_Boolean TFunction_DriverTable::HasDriver(const Standard_GUID& guid, |
93 | const Standard_Integer thread) const |
94 | { |
95 | if (thread == 0) |
96 | return myDrivers.IsBound(guid); |
97 | else if (thread > 0 && !myThreadDrivers.IsNull() && myThreadDrivers->Upper() >= thread) |
98 | return myThreadDrivers->Value(thread).IsBound(guid); |
99 | return Standard_False; |
100 | } |
101 | |
102 | //======================================================================= |
103 | //function : FindDriver |
104 | //purpose : Returns the driver if find |
105 | //======================================================================= |
106 | |
107 | Standard_Boolean TFunction_DriverTable::FindDriver(const Standard_GUID& guid, |
108 | Handle(TFunction_Driver)& driver, |
109 | const Standard_Integer thread) const |
110 | { |
111 | if (thread == 0) |
112 | { |
113 | if (myDrivers.IsBound(guid)) |
114 | { |
115 | driver = myDrivers.Find(guid); |
116 | return Standard_True; |
117 | } |
118 | } |
119 | else if (thread > 0 && !myThreadDrivers.IsNull() && myThreadDrivers->Upper() >= thread) |
120 | { |
121 | if (myThreadDrivers->Value(thread).IsBound(guid)) |
122 | { |
123 | driver = myThreadDrivers->Value(thread).Find(guid); |
124 | return Standard_True; |
125 | } |
126 | } |
127 | return Standard_False; |
128 | } |
129 | |
130 | //======================================================================= |
131 | //function : Dump |
132 | //purpose : |
133 | //======================================================================= |
134 | |
135 | Standard_OStream& TFunction_DriverTable::Dump(Standard_OStream& anOS) const |
136 | { |
137 | TFunction_DataMapIteratorOfDataMapOfGUIDDriver itr(myDrivers); |
138 | for (; itr.More(); itr.Next()) |
139 | { |
140 | itr.Key().ShallowDump(anOS); |
141 | anOS<<"\t"; |
142 | TCollection_ExtendedString es; |
143 | TDF::ProgIDFromGUID(itr.Key(), es); |
144 | anOS<<es<<"\n"; |
145 | } |
146 | return anOS; |
147 | } |
148 | |
149 | //======================================================================= |
150 | //function : RemoveDriver |
151 | //purpose : Removes a driver from the DriverTable |
152 | //======================================================================= |
153 | |
154 | Standard_Boolean TFunction_DriverTable::RemoveDriver(const Standard_GUID& guid, |
155 | const Standard_Integer thread) |
156 | { |
157 | if (thread == 0) |
158 | return myDrivers.UnBind(guid); |
159 | else if (thread > 0 && !myThreadDrivers.IsNull() && myThreadDrivers->Upper() >= thread) |
160 | myThreadDrivers->ChangeValue(thread).UnBind(guid); |
161 | return Standard_False; |
162 | } |
163 | |
164 | //======================================================================= |
165 | //function : Clear |
166 | //purpose : Removes all drivers |
167 | //======================================================================= |
168 | |
169 | void TFunction_DriverTable::Clear() |
170 | { |
171 | myDrivers.Clear(); |
172 | if (!myThreadDrivers.IsNull()) |
173 | myThreadDrivers.Nullify(); |
174 | } |