From 5f60b5427e1e222b795ebae2584eb58fefca8ff8 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Wed, 25 Aug 2010 18:51:29 +0200 Subject: [PATCH] Improve range_config_setting<> now it catches negative values assigned to unsigned settings --- src/conky.cc | 7 ++++--- src/mpd.cc | 2 +- src/mysql.cc | 2 +- src/setting.hh | 32 ++++++++++++++++++++++++-------- src/x11.h | 6 ++++-- 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/conky.cc b/src/conky.cc index 852783f9..6b264c93 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -181,8 +181,8 @@ double update_interval; double update_interval_old; double update_interval_bat; void *global_cpu = NULL; -static conky::range_config_setting max_text_width("max_text_width", 0, - std::numeric_limits::max(), 0, true); +static conky::range_config_setting max_text_width("max_text_width", 0, + std::numeric_limits::max(), 0, true); int ifup_strictness = IFUP_UP; #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) @@ -3594,7 +3594,7 @@ int main(int argc, char **argv) "conky.config = { alignment='top_left', asdf=47, [42]=47, out_to_x=true,\n" " own_window_hints='above, skip_taskbar',\n" " background_colour='pink', own_window=true, double_buffer=true,\n" - " mpd_host='asdf'};\n" + " mpd_port=-47};\n" ); l.call(0, 0); conky::set_config_settings(l); @@ -3612,6 +3612,7 @@ int main(int argc, char **argv) "print('config.own_window_hints = ', conky.config.own_window_hints);\n" "print('config.mpd_host = ', conky.config.mpd_host);\n" "print('config.mpd_password = ', conky.config.mpd_password);\n" + "print('config.mpd_port = ', conky.config.mpd_port);\n" ); l.call(0, 0); diff --git a/src/mpd.cc b/src/mpd.cc index 3075db66..696ce98e 100644 --- a/src/mpd.cc +++ b/src/mpd.cc @@ -114,7 +114,7 @@ namespace { ++s; } - conky::range_config_setting mpd_port("mpd_port", 1, 65535, 6600, false); + conky::range_config_setting mpd_port("mpd_port", 1, 65535, 6600, false); mpd_host_setting mpd_host; mpd_password_setting mpd_password; } diff --git a/src/mysql.cc b/src/mysql.cc index 6e9d992a..d6514b54 100644 --- a/src/mysql.cc +++ b/src/mysql.cc @@ -37,7 +37,7 @@ namespace { conky::simple_config_setting host("mysql_host", "localhost", false); - conky::range_config_setting port("mysql_port", 0, 0xffff, 0, false); + conky::range_config_setting port("mysql_port", 0, 0xffff, 0, false); conky::simple_config_setting user("mysql_user", "root", false); conky::simple_config_setting password("mysql_password", std::string(), false); conky::simple_config_setting db("mysql_db", "mysql", false); diff --git a/src/setting.hh b/src/setting.hh index b9baebd0..a0d527e7 100644 --- a/src/setting.hh +++ b/src/setting.hh @@ -64,8 +64,9 @@ namespace conky { template struct lua_traits { static const lua::Type type = lua::TNUMBER; + typedef lua::integer Type; - static inline std::pair + static inline std::pair convert(lua::state &l, int index, const std::string &) { return {l.tointeger(index), true}; } }; @@ -74,8 +75,9 @@ namespace conky { template struct lua_traits { static const lua::Type type = lua::TNUMBER; + typedef lua::number Type; - static inline std::pair + static inline std::pair convert(lua::state &l, int index, const std::string &) { return {l.tonumber(index), true}; } }; @@ -84,8 +86,9 @@ namespace conky { template<> struct lua_traits { static const lua::Type type = lua::TSTRING; + typedef std::string Type; - static inline std::pair + static inline std::pair convert(lua::state &l, int index, const std::string &) { return {l.tostring(index), true}; } }; @@ -94,8 +97,9 @@ namespace conky { template<> struct lua_traits { static const lua::Type type = lua::TBOOLEAN; + typedef bool Type; - static inline std::pair + static inline std::pair convert(lua::state &l, int index, const std::string &) { return {l.toboolean(index), true}; } }; @@ -105,6 +109,7 @@ namespace conky { template struct lua_traits { static const lua::Type type = lua::TSTRING; + typedef T Type; typedef std::initializer_list> Map; static Map map; @@ -244,7 +249,7 @@ namespace conky { const T default_value; const bool modifiable; - virtual std::pair do_convert(lua::state &l, int index); + virtual std::pair do_convert(lua::state &l, int index); virtual void lua_setter(lua::state &l, bool init); virtual T getter(lua::state &l) @@ -261,7 +266,8 @@ namespace conky { }; template - std::pair simple_config_setting::do_convert(lua::state &l, int index) + std::pair + simple_config_setting::do_convert(lua::state &l, int index) { if(l.isnil(index)) return {default_value, true}; @@ -294,6 +300,16 @@ namespace conky { ++s; } + template + bool between(Signed1 value, Signed2 min, + typename std::enable_if::value, Signed2>::type max) + { return value >= min && value <= max; } + + template + bool between(Signed1 value, Unsigned2 min, + typename std::enable_if::value, Unsigned2>::type max) + { return value >= 0 && value >= min && value <= max; } + // Just like simple_config_setting, except that in only accepts value in the [min, max] range template> class range_config_setting: public simple_config_setting { @@ -311,10 +327,10 @@ namespace conky { { assert(min <= Base::default_value && Base::default_value <= max); } protected: - virtual std::pair do_convert(lua::state &l, int index) + virtual std::pair do_convert(lua::state &l, int index) { auto ret = Base::do_convert(l, index); - if(ret.second && (ret.first < min || ret.first > max)) { + if(ret.second && !between(ret.first, min, max)) { NORM_ERR("Value is out of range for setting '%s'", Base::name.c_str()); // we ignore out-of-range values. an alternative would be to clamp them. do we // want to do that? diff --git a/src/x11.h b/src/x11.h index eb9f1a40..35ca0f09 100644 --- a/src/x11.h +++ b/src/x11.h @@ -176,8 +176,9 @@ namespace priv { struct colour_traits { static const lua::Type type = lua::TSTRING; + typedef unsigned long Type; - static inline std::pair + static inline std::pair convert(lua::state &l, int index, const std::string &) { return {get_x11_color(l.tostring(index)), true}; } }; @@ -219,7 +220,8 @@ extern conky::simple_config_setting own_window_type; struct window_hints_traits { static const lua::Type type = lua::TSTRING; - static std::pair convert(lua::state &l, int index, const std::string &name); + typedef uint16_t Type; + static std::pair convert(lua::state &l, int index, const std::string &name); }; extern conky::simple_config_setting own_window_hints;