Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / INTERP_KERNEL / ExprEval / InterpKernelUnit.hxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef __INTERPKERNELUNIT_HXX__
21 #define __INTERPKERNELUNIT_HXX__
22
23 #include "INTERPKERNELDefines.hxx"
24 #include "InterpKernelException.hxx"
25
26 #include <map>
27 #include <sstream>
28
29 namespace INTERP_KERNEL
30 {
31   class INTERPKERNEL_EXPORT UnitDataBase
32   {
33   public:
34     UnitDataBase();
35     const short *getInfoForUnit(const std::string& unit,
36                                 double& addFact, double& mFact) const throw(INTERP_KERNEL::Exception);
37     static UnitDataBase _uniqueMapForExpr;
38     static const int SIZE_OF_UNIT_BASE=5;
39   private:
40     std::map<std::string,double> _prefix_pow_10;
41     std::map<std::string,const short *> _units_semantic;
42     std::map<std::string,double> _units_mul;
43     std::map<std::string,double> _units_add;
44   private:
45     static const int NB_OF_PREF_POW10=22;
46     static const char *PREF_POW10[NB_OF_PREF_POW10];
47     static const double POW10[NB_OF_PREF_POW10];
48     static const int NB_OF_UNITS_RECOGN=29;
49     static const char *UNITS_RECOGN[NB_OF_UNITS_RECOGN];
50     static const short PROJ_IN_BASE[NB_OF_UNITS_RECOGN][SIZE_OF_UNIT_BASE];
51     static const double MUL_COEFF[NB_OF_UNITS_RECOGN];
52     static const double ADD_COEFF[NB_OF_UNITS_RECOGN];
53   };
54
55   class INTERPKERNEL_EXPORT DecompositionInUnitBase
56   {
57   public:
58     DecompositionInUnitBase();
59     void setInfo(const short *vals, double addFact, double mFact);
60     short operator[](int i) const { return _value[i]; }
61     bool operator==(const DecompositionInUnitBase& other) const;
62     void getTranslationParams(const DecompositionInUnitBase& other, double& mul, double& add) const;
63     bool isEqual(short mass, short lgth, short time, short intensity, short temp,
64                  double add, double mult);
65     bool isUnitary() const;
66     //! \b WARNING no test is done on the fact that unit is adimensionnal.
67     void negate();
68     bool isAdimensional() const;
69     void tryToConvertInUnit(double val) throw(INTERP_KERNEL::Exception);
70     DecompositionInUnitBase &operator*(const DecompositionInUnitBase& other);
71     DecompositionInUnitBase &operator/(const DecompositionInUnitBase& other);
72     DecompositionInUnitBase &operator^(const DecompositionInUnitBase& other) throw(INTERP_KERNEL::Exception);
73   private:
74     void dealWithAddFactor(const DecompositionInUnitBase& other);
75     static int couldItBeConsideredAsInt(double val) throw(INTERP_KERNEL::Exception);
76     static bool areDoubleEquals(double a, double b);
77     static double powInt(double val, int exp);
78   private:
79     short _value[UnitDataBase::SIZE_OF_UNIT_BASE];
80     double _add_to_base;
81     double _mult_fact_to_base;
82   };
83
84   /*!
85    * This class deals with units.
86    * This class has two main responsabilities :
87    *      - interprete units by giving simply their representation in string type.
88    *      - performing operations on these units.
89    *
90    * All the possible units are represented with a unique tuple with 5 elements
91    * representing the unique decomposition of a unit in the following base.
92    *
93    * dimension 0 stands for mass in g (\b NOT kg to simplify parsing).
94    * dimension 1 stands for length in m.
95    * dimension 2 stands for time in s.
96    * dimension 3 stands for elec intensity A.
97    * dimension 4 stands for temperature in K.
98    */
99   class INTERPKERNEL_EXPORT Unit
100   {
101   public:
102     Unit(const char *reprC, bool tryToInterp=true);
103     Unit(const char *reprFortran, int sizeOfRepr, bool tryToInterp=true);
104     void tryToInterprate() const;
105     bool isInterpretationOK() const;
106     bool isCompatibleWith(const Unit& other) const;
107     double convert(const Unit& target, double sourceVal) const;
108     std::string getCoarseRepr() const;
109   private:
110     std::string _coarse_repr;
111     mutable bool _is_interpreted;
112     mutable bool _is_interpretation_ok;
113     mutable DecompositionInUnitBase _decomp_in_base;
114   };
115 }
116
117 #endif