BioCMAMC-ST
env_var.hpp
1#ifndef __BIOMC_COMMON_ENV_VAR_HPP__
2#define __BIOMC_COMMON_ENV_VAR_HPP__
3#include <cstdlib>
4#include <iostream>
5#include <sstream>
6#include <string_view>
7namespace Common
8{
13 template <typename T> T read_env_or(std::string_view varname, T vdefault)
14 {
15 const char* env_var = std::getenv(varname.data());
16 T ret = vdefault;
17 if (env_var != nullptr)
18 {
19 std::istringstream stream(env_var);
20 T value;
21 stream >> value;
22 if (stream)
23 {
24 ret = value;
25 }
26 }
27 return ret;
28 }
29
34 template <typename T> bool set_local_env(std::string_view varname, T val)
35 {
36#ifdef _WIN32
37 // Windows-specific handling (not implemented)
38 std::cerr << "Not implemented for Win32" << std::endl;
39 return false;
40#else
41
42 std::ostringstream oss;
43 oss << val;
44 std::string str_value = oss.str();
45
46 return setenv(varname.data(), str_value.c_str(), 1) == 0;
47#endif
48 }
49
50} // namespace Common
51
52#endif
Definition config_loader.hpp:8
bool set_local_env(std::string_view varname, T val)
Wrapper arround set_env.
Definition env_var.hpp:34
T read_env_or(std::string_view varname, T vdefault)
Wrapper arround get_env to get envariable with fallback to default.
Definition env_var.hpp:13