From 57f20fcbb78c23ec9833e18efb524b5239db7c98 Mon Sep 17 00:00:00 2001 From: Stefan Seyfried Date: Sat, 16 Jan 2016 17:48:30 +0100 Subject: [PATCH] CListHelpers: allow UpDownKey with int type, too --- src/gui/widget/listhelpers.cpp | 25 ++++++++++++++++++------- src/gui/widget/listhelpers.h | 10 +++++++++- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/gui/widget/listhelpers.cpp b/src/gui/widget/listhelpers.cpp index 7ad2e1cec..c06c3504f 100644 --- a/src/gui/widget/listhelpers.cpp +++ b/src/gui/widget/listhelpers.cpp @@ -24,10 +24,10 @@ #include #include -template int CListHelpers::UpDownKey(T list, neutrino_msg_t msg, int lines, int sel) +static int upDownKey(int size, neutrino_msg_t msg, int lines, int sel) { int step; - if (list.empty()) + if (size <= 0) /* list.empty() or similar... */ return -1; if (msg >= CRCInput::RC_MaxRC) { @@ -47,7 +47,6 @@ template int CListHelpers::UpDownKey(T list, neutrino_msg_t msg, int l printf("CListHelpers:%s: invalid key? 0x%lx\n", __func__, msg); return -1; } - int size = (int)list.size(); /* bigger than 0, because we checked for empty() before */ // printf("CListHelpers:%s: key 0x%04lx lines %d size %d sel %d\n", __func__, msg, lines, size, sel); int new_sel = sel + step; if (new_sel < 0) { @@ -65,8 +64,20 @@ template int CListHelpers::UpDownKey(T list, neutrino_msg_t msg, int l return new_sel; } +int CListHelpers::_UpDownKey(int size, neutrino_msg_t msg, int lines, int sel, _id) +{ + return upDownKey(size, msg, lines, sel); +} + +template int CListHelpers::_UpDownKey(T list, neutrino_msg_t msg, int lines, int sel, _id) +{ + return upDownKey(list.size(), msg, lines, sel); +} + /* all used versions need to be prototyped here, to avoid linker errors */ -template int CListHelpers::UpDownKey >(std::vector, neutrino_msg_t, int, int); -template int CListHelpers::UpDownKey >(std::vector, neutrino_msg_t, int, int); -template int CListHelpers::UpDownKey >(std::vector, neutrino_msg_t, int, int); -template int CListHelpers::UpDownKey >(std::vector, neutrino_msg_t, int, int); +/* helper macro for the prototypes */ +#define updown_t(x) template int CListHelpers::_UpDownKey(x, neutrino_msg_t, int, int, _id) +updown_t(std::vector); +updown_t(std::vector); +updown_t(std::vector); +updown_t(std::vector); diff --git a/src/gui/widget/listhelpers.h b/src/gui/widget/listhelpers.h index 4ac2274cc..228bd88d7 100644 --- a/src/gui/widget/listhelpers.h +++ b/src/gui/widget/listhelpers.h @@ -21,10 +21,18 @@ #ifndef __LISTHELPERS__ #define __LISTHELPERS__ +/* allow to trick the compiler into overriding template definitions */ +template struct _id { typedef T type; }; class CListHelpers { public: - template int UpDownKey(T list, neutrino_msg_t k, int lines, int sel); + template int UpDownKey(T list, neutrino_msg_t k, int lines, int sel) { + return _UpDownKey(list, k, lines, sel, _id()); + } + private: + /* stackoverflow.com/questions/3052579 */ + template int _UpDownKey(T list, neutrino_msg_t k, int lines, int sel, _id); + int _UpDownKey(int list, neutrino_msg_t k, int lines, int sel, _id); }; #endif