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 {
46 		VAR_UNKNOWN = -1,
47 
48 		VAR_BOOL,
49 		VAR_INT,
50 		VAR_REAL,
51 		VAR_STRING,
52 
53 		VAR_LAST
54 	};
55 
56 	enum TypeModifier {
57 		MOD_UNKNOWN = -1,
58 
59 		MOD_CONST,
60 
61 		MOD_LAST
62 	};
63 
64 	class ErrUnknownType : public MBDynErrBase {
65 	public:
ErrUnknownType(MBDYN_EXCEPT_ARGS_DECL)66 		ErrUnknownType(MBDYN_EXCEPT_ARGS_DECL) : MBDynErrBase(MBDYN_EXCEPT_ARGS_PASSTHRU) {};
67 	};
68 	class ErrWrongType : public MBDynErrBase {
69 	public:
ErrWrongType(MBDYN_EXCEPT_ARGS_DECL)70 		ErrWrongType(MBDYN_EXCEPT_ARGS_DECL) : MBDynErrBase(MBDYN_EXCEPT_ARGS_PASSTHRU) {};
71 		ErrWrongType(MBDYN_EXCEPT_ARGS_DECL_NOOPT,
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:
ErrUnknownValue(MBDYN_EXCEPT_ARGS_DECL)78 		ErrUnknownValue(MBDYN_EXCEPT_ARGS_DECL) : MBDynErrBase(MBDYN_EXCEPT_ARGS_PASSTHRU) {};
79 	};
80 	class ErrConstraintViolation : public MBDynErrBase {
81 	public:
ErrConstraintViolation(MBDYN_EXCEPT_ARGS_DECL)82 		ErrConstraintViolation(MBDYN_EXCEPT_ARGS_DECL) : MBDynErrBase(MBDYN_EXCEPT_ARGS_PASSTHRU) {};
83 	};
84 
85 protected:
86 	TypedValue::Type type;
87 	bool bConst;
88 	union {
89 		Int i;
90 		Real r;
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:
177 	TypedValue value;
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 
204