MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
mathtyp.h
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbutil/mathtyp.h,v 1.36 2017/04/28 17:59:55 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #ifndef MATHTYP_H
33 #define MATHTYP_H
34 
35 #include <string>
36 #include "except.h"
37 
38 /* definizione dei tipi */
39 typedef double Real;
40 typedef int Int;
41 
42 /* valori con tipo */
43 class TypedValue {
44 public:
45  enum Type {
47 
52 
54  };
55 
56  enum TypeModifier {
58 
60 
62  };
63 
64  class ErrUnknownType : public MBDynErrBase {
65  public:
67  };
68  class ErrWrongType : public MBDynErrBase {
69  public:
72  const TypedValue::Type& to,
73  const TypedValue::Type& from,
74  const std::string r = std::string());
75  };
76  class ErrUnknownValue : public MBDynErrBase {
77  public:
79  };
81  public:
83  };
84 
85 protected:
87  bool bConst;
88  union {
89  Int i;
91  } v;
92  std::string s;
93 
94 public:
95  TypedValue(void);
96  ~TypedValue(void);
97  TypedValue(const bool& b, bool isConst = false);
98  TypedValue(const Int& i, bool isConst = false);
99  TypedValue(const Real& r, bool isConst = false);
100  TypedValue(const std::string& s, bool isConst = false);
101  TypedValue(const TypedValue::Type t, bool isConst = false);
102  TypedValue(const TypedValue& var);
103 
104  TypedValue& operator = (const TypedValue& var);
105  TypedValue& Cast(const TypedValue& var, bool bErr = false);
106 
107  TypedValue::Type GetType(void) const;
108  const char *const GetTypeName(void) const;
109  static const char *const GetTypeName(TypedValue::Type t);
110  bool Const(void) const;
111  bool GetBool(void) const;
112  Int GetInt(void) const;
113  Real GetReal(void) const;
114  const std::string& GetString(void) const;
115 
116  void SetType(TypedValue::Type t, bool isConst = false);
117  void SetConst(bool isConst = true, bool bForce = false);
118  const TypedValue& Set(const bool& b);
119  const TypedValue& Set(const Int& i);
120  const TypedValue& Set(const Real& r);
121  const TypedValue& Set(const std::string& s);
122 
123  bool operator && (const TypedValue& v) const;
124  bool operator || (const TypedValue& v) const;
125  bool operator > (const TypedValue& v) const;
126  bool operator >= (const TypedValue& v) const;
127  bool operator == (const TypedValue& v) const;
128  bool operator <= (const TypedValue& v) const;
129  bool operator < (const TypedValue& v) const;
130  bool operator != (const TypedValue& v) const;
131 
132  TypedValue operator + (const TypedValue& v) const;
133  TypedValue operator - (const TypedValue& v) const;
134  TypedValue operator * (const TypedValue& v) const;
135  TypedValue operator / (const TypedValue& v) const;
136  TypedValue operator % (const TypedValue& v) const;
137 
138  const TypedValue& operator += (const TypedValue& v);
139  const TypedValue& operator -= (const TypedValue& v);
140  const TypedValue& operator *= (const TypedValue& v);
141  const TypedValue& operator /= (const TypedValue& v);
142  const TypedValue& operator %= (const TypedValue& v);
143 };
144 
145 extern bool operator ! (const TypedValue& v);
146 extern TypedValue operator - (const TypedValue& v);
147 extern TypedValue operator + (const TypedValue& v);
148 extern std::ostream& operator << (std::ostream& out, const TypedValue& v);
149 
150 
151 /* classe per la memorizzazione delle variabili */
152 class NamedValue {
153 private:
154  char *name;
155 
156  void AllocName(const char *const s);
157 
158 public:
159  NamedValue(const char *const s);
160  virtual ~NamedValue(void);
161 
162  virtual bool IsVar(void) const;
163 
164  const char *GetName(void) const;
165  virtual TypedValue::Type GetType(void) const = 0;
166  virtual const char *const GetTypeName(void) const;
167  virtual TypedValue GetVal(void) const = 0;
168 
169  // Const() == true means its value cannot be changed by the caller
170  virtual bool Const(void) const = 0;
171  // MayChange() == true means its value may change even if it is Const()
172  virtual bool MayChange(void) const = 0;
173 };
174 
175 class Var : public NamedValue {
176 private:
178 
179 public:
180  Var(const char* const s, const TypedValue& v);
181  Var(const char* const s, const bool& b);
182  Var(const char* const s, const Int& v);
183  Var(const char* const s, const Real& v);
184  Var(const char* const s, const std::string& v);
185  ~Var(void);
186 
187  bool IsVar(void) const;
188 
189  TypedValue::Type GetType(void) const;
190  bool Const(void) const;
191  bool MayChange(void) const;
192  TypedValue GetVal(void) const;
193 
194  void SetVal(const bool& b);
195  void SetVal(const Int& v);
196  void SetVal(const Real& v);
197  void SetVal(const std::string& v);
198  void SetVal(const TypedValue& v);
199  void Cast(const TypedValue& v, bool bErr = false);
200 };
201 
202 #endif /* MATHTYP_H */
203 
TypedValue::Type GetType(void) const
Definition: mathp.cc:1805
TypedValue operator+(const TypedValue &v) const
Definition: mathp.cc:1444
bool IsVar(void) const
Definition: mathp.cc:1799
TypedValue operator+(const TypedValue &v)
Definition: mathp.cc:1689
Definition: mathtyp.h:175
Real GetReal(void) const
Definition: mathp.cc:1228
std::ostream & operator<<(std::ostream &out, const TypedValue &v)
Definition: mathp.cc:1695
const TypedValue & operator-=(const TypedValue &v)
Definition: mathp.cc:1580
TypedValue::Type GetType(void) const
Definition: mathp.cc:1155
virtual TypedValue::Type GetType(void) const =0
bool Const(void) const
Definition: mathp.cc:1811
#define MBDYN_EXCEPT_ARGS_PASSTHRU
Definition: except.h:55
const TypedValue & Set(const bool &b)
Definition: mathp.cc:1285
TypedValue::Type type
Definition: mathtyp.h:86
~TypedValue(void)
Definition: mathp.cc:880
virtual TypedValue GetVal(void) const =0
TypedValue & operator=(const TypedValue &var)
Definition: mathp.cc:961
Int i
Definition: mathtyp.h:89
bool operator>(const TypedValue &v) const
Definition: mathp.cc:1408
~Var(void)
Definition: mathp.cc:1793
union TypedValue::@14 v
#define MBDYN_EXCEPT_ARGS_DECL
Definition: except.h:43
bool operator!(const TypedValue &v)
Definition: mathp.cc:1657
TypedValue & Cast(const TypedValue &var, bool bErr=false)
Definition: mathp.cc:1032
const TypedValue & operator*=(const TypedValue &v)
Definition: mathp.cc:1598
bool operator<(const TypedValue &v) const
Definition: mathp.cc:1432
const char *const GetTypeName(void) const
Definition: mathp.cc:1176
int Int
Definition: mathtyp.h:40
TypedValue operator/(const TypedValue &v) const
Definition: mathp.cc:1504
void Cast(const TypedValue &v, bool bErr=false)
Definition: mathp.cc:1859
TypedValue(void)
Definition: mathp.cc:874
void AllocName(const char *const s)
Definition: mathp.cc:1738
TypedValue GetVal(void) const
Definition: mathp.cc:1823
ErrWrongType(const char *file, int line, const char *func, const std::string r=std::string())
Definition: mathtyp.h:70
ErrConstraintViolation(const char *file, int line, const char *func, const std::string r=std::string())
Definition: mathtyp.h:82
bool operator<=(const TypedValue &v) const
Definition: mathp.cc:1426
virtual ~NamedValue(void)
Definition: mathp.cc:1731
ErrUnknownValue(const char *file, int line, const char *func, const std::string r=std::string())
Definition: mathtyp.h:78
virtual bool MayChange(void) const =0
NamedValue(const char *const s)
Definition: mathp.cc:1725
Var(const char *const s, const TypedValue &v)
Definition: mathp.cc:1763
char * name
Definition: mathtyp.h:154
const std::string & GetString(void) const
Definition: mathp.cc:1247
bool bConst
Definition: mathtyp.h:87
ErrUnknownType(const char *file, int line, const char *func, const std::string r=std::string())
Definition: mathtyp.h:66
TypedValue value
Definition: mathtyp.h:177
TypedValue operator%(const TypedValue &v) const
Definition: mathp.cc:1517
const TypedValue & operator+=(const TypedValue &v)
Definition: mathp.cc:1534
bool operator>=(const TypedValue &v) const
Definition: mathp.cc:1414
bool operator||(const TypedValue &v) const
Definition: mathp.cc:1402
bool operator!=(const TypedValue &v) const
Definition: mathp.cc:1438
TypedValue operator*(const TypedValue &v) const
Definition: mathp.cc:1491
virtual bool Const(void) const =0
bool operator&&(const TypedValue &v) const
Definition: mathp.cc:1396
TypedValue operator-(const TypedValue &v) const
Definition: mathp.cc:1478
std::string s
Definition: mathtyp.h:92
Real r
Definition: mathtyp.h:90
virtual const char *const GetTypeName(void) const
Definition: mathp.cc:1758
void SetConst(bool isConst=true, bool bForce=false)
Definition: mathp.cc:1275
bool operator==(const TypedValue &v) const
Definition: mathp.cc:1420
#define MBDYN_EXCEPT_ARGS_DECL_NOOPT
Definition: except.h:41
const char * GetName(void) const
Definition: mathp.cc:1751
Int GetInt(void) const
Definition: mathp.cc:1209
void SetVal(const bool &b)
Definition: mathp.cc:1829
bool GetBool(void) const
Definition: mathp.cc:1188
double Real
Definition: mathtyp.h:39
const TypedValue & operator%=(const TypedValue &v)
Definition: mathp.cc:1634
bool Const(void) const
Definition: mathp.cc:1182
virtual bool IsVar(void) const
Definition: mathp.cc:1745
TypedValue operator-(const TypedValue &v)
Definition: mathp.cc:1667
bool MayChange(void) const
Definition: mathp.cc:1817
void SetType(TypedValue::Type t, bool isConst=false)
Definition: mathp.cc:1264
const TypedValue & operator/=(const TypedValue &v)
Definition: mathp.cc:1616
TypeModifier
Definition: mathtyp.h:56