GetFEM++  5.3
gmm_std.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2002-2017 Yves Renard
5 
6  This file is a part of GetFEM++
7 
8  GetFEM++ is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program; if not, write to the Free Software Foundation,
19  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, you may use this file as it is a part of a free
22  software library without restriction. Specifically, if other files
23  instantiate templates or use macros or inline functions from this file,
24  or you compile this file and link it with other files to produce an
25  executable, this file does not by itself cause the resulting executable
26  to be covered by the GNU Lesser General Public License. This exception
27  does not however invalidate any other reasons why the executable file
28  might be covered by the GNU Lesser General Public License.
29 
30 ===========================================================================*/
31 
32 /**@file gmm_std.h
33 @author Yves Renard <Yves.Renard@insa-lyon.fr>,
34 @author Julien Pommier <Julien.Pommier@insa-toulouse.fr>
35 @date June 01, 1995.
36 @brief basic setup for gmm (includes, typedefs etc.)
37 */
38 #ifndef GMM_STD_H__
39 #define GMM_STD_H__
40 
41 #ifndef __USE_STD_IOSTREAM
42 # define __USE_STD_IOSTREAM
43 #endif
44 
45 #ifndef __USE_BSD
46 # define __USE_BSD
47 #endif
48 
49 #ifndef __USE_ISOC99
50 # define __USE_ISOC99
51 #endif
52 
53 #if defined(_MSC_VER) && _MSC_VER >= 1400 // Secure versions for VC++
54 # define GMM_SECURE_CRT
55 # define SECURE_NONCHAR_SSCANF sscanf_s
56 # define SECURE_NONCHAR_FSCANF fscanf_s
57 # define SECURE_STRNCPY(a, la, b, lb) strncpy_s(a, la, b, lb)
58 # define SECURE_FOPEN(F, filename, mode) (*(F) = 0, fopen_s(F, filename, mode))
59 # define SECURE_SPRINTF1(S, l, st, p1) sprintf_s(S, l, st, p1)
60 # define SECURE_SPRINTF2(S, l, st, p1, p2) sprintf_s(S, l, st, p1, p2)
61 # define SECURE_SPRINTF4(S, l, st, p1, p2, p3, p4) sprintf_s(S, l, st, p1, p2, p3, p4)
62 # define SECURE_STRDUP(s) _strdup(s)
63 # ifndef _SCL_SECURE_NO_DEPRECATE
64 # error Add the option /D_SCL_SECURE_NO_DEPRECATE to the compilation command
65 # endif
66 #else
67 # define SECURE_NONCHAR_SSCANF sscanf
68 # define SECURE_NONCHAR_FSCANF fscanf
69 # define SECURE_STRNCPY(a, la, b, lb) strncpy(a, b, lb)
70 # define SECURE_FOPEN(F, filename, mode) ((*(F)) = fopen(filename, mode))
71 # define SECURE_SPRINTF1(S, l, st, p1) sprintf(S, st, p1)
72 # define SECURE_SPRINTF2(S, l, st, p1, p2) sprintf(S, st, p1, p2)
73 # define SECURE_SPRINTF4(S, l, st, p1, p2, p3, p4) sprintf(S, st, p1, p2, p3, p4)
74 # define SECURE_STRDUP(s) strdup(s)
75 #endif
76 
77 inline void GMM_NOPERATION_(int) { }
78 #define GMM_NOPERATION(a) { GMM_NOPERATION_(abs(&(a) != &(a))); }
79 
80 /* ********************************************************************** */
81 /* Compilers detection. */
82 /* ********************************************************************** */
83 
84 /* for VISUAL C++ ...
85 #if defined(_MSC_VER) // && !defined(__MWERKS__)
86 #define _GETFEM_MSVCPP_ _MSC_VER
87 #endif
88 */
89 
90 #if defined(__GNUC__)
91 # if (__GNUC__ < 4)
92 # error : PLEASE UPDATE g++ TO AT LEAST 4.8 VERSION
93 # endif
94 #endif
95 
96 /* ********************************************************************** */
97 /* C++ Standard Headers. */
98 /* ********************************************************************** */
99 #include <clocale>
100 #include <cstdlib>
101 #include <cstddef>
102 #include <cmath>
103 #include <cstring>
104 #include <cctype>
105 #include <cassert>
106 #include <climits>
107 #include <iostream>
108 //#include <ios>
109 #include <fstream>
110 #include <ctime>
111 #include <exception>
112 #include <typeinfo>
113 #include <stdexcept>
114 #include <iterator>
115 #include <algorithm>
116 #include <vector>
117 #include <deque>
118 #include <string>
119 #include <complex>
120 #include <limits>
121 #include <sstream>
122 #include <numeric>
123 #include <memory>
124 #include <array>
125 #include <locale.h>
126 
127 #include <gmm/gmm_arch_config.h>
128 
129 namespace std {
130 #if defined(__GNUC__) && (__cplusplus <= 201103L)
131  template<typename _Tp>
132  struct _MakeUniq
133  { typedef unique_ptr<_Tp> __single_object; };
134  template<typename _Tp>
135  struct _MakeUniq<_Tp[]>
136  { typedef unique_ptr<_Tp[]> __array; };
137  template<typename _Tp, size_t _Bound>
138  struct _MakeUniq<_Tp[_Bound]>
139  { struct __invalid_type { }; };
140  /// std::make_unique for single objects
141  template<typename _Tp, typename... _Args>
142  inline typename _MakeUniq<_Tp>::__single_object
143  make_unique(_Args&&... __args)
144  { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
145  /// std::make_unique for arrays of unknown bound
146  template<typename _Tp>
147  inline typename _MakeUniq<_Tp>::__array
148  make_unique(size_t __num)
149  { return unique_ptr<_Tp>(new typename remove_extent<_Tp>::type[__num]()); }
150  /// Disable std::make_unique for arrays of known bound
151  template<typename _Tp, typename... _Args>
152  inline typename _MakeUniq<_Tp>::__invalid_type
153  make_unique(_Args&&...) = delete;
154 #endif
155 
156 
157  // Should simply be replaced by std::shared_ptr<T[]> when it will be supported
158  // by the STL
159  template <typename T> class shared_array_ptr : shared_ptr<T> {
160  public:
161  shared_array_ptr() {}
162  shared_array_ptr(T *q) : std::shared_ptr<T>(q, default_delete<T[]>()) {}
163  template <typename Y> shared_array_ptr(const std::shared_ptr<Y> &p, T *q)
164  : std::shared_ptr<T>(p, q) {}
165  T *get() const { return shared_ptr<T>::get(); }
166  T& operator*() const { return shared_ptr<T>::operator*(); }
167  T* operator->() const { return shared_ptr<T>::operator->(); }
168  };
169 
170  template <typename T> shared_array_ptr<T> make_shared_array(size_t num)
171  { return shared_array_ptr<T>(new T[num]); }
172 }
173 
174 
175 
176 
177 #ifdef GMM_HAVE_OPENMP
178 
179 #include <omp.h>
180  /**number of OpenMP threads*/
181  inline size_t num_threads(){return omp_get_max_threads();}
182  /**index of the current thread*/
183  inline size_t this_thread() {return omp_get_thread_num();}
184  /**is the program running in the parallel section*/
185  inline bool me_is_multithreaded_now(){return static_cast<bool>(omp_in_parallel());}
186 #else
187  inline size_t num_threads(){return size_t(1);}
188  inline size_t this_thread() {return size_t(0);}
189  inline bool me_is_multithreaded_now(){return false;}
190 #endif
191 
192 namespace gmm {
193 
194  using std::endl; using std::cout; using std::cerr;
195  using std::ends; using std::cin; using std::isnan;
196 
197 #ifdef _WIN32
198 
199  class standard_locale {
200  std::string cloc;
201  std::locale cinloc;
202  public :
203  inline standard_locale(void) : cinloc(cin.getloc())
204  {
205  if (!me_is_multithreaded_now()){
206  cloc=setlocale(LC_NUMERIC, 0);
207  setlocale(LC_NUMERIC,"C");
208  }
209  }
210 
211  inline ~standard_locale() {
212  if (!me_is_multithreaded_now())
213  setlocale(LC_NUMERIC, cloc.c_str());
214 
215  }
216  };
217 #else
218  /**this is the above solutions for linux, but it still needs to be tested.*/
219  //class standard_locale {
220  // locale_t oldloc;
221  // locale_t temploc;
222 
223  //public :
224  // inline standard_locale(void) : oldloc(uselocale((locale_t)0))
225  // {
226  // temploc = newlocale(LC_NUMERIC, "C", NULL);
227  // uselocale(temploc);
228  // }
229 
230  // inline ~standard_locale()
231  // {
232  // uselocale(oldloc);
233  // freelocale(temploc);
234  // }
235  //};
236 
237 
239  std::string cloc;
240  std::locale cinloc;
241 
242  public :
243  inline standard_locale(void)
244  : cloc(setlocale(LC_NUMERIC, 0)), cinloc(cin.getloc())
245  { setlocale(LC_NUMERIC,"C"); cin.imbue(std::locale("C")); }
246  inline ~standard_locale()
247  { setlocale(LC_NUMERIC, cloc.c_str()); cin.imbue(cinloc); }
248  };
249 
250 
251 #endif
252 
253  class stream_standard_locale {
254  std::locale cloc;
255  std::ios &io;
256 
257  public :
258  inline stream_standard_locale(std::ios &i)
259  : cloc(i.getloc()), io(i) { io.imbue(std::locale("C")); }
260  inline ~stream_standard_locale() { io.imbue(cloc); }
261  };
262 
263 
264 
265 
266  /* ******************************************************************* */
267  /* Clock functions. */
268  /* ******************************************************************* */
269 
270 # if defined(HAVE_SYS_TIMES)
271  inline double uclock_sec(void) {
272  static double ttclk = 0.;
273  if (ttclk == 0.) ttclk = sysconf(_SC_CLK_TCK);
274  tms t; times(&t); return double(t.tms_utime) / ttclk;
275  }
276 # else
277  inline double uclock_sec(void)
278  { return double(clock())/double(CLOCKS_PER_SEC); }
279 # endif
280 
281  /* ******************************************************************** */
282  /* Fixed size integer types. */
283  /* ******************************************************************** */
284  // Remark : the test program dynamic_array tests the length of
285  // resulting integers
286 
287  template <size_t s> struct fixed_size_integer_generator {
288  typedef void int_base_type;
289  typedef void uint_base_type;
290  };
291 
292  template <> struct fixed_size_integer_generator<sizeof(char)> {
293  typedef signed char int_base_type;
294  typedef unsigned char uint_base_type;
295  };
296 
297  template <> struct fixed_size_integer_generator<sizeof(short int)
298  - ((sizeof(short int) == sizeof(char)) ? 78 : 0)> {
299  typedef signed short int int_base_type;
300  typedef unsigned short int uint_base_type;
301 };
302 
303 template <> struct fixed_size_integer_generator<sizeof(int)
304  - ((sizeof(int) == sizeof(short int)) ? 59 : 0)> {
305  typedef signed int int_base_type;
306  typedef unsigned int uint_base_type;
307  };
308 
309 template <> struct fixed_size_integer_generator<sizeof(long)
310  - ((sizeof(int) == sizeof(long)) ? 93 : 0)> {
311  typedef signed long int_base_type;
312  typedef unsigned long uint_base_type;
313  };
314 
315 template <> struct fixed_size_integer_generator<sizeof(long long)
316  - ((sizeof(long long) == sizeof(long)) ? 99 : 0)> {
317  typedef signed long long int_base_type;
318  typedef unsigned long long uint_base_type;
319  };
320 
321 typedef fixed_size_integer_generator<1>::int_base_type int8_type;
322 typedef fixed_size_integer_generator<1>::uint_base_type uint8_type;
323 typedef fixed_size_integer_generator<2>::int_base_type int16_type;
324 typedef fixed_size_integer_generator<2>::uint_base_type uint16_type;
325 typedef fixed_size_integer_generator<4>::int_base_type int32_type;
326 typedef fixed_size_integer_generator<4>::uint_base_type uint32_type;
327 typedef fixed_size_integer_generator<8>::int_base_type int64_type;
328 typedef fixed_size_integer_generator<8>::uint_base_type uint64_type;
329 
330 // #if INT_MAX == 32767
331 // typedef signed int int16_type;
332 // typedef unsigned int uint16_type;
333 // #elif SHRT_MAX == 32767
334 // typedef signed short int int16_type;
335 // typedef unsigned short int uint16_type;
336 // #else
337 // # error "impossible to build a 16 bits integer"
338 // #endif
339 
340 // #if INT_MAX == 2147483647
341 // typedef signed int int32_type;
342 // typedef unsigned int uint32_type;
343 // #elif SHRT_MAX == 2147483647
344 // typedef signed short int int32_type;
345 // typedef unsigned short int uint32_type;
346 // #elif LONG_MAX == 2147483647
347 // typedef signed long int int32_type;
348 // typedef unsigned long int uint32_type;
349 // #else
350 // # error "impossible to build a 32 bits integer"
351 // #endif
352 
353 // #if INT_MAX == 9223372036854775807L || INT_MAX == 9223372036854775807
354 // typedef signed int int64_type;
355 // typedef unsigned int uint64_type;
356 // #elif LONG_MAX == 9223372036854775807L || LONG_MAX == 9223372036854775807
357 // typedef signed long int int64_type;
358 // typedef unsigned long int uint64_type;
359 // #elif LLONG_MAX == 9223372036854775807LL || LLONG_MAX == 9223372036854775807L || LLONG_MAX == 9223372036854775807
360 // typedef signed long long int int64_type;
361 // typedef unsigned long long int uint64_type;
362 // #else
363 // # error "impossible to build a 64 bits integer"
364 // #endif
365 
366 #if defined(__GNUC__) && !defined(__ICC)
367 /*
368  g++ can issue a warning at each usage of a function declared with this special attribute
369  (also works with typedefs and variable declarations)
370 */
371 # define IS_DEPRECATED __attribute__ ((__deprecated__))
372 /*
373  the specified function is inlined at any optimization level
374 */
375 # define ALWAYS_INLINE __attribute__((always_inline))
376 #else
377 # define IS_DEPRECATED
378 # define ALWAYS_INLINE
379 #endif
380 
381 }
382 
383  /* ******************************************************************** */
384  /* Import/export classes and interfaces from a shared library */
385  /* ******************************************************************** */
386 
387 #if defined(EXPORTED_TO_SHARED_LIB)
388 # if defined(_MSC_VER) || defined(__INTEL_COMPILER)
389 # define APIDECL __declspec(dllexport)
390 # elif defined(__GNUC__)
391 # define __attribute__((visibility("default")))
392 # else
393 # define APIDECL
394 # endif
395 # if defined(IMPORTED_FROM_SHARED_LIB)
396 # error INTENTIONAL COMPILCATION ERROR, DLL IMPORT AND EXPORT ARE INCOMPITABLE
397 # endif
398 #endif
399 
400 #if defined(IMPORTED_FROM_SHARED_LIB)
401 # if defined(_MSC_VER) || defined(__INTEL_COMPILER)
402 # define APIDECL __declspec(dllimport)
403 # else
404 # define APIDECL
405 # endif
406 # if defined(EXPORTED_TO_SHARED_LIB)
407 # error INTENTIONAL COMPILCATION ERROR, DLL IMPORT AND EXPORT ARE INCOMPITABLE
408 # endif
409 #endif
410 
411 #ifndef EXPORTED_TO_SHARED_LIB
412 # ifndef IMPORTED_FROM_SHARED_LIB
413 # define APIDECL //empty, used during static linking
414 # endif
415 #endif
416 
417 #endif /* GMM_STD_H__ */
this is the above solutions for linux, but it still needs to be tested.
Definition: gmm_std.h:238