GetFEM  5.4.3
gmm_std.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2002-2020 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 _CRT_SECURE_NO_DEPRECATE
64 # error Add _CRT_SECURE_NO_DEPRECATE to your compilation options, Microsoft is overstrict (e.g. bans fopen) without offering portable alternatives
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) snprintf(S, l, st, p1)
72 # define SECURE_SPRINTF2(S, l, st, p1, p2) snprintf(S, l, st, p1, p2)
73 # define SECURE_SPRINTF4(S, l, st, p1, p2, p3, p4) snprintf(S, l, 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 namespace gmm {
175 
176  using std::endl; using std::cout; using std::cerr;
177  using std::ends; using std::cin; using std::isnan;
178 
179  class standard_locale {
180  std::string cloc;
181  std::locale cinloc;
182 
183  public :
184  inline standard_locale(void)
185  : cloc(setlocale(LC_NUMERIC, 0)), cinloc(cin.getloc())
186  { setlocale(LC_NUMERIC,"C"); cin.imbue(std::locale("C")); }
187  inline ~standard_locale()
188  { setlocale(LC_NUMERIC, cloc.c_str()); cin.imbue(cinloc); }
189  };
190 
191  class stream_standard_locale {
192  std::locale cloc;
193  std::ios &io;
194 
195  public :
196  inline stream_standard_locale(std::ios &i)
197  : cloc(i.getloc()), io(i) { io.imbue(std::locale("C")); }
198  inline ~stream_standard_locale() { io.imbue(cloc); }
199  };
200 
201  /* ******************************************************************* */
202  /* Clock functions. */
203  /* ******************************************************************* */
204 
205 # if defined(HAVE_SYS_TIMES)
206  inline double uclock_sec(void) {
207  static double ttclk = 0.;
208  if (ttclk == 0.) ttclk = sysconf(_SC_CLK_TCK);
209  tms t; times(&t); return double(t.tms_utime) / ttclk;
210  }
211 # else
212  inline double uclock_sec(void)
213  { return double(clock())/double(CLOCKS_PER_SEC); }
214 # endif
215 
216  /* ******************************************************************** */
217  /* Fixed size integer types. */
218  /* ******************************************************************** */
219  // Remark : the test program dynamic_array tests the length of
220  // resulting integers
221 
222  template <size_t s> struct fixed_size_integer_generator {
223  typedef void int_base_type;
224  typedef void uint_base_type;
225  };
226 
227  template <> struct fixed_size_integer_generator<sizeof(char)> {
228  typedef signed char int_base_type;
229  typedef unsigned char uint_base_type;
230  };
231 
232  template <> struct fixed_size_integer_generator<sizeof(short int)
233  - ((sizeof(short int) == sizeof(char)) ? 78 : 0)> {
234  typedef signed short int int_base_type;
235  typedef unsigned short int uint_base_type;
236 };
237 
238 template <> struct fixed_size_integer_generator<sizeof(int)
239  - ((sizeof(int) == sizeof(short int)) ? 59 : 0)> {
240  typedef signed int int_base_type;
241  typedef unsigned int uint_base_type;
242  };
243 
244 template <> struct fixed_size_integer_generator<sizeof(long)
245  - ((sizeof(int) == sizeof(long)) ? 93 : 0)> {
246  typedef signed long int_base_type;
247  typedef unsigned long uint_base_type;
248  };
249 
250 template <> struct fixed_size_integer_generator<sizeof(long long)
251  - ((sizeof(long long) == sizeof(long)) ? 99 : 0)> {
252  typedef signed long long int_base_type;
253  typedef unsigned long long uint_base_type;
254  };
255 
256 typedef fixed_size_integer_generator<1>::int_base_type int8_type;
257 typedef fixed_size_integer_generator<1>::uint_base_type uint8_type;
258 typedef fixed_size_integer_generator<2>::int_base_type int16_type;
259 typedef fixed_size_integer_generator<2>::uint_base_type uint16_type;
260 typedef fixed_size_integer_generator<4>::int_base_type int32_type;
261 typedef fixed_size_integer_generator<4>::uint_base_type uint32_type;
262 typedef fixed_size_integer_generator<8>::int_base_type int64_type;
263 typedef fixed_size_integer_generator<8>::uint_base_type uint64_type;
264 
265 // #if INT_MAX == 32767
266 // typedef signed int int16_type;
267 // typedef unsigned int uint16_type;
268 // #elif SHRT_MAX == 32767
269 // typedef signed short int int16_type;
270 // typedef unsigned short int uint16_type;
271 // #else
272 // # error "impossible to build a 16 bits integer"
273 // #endif
274 
275 // #if INT_MAX == 2147483647
276 // typedef signed int int32_type;
277 // typedef unsigned int uint32_type;
278 // #elif SHRT_MAX == 2147483647
279 // typedef signed short int int32_type;
280 // typedef unsigned short int uint32_type;
281 // #elif LONG_MAX == 2147483647
282 // typedef signed long int int32_type;
283 // typedef unsigned long int uint32_type;
284 // #else
285 // # error "impossible to build a 32 bits integer"
286 // #endif
287 
288 // #if INT_MAX == 9223372036854775807L || INT_MAX == 9223372036854775807
289 // typedef signed int int64_type;
290 // typedef unsigned int uint64_type;
291 // #elif LONG_MAX == 9223372036854775807L || LONG_MAX == 9223372036854775807
292 // typedef signed long int int64_type;
293 // typedef unsigned long int uint64_type;
294 // #elif LLONG_MAX == 9223372036854775807LL || LLONG_MAX == 9223372036854775807L || LLONG_MAX == 9223372036854775807
295 // typedef signed long long int int64_type;
296 // typedef unsigned long long int uint64_type;
297 // #else
298 // # error "impossible to build a 64 bits integer"
299 // #endif
300 
301 #if defined(__GNUC__) && !defined(__ICC)
302 /*
303  g++ can issue a warning at each usage of a function declared with this special attribute
304  (also works with typedefs and variable declarations)
305 */
306 # define IS_DEPRECATED __attribute__ ((__deprecated__))
307 /*
308  the specified function is inlined at any optimization level
309 */
310 # define ALWAYS_INLINE __attribute__((always_inline))
311 #else
312 # define IS_DEPRECATED
313 # define ALWAYS_INLINE
314 #endif
315 
316 #if defined(_MSC_VER) || defined(__INTEL_COMPILER)
317 # define NOINLINE __declspec(noinline)
318 #elif defined(__GNUC__) && !defined(__ICC)
319 # define NOINLINE __attribute__((noinline))
320 #endif
321 
322 }
323 
324  /* ******************************************************************** */
325  /* Import/export classes and interfaces from a shared library */
326  /* ******************************************************************** */
327 
328 #if defined(EXPORTED_TO_SHARED_LIB)
329 # if defined(_MSC_VER) || defined(__INTEL_COMPILER)
330 # define APIDECL __declspec(dllexport)
331 # elif defined(__GNUC__)
332 # define __attribute__((visibility("default")))
333 # else
334 # define APIDECL
335 # endif
336 # if defined(IMPORTED_FROM_SHARED_LIB)
337 # error INTENTIONAL COMPILATION ERROR, DLL IMPORT AND EXPORT ARE INCOMPATIBLE
338 # endif
339 #endif
340 
341 #if defined(IMPORTED_FROM_SHARED_LIB)
342 # if defined(_MSC_VER) || defined(__INTEL_COMPILER)
343 # define APIDECL __declspec(dllimport)
344 # else
345 # define APIDECL
346 # endif
347 # if defined(EXPORTED_TO_SHARED_LIB)
348 # error INTENTIONAL COMPILATION ERROR, DLL IMPORT AND EXPORT ARE INCOMPATIBLE
349 # endif
350 #endif
351 
352 #ifndef EXPORTED_TO_SHARED_LIB
353 # ifndef IMPORTED_FROM_SHARED_LIB
354 # define APIDECL //empty, used during static linking
355 # endif
356 #endif
357 
358 #endif /* GMM_STD_H__ */