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.

372 lines
12 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2015 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. #define GMM_NOPERATION(a) { abs(&(a) != &(a)); }
  67. /* ********************************************************************** */
  68. /* Compilers detection. */
  69. /* ********************************************************************** */
  70. /* for sun CC 5.0 ...
  71. #if defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x500
  72. # include <stdcomp.h>
  73. # undef _RWSTD_NO_CLASS_PARTIAL_SPEC
  74. # undef _RWSTD_NO_NAMESPACE
  75. #endif
  76. */
  77. /* for VISUAL C++ ...
  78. #if defined(_MSC_VER) // && !defined(__MWERKS__)
  79. #define _GETFEM_MSVCPP_ _MSC_VER
  80. #endif
  81. */
  82. #if defined(__GNUC__)
  83. # if (__GNUC__ < 3)
  84. # error : PLEASE UPDATE g++ TO AT LEAST 3.0 VERSION
  85. # endif
  86. #endif
  87. /* ********************************************************************** */
  88. /* C++ Standard Headers. */
  89. /* ********************************************************************** */
  90. #include <clocale>
  91. #include <cstdlib>
  92. #include <cstddef>
  93. #include <cmath>
  94. #include <cstring>
  95. #include <cctype>
  96. #include <cassert>
  97. #include <climits>
  98. #include <iostream>
  99. //#include <ios>
  100. #include <fstream>
  101. #include <ctime>
  102. #include <exception>
  103. #include <typeinfo>
  104. #include <stdexcept>
  105. #include <iterator>
  106. #include <algorithm>
  107. #include <vector>
  108. #include <deque>
  109. #include <string>
  110. #include <complex>
  111. #include <limits>
  112. #include <sstream>
  113. #include <numeric>
  114. #include <locale.h>
  115. #ifdef GETFEM_HAVE_OPENMP
  116. #include <omp.h>
  117. /**number of OpenMP threads*/
  118. inline size_t num_threads(){return omp_get_max_threads();}
  119. /**index of the current thread*/
  120. inline size_t this_thread() {return omp_get_thread_num();}
  121. /**is the program running in the parallel section*/
  122. inline bool me_is_multithreaded_now(){return static_cast<bool>(omp_in_parallel());}
  123. #else
  124. inline size_t num_threads(){return size_t(1);}
  125. inline size_t this_thread() {return size_t(0);}
  126. inline bool me_is_multithreaded_now(){return false;}
  127. #endif
  128. namespace gmm {
  129. using std::endl; using std::cout; using std::cerr;
  130. using std::ends; using std::cin;
  131. #ifdef _WIN32
  132. class standard_locale {
  133. std::string cloc;
  134. std::locale cinloc;
  135. public :
  136. inline standard_locale(void) : cinloc(cin.getloc())
  137. {
  138. if (!me_is_multithreaded_now()){
  139. cloc=setlocale(LC_NUMERIC, 0);
  140. setlocale(LC_NUMERIC,"C");
  141. }
  142. }
  143. inline ~standard_locale() {
  144. if (!me_is_multithreaded_now())
  145. setlocale(LC_NUMERIC, cloc.c_str());
  146. }
  147. };
  148. #else
  149. /**this is the above solutions for linux, but I still needs to be tested.*/
  150. //class standard_locale {
  151. // locale_t oldloc;
  152. // locale_t temploc;
  153. //public :
  154. // inline standard_locale(void) : oldloc(uselocale((locale_t)0))
  155. // {
  156. // temploc = newlocale(LC_NUMERIC, "C", NULL);
  157. // uselocale(temploc);
  158. // }
  159. // inline ~standard_locale()
  160. // {
  161. // uselocale(oldloc);
  162. // freelocale(temploc);
  163. // }
  164. //};
  165. class standard_locale {
  166. std::string cloc;
  167. std::locale cinloc;
  168. public :
  169. inline standard_locale(void)
  170. : cloc(setlocale(LC_NUMERIC, 0)), cinloc(cin.getloc())
  171. { setlocale(LC_NUMERIC,"C"); cin.imbue(std::locale("C")); }
  172. inline ~standard_locale()
  173. { setlocale(LC_NUMERIC, cloc.c_str()); cin.imbue(cinloc); }
  174. };
  175. #endif
  176. class stream_standard_locale {
  177. std::locale cloc;
  178. std::ios &io;
  179. public :
  180. inline stream_standard_locale(std::ios &i)
  181. : cloc(i.getloc()), io(i) { io.imbue(std::locale("C")); }
  182. inline ~stream_standard_locale() { io.imbue(cloc); }
  183. };
  184. /* ******************************************************************* */
  185. /* Clock functions. */
  186. /* ******************************************************************* */
  187. # if defined(HAVE_SYS_TIMES)
  188. inline double uclock_sec(void) {
  189. static double ttclk = 0.;
  190. if (ttclk == 0.) ttclk = sysconf(_SC_CLK_TCK);
  191. tms t; times(&t); return double(t.tms_utime) / ttclk;
  192. }
  193. # else
  194. inline double uclock_sec(void)
  195. { return double(clock())/double(CLOCKS_PER_SEC); }
  196. # endif
  197. /* ******************************************************************** */
  198. /* Fixed size integer types. */
  199. /* ******************************************************************** */
  200. // Remark : the test program dynamic_array tests the lenght of
  201. // resulting integers
  202. template <size_t s> struct fixed_size_integer_generator {
  203. typedef void int_base_type;
  204. typedef void uint_base_type;
  205. };
  206. template <> struct fixed_size_integer_generator<sizeof(char)> {
  207. typedef signed char int_base_type;
  208. typedef unsigned char uint_base_type;
  209. };
  210. template <> struct fixed_size_integer_generator<sizeof(short int)
  211. - ((sizeof(short int) == sizeof(char)) ? 78 : 0)> {
  212. typedef signed short int int_base_type;
  213. typedef unsigned short int uint_base_type;
  214. };
  215. template <> struct fixed_size_integer_generator<sizeof(int)
  216. - ((sizeof(int) == sizeof(short int)) ? 59 : 0)> {
  217. typedef signed int int_base_type;
  218. typedef unsigned int uint_base_type;
  219. };
  220. template <> struct fixed_size_integer_generator<sizeof(long)
  221. - ((sizeof(int) == sizeof(long)) ? 93 : 0)> {
  222. typedef signed long int_base_type;
  223. typedef unsigned long uint_base_type;
  224. };
  225. template <> struct fixed_size_integer_generator<sizeof(long long)
  226. - ((sizeof(long long) == sizeof(long)) ? 99 : 0)> {
  227. typedef signed long long int_base_type;
  228. typedef unsigned long long uint_base_type;
  229. };
  230. typedef fixed_size_integer_generator<1>::int_base_type int8_type;
  231. typedef fixed_size_integer_generator<1>::uint_base_type uint8_type;
  232. typedef fixed_size_integer_generator<2>::int_base_type int16_type;
  233. typedef fixed_size_integer_generator<2>::uint_base_type uint16_type;
  234. typedef fixed_size_integer_generator<4>::int_base_type int32_type;
  235. typedef fixed_size_integer_generator<4>::uint_base_type uint32_type;
  236. typedef fixed_size_integer_generator<8>::int_base_type int64_type;
  237. typedef fixed_size_integer_generator<8>::uint_base_type uint64_type;
  238. // #if INT_MAX == 32767
  239. // typedef signed int int16_type;
  240. // typedef unsigned int uint16_type;
  241. // #elif SHRT_MAX == 32767
  242. // typedef signed short int int16_type;
  243. // typedef unsigned short int uint16_type;
  244. // #else
  245. // # error "impossible to build a 16 bits integer"
  246. // #endif
  247. // #if INT_MAX == 2147483647
  248. // typedef signed int int32_type;
  249. // typedef unsigned int uint32_type;
  250. // #elif SHRT_MAX == 2147483647
  251. // typedef signed short int int32_type;
  252. // typedef unsigned short int uint32_type;
  253. // #elif LONG_MAX == 2147483647
  254. // typedef signed long int int32_type;
  255. // typedef unsigned long int uint32_type;
  256. // #else
  257. // # error "impossible to build a 32 bits integer"
  258. // #endif
  259. // #if INT_MAX == 9223372036854775807L || INT_MAX == 9223372036854775807
  260. // typedef signed int int64_type;
  261. // typedef unsigned int uint64_type;
  262. // #elif LONG_MAX == 9223372036854775807L || LONG_MAX == 9223372036854775807
  263. // typedef signed long int int64_type;
  264. // typedef unsigned long int uint64_type;
  265. // #elif LLONG_MAX == 9223372036854775807LL || LLONG_MAX == 9223372036854775807L || LLONG_MAX == 9223372036854775807
  266. // typedef signed long long int int64_type;
  267. // typedef unsigned long long int uint64_type;
  268. // #else
  269. // # error "impossible to build a 64 bits integer"
  270. // #endif
  271. #if defined(__GNUC__) && !defined(__ICC)
  272. /*
  273. g++ can issue a warning at each usage of a function declared with this special attribute
  274. (also works with typedefs and variable declarations)
  275. */
  276. # define IS_DEPRECATED __attribute__ ((__deprecated__))
  277. /*
  278. the specified function is inlined at any optimization level
  279. */
  280. # define ALWAYS_INLINE __attribute__((always_inline))
  281. #else
  282. # define IS_DEPRECATED
  283. # define ALWAYS_INLINE
  284. #endif
  285. }
  286. /* ******************************************************************** */
  287. /* Import/export classes and interfaces from a shared library */
  288. /* ******************************************************************** */
  289. #if defined(EXPORTED_TO_SHARED_LIB)
  290. # if defined(_MSC_VER) || defined(__INTEL_COMPILER)
  291. # define APIDECL __declspec(dllexport)
  292. # elif defined(__GNUC__)
  293. # define __attribute__((visibility("default")))
  294. # else
  295. # define APIDECL
  296. # endif
  297. # if defined(IMPORTED_FROM_SHARED_LIB)
  298. # error INTENTIONAL COMPILCATION ERROR, DLL IMPORT AND EXPORT ARE INCOMPITABLE
  299. # endif
  300. #endif
  301. #if defined(IMPORTED_FROM_SHARED_LIB)
  302. # if defined(_MSC_VER) || defined(__INTEL_COMPILER)
  303. # define APIDECL __declspec(dllimport)
  304. # else
  305. # define APIDECL
  306. # endif
  307. # if defined(EXPORTED_TO_SHARED_LIB)
  308. # error INTENTIONAL COMPILCATION ERROR, DLL IMPORT AND EXPORT ARE INCOMPITABLE
  309. # endif
  310. #endif
  311. #ifndef EXPORTED_TO_SHARED_LIB
  312. # ifndef IMPORTED_FROM_SHARED_LIB
  313. # define APIDECL //empty, used during static linking
  314. # endif
  315. #endif
  316. #endif /* GMM_STD_H__ */