0026937: Eliminate NO_CXX_EXCEPTION macro support
[occt.git] / src / Expr / Expr_PolyExpression.cxx
1 // Created on: 1991-04-16
2 // Created by: Arnaud BOUZY
3 // Copyright (c) 1991-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <Expr_GeneralExpression.hxx>
19 #include <Expr_InvalidOperand.hxx>
20 #include <Expr_NamedUnknown.hxx>
21 #include <Expr_PolyExpression.hxx>
22 #include <Standard_DimensionMismatch.hxx>
23 #include <Standard_NumericError.hxx>
24 #include <Standard_OutOfRange.hxx>
25 #include <Standard_Type.hxx>
26
27 IMPLEMENT_STANDARD_RTTIEXT(Expr_PolyExpression,Expr_GeneralExpression)
28
29 Expr_PolyExpression::Expr_PolyExpression()
30 {
31 }
32
33 Standard_Integer Expr_PolyExpression::NbOperands () const
34 {
35   return myOperands.Length();
36 }
37
38 void Expr_PolyExpression::SetOperand (const Handle(Expr_GeneralExpression)& exp, const Standard_Integer index)
39 {
40   Handle(Expr_PolyExpression) me = this;
41   if (exp == me) {
42     throw Expr_InvalidOperand();
43   }
44   if (exp->Contains(me)) {
45     throw Expr_InvalidOperand();
46   }
47   myOperands(index) = exp;
48 }
49
50 void Expr_PolyExpression::AddOperand (const Handle(Expr_GeneralExpression)& exp)
51 {
52   myOperands.Append(exp);
53 }
54
55 void Expr_PolyExpression::RemoveOperand (const Standard_Integer index)
56 {
57   if (myOperands.Length() <= 2) {
58     throw Standard_DimensionMismatch();
59   }
60   myOperands.Remove(index);
61 }
62
63 Standard_Integer Expr_PolyExpression::NbSubExpressions () const
64 {
65   return NbOperands();
66 }
67
68 const Handle(Expr_GeneralExpression)& Expr_PolyExpression::SubExpression (const Standard_Integer I) const
69 {
70   return Operand(I);
71 }
72
73 Standard_Boolean Expr_PolyExpression::ContainsUnknowns () const 
74 {
75   Standard_Boolean found = Standard_False;
76   Standard_Integer nbop = NbOperands();
77   Standard_Integer i = 1;
78   Handle(Expr_GeneralExpression) expop;
79
80   while ((!found) && (i <= nbop)) {
81     expop = Operand(i);
82     found = expop->IsKind(STANDARD_TYPE(Expr_NamedUnknown));
83     i++;
84   }
85   i = 1;
86   while ((!found) && (i <= nbop)) {
87     expop = Operand(i);
88     found = expop->ContainsUnknowns();
89     i++;
90   }
91   return found;
92 }
93
94 Standard_Boolean Expr_PolyExpression::Contains (const Handle(Expr_GeneralExpression)& exp) const
95 {
96   Standard_Boolean found = Standard_False;
97   Standard_Integer nbop = NbOperands();
98   Standard_Integer i = 1;
99   Handle(Expr_GeneralExpression) expop;
100
101   while ((!found) && (i <= nbop)) {
102     expop = Operand(i);
103     found = (expop == exp);
104     i++;
105   }
106   i = 1;
107   while ((!found) && (i <= nbop)) {
108     expop = Operand(i);
109     found = expop->Contains(exp);
110     i++;
111   }
112   return found;
113 }
114
115 void Expr_PolyExpression::Replace (const Handle(Expr_NamedUnknown)& var, const Handle(Expr_GeneralExpression)& with)
116 {
117   Standard_Integer nbop = NbOperands();
118   Standard_Integer i;
119   Handle(Expr_GeneralExpression) expop;
120
121   for(i=1;i <= nbop; i++) {
122     expop = Operand(i);
123     if (expop == var) {
124       SetOperand(with,i);
125     }
126     else {
127       if (expop->Contains(var)) {
128         expop->Replace(var,with);
129       }
130     }
131   }
132 }
133
134
135 Handle(Expr_GeneralExpression) Expr_PolyExpression::Simplified() const
136 {
137   Handle(Expr_PolyExpression) cop = Handle(Expr_PolyExpression)::DownCast(Copy());
138   Standard_Integer i;
139   Standard_Integer max = cop->NbOperands();
140   Handle(Expr_GeneralExpression) op;
141   for (i=1; i<= max; i++) {
142     op = cop->Operand(i);
143     cop->SetOperand(op->Simplified(),i);
144   }
145   return cop->ShallowSimplified();
146 }
147