0022627: Change OCCT memory management defaults
[occt.git] / src / Units / Units_Token.cxx
1 // File:        Units_Token.cxx
2 // Created:     Wed Jun 24 12:49:08 1992
3 // Author:      Gilles DEBARBOUILLE
4 //              <gde@phobox>
5 // Updated:     Gerard GRAS le 090597 
6 //              reason is PRO6934 -> plantage sur HP 10.2
7 //              changes are : Replace the field theword,themean from HAsciiString
8 //                            to AsciiString.
9 //              because the compiler try to destroies the handle of HAsciiString to early
10 //              due to inline use probably.
11 //              See also Units_Token.lxx
12
13 #define PRO8727 //GG_180697
14 //              Mauvaise construction d'un token par copie
15 //              plantatoire sur HP.
16
17 #include <Units_Token.ixx>
18 #include <TCollection_AsciiString.hxx>
19 #include <Units_Operators.hxx>
20
21 #define XTRACE 1
22
23 //=======================================================================
24 //function : Units_Token
25 //purpose  : 
26 //=======================================================================
27
28 Units_Token::Units_Token()
29 {
30   theword=" ";
31   themean=" ";
32   thevalue=0.;
33   thedimensions=new Units_Dimensions(0.,0.,0.,0.,0.,0.,0.,0.,0.);
34 #ifdef TRACE
35   cout << " CREATES Token " << endl;
36   Dump(0,0);
37 #endif
38 }
39
40 //=======================================================================
41 //function : Units_Token
42 //purpose  : 
43 //=======================================================================
44
45 Units_Token::Units_Token(const Standard_CString aword)
46 {
47   theword=aword;
48   themean=" ";
49   thevalue=0.;
50   thedimensions=new Units_Dimensions(0.,0.,0.,0.,0.,0.,0.,0.,0.);
51 #ifdef TRACE
52   cout << " CREATES Token " << endl;
53   Dump(0,0);
54 #endif
55 }
56
57 //=======================================================================
58 //function : Units_Token
59 //purpose  : 
60 //=======================================================================
61
62 Units_Token::Units_Token(const Standard_CString aword,
63                          const Standard_CString amean)
64 {
65   theword=aword;
66   themean=amean;
67   thevalue=0.;
68   thedimensions=new Units_Dimensions(0.,0.,0.,0.,0.,0.,0.,0.,0.);
69 #ifdef TRACE
70   cout << " CREATES Token " << endl;
71   Dump(0,0);
72 #endif
73 }
74
75 //=======================================================================
76 //function : Units_Token
77 //purpose  : 
78 //=======================================================================
79
80 Units_Token::Units_Token(const Standard_CString aword,
81                          const Standard_CString amean,
82                          const Standard_Real avalue)
83 {
84   theword=aword;
85   themean=amean;
86   thevalue=avalue;
87   thedimensions=new Units_Dimensions(0.,0.,0.,0.,0.,0.,0.,0.,0.);
88 #ifdef TRACE
89   cout << " CREATES Token " << endl;
90   Dump(0,0);
91 #endif
92 }
93
94 //=======================================================================
95 //function : Units_Token
96 //purpose  : 
97 //=======================================================================
98
99 Units_Token::Units_Token(const Standard_CString aword,
100                          const Standard_CString amean,
101                          const Standard_Real avalue,
102                          const Handle(Units_Dimensions)& adimensions)
103 {
104   theword=aword;
105   themean=amean;
106   thevalue=avalue;
107   if(adimensions.IsNull())
108     thedimensions = new Units_Dimensions(0.,0.,0.,0.,0.,0.,0.,0.,0.);
109   else
110     thedimensions = new Units_Dimensions(adimensions->Mass(),
111                                          adimensions->Length(),
112                                          adimensions->Time(),
113                                          adimensions->ElectricCurrent(),
114                                          adimensions->ThermodynamicTemperature(),
115                                          adimensions->AmountOfSubstance(),
116                                          adimensions->LuminousIntensity(),
117                                          adimensions->PlaneAngle(),
118                                          adimensions->SolidAngle());
119 #ifdef TRACE
120   cout << " CREATES Token " << endl;
121   Dump(0,0);
122 #endif
123 }
124
125 //=======================================================================
126 //function : Creates
127 //purpose  : 
128 //=======================================================================
129
130 Handle(Units_Token) Units_Token::Creates() const
131 {
132 #ifdef PRO8727
133   TCollection_AsciiString word = Word();
134   TCollection_AsciiString mean = Mean();
135   return new Units_Token(word.ToCString(),mean.ToCString(),Value(),Dimensions());
136 #else
137   return new Units_Token(*this);
138 #endif
139 }
140
141 //=======================================================================
142 //function : Length
143 //purpose  : 
144 //=======================================================================
145
146 Standard_Integer Units_Token::Length() const
147 {
148   return theword.Length();
149 }
150
151 //=======================================================================
152 //function : Dimensions
153 //purpose  : 
154 //=======================================================================
155
156 void Units_Token::Dimensions(const Handle(Units_Dimensions)& adimensions)
157 {
158   if(adimensions.IsNull())
159     thedimensions = new Units_Dimensions(0.,0.,0.,0.,0.,0.,0.,0.,0.);
160   else
161     thedimensions = new Units_Dimensions(adimensions->Mass(),
162                                          adimensions->Length(),
163                                          adimensions->Time(),
164                                          adimensions->ElectricCurrent(),
165                                          adimensions->ThermodynamicTemperature(),
166                                          adimensions->AmountOfSubstance(),
167                                          adimensions->LuminousIntensity(),
168                                          adimensions->PlaneAngle(),
169                                          adimensions->SolidAngle());
170 }
171
172 //=======================================================================
173 //function : Units_Token
174 //purpose  : 
175 //=======================================================================
176
177 Units_Token::Units_Token(const Handle(Units_Token)& atoken)
178 {
179   theword       = atoken->Word();
180   themean       = atoken->Mean();
181   thevalue      = atoken->Value();
182   thedimensions = atoken->Dimensions();
183 }
184
185 //=======================================================================
186 //function : Update
187 //purpose  : 
188 //=======================================================================
189
190 void Units_Token::Update(const Standard_CString amean)
191 {
192   TCollection_AsciiString string = Mean();
193   if(string.Search(amean) != -1)
194     cout<<Word()<<" encountered twice with the same signification : "<<amean<<endl;
195   string = string + amean;
196   themean = string;
197 }
198
199 //=======================================================================
200 //function : Add
201 //purpose  : 
202 //=======================================================================
203
204 Handle(Units_Token) Units_Token::Add (const Standard_Integer) const
205 {
206 //  Standard_CString s=new char[thelength+1];
207 //  strcpy(s,theword);
208 //  s[thelength-1]=s[thelength-1]+int(i);
209   return new Units_Token();
210 }
211
212 //=======================================================================
213 //function : Add
214 //purpose  : 
215 //=======================================================================
216
217 Handle(Units_Token) Units_Token::Add (const Handle(Units_Token)& atoken) const
218 {
219   TCollection_AsciiString word = Word();
220   if(thedimensions->IsEqual(atoken->Dimensions()))
221     return new Units_Token(word.ToCString(), " ", thevalue+atoken->Value(), thedimensions);
222   else
223     return new Units_Token(" ");
224 }
225
226 //=======================================================================
227 //function : Subtract
228 //purpose  : 
229 //=======================================================================
230
231 Handle(Units_Token) Units_Token::Subtract (const Handle(Units_Token)& atoken) const
232 {
233   TCollection_AsciiString word = Word();
234   if(thedimensions->IsEqual(atoken->Dimensions()))
235     return new Units_Token(word.ToCString(), " ", thevalue-atoken->Value(), thedimensions);
236   else
237     return new Units_Token(" ");
238 }
239
240 //=======================================================================
241 //function : Multiply
242 //purpose  : 
243 //=======================================================================
244
245 Handle(Units_Token) Units_Token::Multiply (const Handle(Units_Token)& atoken) const
246 {
247   TCollection_AsciiString string = Word();
248   string.Insert(1,'(');
249   string = string + ")*(";
250   string = string + atoken->Word();
251   string = string + ")";
252   return new Units_Token
253     (string.ToCString()," ", thevalue*atoken->Value(), thedimensions * (atoken->Dimensions()));
254 }
255
256 //=======================================================================
257 //function : Multiplied
258 //purpose  : 
259 //=======================================================================
260
261 Standard_Real Units_Token::Multiplied (const Standard_Real avalue) const
262 {
263   return avalue * thevalue;
264 }
265
266 //=======================================================================
267 //function : Divide
268 //purpose  : 
269 //=======================================================================
270
271 Handle(Units_Token) Units_Token::Divide (const Handle(Units_Token)& atoken)
272      const
273 {
274   if(fabs(atoken->Value())<1.e-40) {
275 #ifdef DEB
276     cout<<"Warning: division on token with value=0 => return initial token."<<endl;
277 #endif
278     return this;
279   }
280   TCollection_AsciiString string = Word();
281   string.Insert(1,'(');
282   string = string + ")/(";
283   string = string + atoken->Word();
284   string = string + ")";
285   return new Units_Token
286     (string.ToCString()," ", thevalue/atoken->Value(), thedimensions / (atoken->Dimensions()));
287 }
288
289 //=======================================================================
290 //function : Divided
291 //purpose  : 
292 //=======================================================================
293
294 Standard_Real Units_Token::Divided (const Standard_Real avalue) const
295 {
296   return avalue / thevalue;
297 }
298
299 //=======================================================================
300 //function : Power
301 //purpose  : 
302 //=======================================================================
303
304 Handle(Units_Token) Units_Token::Power(const Handle(Units_Token)& atoken) const
305 {
306   TCollection_AsciiString string = Word();
307   string.Insert(1,'(');
308   string = string + ")**(";
309   string = string + atoken->Word();
310   string = string + ")";
311   return new Units_Token
312     (string.ToCString()," ",pow(thevalue,atoken->Value()),pow(thedimensions,atoken->Value()));
313 }
314
315 //=======================================================================
316 //function : Power
317 //purpose  : 
318 //=======================================================================
319
320 Handle(Units_Token) Units_Token::Power(const Standard_Real anexponent) const
321 {
322   TCollection_AsciiString exponent(anexponent);
323   TCollection_AsciiString string = Word();
324   string.Insert(1,'(');
325   string = string + ")**(";
326   string = string + exponent;
327   string = string + ")";
328   return new Units_Token
329     (string.ToCString()," ",pow(thevalue,anexponent),pow(thedimensions,anexponent));
330 }
331
332 //=======================================================================
333 //function : IsEqual
334 //purpose  : 
335 //=======================================================================
336
337 Standard_Boolean Units_Token::IsEqual (const Standard_CString astring) const
338 {
339   TCollection_AsciiString string = Word();
340 #ifdef UNX
341   Standard_Integer length = string.Length();
342 #else
343   unsigned int length = string.Length();
344 #endif
345   if(strlen(astring) == length)
346     return (strncmp(string.ToCString(),astring,unsigned(length)) == 0)
347       ? Standard_True : Standard_False;
348   else 
349     return Standard_False;
350 }
351
352 //=======================================================================
353 //function : IsEqual
354 //purpose  : 
355 //=======================================================================
356
357 Standard_Boolean Units_Token::IsEqual (const Handle(Units_Token)& atoken) const
358 {
359   TCollection_AsciiString string1 = Word();
360   TCollection_AsciiString string2 = atoken->Word();
361   Standard_Integer length = string1.Length();
362   if(length == atoken->Length())
363     return (strcmp(string1.ToCString(),string2.ToCString()) == 0) ? Standard_True : Standard_False;
364   else
365     return Standard_False;
366 }
367
368 //=======================================================================
369 //function : Dump
370 //purpose  : 
371 //=======================================================================
372
373 void Units_Token::Dump(const Standard_Integer ashift,
374                        const Standard_Integer alevel) const
375 {
376   int i;
377   TCollection_AsciiString word = Word();
378   TCollection_AsciiString mean = Mean();
379
380   for(i=0; i<ashift; i++)cout<<"  ";
381   cout << "Units_Token::Dump of " << hex << (long ) this << dec << endl;
382   for(i=0; i<ashift; i++)cout<<"  ";
383   cout<<word.ToCString()<<endl;
384   for(i=0; i<ashift; i++)cout<<"  ";
385   cout<<"  value : "<<thevalue<<endl;
386   for(i=0; i<ashift; i++)cout<<"  ";
387   cout<<"  mean  : "<<mean.ToCString()<<endl;
388   if(alevel)thedimensions->Dump(ashift);
389 }
390
391 //=======================================================================
392 //function : operator +
393 //purpose  : 
394 //=======================================================================
395
396 Handle(Units_Token) operator +(const Handle(Units_Token)& atoken,const Standard_Integer aninteger)
397 {
398   return atoken->Add(aninteger);
399 }
400
401 //=======================================================================
402 //function : operator +
403 //purpose  : 
404 //=======================================================================
405
406 Handle(Units_Token) operator +(const Handle(Units_Token)& atoken1,const Handle(Units_Token)& atoken2)
407 {
408   return atoken1->Add(atoken2);
409 }
410
411 //=======================================================================
412 //function : operator -
413 //purpose  : 
414 //=======================================================================
415
416 Handle(Units_Token) operator -(const Handle(Units_Token)& atoken1,const Handle(Units_Token)& atoken2)
417 {
418   return atoken1->Subtract(atoken2);
419 }
420
421 //=======================================================================
422 //function : operator *
423 //purpose  : 
424 //=======================================================================
425
426 Handle(Units_Token) operator *(const Handle(Units_Token)& atoken1,const Handle(Units_Token)& atoken2)
427 {
428   return atoken1->Multiply(atoken2);
429 }
430
431 //=======================================================================
432 //function : operator /
433 //purpose  : 
434 //=======================================================================
435
436 Handle(Units_Token) operator /(const Handle(Units_Token)& atoken1,const Handle(Units_Token)& atoken2)
437 {
438   return atoken1->Divide(atoken2);
439 }
440
441 //=======================================================================
442 //function : pow
443 //purpose  : 
444 //=======================================================================
445
446 Handle(Units_Token) pow(const Handle(Units_Token)& atoken1, const Handle(Units_Token)& atoken2)
447 {
448   return atoken1->Power(atoken2);
449 }
450
451 //=======================================================================
452 //function : pow
453 //purpose  : 
454 //=======================================================================
455
456 Handle(Units_Token) pow(const Handle(Units_Token)& atoken,const Standard_Real areal)
457 {
458   return atoken->Power(areal);
459 }
460
461 //=======================================================================
462 //function : operator ==
463 //purpose  : 
464 //=======================================================================
465
466 Standard_Boolean operator ==(const Handle(Units_Token)& atoken,const Standard_CString astring)
467 {
468   return atoken->IsEqual(astring);
469 }
470
471 //=======================================================================
472 //function : operator ==
473 //purpose  : 
474 //=======================================================================
475
476 //Standard_Boolean operator ==(const Handle(Units_Token)& atoken1,const Handle(Units_Token)& atoken2)
477 //{
478 //  return atoken1->IsEqual(atoken2);
479 //}
480
481 //=======================================================================
482 //function : operator !=
483 //purpose  : 
484 //=======================================================================
485
486 Standard_Boolean operator !=(const Handle(Units_Token)& atoken,const Standard_CString astring)
487 {
488   return atoken->IsNotEqual(astring);
489 }
490
491 //=======================================================================
492 //function : operator !=
493 //purpose  : 
494 //=======================================================================
495
496 //Standard_Boolean operator !=(const Handle(Units_Token)& atoken1,const Handle(Units_Token)& atoken2)
497 //{
498 //  return atoken1->IsNotEqual(atoken2);
499 //}
500
501 //=======================================================================
502 //function : operator <=
503 //purpose  : 
504 //=======================================================================
505
506 Standard_Boolean operator <=(const Handle(Units_Token)& atoken,const Standard_CString astring)
507 {
508   return atoken->IsLessOrEqual(astring);
509 }
510
511 //=======================================================================
512 //function : operator >
513 //purpose  : 
514 //=======================================================================
515
516 Standard_Boolean operator >(const Handle(Units_Token)& atoken,const Standard_CString astring)
517 {
518   return atoken->IsGreater(astring);
519 }
520
521 //=======================================================================
522 //function : operator >
523 //purpose  : 
524 //=======================================================================
525
526 Standard_Boolean operator >(const Handle(Units_Token)& atoken1,const Handle(Units_Token)& atoken2)
527 {
528   return atoken1->IsGreater(atoken2);
529 }
530
531 //=======================================================================
532 //function : operator >=
533 //purpose  : 
534 //=======================================================================
535
536 Standard_Boolean operator >=(const Handle(Units_Token)& atoken1,const Handle(Units_Token)& atoken2)
537 {
538   return atoken1->IsGreaterOrEqual(atoken2);
539 }
540
541 void Units_Token::Destroy () {
542
543 #ifdef TRACE
544  cout << " DESTROIES Token" << endl;
545  Dump(0,0);
546 #endif
547 }