]> SALOME platform Git repositories - modules/kernel.git/blob - src/Notebook/SALOME_Parameter.cxx
Salome HOME
Base implementation of Notebook
[modules/kernel.git] / src / Notebook / SALOME_Parameter.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SALOME_Parameter.cxx
23 //  Author : Alexandre SOLOVYOV
24 //  Module : SALOME
25 //
26
27 #include <SALOME_Parameter.hxx>
28 #include <SALOME_Notebook.hxx>
29 #include <Utils_SALOME_Exception.hxx>
30 #include <utilities.h>
31 #include <stdio.h>
32
33 SALOME_Parameter::SALOME_Parameter( SALOME_Notebook* theNotebook, const std::string& theName, double theValue )
34 : myNotebook( theNotebook ), myName( theName ), myResult( theValue ), myIsAnonimous( false ), myIsCalculable( false )
35 {
36 }
37
38 SALOME_Parameter::SALOME_Parameter( SALOME_Notebook* theNotebook, const std::string& theName, const std::string& theExpr )
39 : myNotebook( theNotebook ), myName( theName ), myExpr( theExpr ), myIsAnonimous( false ), myIsCalculable( true )
40 {
41   Update();
42 }
43
44 SALOME_Parameter::SALOME_Parameter( SALOME_Notebook* theNotebook, const std::string& theExpr )
45 : myNotebook( theNotebook ), myName( theExpr ), myExpr( theExpr ), myIsAnonimous( true ), myIsCalculable( true )
46 {
47   Update();
48 }
49
50 SALOME_Parameter::~SALOME_Parameter()
51 {
52 }
53
54 char* SALOME_Parameter::GetEntry()
55 {
56   return CORBA::string_dup( myName.c_str() );
57 }
58
59 char* SALOME_Parameter::GetComponent()
60 {
61   return CORBA::string_dup( PARAM_COMPONENT.c_str() );
62 }
63
64 CORBA::Boolean SALOME_Parameter::IsValid()
65 {
66   return myResult.isValid();
67 }
68
69 void SALOME_Parameter::Update()
70 {
71   //printf( "Update of %s\n", GetEntry() );
72   if( myIsCalculable )
73   {
74     //1. Set parameters values
75     SALOME_StringList deps = myExpr.parser()->parameters();
76     SALOME_StringList::const_iterator it = deps.begin(), last = deps.end();
77     for( ; it!=last; it++ )
78     {
79       std::string aName = *it;
80       SALOME_Parameter* aParam = myNotebook->ParamPtr( const_cast<char*>( aName.c_str() ) );
81       if( aParam )
82       {
83         //printf( "\tset %s = %lf\n", aName.c_str(), aParam->AsReal() );
84         myExpr.parser()->setParameter( aName, aParam->myResult );
85       }
86       else
87       {
88         myResult = SALOME_EvalVariant();
89         return;
90       }
91     }
92
93     //2. Calculate
94     myResult = myExpr.calculate();
95     //printf( "\tresult = %lf\n", AsReal() );
96   }
97 }
98
99 void SALOME_Parameter::SetExpr( const char* theExpr )
100 {
101   if( myIsAnonimous )
102     myNotebook->AddExpr( theExpr );
103
104   else
105   {
106     myExpr.setExpression( theExpr );
107     myIsCalculable = true;
108     Update();
109     myNotebook->SetToUpdate( _this() );
110   }
111 }
112
113 void SALOME_Parameter::SetReal( CORBA::Double theValue )
114 {
115   if( myIsAnonimous )
116   {
117   }
118   else
119   {
120     myResult = theValue;
121     myIsCalculable = false;
122     Update();
123     myNotebook->SetToUpdate( _this() );
124   }
125 }
126
127 SALOME::ParamType SALOME_Parameter::GetType()
128 {
129   switch( myResult.type() )
130   {
131   case SALOME_EvalVariant_Boolean:
132     return SALOME::TBoolean;
133
134   case SALOME_EvalVariant_Int:
135   case SALOME_EvalVariant_UInt:
136     return SALOME::TInteger;
137
138   case SALOME_EvalVariant_Double:
139     return SALOME::TReal;
140
141   case SALOME_EvalVariant_String:
142     return SALOME::TString;
143
144   default:
145     return SALOME::TUnknown;
146   }
147 }
148
149 char* SALOME_Parameter::AsString()
150 {
151   return CORBA::string_dup( myResult.toString().c_str() );
152 }
153
154 CORBA::Long SALOME_Parameter::AsInteger()
155 {
156   bool ok;
157   return myResult.toInt( &ok );
158 }
159
160 CORBA::Double SALOME_Parameter::AsReal()
161 {
162   bool ok;
163   return myResult.toDouble( &ok );
164 }
165
166 CORBA::Boolean SALOME_Parameter::AsBoolean()
167 {
168   return myResult.toBool();
169 }
170
171 SALOME_StringList SALOME_Parameter::Dependencies() const
172 {
173   return myIsCalculable ? myExpr.parser()->parameters() : SALOME_StringList();
174 }