Flow123d  master-f44eb46
system.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file system.hh
15  * @brief
16  */
17 
18 #ifndef SYSTEM_H
19 #define SYSTEM_H
20 
21 
22 #include <mpi.h>
23 //#include <iostream>
24 
25 #include "system/global_defs.h"
26 #include "system/exc_common.hh"
27 #include <stdio.h> // for FILE
28 
29 
30 #include <string> // for string
31 #include "system/exceptions.hh" // for ExcChkErr::~Ex...
32 #include "system/fmt/posix.h" // for FMT_UNUSED
33 
34 
35 #ifndef _BSD_SOURCE
36  #define _BSD_SOURCE
37 #endif
38 
39 #ifdef WINDOWS
40  #include <direct.h>
41  #define GetCurrentDir _getcwd
42  #define PATH_MAX MAX_PATH
43 #else
44  //#include <unistd.h>
45  //#define GetCurrentDir getcwd
46  //#include <limits.h> //PATH_MAX
47 #endif
48 
49 #define strcmpi strcasecmp
50 #define DIR_DELIMITER '/'
51 
52 
53 // Assuming all compilers supports CXX11 features
54 #define OPERATOR_NEW_THROW_EXCEPTION
55 
56 
57 
58 using namespace std;
59 
60 
61 // **************************************************************
62 /*! @brief Identifiers for various output messages.
63  */
64 typedef enum MessageType {
67 
68 
69 // **************************************************************
70 /*! @brief System structure for various global variables.
71  */
72 typedef struct SystemInfo {
73  int verbosity; // system printf verbosity
74  int pause_after_run; // to keep terminal open on Windows
75  string log_fname; // name of the master log file
76  FILE *log; // log file handle
77 
78  int n_proc; // number of processors
79  int my_proc; // self processor number
80 
82 
84 
85 extern SystemInfo sys_info;
86 
87 /*
88 char * get_log_fname( void );
89 char * get_log_file( void );
90 void resume_log_file( void );
91 */
92 
93 // TODO: implement as a templated function
94 /*
95 #ifndef xfree
96  #define xfree(p) \
97  do { if (p) { free(p); (p)=NULL; } \
98  } while (0) /// test & free memory
99 #endif
100 */
101 /**
102  * @brief Replacement of new/delete operator in the spirit of xmalloc.
103  *
104  * Up to my knowledge overloading of original new/delete is the only clean.
105  * Possibly disadvantage is that all 'new' calls in system and other templates
106  * become also overloaded.
107  *
108  */
109 // @{
110 /*
111 void *operator new (std::size_t size) OPERATOR_NEW_THROW_EXCEPTION;
112 void *operator new[] (std::size_t size) OPERATOR_NEW_THROW_EXCEPTION;
113 void operator delete( void *p) throw();
114 void operator delete[]( void *p) throw();
115 */
116 // @}
117 
118 //int xsystem(const char*);
119 
120 //! @brief Operations on files and directories
121 /// @{
122 /*
123 int xmkdir( const char *s ); ///< Create directory (GLIBC function, original in <sys/stat.h>)
124 int xchdir( const char *s ); ///< Change directory (GLIBC function, original in <unistd.h>)
125 int xremove( const char *s ); ///< Remove file or directory (function)
126 char * xgetcwd( void ); ///< Get current working directory (GLIBC function, original in <unistd.h>)
127 int xrename( const char * oldname, const char * newname ); ///< Rename file (function)
128 */
129 //! @}
130 
131 // string operations
132 /*
133 char * xstrcpy(const char*);
134 char * xstrtok(char *s, int position = -1);
135 char * xstrtok(char*,const char* delim, int position = -1);
136 int xchomp( char * s );
137 */
138 
139 /**
140  * Wrapper to check return codes of C functions. In particular PETSC calls.
141  */
142 inline void chkerr(unsigned int ierr) {
143  do {
144  if (ierr != 0) THROW( ExcChkErr() << EI_ErrCode(ierr));
145  } while (0);
146 }
147 
148 /**
149  * Wrapper to check return codes of C functions. In particular PETSC calls.
150  * This version do the check just as an debugging assert. So the code is empty
151  * in release version.
152  */
153 inline void chkerr_assert(unsigned int ierr) {
154 #ifdef FLOW123D_DEBUG_ASSERTS
155  chkerr(ierr);
156 #else
157  (void)ierr; // not used
158 #endif
159 }
160 
161 #endif
162 //-----------------------------------------------------------------------------
163 // vim: set cindent:
Global macros to enhance readability and debugging, general constants.
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
int MPI_Comm
Definition: mpi.h:141
System structure for various global variables.
Definition: system.hh:72
FILE * log
Definition: system.hh:76
int my_proc
Definition: system.hh:79
int n_proc
Definition: system.hh:78
MPI_Comm comm
Definition: system.hh:81
int pause_after_run
Definition: system.hh:74
int verbosity
Definition: system.hh:73
string log_fname
Definition: system.hh:75
struct SystemInfo SystemInfo
System structure for various global variables.
void chkerr(unsigned int ierr)
Replacement of new/delete operator in the spirit of xmalloc.
Definition: system.hh:142
MessageType
Identifiers for various output messages.
Definition: system.hh:64
@ MsgDbg
Definition: system.hh:65
@ MsgVerb
Definition: system.hh:65
@ PrgErr
Definition: system.hh:65
@ Warn
Definition: system.hh:65
@ UsrErr
Definition: system.hh:65
@ Err
Definition: system.hh:65
@ MsgLog
Definition: system.hh:65
@ Msg
Definition: system.hh:65
SystemInfo sys_info
Definition: system.cc:41
void chkerr_assert(unsigned int ierr)
Definition: system.hh:153