Date:2010-07-28 16:15:52 (13 years 4 months ago)
Author:Maarten ter Huurne
Commit:b54a595e2642a85f7d98c577ef26cd93c6e575a3
Message: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.
Files: src/gmenu2x.cpp (2 diffs)
src/gmenu2x.h (2 diffs)

Change Details

src/gmenu2x.cpp
8282using namespace std;
8383using namespace fastdelegate;
8484
85// Note: Keep this in sync with the enum!
86static const char *colorNames[NUM_COLORS] = {
87    "topBarBg",
88    "bottomBarBg",
89    "selectionBg",
90    "messageBoxBg",
91    "messageBoxBorder",
92    "messageBoxSelection",
93};
94
95static enum color stringToColor(const string &name)
96{
97    for (unsigned int i = 0; i < NUM_COLORS; i++) {
98        if (strcmp(colorNames[i], name.c_str()) == 0) {
99            return (enum color)i;
100        }
101    }
102    return (enum color)-1;
103}
104
105static const char *colorToString(enum color c)
106{
107    return colorNames[c];
108}
109
85110int main(int /*argc*/, char */*argv*/[]) {
86111    cout << "----" << endl;
87112    cout << "GMenu2X starting: If you read this message in the logs, check http://gmenu2x.sourceforge.net/page/Troubleshooting for a solution" << endl;
...... 
10961121    }
10971122}
10981123
1099enum color GMenu2X::stringToColor(const string &name)
1100{
1101    if (name == "topBarBg")
1102        return COLOR_TOP_BAR_BG;
1103    else if (name == "bottomBarBg")
1104        return COLOR_BOTTOM_BAR_BG;
1105    else if (name == "messageBoxBg")
1106        return COLOR_MESSAGE_BOX_BG;
1107    else if (name == "messageBoxBorder")
1108        return COLOR_MESSAGE_BOX_BORDER;
1109    else if (name == "messageBoxSelection")
1110        return COLOR_MESSAGE_BOX_SELECTION;
1111    else
1112        return (enum color)-1;
1113}
1114
1115
1116
1117const string &GMenu2X::colorToString(enum color c)
1118{
1119    static const std::string colorNames[NUM_COLORS + 1] = {
1120        "topBarBg",
1121        "bottomBarBg",
1122        "messageBoxBg",
1123        "messageBoxBorder",
1124        "messageBoxSelection",
1125        "unkown",
1126    };
1127
1128    if (c < NUM_COLORS)
1129        return colorNames[c];
1130    else
1131        return colorNames[NUM_COLORS];
1132}
1133
1134
11351124void GMenu2X::toggleTvOut() {
11361125#ifdef TARGET_GP2X
11371126/* if (cx25874!=0)
src/gmenu2x.h
5252
5353extern void jz_cpuspeed(unsigned clockspeed);
5454
55// Note: Keep this in sync with colorNames!
5556enum color {
5657    COLOR_TOP_BAR_BG,
5758    COLOR_BOTTOM_BAR_BG,
...... 
144145    void gp2x_deinit();
145146    void toggleTvOut();
146147
147    enum color stringToColor(const string &name);
148    const string &colorToString(enum color);
149
150148public:
151149    GMenu2X();
152150    ~GMenu2X();

Archive Download the corresponding diff file



interactive