fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / khtml / misc / translator.h
blob09128a6293e61f4741f12e017e18bb8d9abe4b9e
1 /**
2 * This file is part of the KHTML renderer for KDE.
4 * Copyright (C) 2006, 2007 Maksim Orlovich (maksim@kde.org)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #ifndef KHTML_MISC_TRANSLATOR_H
23 #define KHTML_MISC_TRANSLATOR_H
25 #include <QtCore/QMap>
27 //---------------------------------------------------------------------------------------------
28 /* This class is used to do remapping between different encodings reasonably compactly.
29 It stores things as (L,R) tables, but reads left-side from memory as MemL */
30 template<typename L, typename R, typename MemL>
31 class IDTranslator
33 public:
34 struct Info {
35 MemL l;
36 R r;
39 IDTranslator(const Info* table) {
40 for (const Info* cursor = table; cursor->l; ++cursor) {
41 m_lToR.insert(cursor->l, cursor->r);
42 m_rToL.insert(cursor->r, cursor->l);
46 bool hasLeft(L l) {
47 typename QMap<L,R>::iterator i = m_lToR.find(l);
48 return i != m_lToR.end();
51 L toLeft(R r) {
52 typename QMap<R,L>::iterator i( m_rToL.find(r) );
53 if (i != m_rToL.end())
54 return *i;
55 return L();
58 R toRight(L l) {
59 typename QMap<L,R>::iterator i = m_lToR.find(l);
60 if (i != m_lToR.end())
61 return *i;
62 return R();
65 private:
66 QMap<L, R> m_lToR;
67 QMap<R, L> m_rToL;
70 #define MAKE_TRANSLATOR(name,L,R,MR,table) static IDTranslator<L,R,MR>* s_##name; \
71 static IDTranslator<L,R,MR>* name() { if (!s_##name) s_##name = new IDTranslator<L,R,MR>(table); \
72 return s_##name; }
75 #endif