42template <
typename... Args>
50using EnvChoice = std::tuple<Tp, std::string, std::string>;
64 typedef std::multimap<string_t, string_t>
env_map_t;
75 template <
typename Tp>
76 void insert(
const std::string& env_id, Tp val)
79 ss << std::boolalpha << val;
81 if(m_env.find(env_id) != m_env.end())
83 for(
const auto& itr : m_env)
84 if(itr.first == env_id && itr.second == ss.str())
94 template <
typename Tp>
97 Tp& val = std::get<0>(choice);
98 std::string& str_val = std::get<1>(choice);
99 std::string& descript = std::get<2>(choice);
101 std::stringstream ss, ss_long;
102 ss << std::boolalpha << val;
103 ss_long << std::boolalpha << std::setw(8) << std::left << val <<
" # (\""
104 << str_val <<
"\") " << descript;
106 if(m_env.find(env_id) != m_env.end())
108 for(
const auto& itr : m_env)
109 if(itr.first == env_id && itr.second == ss.str())
115 m_env.insert(
env_pair_t(env_id, ss_long.str()));
124 std::stringstream filler;
126 filler << std::setw(90) <<
"";
127 std::stringstream ss;
128 ss << filler.str() <<
"\n# Environment settings:\n";
130 for(
const auto& itr : env.
get())
132 ss <<
"# " << std::setw(35) << std::right << itr.first <<
"\t = \t"
133 << std::left << itr.second <<
"\n";
135 env.
mutex().unlock();
137 os << ss.str() << std::endl;
153template <
typename Tp>
155GetEnv(
const std::string& env_id, Tp _default = Tp())
157 char* env_var = std::getenv(env_id.c_str());
160 std::string str_var = std::string(env_var);
161 std::istringstream iss(str_var);
180GetEnv(
const std::string& env_id,
bool _default)
182 char* env_var = std::getenv(env_id.c_str());
185 std::string var = std::string(env_var);
187 if(var.find_first_not_of(
"0123456789") == std::string::npos)
188 val = (bool) atoi(var.c_str());
193 if(var ==
"off" || var ==
"false")
210template <
typename Tp>
212GetEnv(
const std::string& env_id, Tp _default,
const std::string& msg)
214 char* env_var = std::getenv(env_id.c_str());
217 std::string str_var = std::string(env_var);
218 std::istringstream iss(str_var);
221 std::cout <<
"Environment variable \"" << env_id <<
"\" enabled with "
222 <<
"value == " << var <<
". " << msg << std::endl;
244template <
typename Tp>
248 auto asupper = [](std::string var) {
254 char* env_var = std::getenv(env_id.c_str());
257 std::string str_var = std::string(env_var);
258 std::string upp_var = asupper(str_var);
261 for(
const auto& itr : _choices)
263 if(asupper(std::get<1>(itr)) == upp_var)
267 return std::get<0>(itr);
270 std::istringstream iss(str_var);
273 for(
const auto& itr : _choices)
275 if(var == std::get<0>(itr))
283 std::stringstream ss;
284 ss <<
"\n### Environment setting error @ " << __FUNCTION__ <<
" (line "
285 << __LINE__ <<
")! Invalid selection for \"" << env_id
286 <<
"\". Valid choices are:\n";
287 for(
const auto& itr : _choices)
288 ss <<
"\t\"" << std::get<0>(itr) <<
"\" or \"" << std::get<1>(itr) <<
"\" ("
289 << std::get<2>(itr) <<
")\n";
290 std::cerr << ss.str() << std::endl;
294 std::string _name =
"???";
295 std::string _desc =
"description not provided";
296 for(
const auto& itr : _choices)
297 if(std::get<0>(itr) == _default)
299 _name = std::get<1>(itr);
300 _desc = std::get<2>(itr);
313template <
typename Tp>
317 auto asupper = [](std::string var) {
323 std::string upp_var = asupper(str_var);
326 for(
const auto& itr : _choices)
328 if(asupper(std::get<1>(itr)) == upp_var)
331 return std::get<0>(itr);
334 std::istringstream iss(str_var);
337 for(
const auto& itr : _choices)
339 if(var == std::get<0>(itr))
346 std::stringstream ss;
347 ss <<
"\n### Environment setting error @ " << __FUNCTION__ <<
" (line " << __LINE__
348 <<
")! Invalid selection \"" << str_var <<
"\". Valid choices are:\n";
349 for(
const auto& itr : _choices)
350 ss <<
"\t\"" << std::get<0>(itr) <<
"\" or \"" << std::get<1>(itr) <<
"\" ("
351 << std::get<2>(itr) <<
")\n";
352 std::cerr << ss.str() << std::endl;
void insert(const std::string &env_id, EnvChoice< Tp > choice)
void insert(const std::string &env_id, Tp val)
std::pair< string_t, string_t > env_pair_t
static EnvSettings * GetInstance()
friend std::ostream & operator<<(std::ostream &os, const EnvSettings &env)
const env_map_t & get() const
std::multimap< string_t, string_t > env_map_t
void PrintEnv(std::ostream &os=std::cout)
std::set< EnvChoice< Tp > > EnvChoiceList
Tp GetChoice(const EnvChoiceList< Tp > &_choices, const std::string str_var)
std::tuple< Tp, std::string, std::string > EnvChoice
Tp GetEnv(const std::string &env_id, Tp _default=Tp())
void ConsumeParameters(Args...)