GetFEM  5.5
gmm_std.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2002-2026 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, see https://www.gnu.org/licenses/.
19 
20  As a special exception, you may use this file as it is a part of a free
21  software library without restriction. Specifically, if other files
22  instantiate templates or use macros or inline functions from this file,
23  or you compile this file and link it with other files to produce an
24  executable, this file does not by itself cause the resulting executable
25  to be covered by the GNU Lesser General Public License. This exception
26  does not however invalidate any other reasons why the executable file
27  might be covered by the GNU Lesser General Public License.
28 
29 ===========================================================================*/
30 
31 /**@file gmm_std.h
32 @author Yves Renard <[email protected]>,
33 @author Julien Pommier <[email protected]>
34 @date June 01, 1995.
35 @brief basic setup for gmm (includes, typedefs etc.)
36 */
37 #ifndef GMM_STD_H__
38 #define GMM_STD_H__
39 
40 #ifndef __USE_STD_IOSTREAM
41 # define __USE_STD_IOSTREAM
42 #endif
43 
44 #ifndef __USE_BSD
45 # define __USE_BSD
46 #endif
47 
48 #ifndef __USE_ISOC99
49 # define __USE_ISOC99
50 #endif
51 
52 #if defined(_MSC_VER) && _MSC_VER >= 1400 // Secure versions for VC++
53 # define GMM_SECURE_CRT
54 # define SECURE_NONCHAR_SSCANF sscanf_s
55 # define SECURE_NONCHAR_FSCANF fscanf_s
56 # define SECURE_STRNCPY(a, la, b, lb) strncpy_s(a, la, b, lb)
57 # define SECURE_FOPEN(F, filename, mode) (*(F) = 0, fopen_s(F, filename, mode))
58 # define SECURE_SPRINTF1(S, l, st, p1) sprintf_s(S, l, st, p1)
59 # define SECURE_SPRINTF2(S, l, st, p1, p2) sprintf_s(S, l, st, p1, p2)
60 # define SECURE_SPRINTF4(S, l, st, p1, p2, p3, p4) sprintf_s(S, l, st, p1, p2, p3, p4)
61 # define SECURE_STRDUP(s) _strdup(s)
62 # ifndef _CRT_SECURE_NO_DEPRECATE
63 # error Add _CRT_SECURE_NO_DEPRECATE to your compilation options, Microsoft is overstrict (e.g. bans fopen) without offering portable alternatives
64 # endif
65 #else
66 # define SECURE_NONCHAR_SSCANF sscanf
67 # define SECURE_NONCHAR_FSCANF fscanf
68 # define SECURE_STRNCPY(a, la, b, lb) strncpy(a, b, lb)
69 # define SECURE_FOPEN(F, filename, mode) ((*(F)) = fopen(filename, mode))
70 # define SECURE_SPRINTF1(S, l, st, p1) snprintf(S, l, st, p1)
71 # define SECURE_SPRINTF2(S, l, st, p1, p2) snprintf(S, l, st, p1, p2)
72 # define SECURE_SPRINTF4(S, l, st, p1, p2, p3, p4) snprintf(S, l, st, p1, p2, p3, p4)
73 # define SECURE_STRDUP(s) strdup(s)
74 #endif
75 
76 inline void GMM_NOPERATION_(int) { }
77 #define GMM_NOPERATION(a) { GMM_NOPERATION_(abs(&(a) != &(a))); }
78 
79 /* ********************************************************************** */
80 /* Compilers detection. */
81 /* ********************************************************************** */
82 
83 #if defined(__GNUC__)
84 # if (__GNUC__ < 4)
85 # error : PLEASE UPDATE g++ TO AT LEAST 4.8 VERSION
86 # endif
87 #endif
88 
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 #include <memory>
117 #include <array>
118 #include <locale.h>
119 
120 #include <gmm/gmm_arch_config.h>
121 
122 namespace std {
123 #if defined(__GNUC__) && (__cplusplus <= 201103L)
124  template<typename _Tp>
125  struct _MakeUniq
126  { typedef unique_ptr<_Tp> __single_object; };
127  template<typename _Tp>
128  struct _MakeUniq<_Tp[]>
129  { typedef unique_ptr<_Tp[]> __array; };
130  template<typename _Tp, size_t _Bound>
131  struct _MakeUniq<_Tp[_Bound]>
132  { struct __invalid_type { }; };
133  /// std::make_unique for single objects
134  template<typename _Tp, typename... _Args>
135  inline typename _MakeUniq<_Tp>::__single_object
136  make_unique(_Args&&... __args)
137  { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
138  /// std::make_unique for arrays of unknown bound
139  template<typename _Tp>
140  inline typename _MakeUniq<_Tp>::__array
141  make_unique(size_t __num)
142  { return unique_ptr<_Tp>(new typename remove_extent<_Tp>::type[__num]()); }
143  /// Disable std::make_unique for arrays of known bound
144  template<typename _Tp, typename... _Args>
145  inline typename _MakeUniq<_Tp>::__invalid_type
146  make_unique(_Args&&...) = delete;
147 #endif
148 
149 
150  // Should simply be replaced by std::shared_ptr<T[]> when it will be supported
151  // by the STL
152  template <typename T> class shared_array_ptr : shared_ptr<T> {
153  public:
154  shared_array_ptr() {}
155  shared_array_ptr(T *q) : std::shared_ptr<T>(q, default_delete<T[]>()) {}
156  template <typename Y> shared_array_ptr(const std::shared_ptr<Y> &p, T *q)
157  : std::shared_ptr<T>(p, q) {}
158  T *get() const { return shared_ptr<T>::get(); }
159  T& operator*() const { return shared_ptr<T>::operator*(); }
160  T* operator->() const { return shared_ptr<T>::operator->(); }
161  };
162 
163  template <typename T> shared_array_ptr<T> make_shared_array(size_t num)
164  { return shared_array_ptr<T>(new T[num]); }
165 }
166 
167 namespace gmm {
168 
169  using std::endl; using std::cout; using std::cerr;
170  using std::ends; using std::cin; using std::isnan;
171 
172  class standard_locale {
173  std::string cloc;
174  std::locale cinloc;
175 
176  public :
177  inline standard_locale(void)
178  : cloc(setlocale(LC_NUMERIC, 0)), cinloc(cin.getloc())
179  { setlocale(LC_NUMERIC,"C"); cin.imbue(std::locale("C")); }
180  inline ~standard_locale()
181  { setlocale(LC_NUMERIC, cloc.c_str()); cin.imbue(cinloc); }
182  };
183 
184  class stream_standard_locale {
185  std::locale cloc;
186  std::ios &io;
187 
188  public :
189  inline stream_standard_locale(std::ios &i)
190  : cloc(i.getloc()), io(i) { io.imbue(std::locale("C")); }
191  inline ~stream_standard_locale() { io.imbue(cloc); }
192  };
193 
194  /* ******************************************************************* */
195  /* Clock functions. */
196  /* ******************************************************************* */
197 
198 # if defined(HAVE_SYS_TIMES)
199  inline double uclock_sec(void) {
200  static double ttclk = 0.;
201  if (ttclk == 0.) ttclk = sysconf(_SC_CLK_TCK);
202  tms t; times(&t); return double(t.tms_utime) / ttclk;
203  }
204 # else
205  inline double uclock_sec(void)
206  { return double(clock())/double(CLOCKS_PER_SEC); }
207 # endif
208 
209  /* ******************************************************************** */
210  /* Fixed size integer types. */
211  /* ******************************************************************** */
212  // Remark : the test program dynamic_array tests the length of
213  // resulting integers
214 
215  template <size_t s> struct fixed_size_integer_generator {
216  typedef void int_base_type;
217  typedef void uint_base_type;
218  };
219 
220  template <> struct fixed_size_integer_generator<sizeof(char)> {
221  typedef signed char int_base_type;
222  typedef unsigned char uint_base_type;
223  };
224 
225  template <> struct fixed_size_integer_generator<sizeof(short int)
226  - ((sizeof(short int) == sizeof(char)) ? 78 : 0)> {
227  typedef signed short int int_base_type;
228  typedef unsigned short int uint_base_type;
229 };
230 
231 template <> struct fixed_size_integer_generator<sizeof(int)
232  - ((sizeof(int) == sizeof(short int)) ? 59 : 0)> {
233  typedef signed int int_base_type;
234  typedef unsigned int uint_base_type;
235  };
236 
237 template <> struct fixed_size_integer_generator<sizeof(long)
238  - ((sizeof(int) == sizeof(long)) ? 93 : 0)> {
239  typedef signed long int_base_type;
240  typedef unsigned long uint_base_type;
241  };
242 
243 template <> struct fixed_size_integer_generator<sizeof(long long)
244  - ((sizeof(long long) == sizeof(long)) ? 99 : 0)> {
245  typedef signed long long int_base_type;
246  typedef unsigned long long uint_base_type;
247  };
248 
249 typedef fixed_size_integer_generator<1>::int_base_type int8_type;
250 typedef fixed_size_integer_generator<1>::uint_base_type uint8_type;
251 typedef fixed_size_integer_generator<2>::int_base_type int16_type;
252 typedef fixed_size_integer_generator<2>::uint_base_type uint16_type;
253 typedef fixed_size_integer_generator<4>::int_base_type int32_type;
254 typedef fixed_size_integer_generator<4>::uint_base_type uint32_type;
255 typedef fixed_size_integer_generator<8>::int_base_type int64_type;
256 typedef fixed_size_integer_generator<8>::uint_base_type uint64_type;
257 
258 // #if INT_MAX == 32767
259 // typedef signed int int16_type;
260 // typedef unsigned int uint16_type;
261 // #elif SHRT_MAX == 32767
262 // typedef signed short int int16_type;
263 // typedef unsigned short int uint16_type;
264 // #else
265 // # error "impossible to build a 16 bits integer"
266 // #endif
267 
268 // #if INT_MAX == 2147483647
269 // typedef signed int int32_type;
270 // typedef unsigned int uint32_type;
271 // #elif SHRT_MAX == 2147483647
272 // typedef signed short int int32_type;
273 // typedef unsigned short int uint32_type;
274 // #elif LONG_MAX == 2147483647
275 // typedef signed long int int32_type;
276 // typedef unsigned long int uint32_type;
277 // #else
278 // # error "impossible to build a 32 bits integer"
279 // #endif
280 
281 // #if INT_MAX == 9223372036854775807L || INT_MAX == 9223372036854775807
282 // typedef signed int int64_type;
283 // typedef unsigned int uint64_type;
284 // #elif LONG_MAX == 9223372036854775807L || LONG_MAX == 9223372036854775807
285 // typedef signed long int int64_type;
286 // typedef unsigned long int uint64_type;
287 // #elif LLONG_MAX == 9223372036854775807LL || LLONG_MAX == 9223372036854775807L || LLONG_MAX == 9223372036854775807
288 // typedef signed long long int int64_type;
289 // typedef unsigned long long int uint64_type;
290 // #else
291 // # error "impossible to build a 64 bits integer"
292 // #endif
293 
294 #if defined(__GNUC__) && !defined(__ICC)
295 /*
296  g++ can issue a warning at each usage of a function declared with this special attribute
297  (also works with typedefs and variable declarations)
298 */
299 # define IS_DEPRECATED __attribute__ ((__deprecated__))
300 /*
301  the specified function is inlined at any optimization level
302 */
303 # define ALWAYS_INLINE __attribute__((always_inline))
304 #else
305 # define IS_DEPRECATED
306 # define ALWAYS_INLINE
307 #endif
308 
309 #if defined(_MSC_VER) || defined(__INTEL_COMPILER)
310 # define NOINLINE __declspec(noinline)
311 #elif defined(__GNUC__) && !defined(__ICC)
312 # define NOINLINE __attribute__((noinline))
313 #endif
314 
315 }
316 
317  /* ******************************************************************** */
318  /* Import/export classes and interfaces from a shared library */
319  /* ******************************************************************** */
320 
321 #if defined(EXPORTED_TO_SHARED_LIB)
322 # if defined(_MSC_VER) || defined(__INTEL_COMPILER)
323 # define APIDECL __declspec(dllexport)
324 # elif defined(__GNUC__)
325 # define __attribute__((visibility("default")))
326 # else
327 # define APIDECL
328 # endif
329 # if defined(IMPORTED_FROM_SHARED_LIB)
330 # error INTENTIONAL COMPILATION ERROR, DLL IMPORT AND EXPORT ARE INCOMPATIBLE
331 # endif
332 #endif
333 
334 #if defined(IMPORTED_FROM_SHARED_LIB)
335 # if defined(_MSC_VER) || defined(__INTEL_COMPILER)
336 # define APIDECL __declspec(dllimport)
337 # else
338 # define APIDECL
339 # endif
340 # if defined(EXPORTED_TO_SHARED_LIB)
341 # error INTENTIONAL COMPILATION ERROR, DLL IMPORT AND EXPORT ARE INCOMPATIBLE
342 # endif
343 #endif
344 
345 #ifndef EXPORTED_TO_SHARED_LIB
346 # ifndef IMPORTED_FROM_SHARED_LIB
347 # define APIDECL //empty, used during static linking
348 # endif
349 #endif
350 
351 #endif /* GMM_STD_H__ */