From: Michael Andres Date: Wed, 23 Nov 2011 13:21:58 +0000 (+0100) Subject: Fix some static initializer issues X-Git-Tag: 1.6.18~9 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6169146191a853d7d76c905fa49ce273e98a8c1f;p=platform%2Fupstream%2Fzypper.git Fix some static initializer issues --- diff --git a/src/Config.cc b/src/Config.cc index 536a220..35b51ee 100644 --- a/src/Config.cc +++ b/src/Config.cc @@ -6,7 +6,7 @@ \*---------------------------------------------------------------------------*/ #include -#include +#include extern "C" { #include @@ -30,8 +30,6 @@ extern "C" using namespace std; using namespace zypp; -static map _table; -static map _table_str; const ConfigOption ConfigOption::MAIN_SHOW_ALIAS(ConfigOption::MAIN_SHOW_ALIAS_e); const ConfigOption ConfigOption::MAIN_REPO_LIST_COLUMNS(ConfigOption::MAIN_REPO_LIST_COLUMNS_e); const ConfigOption ConfigOption::SOLVER_INSTALL_RECOMMENDS(ConfigOption::SOLVER_INSTALL_RECOMMENDS_e); @@ -50,64 +48,82 @@ const ConfigOption ConfigOption::COLOR_PROMPT_SHORTHAND(ConfigOption::COLOR_PROM const ConfigOption ConfigOption::OBS_BASE_URL(ConfigOption::OBS_BASE_URL_e); const ConfigOption ConfigOption::OBS_PLATFORM(ConfigOption::OBS_PLATFORM_e); +////////////////////////////////////////////////////////////////// +namespace +{ + typedef std::pair OptionPair; + /* add new options here: */ + const std::vector & optionPairs() + { + static const std::vector _data = { + { "main/showAlias", ConfigOption::MAIN_SHOW_ALIAS_e }, + { "main/repoListColumns", ConfigOption::MAIN_REPO_LIST_COLUMNS_e }, + { "solver/installRecommends", ConfigOption::SOLVER_INSTALL_RECOMMENDS_e }, + { "solver/forceResolutionCommands", ConfigOption::SOLVER_FORCE_RESOLUTION_COMMANDS_e}, + { "color/useColors", ConfigOption::COLOR_USE_COLORS_e }, + { "color/background", ConfigOption::COLOR_BACKGROUND_e }, + { "color/result", ConfigOption::COLOR_RESULT_e }, + { "color/msgStatus", ConfigOption::COLOR_MSG_STATUS_e }, + { "color/msgError", ConfigOption::COLOR_MSG_ERROR_e }, + { "color/msgWarning", ConfigOption::COLOR_MSG_WARNING_e }, + { "color/positive", ConfigOption::COLOR_POSITIVE_e }, + { "color/negative", ConfigOption::COLOR_NEGATIVE_e }, + { "color/highlight", ConfigOption::COLOR_HIGHLIGHT_e }, + { "color/promptOption", ConfigOption::COLOR_PROMPT_OPTION_e }, + { "obs/baseUrl", ConfigOption::OBS_BASE_URL_e }, + { "obs/platform", ConfigOption::OBS_PLATFORM_e } + }; + return _data; + } +} // namespace +////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////// +namespace std +{ + template<> + struct hash + { + size_t operator()( const ConfigOption::Option & __s ) const + { return __s; } + }; +} // namespace std +////////////////////////////////////////////////////////////////// + ConfigOption::ConfigOption(const std::string & strval_r) : _value(parse(strval_r)) {} ConfigOption::Option ConfigOption::parse(const std::string & strval_r) { - if (_table.empty()) - { - // initialize it - _table["main/showAlias"] = MAIN_SHOW_ALIAS_e; - _table["main/repoListColumns"] = MAIN_REPO_LIST_COLUMNS_e; - _table["solver/installRecommends"] = SOLVER_INSTALL_RECOMMENDS_e; - _table["solver/forceResolutionCommands"] = SOLVER_FORCE_RESOLUTION_COMMANDS_e; - _table["color/useColors"] = COLOR_USE_COLORS_e; - _table["color/background"] = COLOR_BACKGROUND_e; - _table["color/result"] = COLOR_RESULT_e; - _table["color/msgStatus"] = COLOR_MSG_STATUS_e; - _table["color/msgError"] = COLOR_MSG_ERROR_e; - _table["color/msgWarning"] = COLOR_MSG_WARNING_e; - _table["color/positive"] = COLOR_POSITIVE_e; - _table["color/negative"] = COLOR_NEGATIVE_e; - _table["color/highlight"] = COLOR_HIGHLIGHT_e; - _table["color/promptOption"] = COLOR_PROMPT_OPTION_e; - } - map::const_iterator it = _table.find(strval_r); - if (it == _table.end()) + static std::unordered_map _index = [](){ + // index into optionPairs + std::unordered_map data; + for ( const auto & p : optionPairs() ) + data[p.first] = p.second; + return data; + }(); + + auto it( _index.find( strval_r ) ); + if ( it == _index.end() ) { - string message = - zypp::str::form(_("Unknown configuration option '%s'"), strval_r.c_str()); - ZYPP_THROW(zypp::Exception(message)); + ZYPP_THROW(zypp::Exception( + zypp::str::form(_("Unknown configuration option '%s'"), strval_r.c_str()))); } return it->second; } -const string ConfigOption::asString() const +string ConfigOption::asString() const { - if (_table.empty()) - { - // initialize it - _table_str[MAIN_SHOW_ALIAS_e] = "main/showAlias"; - _table_str[MAIN_REPO_LIST_COLUMNS_e] = "main/repoListColumns"; - _table_str[SOLVER_INSTALL_RECOMMENDS_e] = "solver/installRecommends"; - _table_str[SOLVER_FORCE_RESOLUTION_COMMANDS_e] = "solver/forceResolutionCommands"; - _table_str[COLOR_USE_COLORS_e] = "color/useColors"; - _table_str[COLOR_BACKGROUND_e] = "color/background"; - _table_str[COLOR_RESULT_e] = "color/result"; - _table_str[COLOR_MSG_STATUS_e] = "color/msgStatus"; - _table_str[COLOR_MSG_ERROR_e] = "color/msgError"; - _table_str[COLOR_MSG_WARNING_e] = "color/msgWarning"; - _table_str[COLOR_POSITIVE_e] = "color/positive"; - _table_str[COLOR_NEGATIVE_e] = "color/negative"; - _table_str[COLOR_HIGHLIGHT_e] = "color/highlight"; - _table_str[COLOR_PROMPT_OPTION_e] = "color/promptOption"; - _table_str[OBS_BASE_URL_e] = "obs/baseUrl"; - _table_str[OBS_PLATFORM_e] = "obs/platform"; - } - map::const_iterator it = _table_str.find(_value); - if (it != _table_str.end()) + static std::unordered_map _index = [](){ + // index into optionPairs + std::unordered_map data; + for ( const auto & p : optionPairs() ) + data[p.second] = p.first; + return data; + }(); + + auto it( _index.find( _value ) ); + if ( it != _index.end() ) return it->second; return string(); } diff --git a/src/Config.h b/src/Config.h index 17fa1df..a2625b1 100644 --- a/src/Config.h +++ b/src/Config.h @@ -71,7 +71,7 @@ public: ConfigOption::Option parse(const std::string & strval_r); - const std::string asString() const; + std::string asString() const; private: Option _value; diff --git a/src/utils/colors.cc b/src/utils/colors.cc index b1ead85..cd1d27e 100644 --- a/src/utils/colors.cc +++ b/src/utils/colors.cc @@ -18,7 +18,6 @@ using namespace std; -static map str2esc; Color::Color(const string & color_str) : _value(parse(color_str)) @@ -26,32 +25,28 @@ Color::Color(const string & color_str) string Color::parse(const string & value) { - if (value.empty()) - return value; - - if (str2esc.empty()) - { - str2esc["green"] = COLOR_GREEN; - str2esc["lightgreen"] = COLOR_GREEN_LIGHT; - str2esc["red"] = COLOR_RED; - str2esc["lightred"] = COLOR_RED_LIGHT; - str2esc["grey"] = COLOR_WHITE; - str2esc["white"] = COLOR_WHITE_LIGHT; - str2esc["brown"] = COLOR_YELLOW; - str2esc["yellow"] = COLOR_YELLOW_LIGHT; - str2esc["purple"] = COLOR_PURPLE; - str2esc["lightpurple"] = COLOR_PURPLE_LIGHT; - str2esc["blue"] = COLOR_BLUE; - str2esc["lightblue"] = COLOR_BLUE_LIGHT; - str2esc["cyan"] = COLOR_CYAN; - str2esc["lightcyan"] = COLOR_CYAN_LIGHT; - str2esc["black"] = COLOR_BLACK; - str2esc["darkgrey"] = COLOR_GREY_DARK; - - str2esc["reset"] = COLOR_RESET; - } - - map::const_iterator it = str2esc.find(value); + static map str2esc = { + { "green", COLOR_GREEN }, + { "lightgreen", COLOR_GREEN_LIGHT }, + { "red", COLOR_RED }, + { "lightred", COLOR_RED_LIGHT }, + { "grey", COLOR_WHITE }, + { "white", COLOR_WHITE_LIGHT }, + { "brown", COLOR_YELLOW }, + { "yellow", COLOR_YELLOW_LIGHT }, + { "purple", COLOR_PURPLE }, + { "lightpurple", COLOR_PURPLE_LIGHT }, + { "blue", COLOR_BLUE }, + { "lightblue", COLOR_BLUE_LIGHT }, + { "cyan", COLOR_CYAN }, + { "lightcyan", COLOR_CYAN_LIGHT }, + { "black", COLOR_BLACK }, + { "darkgrey", COLOR_GREY_DARK }, + + { "reset", COLOR_RESET } + }; + + auto it = str2esc.find(value); if (it == str2esc.end()) { ERR << "Unknown color '" << value << "'" << endl; diff --git a/src/utils/misc.cc b/src/utils/misc.cc index eaa7904..7f64b84 100644 --- a/src/utils/misc.cc +++ b/src/utils/misc.cc @@ -133,13 +133,6 @@ string string_patch_status(const PoolItem & pi) bool looks_like_url (const string& s) { -/* - static bool schemes_shown = false; - if (!schemes_shown) { - DBG << "Registered schemes: " << Url::getRegisteredSchemes () << endl; - schemes_shown = true; - } -*/ string::size_type pos = s.find (':'); if (pos != string::npos) {