IgANet
IGAnets - Isogeometric Analysis Networks
Loading...
Searching...
No Matches
getenv.hpp
Go to the documentation of this file.
1
15#pragma once
16
17#include <sstream>
18#include <stdlib.h>
19#include <vector>
20
21namespace iganet::utils {
22
28template <typename T> T getenv(std::string variable, const T &default_value) {
29
30 char *env_value = std::getenv(variable.c_str());
31 if (env_value != nullptr)
32 if constexpr (std::is_integral_v<T>)
33 return static_cast<T>(std::atoll(env_value));
34 else if constexpr (std::is_floating_point_v<T>)
35 return static_cast<T>(std::atof(env_value));
36 else
37 return static_cast<T>(env_value);
38 else
39 return default_value;
40}
41
47template <typename T>
48std::vector<T> getenv(std::string variable,
49 std::initializer_list<T> default_value) {
50
51 char *env_value = std::getenv(variable.c_str());
52 if (env_value != nullptr) {
53 std::stringstream ss(env_value);
54 std::vector<T> result;
55 std::string item;
56
57 if constexpr (std::is_integral_v<T>)
58 while (std::getline(ss, item, ','))
59 result.emplace_back(static_cast<T>(std::atoll(item.c_str())));
60 else if constexpr (std::is_floating_point_v<T>)
61 while (std::getline(ss, item, ','))
62 result.emplace_back(static_cast<T>(std::atof(item.c_str())));
63 else
64 while (std::getline(ss, item, ','))
65 result.emplace_back(static_cast<T>(item));
66 return result;
67 } else
68 return std::vector<T>{default_value};
69}
70
71} // namespace iganet::utils
Definition blocktensor.hpp:24
T getenv(std::string variable, const T &default_value)
Returns the value from an environment variable.
Definition getenv.hpp:28