File indexing completed on 2024-05-26 08:35:02

0001 /*
0002     SPDX-FileCopyrightText: 2012 Sebastian Sauer <sebastian.sauer@kdab.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #ifndef QACCESSIBILITYCLIENT_CACHESTRATEGY_P_H
0008 #define QACCESSIBILITYCLIENT_CACHESTRATEGY_P_H
0009 
0010 #include "accessibleobject.h"
0011 
0012 #include <QPair>
0013 
0014 namespace QAccessibleClient {
0015 
0016 class ObjectCache
0017 {
0018 public:
0019     virtual QStringList ids() const = 0;
0020     virtual QSharedPointer<AccessibleObjectPrivate> get(const QString &id) const = 0;
0021     virtual void add(const QString &id, const QSharedPointer<AccessibleObjectPrivate> &objectPrivate) = 0;
0022     virtual bool remove(const QString &id) = 0;
0023     virtual void clear() = 0;
0024     virtual AccessibleObject::Interfaces interfaces(const AccessibleObject &object) = 0;
0025     virtual void setInterfaces(const AccessibleObject &object, AccessibleObject::Interfaces interfaces) = 0;
0026     virtual quint64 state(const AccessibleObject &object) = 0;
0027     virtual void setState(const AccessibleObject &object, quint64 state) = 0;
0028     virtual void cleanState(const AccessibleObject &object) = 0;
0029     virtual ~ObjectCache() {}
0030     static const quint64 StateNotFound = ~0;
0031 };
0032 
0033 class CacheWeakStrategy : public ObjectCache
0034 {
0035 public:
0036     QStringList ids() const override
0037     {
0038         return accessibleObjectsHash.keys();
0039     }
0040     QSharedPointer<AccessibleObjectPrivate> get(const QString &id) const override
0041     {
0042         return accessibleObjectsHash[id].first;
0043     }
0044     void add(const QString &id, const QSharedPointer<AccessibleObjectPrivate> &objectPrivate) override
0045     {
0046         accessibleObjectsHash[id] = QPair<QWeakPointer<AccessibleObjectPrivate>, AccessibleObjectPrivate*>(objectPrivate, objectPrivate.data());
0047     }
0048     bool remove(const QString &id) override
0049     {
0050         QPair<QWeakPointer<AccessibleObjectPrivate>, AccessibleObjectPrivate*> data = accessibleObjectsHash.take(id);
0051         return (interfaceHash.remove(data.second) >= 1) || (stateHash.remove(data.second) >= 1);
0052     }
0053     void clear() override
0054     {
0055         accessibleObjectsHash.clear();
0056         stateHash.clear();
0057         interfaceHash.clear();
0058     }
0059     AccessibleObject::Interfaces interfaces(const AccessibleObject &object) override
0060     {
0061         if (!interfaceHash.contains(object.d.data()))
0062             return AccessibleObject::InvalidInterface;
0063         return interfaceHash.value(object.d.data());
0064     }
0065     void setInterfaces(const AccessibleObject &object, AccessibleObject::Interfaces interfaces) override
0066     {
0067         interfaceHash.insert(object.d.data(), interfaces);
0068     }
0069     quint64 state(const AccessibleObject &object) override
0070     {
0071         if (!stateHash.contains(object.d.data()))
0072             return ObjectCache::StateNotFound;
0073 
0074         return stateHash.value(object.d.data());
0075     }
0076     void setState(const AccessibleObject &object, quint64 state) override
0077     {
0078         stateHash[object.d.data()] = state;
0079     }
0080     void cleanState(const AccessibleObject &object) override
0081     {
0082         stateHash.remove(object.d.data());
0083     }
0084 
0085 private:
0086     QHash<QString, QPair<QWeakPointer<AccessibleObjectPrivate>, AccessibleObjectPrivate*> > accessibleObjectsHash;
0087     QHash<AccessibleObjectPrivate*, AccessibleObject::Interfaces> interfaceHash;
0088     QHash<AccessibleObjectPrivate*, qint64> stateHash;
0089 };
0090 
0091 }
0092 
0093 #endif
0094