From b54a595e2642a85f7d98c577ef26cd93c6e575a3 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Wed, 28 Jul 2010 16:15:52 +0200 Subject: [PATCH] Improved conversion from color enum to string and vice versa. The stringToColor() and colorToString() methods were changed into file-scope functions, so they can be inlined by the compiler. The colorNames array is now used for lookups in both directions, removing duplication of the color names. The missing "selectionBg" entry was added to the colorNames array. --- src/gmenu2x.cpp | 61 ++++++++++++++++++++----------------------------- src/gmenu2x.h | 4 +--- 2 files changed, 26 insertions(+), 39 deletions(-) diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index d7484e2..f9ec5a7 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -82,6 +82,31 @@ const char *CARD_ROOT = "/card"; using namespace std; using namespace fastdelegate; +// Note: Keep this in sync with the enum! +static const char *colorNames[NUM_COLORS] = { + "topBarBg", + "bottomBarBg", + "selectionBg", + "messageBoxBg", + "messageBoxBorder", + "messageBoxSelection", +}; + +static enum color stringToColor(const string &name) +{ + for (unsigned int i = 0; i < NUM_COLORS; i++) { + if (strcmp(colorNames[i], name.c_str()) == 0) { + return (enum color)i; + } + } + return (enum color)-1; +} + +static const char *colorToString(enum color c) +{ + return colorNames[c]; +} + int main(int /*argc*/, char */*argv*/[]) { cout << "----" << endl; cout << "GMenu2X starting: If you read this message in the logs, check http://gmenu2x.sourceforge.net/page/Troubleshooting for a solution" << endl; @@ -1096,42 +1121,6 @@ void GMenu2X::skinMenu() { } } -enum color GMenu2X::stringToColor(const string &name) -{ - if (name == "topBarBg") - return COLOR_TOP_BAR_BG; - else if (name == "bottomBarBg") - return COLOR_BOTTOM_BAR_BG; - else if (name == "messageBoxBg") - return COLOR_MESSAGE_BOX_BG; - else if (name == "messageBoxBorder") - return COLOR_MESSAGE_BOX_BORDER; - else if (name == "messageBoxSelection") - return COLOR_MESSAGE_BOX_SELECTION; - else - return (enum color)-1; -} - - - -const string &GMenu2X::colorToString(enum color c) -{ - static const std::string colorNames[NUM_COLORS + 1] = { - "topBarBg", - "bottomBarBg", - "messageBoxBg", - "messageBoxBorder", - "messageBoxSelection", - "unkown", - }; - - if (c < NUM_COLORS) - return colorNames[c]; - else - return colorNames[NUM_COLORS]; -} - - void GMenu2X::toggleTvOut() { #ifdef TARGET_GP2X /* if (cx25874!=0) diff --git a/src/gmenu2x.h b/src/gmenu2x.h index dd0be68..aba0d34 100644 --- a/src/gmenu2x.h +++ b/src/gmenu2x.h @@ -52,6 +52,7 @@ extern const char *CARD_ROOT; extern void jz_cpuspeed(unsigned clockspeed); +// Note: Keep this in sync with colorNames! enum color { COLOR_TOP_BAR_BG, COLOR_BOTTOM_BAR_BG, @@ -144,9 +145,6 @@ private: void gp2x_deinit(); void toggleTvOut(); - enum color stringToColor(const string &name); - const string &colorToString(enum color); - public: GMenu2X(); ~GMenu2X();