50#if !defined(TIMEMORY_USE_ENVIRONMENT_EXTERN)
61(env_settings* _global,
int _id)
63, m_env(
new env_uomap_t)
65 if(_global && _id != 0)
67 auto_lock_t lk(type_mutex<env_settings>(), std::defer_lock);
68 lock_flag().store(
true);
71 _global->m_env_other.emplace_back(m_env);
72 lock_flag().store(
false);
85 for(
auto& itr : m_env_other)
95 auto_lock_t lk(type_mutex<env_settings>(), std::defer_lock);
96 lock_flag().store(
true);
102 for(
const auto& itr : _tmp)
103 _ret[itr.first] = itr.second;
105 lock_flag().store(
false);
115 const_cast<env_settings&
>(*this).collapse();
122 for(
const auto& itr : _data)
124 if(_filter(itr.first))
126 _wl = std::max<size_t>(itr.first.length(), _wl);
127 _wr = std::max<size_t>(itr.second.length(), _wr);
135 std::stringstream filler{};
139 std::stringstream _tmp{};
140 _tmp <<
" " << std::setw(_wl) << std::right <<
" "
141 <<
" = " << std::setw(_wr) << std::left <<
" "
143 filler << std::setw(_tmp.str().length() + 1) <<
"";
147 std::stringstream ss;
148 ss << filler.str() <<
'\n';
149 ss <<
"# Environment settings:\n";
150 for(
const auto& itr : _data)
152 if(_filter(itr.first))
154 ss <<
"# " << std::setw(_wl) << std::right << itr.first <<
" = "
155 << std::setw(_wr) << std::left << itr.second <<
'\n';
159 os << ss.str() <<
'\n';
166env_settings::collapse()
171 auto_lock_t lk(type_mutex<env_settings>(), std::defer_lock);
175 lock_flag().store(
true);
178 for(
size_t i = 0; i < m_env_other.size(); ++i)
181 auto* itr = m_env_other[i];
185 for(
const auto& mitr : *m_env_other[i])
187 auto key = mitr.first;
188 if(m_env->find(key) != m_env->end())
192 if(m_env->find(key)->second != mitr.second)
195 (*m_env)[key] = mitr.second;
202 (*m_env)[key] = mitr.second;
207 lock_flag().store(
false);
222 char* env_var = std::getenv(env_id.c_str());
225 std::stringstream ss;
227 if(_env_settings && _store)
228 _env_settings->insert(env_id, ss.str());
232 if(_env_settings && _store)
233 _env_settings->insert(env_id, _default);
251 char* env_var = std::getenv(env_id.c_str());
256 if(var.find_first_not_of(
"0123456789") == std::string::npos)
258 val = (bool) atoi(var.c_str());
264 for(
const auto& itr : {
"off",
"false",
"no",
"n",
"f",
"0" })
268 if(_env_settings && _store)
269 _env_settings->insert<
bool>(env_id,
false);
274 if(_env_settings && _store)
275 _env_settings->insert<
bool>(env_id, val);
279 if(_env_settings && _store)
280 _env_settings->insert<
bool>(env_id, _default);
298 auto itr = _env_settings->get(env_id);
299 if(itr != _env_settings->end())
314 namespace regex_const = std::regex_constants;
320 auto itr = _env_settings->get(env_id);
321 if(itr != _env_settings->end())
323 auto val = itr->second;
324 const auto regex_constants = regex_const::egrep | regex_const::icase;
325 const std::string pattern =
"^(off|false|no|n|f|0)$";
329 _match = std::regex_match(val, std::regex(pattern, regex_constants));
330 }
catch(std::bad_cast&)
332 for(
auto& vitr : val)
333 vitr = tolower(vitr);
334 for(
const auto& vitr : {
"off",
"false",
"no",
"n",
"f",
"0" })
343 return (_match) ?
false :
true;
360 static std::atomic<int> _count{ 0 };
362 static thread_local int _id = _count++;
364 (_id == 0) ? (&_instance) :
new env_settings{ &_instance, _id };
366 if(_local == &_instance || _id == 0)
std::map< string_t, string_t > env_map_t
static env_settings * instance()
void print(std::ostream &, filter_func_t &&_filter=[](const std::string &) { return true;}) const
const hash_alias_ptr_t hash_value_t std::string *& _ret
std::unique_lock< mutex_t > auto_lock_t
Unique lock type around mutex_t.
void print_env(std::ostream &os, FuncT &&_filter)
Print all environment queries and their values which satisfy the filter conditions.
Tp get_env(const std::string &env_id, Tp _default, bool _store)
Tp load_env(const std::string &env_id, Tp _default)
tim::mpl::apply< std::string > string
const std::string std::ostream * os
TIMEMORY_ENVIRONMENT_LINKAGE(env_settings::~env_settings())
provides an object which can be returned from functions that will execute the lambda provided during ...