void initialize_globals_from_environment(std::string argv0) { for (auto& i: get_global_registry()) { std::string envname = i.name; char* envval = getenv(envname.c_str()); if (envval) { set_value_from_string_visitor visitor; visitor.new_value = envval; if (i.value.apply_visitor(visitor)) { logstream(LOG_EMPH) << "Setting configuration variable " << i.name << " to " << visitor.new_value << std::endl; } else { logstream(LOG_EMPH) << "Cannot set configuration variable " << i.name << " to " << visitor.new_value << std::endl; } } } // these two special variables cannot be environment overidden, // so set them last boost::filesystem::path argvpath(argv0); argvpath = boost::filesystem::absolute(argvpath); GLOBALS_MAIN_PROCESS_BINARY = argvpath.native(); GLOBALS_MAIN_PROCESS_PATH = argvpath.parent_path().native(); // logstream(LOG_INFO) << "Main process binary: " << GLOBALS_MAIN_PROCESS_BINARY << std::endl; // logstream(LOG_INFO) << "Main process path: " << GLOBALS_MAIN_PROCESS_PATH << std::endl; }
register_global<std::string>::register_global(std::string name, std::string* value, bool runtime_modifiable, std::function<bool(std::string)> value_check) { get_global_registry_map()[name] = get_global_registry().size(); get_global_registry().push_back( global_value{name, value_and_value_check<std::string>{value, value_check}, runtime_modifiable}); if (runtime_modifiable) { logstream(LOG_INFO) << "Registering runtime modifiable configuration variable " << name << " = " << (*value) << " (string)" << std::endl; } else { logstream(LOG_INFO) << "Registering environment modifiable configuration variable " << name << " = " << (*value) << " (string)" << std::endl; } }
std::vector<std::pair<std::string, flexible_type> > list_globals(bool runtime_modifiable) { std::vector<std::pair<std::string, flexible_type> > ret; for (auto& i: get_global_registry()) { if (i.runtime_modifiable == runtime_modifiable) { ret.push_back({i.name, boost::apply_visitor(get_value_visitor(), i.value)}); } } return ret; }
any::iholder* any::iholder::load(iarchive_soft_fail &arc) { registry_map_type& global_registry = get_global_registry(); uint64_t idload; arc >> idload; registry_map_type::const_iterator iter = global_registry.find(idload); if(iter == global_registry.end()) { logstream(LOG_FATAL) << "Cannot load object with hashed type [" << idload << "] from stream!" << std::endl << "\t A possible cause of this problem is that the type" << std::endl << "\t is never explicity used in this program.\n\n" << std::endl; return NULL; } // Otherwise the iterator points to the deserialization routine // for this type return iter->second(arc); }
set_global_error_codes set_global(std::string name, flexible_type val) { if (get_global_registry_map().count(name) == 0) { logstream(LOG_INFO) << "Unable to change value of " << name << " to " << val << ". No such configuration variable." << std::endl; return set_global_error_codes::NO_NAME; } size_t idx = get_global_registry_map()[name]; if (get_global_registry()[idx].runtime_modifiable == false) { logstream(LOG_INFO) << "Unable to change value of " << name << " to " << val << ". Variable is not runtime modifiable." << std::endl; return set_global_error_codes::NOT_RUNTIME_MODIFIABLE; } if (set_global_impl(name, val) == false) { logstream(LOG_INFO) << "Unable to change value of " << name << " to " << val << ". Invalid value." << std::endl; return set_global_error_codes::INVALID_VAL; } return set_global_error_codes::SUCCESS; }
bool set_global_impl(std::string name, flexible_type val) { size_t idx = get_global_registry_map()[name]; set_value_visitor visitor; visitor.new_value = val; return boost::apply_visitor(visitor, get_global_registry()[idx].value); }
flexible_type get_global(std::string name) { if (get_global_registry_map().count(name) == 0) return FLEX_UNDEFINED; size_t idx = get_global_registry_map()[name]; return boost::apply_visitor(get_value_visitor(), get_global_registry()[idx].value); }