File indexing completed on 2024-05-19 05:44:11

0001 /*
0002     Copyright (C) 2015 Volker Krause <vkrause@kde.org>
0003 
0004     This program is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program.  If not, see <https://www.gnu.org/licenses/>.
0016 */
0017 
0018 #ifndef PRINTERUTILS_P_H
0019 #define PRINTERUTILS_P_H
0020 
0021 #include <QByteArray>
0022 #include <QList>
0023 
0024 template <typename T>
0025 struct LookupTableEntry
0026 {
0027     T value;
0028     const char* const label;
0029 };
0030 
0031 // NOTE: the tables don't have to be sorted, so these lookups are linear.
0032 
0033 template <typename T, std::size_t N>
0034 QByteArray lookupLabel(T value, const LookupTableEntry<T> (&lookupTable)[N])
0035 {
0036     for (std::size_t i = 0; i < N; ++i) {
0037         if (lookupTable[i].value == value)
0038             return QByteArray::fromRawData(lookupTable[i].label, strlen(lookupTable[i].label));
0039     }
0040     return QByteArray("unknown (") + QByteArray::number(value) + ')';
0041 }
0042 
0043 template <typename T, std::size_t N>
0044 QByteArray lookupFlags(T flags, const LookupTableEntry<T> (&lookupTable)[N])
0045 {
0046     QList<QByteArray> l;
0047     T handledFlags = 0;
0048 
0049     for (std::size_t i = 0; i < N; ++i) {
0050         if (flags & lookupTable[i].value)
0051             l.push_back(QByteArray::fromRawData(lookupTable[i].label, strlen(lookupTable[i].label)));
0052         handledFlags |= lookupTable[i].value;
0053     }
0054 
0055     if (flags & ~handledFlags)
0056         l.push_back(QByteArray("unhandled flag 0x") + QByteArray::number(qulonglong(flags & ~handledFlags), 16));
0057 
0058     if (l.isEmpty())
0059         return "none";
0060     return l.join(", ");
0061 }
0062 
0063 #endif