File indexing completed on 2025-01-18 09:58:13
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041 #ifndef G4ENVIRONMENTUTILS_HH
0042 #define G4ENVIRONMENTUTILS_HH
0043
0044 #include <cstdlib>
0045 #include <iomanip>
0046 #include <iostream>
0047 #include <map>
0048 #include <mutex>
0049 #include <sstream>
0050 #include <string>
0051
0052 #include "G4Exception.hh"
0053 #include "G4ExceptionSeverity.hh"
0054 #include "G4String.hh"
0055 #include "G4ios.hh"
0056
0057 class G4EnvSettings
0058 {
0059
0060
0061
0062 public:
0063 using string_t = std::string;
0064 using env_map_t = std::map<string_t, string_t>;
0065 using env_pair_t = std::pair<string_t, string_t>;
0066
0067 static G4EnvSettings* GetInstance()
0068 {
0069 static auto* _instance = new G4EnvSettings();
0070 return _instance;
0071 }
0072
0073 template <typename _Tp>
0074 void insert(const std::string& env_id, _Tp val)
0075 {
0076 std::stringstream ss;
0077 ss << val;
0078
0079
0080 static std::mutex _mutex;
0081 _mutex.lock();
0082 m_env.insert(env_pair_t(env_id, ss.str()));
0083 _mutex.unlock();
0084 }
0085
0086 const env_map_t& get() const { return m_env; }
0087
0088 friend std::ostream& operator<<(std::ostream& os, const G4EnvSettings& env)
0089 {
0090 std::stringstream filler;
0091 filler.fill('#');
0092 filler << std::setw(90) << "";
0093 std::stringstream ss;
0094 ss << filler.str() << "\n# Environment settings:\n";
0095 for(const auto& itr : env.get())
0096 {
0097 ss << "# " << std::setw(35) << std::right << itr.first << "\t = \t"
0098 << std::left << itr.second << "\n";
0099 }
0100 ss << filler.str();
0101 os << ss.str() << std::endl;
0102 return os;
0103 }
0104
0105 private:
0106 env_map_t m_env;
0107 };
0108
0109
0110
0111
0112
0113
0114
0115 template <typename _Tp>
0116 _Tp G4GetEnv(const std::string& env_id, _Tp _default = _Tp())
0117 {
0118 char* env_var = std::getenv(env_id.c_str());
0119 if(env_var)
0120 {
0121 std::string str_var = std::string(env_var);
0122 std::istringstream iss(str_var);
0123 _Tp var = _Tp();
0124 iss >> var;
0125
0126 G4EnvSettings::GetInstance()->insert<_Tp>(env_id, var);
0127 return var;
0128 }
0129
0130 G4EnvSettings::GetInstance()->insert<_Tp>(env_id, _default);
0131
0132
0133 return _default;
0134 }
0135
0136
0137
0138
0139
0140
0141
0142 template <>
0143 inline G4bool G4GetEnv(const std::string& env_id, bool _default)
0144 {
0145 char* env_var = std::getenv(env_id.c_str());
0146 if(env_var != nullptr)
0147 {
0148
0149 G4EnvSettings::GetInstance()->insert<bool>(env_id, true);
0150 return true;
0151 }
0152
0153 G4EnvSettings::GetInstance()->insert<bool>(env_id, false);
0154
0155
0156 return _default;
0157 }
0158
0159
0160
0161
0162
0163
0164
0165
0166 template <typename _Tp>
0167 _Tp G4GetEnv(const std::string& env_id, _Tp _default, const std::string& msg)
0168 {
0169 char* env_var = std::getenv(env_id.c_str());
0170 if(env_var)
0171 {
0172 std::string str_var = std::string(env_var);
0173 std::istringstream iss(str_var);
0174 _Tp var = _Tp();
0175 iss >> var;
0176 G4cout << "Environment variable \"" << env_id << "\" enabled with "
0177 << "value == " << var << ". " << msg << G4endl;
0178
0179 G4EnvSettings::GetInstance()->insert<_Tp>(env_id, var);
0180 return var;
0181 }
0182
0183 G4EnvSettings::GetInstance()->insert<_Tp>(env_id, _default);
0184
0185
0186 return _default;
0187 }
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198 inline G4String G4GetDataEnv(const std::string& env_id,
0199 const char* originOfException,
0200 const char* exceptionCode,
0201 G4ExceptionSeverity severity,
0202 const char* description)
0203 {
0204 char* env_var = std::getenv(env_id.c_str());
0205 if(env_var != nullptr)
0206 {
0207 std::string str_var = std::string(env_var);
0208 std::istringstream iss(str_var);
0209 G4String var = "";
0210 iss >> var;
0211
0212 G4EnvSettings::GetInstance()->insert<G4String>(env_id, var);
0213 return var;
0214 }
0215
0216
0217 G4Exception(originOfException, exceptionCode, severity, description);
0218
0219
0220 return "";
0221 }
0222
0223 const char* G4FindDataDir(const char*);
0224
0225
0226
0227
0228 inline void G4PrintEnv(std::ostream& os = G4cout)
0229 {
0230 os << (*G4EnvSettings::GetInstance());
0231 }
0232
0233 #endif