Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-02 08:28:09

0001 //
0002 // MIT License
0003 // Copyright (c) 2020 Jonathan R. Madsen
0004 // Permission is hereby granted, free of charge, to any person obtaining a copy
0005 // of this software and associated documentation files (the "Software"), to deal
0006 // in the Software without restriction, including without limitation the rights
0007 // to use, copy, modify, merge, publish, distribute, sublicense, and
0008 // copies of the Software, and to permit persons to whom the Software is
0009 // furnished to do so, subject to the following conditions:
0010 // The above copyright notice and this permission notice shall be included in
0011 // all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
0012 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
0013 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
0014 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
0015 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0016 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0017 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0018 //
0019 //
0020 
0021 #pragma once
0022 
0023 #include <cctype>
0024 #include <cstdlib>
0025 #include <functional>
0026 #include <iomanip>
0027 #include <iostream>
0028 #include <map>
0029 #include <mutex>
0030 #include <set>
0031 #include <sstream>  // IWYU pragma: keep
0032 #include <string>
0033 #include <tuple>
0034 #include <utility>
0035 
0036 namespace PTL
0037 {
0038 //--------------------------------------------------------------------------------------//
0039 //  use this function to get an environment variable setting +
0040 //  a default if not defined, e.g.
0041 //      int num_threads =
0042 //          GetEnv<int>("FORCENUMBEROFTHREADS",
0043 //                          std::thread::hardware_concurrency());
0044 //
0045 template <typename Tp>
0046 Tp
0047 GetEnv(const std::string& env_id, Tp _default = Tp())
0048 {
0049     char* env_var = std::getenv(env_id.c_str());
0050     if(env_var)
0051     {
0052         std::string        str_var = std::string(env_var);
0053         std::istringstream iss(str_var);
0054         Tp                 var = Tp();
0055         iss >> var;
0056         return var;
0057     }
0058 
0059     // return default if not specified in environment
0060     return _default;
0061 }
0062 
0063 //--------------------------------------------------------------------------------------//
0064 //  overload for boolean
0065 //
0066 template <>
0067 inline bool
0068 GetEnv(const std::string& env_id, bool _default)
0069 {
0070     char* env_var = std::getenv(env_id.c_str());
0071     if(env_var)
0072     {
0073         std::string var = std::string(env_var);
0074         bool        val = true;
0075         if(var.find_first_not_of("0123456789") == std::string::npos)
0076             val = (bool) atoi(var.c_str());
0077         else
0078         {
0079             for(auto& itr : var)
0080                 itr = tolower(itr);
0081             if(var == "off" || var == "false")
0082                 val = false;
0083         }
0084         return val;
0085     }
0086 
0087     // return default if not specified in environment
0088     return _default;
0089 }
0090 
0091 //--------------------------------------------------------------------------------------//
0092 
0093 }  // namespace PTL