Date:2014-07-16 04:15:10 (3 months 5 days ago)
Author:Nebuleon Fumika
Commit:98a63e2940fc9ed865508c279bcbc31fe4d005bf
Message:Fix setting default values for configuration entries.

Previously, one would check the value in &confInt["someKey"] by passing a
reference to it to evalIntConf. However, because this passing of the reference
went through std::unordered_map::operator[], it created a slot with the named
key and initialised its value with the default constructor of int, which
placed 0 there, if it didn't exist. If the value of 0 was acceptable for the
setting, then 0 as the value selected by the user was indistinguishable from
a slot that had been just created and had to be set to its default.

Now, the std::unordered_map is passed along with the key so that evalIntConf
can check whether the key exists and, if it doesn't exist, set the value to
its default.

Include utilities.h in gmenu2x.h instead of the reverse. One type definition
used by utilities.cpp is moved there (ConfIntHash) and for consistency
ConfStrHash is moved there as well.
Files: src/gmenu2x.cpp (2 diffs)
src/gmenu2x.h (2 diffs)
src/utilities.cpp (1 diff)
src/utilities.h (2 diffs)

Change Details

src/gmenu2x.cpp
490490    if (confStr["skin"].empty() || SurfaceCollection::getSkinPath(confStr["skin"]).empty())
491491        confStr["skin"] = "Default";
492492
493    evalIntConf( &confInt["outputLogs"], 0, 0,1 );
493    evalIntConf( confInt, "outputLogs", 0, 0,1 );
494494#ifdef ENABLE_CPUFREQ
495    evalIntConf( &confInt["maxClock"],
495    evalIntConf( confInt, "maxClock",
496496                 cpuFreqSafeMax, cpuFreqMin, cpuFreqMax );
497    evalIntConf( &confInt["menuClock"],
497    evalIntConf( confInt, "menuClock",
498498                 cpuFreqMenuDefault, cpuFreqMin, cpuFreqSafeMax );
499499#endif
500    evalIntConf( &confInt["backlightTimeout"], 15, 0,120 );
501    evalIntConf( &confInt["videoBpp"], 32, 16, 32 );
500    evalIntConf( confInt, "backlightTimeout", 15, 0,120 );
501    evalIntConf( confInt, "videoBpp", 32, 16, 32 );
502502
503503    if (confStr["tvoutEncoding"] != "PAL") confStr["tvoutEncoding"] = "NTSC";
504504    resX = constrain( confInt["resolutionX"], 320,1920 );
...... 
846846        }
847847    }
848848
849    evalIntConf(&skinConfInt["topBarHeight"], 50, 32, 120);
850    evalIntConf(&skinConfInt["bottomBarHeight"], 20, 20, 120);
851    evalIntConf(&skinConfInt["linkHeight"], 50, 32, 120);
852    evalIntConf(&skinConfInt["linkWidth"], 80, 32, 120);
849    evalIntConf(skinConfInt, "topBarHeight", 50, 32, 120);
850    evalIntConf(skinConfInt, "bottomBarHeight", 20, 20, 120);
851    evalIntConf(skinConfInt, "linkHeight", 50, 32, 120);
852    evalIntConf(skinConfInt, "linkWidth", 80, 32, 120);
853853
854854    if (menu != NULL) menu->skinUpdated();
855855
src/gmenu2x.h
2727#include "touchscreen.h"
2828#include "inputmanager.h"
2929#include "surface.h"
30#include "utilities.h"
3031
3132#include <iostream>
3233#include <memory>
3334#include <string>
34#include <unordered_map>
3535#include <vector>
3636
3737class Button;
...... 
6868    NUM_COLORS,
6969};
7070
71typedef std::unordered_map<std::string, std::string, std::hash<std::string> > ConfStrHash;
72typedef std::unordered_map<std::string, int, std::hash<std::string> > ConfIntHash;
73
7471class GMenu2X {
7572private:
7673    Touchscreen ts;
src/utilities.cpp
9393}
9494
9595//Configuration parsing utilities
96int evalIntConf (int val, int def, int imin, int imax) {
97    if (val==0 && (val<imin || val>imax))
98        return def;
99    val = constrain(val, imin, imax);
100    return val;
101}
102int evalIntConf (int *val, int def, int imin, int imax) {
103    *val = evalIntConf(*val, def, imin, imax);
104    return *val;
96int evalIntConf (ConfIntHash& hash, const std::string &key, int def, int imin, int imax) {
97    auto it = hash.find(key);
98    if (it == hash.end()) {
99        return hash[key] = def;
100    } else {
101        return it->second = constrain(it->second, imin, imax);
102    }
105103}
106104
107105bool split (vector<string> &vec, const string &str, const string &delim, bool destructive) {
src/utilities.h
2323
2424#include <string>
2525#include <vector>
26#include <unordered_map>
2627
2728#include "inputmanager.h"
2829
30typedef std::unordered_map<std::string, std::string, std::hash<std::string> > ConfStrHash;
31typedef std::unordered_map<std::string, int, std::hash<std::string> > ConfIntHash;
32
2933class case_less {
3034public:
3135    bool operator()(const std::string &left, const std::string &right) const;
...... 
4044
4145int constrain(int x, int imin, int imax);
4246
43int evalIntConf(int val, int def, int imin, int imax);
44int evalIntConf(int *val, int def, int imin, int imax);
47int evalIntConf(ConfIntHash& hash, const std::string &key, int def, int imin, int imax);
4548
4649bool split(std::vector<std::string> &vec, const std::string &str,
4750        const std::string &delim, bool destructive=true);

Archive Download the corresponding diff file



interactive