1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-16 18:15:17 +00:00

Improve range_config_setting<>

now it catches negative values assigned to unsigned settings
This commit is contained in:
Pavel Labath 2010-08-25 18:51:29 +02:00
parent 2c96799e59
commit 5f60b5427e
5 changed files with 34 additions and 15 deletions

View File

@ -181,8 +181,8 @@ double update_interval;
double update_interval_old; double update_interval_old;
double update_interval_bat; double update_interval_bat;
void *global_cpu = NULL; void *global_cpu = NULL;
static conky::range_config_setting<int> max_text_width("max_text_width", 0, static conky::range_config_setting<unsigned int> max_text_width("max_text_width", 0,
std::numeric_limits<int>::max(), 0, true); std::numeric_limits<unsigned int>::max(), 0, true);
int ifup_strictness = IFUP_UP; int ifup_strictness = IFUP_UP;
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) #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" "conky.config = { alignment='top_left', asdf=47, [42]=47, out_to_x=true,\n"
" own_window_hints='above, skip_taskbar',\n" " own_window_hints='above, skip_taskbar',\n"
" background_colour='pink', own_window=true, double_buffer=true,\n" " background_colour='pink', own_window=true, double_buffer=true,\n"
" mpd_host='asdf'};\n" " mpd_port=-47};\n"
); );
l.call(0, 0); l.call(0, 0);
conky::set_config_settings(l); 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.own_window_hints = ', conky.config.own_window_hints);\n"
"print('config.mpd_host = ', conky.config.mpd_host);\n" "print('config.mpd_host = ', conky.config.mpd_host);\n"
"print('config.mpd_password = ', conky.config.mpd_password);\n" "print('config.mpd_password = ', conky.config.mpd_password);\n"
"print('config.mpd_port = ', conky.config.mpd_port);\n"
); );
l.call(0, 0); l.call(0, 0);

View File

@ -114,7 +114,7 @@ namespace {
++s; ++s;
} }
conky::range_config_setting<int> mpd_port("mpd_port", 1, 65535, 6600, false); conky::range_config_setting<in_port_t> mpd_port("mpd_port", 1, 65535, 6600, false);
mpd_host_setting mpd_host; mpd_host_setting mpd_host;
mpd_password_setting mpd_password; mpd_password_setting mpd_password;
} }

View File

