diff options
| author | Johan B. C. Engelen <jbc.engelen@swissonline.ch> | 2008-07-16 21:38:16 +0000 |
|---|---|---|
| committer | johanengelen <johanengelen@users.sourceforge.net> | 2008-07-16 21:38:16 +0000 |
| commit | d80050f0b63965cb3b2b70317cae4981c688708e (patch) | |
| tree | c7f8bd2cfe10bc82b5c71bd39d865a7e25324a43 /src/util/enums.h | |
| parent | update to latest 2geom (rev1497) (diff) | |
| download | inkscape-d80050f0b63965cb3b2b70317cae4981c688708e.tar.gz inkscape-d80050f0b63965cb3b2b70317cae4981c688708e.zip | |
fix combo enum, to handle enums of all types (not only the ones that range from 0..10)
(bzr r6333)
Diffstat (limited to 'src/util/enums.h')
| -rw-r--r-- | src/util/enums.h | 51 |
1 files changed, 34 insertions, 17 deletions
diff --git a/src/util/enums.h b/src/util/enums.h index 69514b202..cbb1cb9a1 100644 --- a/src/util/enums.h +++ b/src/util/enums.h @@ -15,6 +15,7 @@ * Instead, one must use N_(...) and do the translation every time the string is retreived. * * Note that get_id_from_key and get_id_from_label return 0 if it cannot find an entry for that key string + * Note that get_label and get_key return an empty string when the requested id is not in the list. */ @@ -26,28 +27,30 @@ namespace Inkscape { namespace Util { -template<typename E> class EnumData +template<typename E> +struct EnumData { -public: E id; const Glib::ustring label; const Glib::ustring key; }; +const Glib::ustring empty_string(""); + template<typename E> class EnumDataConverter { public: typedef EnumData<E> Data; - EnumDataConverter(const EnumData<E>* cd, const int endval) - : end(endval), _data(cd) + EnumDataConverter(const EnumData<E>* cd, const unsigned int length) + : _length(length), _data(cd) {} E get_id_from_label(const Glib::ustring& label) const { - for(int i = 0; i < end; ++i) { + for(unsigned int i = 0; i < _length; ++i) { if(_data[i].label == label) - return (E)i; + return _data[i].id; } return (E)0; @@ -55,9 +58,9 @@ public: E get_id_from_key(const Glib::ustring& key) const { - for(int i = 0; i < end; ++i) { + for(unsigned int i = 0; i < _length; ++i) { if(_data[i].key == key) - return (E)i; + return _data[i].id; } return (E)0; @@ -65,7 +68,7 @@ public: bool is_valid_key(const Glib::ustring& key) const { - for(int i = 0; i < end; ++i) { + for(unsigned int i = 0; i < _length; ++i) { if(_data[i].key == key) return true; } @@ -73,27 +76,41 @@ public: return false; } - bool is_valid_id(const E e) const + bool is_valid_id(const E id) const { - return ( (int)e < end ); + for(unsigned int i = 0; i < _length; ++i) { + if(_data[i].id == id) + return true; + } + return false; } - const Glib::ustring& get_label(const E e) const + const Glib::ustring& get_label(const E id) const { - return _data[e].label; + for(unsigned int i = 0; i < _length; ++i) { + if(_data[i].id == id) + return _data[i].label; + } + + return empty_string; } - const Glib::ustring& get_key(const E e) const + const Glib::ustring& get_key(const E id) const { - return _data[e].key; + for(unsigned int i = 0; i < _length; ++i) { + if(_data[i].id == id) + return _data[i].key; + } + + return empty_string; } - const EnumData<E>& data(const int i) const + const EnumData<E>& data(const unsigned int i) const { return _data[i]; } - const int end; + const unsigned int _length; private: const EnumData<E>* _data; }; |
