11template <
typename... Args>
14 std::ostringstream oss;
16 std::cerr <<
"[ERROR] " << oss.str() << std::endl;
22bool is_any_of(
const char*
const (&names)[N],
const std::string& str)
24 return std::any_of(std::begin(names), std::end(names), [&](
const char* inner_str) {
25 return str == inner_str;
38 std::string value_env_str{vp};
40 for(
auto& c : value_env_str)
42 if(std::isalpha(c) != 0)
44 c = std::tolower(
static_cast<unsigned char>(c));
48 if(
is_any_of(enabled_names, value_env_str))
52 else if(
is_any_of(disabled_names, value_env_str))
58 throw std::runtime_error(
"Invalid value for env variable");
65 static constexpr const char* enabled_names[] = {
"enable",
"enabled",
"1",
"yes",
"on",
"true"};
66 static constexpr const char* disabled_names[] = {
67 "disable",
"disabled",
"0",
"no",
"off",
"false"};
96 void Unset() { is_unset =
true; }
104 explicit EnvVar(
const char*
const name,
const T& def_val)
107 const char* vp = std::getenv(name);
124#define CK_TILE_DECLARE_ENV_VAR(name, type, default_val) \
125 namespace ck_tile::env { \
128 static_assert(std::is_same_v<name, ::ck_tile::env::name>, \
129 "CK_TILE_DECLARE_ENV* must be used in the global namespace"); \
130 using value_type = type; \
131 static ck_tile::internal::EnvVar<type>& Ref() \
133 static ck_tile::internal::EnvVar<type> var{#name, default_val}; \
139#define CK_TILE_DECLARE_ENV_VAR_BOOL(name) CK_TILE_DECLARE_ENV_VAR(name, bool, false)
141#define CK_TILE_DECLARE_ENV_VAR_UINT64(name) CK_TILE_DECLARE_ENV_VAR(name, uint64_t, 0)
143#define CK_TILE_DECLARE_ENV_VAR_STR(name) CK_TILE_DECLARE_ENV_VAR(name, std::string, "")
145#define CK_TILE_ENV(name) \
146 ck_tile::env::name {}
148template <
class EnvVar>
151 static_assert(std::is_same_v<typename EnvVar::value_type, std::string>);
152 return EnvVar::Ref().GetValue();
155template <
class EnvVar>
158 static_assert(std::is_same_v<typename EnvVar::value_type, bool>);
159 return !EnvVar::Ref().IsUnset() && EnvVar::Ref().GetValue();
162template <
class EnvVar>
165 static_assert(std::is_same_v<typename EnvVar::value_type, bool>);
166 return !EnvVar::Ref().IsUnset() && !EnvVar::Ref().GetValue();
169template <
class EnvVar>
172 static_assert(std::is_same_v<typename EnvVar::value_type, uint64_t>);
173 return EnvVar::Ref().GetValue();
176template <
class EnvVar>
179 return EnvVar::Ref().IsUnset();
182template <
class EnvVar>
185 EnvVar::Ref().Unset();
189template <
typename EnvVar,
typename ValueType>
192 static_assert(std::is_same_v<typename EnvVar::value_type, ValueType>);
193 EnvVar::Ref().UpdateValue(val);
196template <
typename EnvVar>
199 EnvVar::Ref().UpdateValue(
bool is_any_of(const char *const (&names)[N], const std::string &str)
Definition tile/core/utility/env.hpp:22
Definition tile/core/algorithm/cluster_descriptor.hpp:13
bool EnvIsEnabled(EnvVar)
Definition tile/core/utility/env.hpp:156
void CK_TILE_ERROR(Args &&... args) noexcept
Definition tile/core/utility/env.hpp:12
bool EnvIsUnset(EnvVar)
Definition tile/core/utility/env.hpp:177
uint64_t EnvValue(EnvVar)
Definition tile/core/utility/env.hpp:170
void UpdateEnvVar(EnvVar, const ValueType &val)
Updates the cached value of an environment variable.
Definition tile/core/utility/env.hpp:190
bool EnvIsDisabled(EnvVar)
Definition tile/core/utility/env.hpp:163
const std::string & EnvGetString(EnvVar)
Definition tile/core/utility/env.hpp:149
void EnvUnset(EnvVar)
Definition tile/core/utility/env.hpp:183
Definition allocators.h:459
unsigned __int64 uint64_t
Definition stdint.h:136
void UpdateValue(const T &val)
Definition tile/core/utility/env.hpp:98
EnvVar(const char *const name, const T &def_val)
Definition tile/core/utility/env.hpp:104
void Unset()
Definition tile/core/utility/env.hpp:96
const T & GetValue() const
Definition tile/core/utility/env.hpp:92
bool IsUnset() const
Definition tile/core/utility/env.hpp:94
static bool parse_env_var_value(const char *vp)
Definition tile/core/utility/env.hpp:36
static std::string parse_env_var_value(const char *vp)
Definition tile/core/utility/env.hpp:81
static uint64_t parse_env_var_value(const char *vp)
Definition tile/core/utility/env.hpp:75
Definition tile/core/utility/env.hpp:31
#define CK_TILE_DECLARE_ENV_VAR_BOOL(name)
Definition tile/core/utility/env.hpp:139