@ -37,7 +37,7 @@
namespace { namespace {
conky::simple_config_setting<std::string> host("mysql_host", "localhost", false); conky::simple_config_setting<std::string> host("mysql_host", "localhost", false);
conky::range_config_setting<int> port("mysql_port", 0, 0xffff, 0, false); conky::range_config_setting<in_port_t> port("mysql_port", 0, 0xffff, 0, false);
conky::simple_config_setting<std::string> user("mysql_user", "root", false); conky::simple_config_setting<std::string> user("mysql_user", "root", false);
conky::simple_config_setting<std::string> password("mysql_password", std::string(), false); conky::simple_config_setting<std::string> password("mysql_password", std::string(), false);
conky::simple_config_setting<std::string> db("mysql_db", "mysql", false); conky::simple_config_setting<std::string> db("mysql_db", "mysql", false);

View File

@ -64,8 +64,9 @@ namespace conky {
template<typename T> template<typename T>
struct lua_traits<T, true, false, false> { struct lua_traits<T, true, false, false> {
static const lua::Type type = lua::TNUMBER; static const lua::Type type = lua::TNUMBER;
typedef lua::integer Type;
static inline std::pair<T, bool> static inline std::pair<Type, bool>
convert(lua::state &l, int index, const std::string &) convert(lua::state &l, int index, const std::string &)
{ return {l.tointeger(index), true}; } { return {l.tointeger(index), true}; }
}; };
@ -74,8 +75,9 @@ namespace conky {
template<typename T> template<typename T>
struct lua_traits<T, false, true, false> { struct lua_traits<T, false, true, false> {
static const lua::Type type = lua::TNUMBER; static const lua::Type type = lua::TNUMBER;
typedef lua::number Type;
static inline std::pair<T, bool> static inline std::pair<Type, bool>
convert(lua::state &l, int index, const std::string &) convert(lua::state &l, int index, const std::string &)
{ return {l.tonumber(index), true}; } { return {l.tonumber(index), true}; }
}; };
@ -84,8 +86,9 @@ namespace conky {
template<> template<>
struct lua_traits<std::string, false, false, false> { struct lua_traits<std::string, false, false, false> {
static const lua::Type type = lua::TSTRING; static const lua::Type type = lua::TSTRING;
typedef std::string Type;
static inline std::pair<std::string, bool> static inline std::pair<Type, bool>
convert(lua::state &l, int index, const std::string &) convert(lua::state &l, int index, const std::string &)
{ return {l.tostring(index), true}; } { return {l.tostring(index), true}; }
}; };
@ -94,8 +97,9 @@ namespace conky {
template<> template<>
struct lua_traits<bool, true, false, false> { struct lua_traits<bool, true, false, false> {
static const lua::Type type = lua::TBOOLEAN; static const lua::Type type = lua::TBOOLEAN;
typedef bool Type;
static inline std::pair<bool, bool> static inline std::pair<Type, bool>
convert(lua::state &l, int index, const std::string &) convert(lua::state &l, int index, const std::string &)
{ return {l.toboolean(index), true}; } { return {l.toboolean(index), true}; }
}; };
@ -105,6 +109,7 @@ namespace conky {
template<typename T> template<typename T>
struct lua_traits<T, false, false, true> { struct lua_traits<T, false, false, true> {
static const lua::Type type = lua::TSTRING; static const lua::Type type = lua::TSTRING;
typedef T Type;
typedef std::initializer_list<std::pair<std::string, T>> Map; typedef std::initializer_list<std::pair<std::string, T>> Map;
static Map map; static Map map;
@ -244,7 +249,7 @@ namespace conky {
const T default_value; const T default_value;
const bool modifiable; const bool modifiable;
virtual std::pair<T, bool> do_convert(lua::state &l, int index); virtual std::pair<typename Traits::Type, bool> do_convert(lua::state &l, int index);
virtual void lua_setter(lua::state &l, bool init); virtual void lua_setter(lua::state &l, bool init);
virtual T getter(lua::state &l) virtual T getter(lua::state &l)
@ -261,7 +266,8 @@ namespace conky {
}; };
template<typename T, typename Traits> template<typename T, typename Traits>
std::pair<T, bool> simple_config_setting<T, Traits>::do_convert(lua::state &l, int index) std::pair<typename Traits::Type, bool>
simple_config_setting<T, Traits>::do_convert(lua::state &l, int index)
{ {
if(l.isnil(index)) if(l.isnil(index))
return {default_value, true}; return {default_value, true};
@ -294,6 +300,16 @@ namespace conky {
++s; ++s;
} }
template<typename Signed1, typename Signed2>
bool between(Signed1 value, Signed2 min,
typename std::enable_if<std::is_signed<Signed2>::value, Signed2>::type max)
{ return value >= min && value <= max; }
template<typename Signed1, typename Unsigned2>
bool between(Signed1 value, Unsigned2 min,
typename std::enable_if<std::is_unsigned<Unsigned2>::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 // Just like simple_config_setting, except that in only accepts value in the [min, max] range
template<typename T, typename Traits = lua_traits<T>> template<typename T, typename Traits = lua_traits<T>>
class range_config_setting: public simple_config_setting<T, Traits> { class range_config_setting: public simple_config_setting<T, Traits> {
@ -311,10 +327,10 @@ namespace conky {
{ assert(min <= Base::default_value && Base::default_value <= max); } { assert(min <= Base::default_value && Base::default_value <= max); }
protected: protected:
virtual std::pair<T, bool> do_convert(lua::state &l, int index) virtual std::pair<typename Traits::Type, bool> do_convert(lua::state &l, int index)
{ {
auto ret = Base::do_convert(l, 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()); 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 // we ignore out-of-range values. an alternative would be to clamp them. do we
// want to do that? // want to do that?

View File

@ -176,8 +176,9 @@ namespace priv {
struct colour_traits { struct colour_traits {
static const lua::Type type = lua::TSTRING; static const lua::Type type = lua::TSTRING;
typedef unsigned long Type;
static inline std::pair<unsigned long, bool> static inline std::pair<Type, bool>
convert(lua::state &l, int index, const std::string &) convert(lua::state &l, int index, const std::string &)
{ return {get_x11_color(l.tostring(index)), true}; } { return {get_x11_color(l.tostring(index)), true}; }
}; };
@ -219,7 +220,8 @@ extern conky::simple_config_setting<window_type> own_window_type;
struct window_hints_traits { struct window_hints_traits {
static const lua::Type type = lua::TSTRING; static const lua::Type type = lua::TSTRING;
static std::pair<uint16_t, bool> convert(lua::state &l, int index, const std::string &name); typedef uint16_t Type;
static std::pair<Type, bool> convert(lua::state &l, int index, const std::string &name);
}; };
extern conky::simple_config_setting<uint16_t, window_hints_traits> own_window_hints; extern conky::simple_config_setting<uint16_t, window_hints_traits> own_window_hints;