Code_TYMPAN  4.4.0
Industrial site acoustic simulation
macros.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2014> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 #ifndef TY_MACROS
17 #define TY_MACROS
18 
19 #include <assert.h>
20 #include <complex>
21 
22 #include "Tympan/core/defines.h"
23 
24 // Type pour les valeurs complexes.
25 typedef std::complex<double> TYComplex;
26 
27 #define CPLX_UN TYComplex(1.0, 0.0)
28 #define CPLX_MUN TYComplex(-1.0, 0.0)
29 #define CPLX_J TYComplex(0.0, 1.0)
30 
34 inline TYComplex cotanh(const TYComplex& valeur)
35 {
36  double reel = 0.0;
37  double imag = 0.0;
38 
39  if (valeur.real() >= 100.0)
40  {
41  reel = 100.0;
42  }
43  else if (valeur.real() <= -100.0)
44  {
45  reel = -100.0;
46  }
47  else // -100 <= valeur.real() <= 100
48  {
49  reel = valeur.real();
50  }
51 
52  imag = valeur.imag();
53 
54  TYComplex valeurTravail = TYComplex(reel, imag);
55 
56  return (std::cosh(valeurTravail) / std::sinh(valeurTravail));
57 }
58 
66 inline std::string indentNbToStr(int indentNb)
67 {
68  std::string str = "";
69 
70  while (indentNb--)
71  {
72  str += "\t";
73  }
74 
75  return str;
76 }
77 
78 #if TY_ARCH_TYPE == TY_ARCHITECTURE_64
86 inline std::string uintToStr(unsigned int val)
87 {
88  std::ostringstream oss;
89  oss << val;
90  return oss.str();
91 }
92 
100 inline std::string uintToStr(size_t val)
101 {
102  std::ostringstream oss;
103  oss << val;
104  return oss.str();
105 }
106  #if TY_COMPILER == TY_COMPILER_MSVC
114 inline std::string uintToStr(unsigned long val)
115 {
116  std::ostringstream oss;
117  oss << val;
118  return oss.str();
119 }
120  #endif
121 #else
129 inline std::string uintToStr(size_t val)
130 {
131  std::ostringstream oss;
132  oss << val;
133  return oss.str();
134 }
135 
143 inline std::string uintToStr(unsigned long val)
144 {
145  std::ostringstream oss;
146  oss << val;
147  return oss.str();
148 }
149 #endif
150 
158 inline std::string intToStr(int val)
159 {
160  std::ostringstream oss;
161  oss << val;
162  return oss.str();
163 }
164 
173 inline std::string floatToStr(float val)
174 {
175  std::ostringstream oss;
176  oss << val;
177  return oss.str();
178 }
179 
188 inline std::string doubleToStr(double val)
189 {
190  std::ostringstream oss;
191  oss << val;
192  return oss.str();
193 }
194 
205 inline std::string doubleToStrPre(double val, int precision = 2)
206 {
207  std::ostringstream oss;
208  oss.precision(precision);
209  oss << std::fixed << val;
210  return oss.str();
211 }
212 
213 template <class T> inline T fromString(const std::string& str, std::ios_base& (*f)(std::ios_base&) = std::dec)
214 {
215  T value;
216  std::istringstream iss(str);
217  iss >> f >> value;
218  return value;
219 }
220 
221 #ifndef SAFE_DELETE
225  #define SAFE_DELETE(_p) \
226  { \
227  if ((_p) != NULL) \
228  { \
229  delete (_p); \
230  (_p) = NULL; \
231  } \
232  }
233 #endif
234 
235 #ifndef SAFE_DELETE_LIST
239  #define SAFE_DELETE_LIST(_p) \
240  { \
241  if ((_p) != NULL) \
242  { \
243  delete[] (_p); \
244  (_p) = NULL; \
245  } \
246  }
247 #endif
248 
249 #endif // TY_MACROS
std::string floatToStr(float val)
Definition: macros.h:173
std::string uintToStr(unsigned int val)
Definition: macros.h:86
std::complex< double > TYComplex
Definition: macros.h:25
std::string doubleToStrPre(double val, int precision=2)
Definition: macros.h:205
TYComplex cotanh(const TYComplex &valeur)
Definition: macros.h:34
T fromString(const std::string &str, std::ios_base &(*f)(std::ios_base &)=std::dec)
Definition: macros.h:213
std::string doubleToStr(double val)
Definition: macros.h:188
std::string indentNbToStr(int indentNb)
Definition: macros.h:66
std::string intToStr(int val)
Definition: macros.h:158