You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
9.7 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2012 Yves Renard
  4. This file is a part of GETFEM++
  5. Getfem++ is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version along with the GCC Runtime Library
  9. Exception either version 3.1 or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License and GCC Runtime Library Exception for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  17. As a special exception, you may use this file as it is a part of a free
  18. software library without restriction. Specifically, if other files
  19. instantiate templates or use macros or inline functions from this file,
  20. or you compile this file and link it with other files to produce an
  21. executable, this file does not by itself cause the resulting executable
  22. to be covered by the GNU Lesser General Public License. This exception
  23. does not however invalidate any other reasons why the executable file
  24. might be covered by the GNU Lesser General Public License.
  25. ===========================================================================*/
  26. /**@file gmm_std.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>,
  28. @author Julien Pommier <Julien.Pommier@insa-toulouse.fr>
  29. @date June 01, 1995.
  30. @brief basic setup for gmm (includes, typedefs etc.)
  31. */
  32. #ifndef GMM_STD_H__
  33. #define GMM_STD_H__
  34. #ifndef __USE_STD_IOSTREAM
  35. # define __USE_STD_IOSTREAM
  36. #endif
  37. #ifndef __USE_BSD
  38. # define __USE_BSD
  39. #endif
  40. #ifndef __USE_ISOC99
  41. # define __USE_ISOC99
  42. #endif
  43. #if defined(_MSC_VER) && _MSC_VER >= 1400 // Secure versions for VC++
  44. # define GMM_SECURE_CRT
  45. # define SECURE_NONCHAR_SSCANF sscanf_s
  46. # define SECURE_NONCHAR_FSCANF fscanf_s
  47. # define SECURE_STRNCPY(a, la, b, lb) strncpy_s(a, la, b, lb)
  48. # define SECURE_FOPEN(F, filename, mode) (*(F) = 0, fopen_s(F, filename, mode))
  49. # define SECURE_SPRINTF1(S, l, st, p1) sprintf_s(S, l, st, p1)
  50. # define SECURE_SPRINTF2(S, l, st, p1, p2) sprintf_s(S, l, st, p1, p2)
  51. # define SECURE_SPRINTF4(S, l, st, p1, p2, p3, p4) sprintf_s(S, l, st, p1, p2, p3, p4)
  52. # define SECURE_STRDUP(s) _strdup(s)
  53. # ifndef _SCL_SECURE_NO_DEPRECATE
  54. # error Add the option /D_SCL_SECURE_NO_DEPRECATE to the compilation command
  55. # endif
  56. #else
  57. # define SECURE_NONCHAR_SSCANF sscanf
  58. # define SECURE_NONCHAR_FSCANF fscanf
  59. # define SECURE_STRNCPY(a, la, b, lb) strncpy(a, b, lb)
  60. # define SECURE_FOPEN(F, filename, mode) ((*(F)) = fopen(filename, mode))
  61. # define SECURE_SPRINTF1(S, l, st, p1) sprintf(S, st, p1)
  62. # define SECURE_SPRINTF2(S, l, st, p1, p2) sprintf(S, st, p1, p2)
  63. # define SECURE_SPRINTF4(S, l, st, p1, p2, p3, p4) sprintf(S, st, p1, p2, p3, p4)
  64. # define SECURE_STRDUP(s) strdup(s)
  65. #endif
  66. #if !defined(GMM_USES_MPI) && GETFEM_PARA_LEVEL > 0
  67. # define GMM_USES_MPI
  68. #endif
  69. /* ********************************************************************** */
  70. /* Compilers detection. */
  71. /* ********************************************************************** */
  72. /* for sun CC 5.0 ...
  73. #if defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x500
  74. # include <stdcomp.h>
  75. # undef _RWSTD_NO_CLASS_PARTIAL_SPEC
  76. # undef _RWSTD_NO_NAMESPACE
  77. #endif
  78. */
  79. /* for VISUAL C++ ...
  80. #if defined(_MSC_VER) // && !defined(__MWERKS__)
  81. #define _GETFEM_MSVCPP_ _MSC_VER
  82. #endif
  83. */
  84. #if defined(__GNUC__)
  85. # if (__GNUC__ < 3)
  86. # error : PLEASE UPDATE g++ TO AT LEAST 3.0 VERSION
  87. # endif
  88. #endif
  89. /* ********************************************************************** */
  90. /* C++ Standard Headers. */
  91. /* ********************************************************************** */
  92. #include <clocale>
  93. #include <cstdlib>
  94. #include <cstddef>
  95. #include <cmath>
  96. #include <cstring>
  97. #include <cctype>
  98. #include <cassert>
  99. #include <climits>
  100. #include <iostream>
  101. //#include <ios>
  102. #include <fstream>
  103. #include <ctime>
  104. #include <exception>
  105. #include <typeinfo>
  106. #include <stdexcept>
  107. #include <iterator>
  108. #include <algorithm>
  109. #include <vector>
  110. #include <deque>
  111. #include <string>
  112. #include <complex>
  113. #include <limits>
  114. #include <sstream>
  115. #include <numeric>
  116. namespace gmm {
  117. using std::endl; using std::cout; using std::cerr;
  118. using std::ends; using std::cin;
  119. /* ********************************************************************* */
  120. /* Change locale temporarily. */
  121. /* ********************************************************************* */
  122. class standard_locale {
  123. std::string cloc;
  124. std::locale cinloc;
  125. public :
  126. inline standard_locale(void)
  127. : cloc(setlocale(LC_NUMERIC, 0)), cinloc(cin.getloc())
  128. { setlocale(LC_NUMERIC,"C"); cin.imbue(std::locale("C")); }
  129. inline ~standard_locale()
  130. { setlocale(LC_NUMERIC, cloc.c_str()); cin.imbue(cinloc); }
  131. };
  132. class stream_standard_locale {
  133. std::locale cloc;
  134. std::ios &io;
  135. public :
  136. inline stream_standard_locale(std::ios &i)
  137. : cloc(i.getloc()), io(i) { io.imbue(std::locale("C")); }
  138. inline ~stream_standard_locale() { io.imbue(cloc); }
  139. };
  140. /* ******************************************************************* */
  141. /* Clock functions. */
  142. /* ******************************************************************* */
  143. # if defined(HAVE_SYS_TIMES)
  144. inline double uclock_sec(void) {
  145. static double ttclk = 0.;
  146. if (ttclk == 0.) ttclk = sysconf(_SC_CLK_TCK);
  147. tms t; times(&t); return double(t.tms_utime) / ttclk;
  148. }
  149. # else
  150. inline double uclock_sec(void)
  151. { return double(clock())/double(CLOCKS_PER_SEC); }
  152. # endif
  153. /* ******************************************************************** */
  154. /* Fixed size integer types. */
  155. /* ******************************************************************** */
  156. // Remark : the test program dynamic_array tests the lenght of
  157. // resulting integers
  158. template <size_t s> struct fixed_size_integer_generator {
  159. typedef void int_base_type;
  160. typedef void uint_base_type;
  161. };
  162. template <> struct fixed_size_integer_generator<sizeof(char)> {
  163. typedef signed char int_base_type;
  164. typedef unsigned char uint_base_type;
  165. };
  166. template <> struct fixed_size_integer_generator<sizeof(short int)
  167. - ((sizeof(short int) == sizeof(char)) ? 78 : 0)> {
  168. typedef signed short int int_base_type;
  169. typedef unsigned short int uint_base_type;
  170. };
  171. template <> struct fixed_size_integer_generator<sizeof(int)
  172. - ((sizeof(int) == sizeof(short int)) ? 59 : 0)> {
  173. typedef signed int int_base_type;
  174. typedef unsigned int uint_base_type;
  175. };
  176. template <> struct fixed_size_integer_generator<sizeof(long)
  177. - ((sizeof(int) == sizeof(long)) ? 93 : 0)> {
  178. typedef signed long int_base_type;
  179. typedef unsigned long uint_base_type;
  180. };
  181. template <> struct fixed_size_integer_generator<sizeof(long long)
  182. - ((sizeof(long long) == sizeof(long)) ? 99 : 0)> {
  183. typedef signed long long int_base_type;
  184. typedef unsigned long long uint_base_type;
  185. };
  186. typedef fixed_size_integer_generator<1>::int_base_type int8_type;
  187. typedef fixed_size_integer_generator<1>::uint_base_type uint8_type;
  188. typedef fixed_size_integer_generator<2>::int_base_type int16_type;
  189. typedef fixed_size_integer_generator<2>::uint_base_type uint16_type;
  190. typedef fixed_size_integer_generator<4>::int_base_type int32_type;
  191. typedef fixed_size_integer_generator<4>::uint_base_type uint32_type;
  192. typedef fixed_size_integer_generator<8>::int_base_type int64_type;
  193. typedef fixed_size_integer_generator<8>::uint_base_type uint64_type;
  194. // #if INT_MAX == 32767
  195. // typedef signed int int16_type;
  196. // typedef unsigned int uint16_type;
  197. // #elif SHRT_MAX == 32767
  198. // typedef signed short int int16_type;
  199. // typedef unsigned short int uint16_type;
  200. // #else
  201. // # error "impossible to build a 16 bits integer"
  202. // #endif
  203. // #if INT_MAX == 2147483647
  204. // typedef signed int int32_type;
  205. // typedef unsigned int uint32_type;
  206. // #elif SHRT_MAX == 2147483647
  207. // typedef signed short int int32_type;
  208. // typedef unsigned short int uint32_type;
  209. // #elif LONG_MAX == 2147483647
  210. // typedef signed long int int32_type;
  211. // typedef unsigned long int uint32_type;
  212. // #else
  213. // # error "impossible to build a 32 bits integer"
  214. // #endif
  215. // #if INT_MAX == 9223372036854775807L || INT_MAX == 9223372036854775807
  216. // typedef signed int int64_type;
  217. // typedef unsigned int uint64_type;
  218. // #elif LONG_MAX == 9223372036854775807L || LONG_MAX == 9223372036854775807
  219. // typedef signed long int int64_type;
  220. // typedef unsigned long int uint64_type;
  221. // #elif LLONG_MAX == 9223372036854775807LL || LLONG_MAX == 9223372036854775807L || LLONG_MAX == 9223372036854775807
  222. // typedef signed long long int int64_type;
  223. // typedef unsigned long long int uint64_type;
  224. // #else
  225. // # error "impossible to build a 64 bits integer"
  226. // #endif
  227. #if defined(__GNUC__) && !defined(__ICC)
  228. /*
  229. g++ can issue a warning at each usage of a function declared with this special attribute
  230. (also works with typedefs and variable declarations)
  231. */
  232. # define IS_DEPRECATED __attribute__ ((__deprecated__))
  233. /*
  234. the specified function is inlined at any optimization level
  235. */
  236. # define ALWAYS_INLINE __attribute__((always_inline))
  237. #else
  238. # define IS_DEPRECATED
  239. # define ALWAYS_INLINE
  240. #endif
  241. }
  242. #endif /* GMM_STD_H__ */