File indexing completed on 2025-01-12 03:46:26

0001 /*
0002  *  Portions Copyright (C) 2009 by Davide Bettio <davide.bettio@kdemail.net>
0003  *  Copyright (C) 2010 Parker Coates <coates@kde.org>
0004  *
0005  *  This program is free software; you can redistribute it and/or
0006  *  modify it under the terms of the GNU General Public License as
0007  *  published by the Free Software Foundation; either version 2 of
0008  *  the License, or (at your option) any later version.
0009  *
0010  *  This program is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  *  GNU General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU General Public License
0016  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017  *
0018  */
0019 
0020 #ifndef COMMON_P_H
0021 #define COMMON_P_H
0022 
0023 // KF
0024 #include <KSharedDataCache>
0025 // Qt
0026 #include <QDataStream>
0027 #include <QIODevice>
0028 class QString;
0029 
0030 template<class T>
0031 bool cacheFind(KSharedDataCache *cache, const QString &key, T *result)
0032 {
0033     QByteArray buffer;
0034     if (cache->find(key, &buffer)) {
0035         QDataStream stream(&buffer, QIODevice::ReadOnly);
0036         stream >> *result;
0037         return true;
0038     }
0039     return false;
0040 }
0041 
0042 template<class T>
0043 bool cacheInsert(KSharedDataCache *cache, const QString &key, const T &value)
0044 {
0045     QByteArray buffer;
0046     QDataStream stream(&buffer, QIODevice::WriteOnly);
0047     stream << value;
0048     return cache->insert(key, buffer);
0049 }
0050 
0051 #endif