From cdc59d06c8d68d5567c97ba91967c4cd984ac605 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> Date: Wed, 15 Apr 2015 12:08:41 -0400 Subject: [PATCH] utils: Add 'FlagPack<>' template class This will ensure at compile time that all flags are from the same enum. The compiler will hopefully optimize it back to an integer. Refs #70829 --- CMakeLists.txt | 4 +- src/callmodel.cpp | 2 +- src/categorizedbookmarkmodel.cpp | 2 +- src/categorizedhistorymodel.cpp | 2 +- src/certificatemodel.cpp | 6 +- src/collectioninterface.h | 17 +++-- src/collectionmanagerinterface.h | 12 ++-- src/collectionmanagerinterface.hpp | 12 ++-- src/collectionmodel.cpp | 2 +- src/daemoncertificatecollection.cpp | 6 +- src/daemoncertificatecollection.h | 2 +- src/fallbackpersoncollection.cpp | 6 +- src/fallbackpersoncollection.h | 2 +- ...on.cpp => foldercertificatecollection.cpp} | 50 +++++++------- ...ection.h => foldercertificatecollection.h} | 33 ++++++--- src/numbercategory.cpp | 9 ++- src/numbercategory.h | 2 +- src/profilemodel.cpp | 20 +++--- src/transitionalpersonbackend.cpp | 2 +- src/transitionalpersonbackend.h | 2 +- src/typedefs.h | 68 +++++++++++++++++++ 21 files changed, 176 insertions(+), 85 deletions(-) rename src/{fallbacklocalcertificatecollection.cpp => foldercertificatecollection.cpp} (83%) rename src/{fallbacklocalcertificatecollection.h => foldercertificatecollection.h} (60%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 47db02f3..1281812f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -237,7 +237,7 @@ SET( libringclient_LIB_SRCS src/collectioneditor.cpp src/fallbackpersoncollection.cpp src/daemoncertificatecollection.cpp - src/fallbacklocalcertificatecollection.cpp + src/foldercertificatecollection.cpp #Communication src/dbus/configurationmanager.cpp @@ -330,7 +330,7 @@ SET( libringclient_LIB_HDRS src/collectioneditor.hpp src/fallbackpersoncollection.h src/daemoncertificatecollection.h - src/fallbacklocalcertificatecollection.h + src/foldercertificatecollection.h src/securityflaw.h src/collectioncreationinterface.h src/collectionconfigurationinterface.h diff --git a/src/callmodel.cpp b/src/callmodel.cpp index 402c5e42..1fe531ba 100644 --- a/src/callmodel.cpp +++ b/src/callmodel.cpp @@ -1051,7 +1051,7 @@ void CallModelPrivate::slotCallStateChanged(const QString& callID, const QString //Add to history if (call->lifeCycleState() == Call::LifeCycleState::FINISHED) { if (!call->collection()) { - foreach (CollectionInterface* backend, CategorizedHistoryModel::instance()->collections(CollectionInterface::ADD)) { + foreach (CollectionInterface* backend, CategorizedHistoryModel::instance()->collections(CollectionInterface::SupportedFeatures::ADD)) { if (backend->editor<Call>()->addNew(call)) call->setCollection(backend); } diff --git a/src/categorizedbookmarkmodel.cpp b/src/categorizedbookmarkmodel.cpp index 66df2e0a..39abc18d 100644 --- a/src/categorizedbookmarkmodel.cpp +++ b/src/categorizedbookmarkmodel.cpp @@ -582,7 +582,7 @@ bool CategorizedBookmarkModel::removeItemCallback(const ContactMethod* item) bool CategorizedBookmarkModel::clearAllCollections() const { foreach (CollectionInterface* backend, collections()) { - if (backend->supportedFeatures() & CollectionInterface::ADD) { + if (backend->supportedFeatures() & CollectionInterface::SupportedFeatures::ADD) { backend->clear(); } } diff --git a/src/categorizedhistorymodel.cpp b/src/categorizedhistorymodel.cpp index 12a4a967..8b60ace4 100644 --- a/src/categorizedhistorymodel.cpp +++ b/src/categorizedhistorymodel.cpp @@ -617,7 +617,7 @@ void CategorizedHistoryModel::collectionAddedCallback(CollectionInterface* backe bool CategorizedHistoryModel::clearAllCollections() const { foreach (CollectionInterface* backend, collections()) { //TODO use the filter API - if (backend->supportedFeatures() & CollectionInterface::CLEAR) { + if (backend->supportedFeatures() & CollectionInterface::SupportedFeatures::CLEAR) { backend->clear(); } } diff --git a/src/certificatemodel.cpp b/src/certificatemodel.cpp index e2d842cc..4a37eb68 100644 --- a/src/certificatemodel.cpp +++ b/src/certificatemodel.cpp @@ -32,7 +32,7 @@ //Ring #include "certificate.h" #include "account.h" -#include "fallbacklocalcertificatecollection.h" +#include "foldercertificatecollection.h" #include "private/matrixutils.h" enum class DetailType : uchar @@ -112,7 +112,7 @@ private: }; //TODO remove -static FallbackLocalCertificateCollection* m_pFallbackCollection = nullptr; +static FolderCertificateCollection* m_pFallbackCollection = nullptr; CertificateModel* CertificateModelPrivate::m_spInstance = nullptr; @@ -141,7 +141,7 @@ CertificateModel::CertificateModel(QObject* parent) : QAbstractItemModel(parent) { setObjectName("CertificateModel"); //TODO replace with something else - m_pFallbackCollection = addCollection<FallbackLocalCertificateCollection>(); + m_pFallbackCollection = addCollection<FolderCertificateCollection>(); m_pFallbackCollection->load(); } diff --git a/src/collectioninterface.h b/src/collectioninterface.h index 345e94a9..11d4d0a7 100644 --- a/src/collectioninterface.h +++ b/src/collectioninterface.h @@ -61,7 +61,7 @@ public: * are implemented anyway, each backend should list what is officially supposed to work and what is * not. */ - enum SupportedFeatures { + enum class SupportedFeatures { NONE = 0x0 , LOAD = 0x1 << 0, /*!< Load this backend, DO NOT load anything before "load" is called */ SAVE = 0x1 << 1, /*!< Save an item */ @@ -83,11 +83,6 @@ public: typedef QByteArray Element; - //Constructor - template<typename T> - explicit CollectionInterface(CollectionEditor<T>* editor, CollectionInterface* parent = nullptr); - virtual ~CollectionInterface(); - //Generic information getters /** @@ -132,7 +127,7 @@ public: * * @see SupportedFeatures */ - virtual SupportedFeatures supportedFeatures() const = 0; + virtual FlagPack<SupportedFeatures> supportedFeatures() const = 0; //Management methods @@ -260,6 +255,12 @@ public: CollectionConfigurationInterface* configurator() const; protected: + + //Constructor + template<typename T> + explicit CollectionInterface(CollectionEditor<T>* editor, CollectionInterface* parent = nullptr); + virtual ~CollectionInterface(); + void setConfigurator(std::function<CollectionConfigurationInterface*()> getter); void addChildren(CollectionInterface* c); @@ -275,6 +276,8 @@ private: CollectionInterfacePrivate* d_ptr; }; +DECLARE_ENUM_FLAGS(CollectionInterface::SupportedFeatures) + #include <collectioninterface.hpp> #endif diff --git a/src/collectionmanagerinterface.h b/src/collectionmanagerinterface.h index 8920c56f..f212b244 100644 --- a/src/collectionmanagerinterface.h +++ b/src/collectionmanagerinterface.h @@ -48,8 +48,8 @@ class CollectionConfigurationInterface; */ class LIB_EXPORT CollectionManagerInterfaceBase { public: - virtual bool hasEnabledCollections (CollectionInterface::SupportedFeatures features = CollectionInterface::SupportedFeatures::NONE) const = 0; - virtual bool hasCollections (CollectionInterface::SupportedFeatures features = CollectionInterface::SupportedFeatures::NONE) const = 0; + virtual bool hasEnabledCollections (FlagPack<CollectionInterface::SupportedFeatures> features = CollectionInterface::SupportedFeatures::NONE) const = 0; + virtual bool hasCollections (FlagPack<CollectionInterface::SupportedFeatures> features = CollectionInterface::SupportedFeatures::NONE) const = 0; ///Enable / disable a collection virtual bool enableCollection( CollectionInterface* collection, bool enabled) = 0; @@ -140,12 +140,12 @@ public: /// Do this manager have active collections - virtual bool hasEnabledCollections (CollectionInterface::SupportedFeatures features = CollectionInterface::SupportedFeatures::NONE) const final; - virtual bool hasCollections (CollectionInterface::SupportedFeatures features = CollectionInterface::SupportedFeatures::NONE) const final; + virtual bool hasEnabledCollections (FlagPack<CollectionInterface::SupportedFeatures> features = CollectionInterface::SupportedFeatures::NONE) const final; + virtual bool hasCollections (FlagPack<CollectionInterface::SupportedFeatures> features = CollectionInterface::SupportedFeatures::NONE) const final; /// List all Collections - virtual const QVector< CollectionInterface* > collections (CollectionInterface::SupportedFeatures features = CollectionInterface::SupportedFeatures::NONE) const final; - virtual const QVector< CollectionInterface* > enabledCollections(CollectionInterface::SupportedFeatures features = CollectionInterface::SupportedFeatures::NONE) const final; + virtual const QVector< CollectionInterface* > collections (FlagPack<CollectionInterface::SupportedFeatures> features = CollectionInterface::SupportedFeatures::NONE) const final; + virtual const QVector< CollectionInterface* > enabledCollections(FlagPack<CollectionInterface::SupportedFeatures> features = CollectionInterface::SupportedFeatures::NONE) const final; ///Enable / disable a collection virtual bool enableCollection( CollectionInterface* collection, bool enabled) final; diff --git a/src/collectionmanagerinterface.hpp b/src/collectionmanagerinterface.hpp index a17da5dd..fa0d2851 100644 --- a/src/collectionmanagerinterface.hpp +++ b/src/collectionmanagerinterface.hpp @@ -33,7 +33,7 @@ public: CollectionManagerInterface<T>* i_ptr; CollectionMediator<T>* itemMediator() const; - inline const QVector< CollectionInterface* > filterCollections(QVector< CollectionInterface* > in, CollectionInterface::SupportedFeatures features) const; + inline const QVector< CollectionInterface* > filterCollections(QVector< CollectionInterface* > in, FlagPack<CollectionInterface::SupportedFeatures> features) const; }; template<class T> @@ -113,7 +113,7 @@ CollectionManagerInterface<T>::CollectionManagerInterface(QAbstractItemModel* se } template<class T> -const QVector< CollectionInterface* > CollectionManagerInterfacePrivate<T>::filterCollections(QVector< CollectionInterface* > in, CollectionInterface::SupportedFeatures features) const +const QVector< CollectionInterface* > CollectionManagerInterfacePrivate<T>::filterCollections(QVector< CollectionInterface* > in, FlagPack<CollectionInterface::SupportedFeatures> features) const { QVector< CollectionInterface* > out; for (CollectionInterface* col : in) { @@ -129,7 +129,7 @@ const QVector< CollectionInterface* > CollectionManagerInterfacePrivate<T>::filt * @note Please note that this method complexity is O(N) when "features" is set */ template<class T> -const QVector< CollectionInterface* > CollectionManagerInterface<T>::collections(CollectionInterface::SupportedFeatures features) const +const QVector< CollectionInterface* > CollectionManagerInterface<T>::collections(FlagPack<CollectionInterface::SupportedFeatures> features) const { if (features != CollectionInterface::SupportedFeatures::NONE) return d_ptr->filterCollections(d_ptr->m_lCollections, features); @@ -143,7 +143,7 @@ const QVector< CollectionInterface* > CollectionManagerInterface<T>::collections * @note Please note that this method complexity is O(N) when "features" is set */ template<class T> -const QVector< CollectionInterface* > CollectionManagerInterface<T>::enabledCollections(CollectionInterface::SupportedFeatures features) const +const QVector< CollectionInterface* > CollectionManagerInterface<T>::enabledCollections(FlagPack<CollectionInterface::SupportedFeatures> features) const { if (features != CollectionInterface::SupportedFeatures::NONE) return d_ptr->filterCollections(d_ptr->m_lEnabledCollections, features); @@ -157,7 +157,7 @@ const QVector< CollectionInterface* > CollectionManagerInterface<T>::enabledColl * @note Please note that this method complexity is O(N) when "features" is set */ template<class T> -bool CollectionManagerInterface<T>::hasEnabledCollections(CollectionInterface::SupportedFeatures features) const +bool CollectionManagerInterface<T>::hasEnabledCollections(FlagPack<CollectionInterface::SupportedFeatures> features) const { if (features != CollectionInterface::SupportedFeatures::NONE) return d_ptr->filterCollections(d_ptr->m_lEnabledCollections, features).size(); @@ -171,7 +171,7 @@ bool CollectionManagerInterface<T>::hasEnabledCollections(CollectionInterface::S * @note Please note that this method complexity is O(N) when "features" is set */ template<class T> -bool CollectionManagerInterface<T>::hasCollections(CollectionInterface::SupportedFeatures features) const +bool CollectionManagerInterface<T>::hasCollections(FlagPack<CollectionInterface::SupportedFeatures> features) const { if (features != CollectionInterface::SupportedFeatures::NONE) return d_ptr->filterCollections(d_ptr->m_lCollections, features).size(); diff --git a/src/collectionmodel.cpp b/src/collectionmodel.cpp index 0898b576..8162e532 100644 --- a/src/collectionmodel.cpp +++ b/src/collectionmodel.cpp @@ -128,7 +128,7 @@ QVariant CollectionModel::data (const QModelIndex& idx, int role) const //Retro-map to the SupportedFeatures //WARNING if SupportedFeatures change, this need to be updated if (role > Qt::UserRole && role <= Qt::UserRole+14) { - return (bool) (role == Qt::UserRole+1 ? true : item->collection->supportedFeatures() & (0x01 << (role - Qt::UserRole - 2))); + return (bool) (role == Qt::UserRole+1 ? true : bool(item->collection->supportedFeatures() & ((CollectionInterface::SupportedFeatures)(0x01 << (role - Qt::UserRole - 2))))); } } else { diff --git a/src/daemoncertificatecollection.cpp b/src/daemoncertificatecollection.cpp index cd92899e..d9febb46 100644 --- a/src/daemoncertificatecollection.cpp +++ b/src/daemoncertificatecollection.cpp @@ -94,15 +94,15 @@ QByteArray DaemonCertificateCollection::id() const return "DaemonCertificateCollection"; } -CollectionInterface::SupportedFeatures DaemonCertificateCollection::supportedFeatures() const +FlagPack<CollectionInterface::SupportedFeatures> DaemonCertificateCollection::supportedFeatures() const { - return (CollectionInterface::SupportedFeatures) ( + return CollectionInterface::SupportedFeatures::NONE | CollectionInterface::SupportedFeatures::LOAD | CollectionInterface::SupportedFeatures::CLEAR | CollectionInterface::SupportedFeatures::REMOVE | CollectionInterface::SupportedFeatures::LISTABLE | - CollectionInterface::SupportedFeatures::ADD ); + CollectionInterface::SupportedFeatures::ADD ; } diff --git a/src/daemoncertificatecollection.h b/src/daemoncertificatecollection.h index b0f7dbbb..299aeec6 100644 --- a/src/daemoncertificatecollection.h +++ b/src/daemoncertificatecollection.h @@ -48,7 +48,7 @@ public: virtual bool isEnabled() const override; virtual QByteArray id () const override; - virtual SupportedFeatures supportedFeatures() const override; + virtual FlagPack<SupportedFeatures> supportedFeatures() const override; private: DaemonCertificateCollectionPrivate* d_ptr; diff --git a/src/fallbackpersoncollection.cpp b/src/fallbackpersoncollection.cpp index a94c44c2..1df40ac9 100644 --- a/src/fallbackpersoncollection.cpp +++ b/src/fallbackpersoncollection.cpp @@ -211,15 +211,15 @@ bool FallbackPersonCollection::reload() return false; } -CollectionInterface::SupportedFeatures FallbackPersonCollection::supportedFeatures() const +FlagPack<CollectionInterface::SupportedFeatures> FallbackPersonCollection::supportedFeatures() const { - return (CollectionInterface::SupportedFeatures) ( + return CollectionInterface::SupportedFeatures::NONE | CollectionInterface::SupportedFeatures::LOAD | CollectionInterface::SupportedFeatures::CLEAR | CollectionInterface::SupportedFeatures::MANAGEABLE | CollectionInterface::SupportedFeatures::REMOVE | - CollectionInterface::SupportedFeatures::ADD ); + CollectionInterface::SupportedFeatures::ADD ; } bool FallbackPersonCollection::clear() diff --git a/src/fallbackpersoncollection.h b/src/fallbackpersoncollection.h index 956e9e80..1a2570ac 100644 --- a/src/fallbackpersoncollection.h +++ b/src/fallbackpersoncollection.h @@ -50,7 +50,7 @@ public: virtual bool isEnabled() const override; virtual QByteArray id () const override; - virtual SupportedFeatures supportedFeatures() const override; + virtual FlagPack<SupportedFeatures> supportedFeatures() const override; private: FallbackPersonCollectionPrivate* d_ptr; diff --git a/src/fallbacklocalcertificatecollection.cpp b/src/foldercertificatecollection.cpp similarity index 83% rename from src/fallbacklocalcertificatecollection.cpp rename to src/foldercertificatecollection.cpp index c1d5df8d..fa73d175 100644 --- a/src/fallbacklocalcertificatecollection.cpp +++ b/src/foldercertificatecollection.cpp @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License * * along with this program. If not, see <http://www.gnu.org/licenses/>. * ***************************************************************************/ -#include "fallbacklocalcertificatecollection.h" +#include "foldercertificatecollection.h" //Qt #include <QtCore/QThread> @@ -71,13 +71,13 @@ class BackgroundLoader : public QThread { Q_OBJECT public: - BackgroundLoader(FallbackLocalCertificateCollection* parent); + BackgroundLoader(FolderCertificateCollection* parent); //Attributes QList<QByteArray> m_lIdList ; QMutex m_LoaderMutex; QList<QByteArray> m_lQueue ; - FallbackLocalCertificateCollection* m_pFallbackCollection; + FolderCertificateCollection* m_pFallbackCollection; //Helpers QByteArray loadCertificate(const QByteArray& id); @@ -89,7 +89,7 @@ Q_SIGNALS: void listLoaded(const QList<QByteArray>& list); }; -class FallbackLocalCertificateCollectionPrivate +class FolderCertificateCollectionPrivate { public: @@ -97,13 +97,17 @@ public: QList<CollectionInterface::Element> getCertificateList(); }; -FallbackLocalCertificateCollection::FallbackLocalCertificateCollection(CollectionMediator<Certificate>* mediator, const QString& path) : -CollectionInterface(new FallbackLocalCertificateEditor(mediator,path)),d_ptr(new FallbackLocalCertificateCollectionPrivate()) +FolderCertificateCollection::FolderCertificateCollection(CollectionMediator<Certificate>* mediator, + const QString& path , + const FlagPack<Options>& options , + const QString& name + ) : +CollectionInterface(new FallbackLocalCertificateEditor(mediator,path)),d_ptr(new FolderCertificateCollectionPrivate()) { } -FallbackLocalCertificateCollection::~FallbackLocalCertificateCollection() +FolderCertificateCollection::~FolderCertificateCollection() { delete d_ptr; } @@ -120,7 +124,7 @@ QByteArray BackgroundLoader::loadCertificate(const QByteArray& id) return file.readAll(); } -bool FallbackLocalCertificateCollection::load() +bool FolderCertificateCollection::load() { //Load the stored certificates BackgroundLoader* loader = new BackgroundLoader(this); @@ -136,55 +140,55 @@ bool FallbackLocalCertificateCollection::load() return false; } -bool FallbackLocalCertificateCollection::reload() +bool FolderCertificateCollection::reload() { return false; } -QList<CollectionInterface::Element> FallbackLocalCertificateCollection::listId() const +QList<CollectionInterface::Element> FolderCertificateCollection::listId() const { return d_ptr->getCertificateList(); } -bool FallbackLocalCertificateCollection::clear() +bool FolderCertificateCollection::clear() { return false; } -QString FallbackLocalCertificateCollection::name() const +QString FolderCertificateCollection::name() const { return QObject::tr("Local certificate store"); } -QString FallbackLocalCertificateCollection::category() const +QString FolderCertificateCollection::category() const { return QObject::tr("Certificate"); } -QVariant FallbackLocalCertificateCollection::icon() const +QVariant FolderCertificateCollection::icon() const { return QVariant(); } -bool FallbackLocalCertificateCollection::isEnabled() const +bool FolderCertificateCollection::isEnabled() const { return true; } -QByteArray FallbackLocalCertificateCollection::id() const +QByteArray FolderCertificateCollection::id() const { - return "FallbackLocalCertificateCollection"; + return "FolderCertificateCollection"; } -CollectionInterface::SupportedFeatures FallbackLocalCertificateCollection::supportedFeatures() const +FlagPack<CollectionInterface::SupportedFeatures> FolderCertificateCollection::supportedFeatures() const { - return (CollectionInterface::SupportedFeatures) ( + return CollectionInterface::SupportedFeatures::NONE | CollectionInterface::SupportedFeatures::LISTABLE | CollectionInterface::SupportedFeatures::LOAD | CollectionInterface::SupportedFeatures::CLEAR | CollectionInterface::SupportedFeatures::REMOVE | - CollectionInterface::SupportedFeatures::ADD ); + CollectionInterface::SupportedFeatures::ADD ; } @@ -264,13 +268,13 @@ QVector<Certificate*> FallbackLocalCertificateEditor::items() const * * ******************************************************************************/ -BackgroundLoader::BackgroundLoader(FallbackLocalCertificateCollection* parent) : QThread(nullptr), +BackgroundLoader::BackgroundLoader(FolderCertificateCollection* parent) : QThread(nullptr), m_pFallbackCollection(parent) { } -QList<CollectionInterface::Element> FallbackLocalCertificateCollectionPrivate::getCertificateList() +QList<CollectionInterface::Element> FolderCertificateCollectionPrivate::getCertificateList() { // QMutexLocker(&this->m_Mutex); QDir dir(QStandardPaths::writableLocation(QStandardPaths::DataLocation)+"/certs/"); @@ -299,4 +303,4 @@ void BackgroundLoader::run() QThread::exit(0); } -#include "fallbacklocalcertificatecollection.moc" +#include "foldercertificatecollection.moc" diff --git a/src/fallbacklocalcertificatecollection.h b/src/foldercertificatecollection.h similarity index 60% rename from src/fallbacklocalcertificatecollection.h rename to src/foldercertificatecollection.h index 6cb32a21..b001a571 100644 --- a/src/fallbacklocalcertificatecollection.h +++ b/src/foldercertificatecollection.h @@ -15,11 +15,13 @@ * You should have received a copy of the GNU General Public License * * along with this program. If not, see <http://www.gnu.org/licenses/>. * ***************************************************************************/ +#ifndef FOLDERCERTIFICATECOLLECTION_H +#define FOLDERCERTIFICATECOLLECTION_H #include <collectioninterface.h> #include <typedefs.h> -class FallbackLocalCertificateCollectionPrivate; +class FolderCertificateCollectionPrivate; class Certificate; template<typename T> class CollectionMediator; @@ -29,11 +31,23 @@ template<typename T> class CollectionMediator; * locally by LibRingClient. If the platform have a certificate hosting facility, * it is strongly encourages to use it rather than this. */ -class LIB_EXPORT FallbackLocalCertificateCollection : public CollectionInterface +class LIB_EXPORT FolderCertificateCollection : public CollectionInterface { public: - explicit FallbackLocalCertificateCollection(CollectionMediator<Certificate>* mediator, const QString& path = QString()); - virtual ~FallbackLocalCertificateCollection(); + ///@enum Options load and behavior parameters for a FolderCertificateCollection + enum class Options { + READ_WRITE = 0 << 0, /*!< Try to add certificates to that store (default) */ + READ_ONLY = 1 << 0, /*!< Do not try to add certificate to that store */ + RECURSIVE = 1 << 1, /*!< Read all sub-folders recursively */ + TOP_LEVEL = 1 << 2, /*!< Consider those certificates as top-level entities */ + }; + + explicit FolderCertificateCollection(CollectionMediator<Certificate>* mediator, + const QString& path = QString(), + const FlagPack<Options>& options = Options::READ_WRITE, + const QString& name = QString() + ); + virtual ~FolderCertificateCollection(); virtual bool load () override; virtual bool reload() override; @@ -46,10 +60,13 @@ public: virtual QByteArray id () const override; virtual QList<Element> listId () const override; - virtual SupportedFeatures supportedFeatures() const override; + virtual FlagPack<SupportedFeatures> supportedFeatures() const override; private: - FallbackLocalCertificateCollectionPrivate* d_ptr; - Q_DECLARE_PRIVATE(FallbackLocalCertificateCollection) + FolderCertificateCollectionPrivate* d_ptr; + Q_DECLARE_PRIVATE(FolderCertificateCollection) }; -Q_DECLARE_METATYPE(FallbackLocalCertificateCollection*) \ No newline at end of file +Q_DECLARE_METATYPE(FolderCertificateCollection*) +DECLARE_ENUM_FLAGS(FolderCertificateCollection::Options) + +#endif diff --git a/src/numbercategory.cpp b/src/numbercategory.cpp index 75629364..83171cc0 100644 --- a/src/numbercategory.cpp +++ b/src/numbercategory.cpp @@ -96,12 +96,11 @@ int NumberCategory::key() const return d_ptr->m_Key; } -CollectionInterface::SupportedFeatures NumberCategory::supportedFeatures() const +FlagPack<CollectionInterface::SupportedFeatures> NumberCategory::supportedFeatures() const { - return (CollectionInterface::SupportedFeatures) ( - CollectionInterface::SupportedFeatures::NONE | - CollectionInterface::SupportedFeatures::MANAGEABLE | - CollectionInterface::SupportedFeatures::LOAD ); + return CollectionInterface::SupportedFeatures::NONE | + CollectionInterface::SupportedFeatures::MANAGEABLE | + CollectionInterface::SupportedFeatures::LOAD ; } void NumberCategory::setIcon(const QVariant& pixmap) diff --git a/src/numbercategory.h b/src/numbercategory.h index 33ff3021..ec82cd41 100644 --- a/src/numbercategory.h +++ b/src/numbercategory.h @@ -50,7 +50,7 @@ public: QVariant icon(bool isTracked, bool isPresent = false) const; - virtual SupportedFeatures supportedFeatures() const override; + virtual FlagPack<SupportedFeatures> supportedFeatures() const override; virtual bool load() override; //Setter diff --git a/src/profilemodel.cpp b/src/profilemodel.cpp index 7f86804b..8a3fbe21 100644 --- a/src/profilemodel.cpp +++ b/src/profilemodel.cpp @@ -83,7 +83,7 @@ public: virtual QByteArray id ( ) const override; virtual bool load ( ) override; virtual bool reload ( ) override; - SupportedFeatures supportedFeatures( ) const override; + FlagPack<SupportedFeatures> supportedFeatures( ) const override; //Attributes bool m_needSaving; @@ -395,16 +395,16 @@ bool ProfileContentBackend::saveAll() return true; } -ProfileContentBackend::SupportedFeatures ProfileContentBackend::supportedFeatures() const +FlagPack<ProfileContentBackend::SupportedFeatures> ProfileContentBackend::supportedFeatures() const { - return (ProfileContentBackend::SupportedFeatures)(SupportedFeatures::NONE - | SupportedFeatures::LOAD //= 0x1 << 0, /* Load this backend, DO NOT load anything before "load" is called */ - | SupportedFeatures::EDIT //= 0x1 << 2, /* Edit, but **DOT NOT**, save an item) */ - | SupportedFeatures::ADD //= 0x1 << 4, /* Add (and save) a new item to the backend */ - | SupportedFeatures::SAVE_ALL //= 0x1 << 5, /* Save all items at once, this may or may not be faster than "add" */ - | SupportedFeatures::REMOVE //= 0x1 << 7, /* Remove a single item */ - | SupportedFeatures::ENABLEABLE //= 0x1 << 10, /*Can be enabled, I know, it is not a word, but Java use it too */ - | SupportedFeatures::MANAGEABLE); //= 0x1 << 12, /* Can be managed the config GUI */ + return SupportedFeatures::NONE + | SupportedFeatures::LOAD //= 0x1 << 0, /* Load this backend, DO NOT load anything before "load" is called */ + | SupportedFeatures::EDIT //= 0x1 << 2, /* Edit, but **DOT NOT**, save an item) */ + | SupportedFeatures::ADD //= 0x1 << 4, /* Add (and save) a new item to the backend */ + | SupportedFeatures::SAVE_ALL //= 0x1 << 5, /* Save all items at once, this may or may not be faster than "add" */ + | SupportedFeatures::REMOVE //= 0x1 << 7, /* Remove a single item */ + | SupportedFeatures::ENABLEABLE //= 0x1 << 10, /*Can be enabled, I know, it is not a word, but Java use it too */ + | SupportedFeatures::MANAGEABLE; //= 0x1 << 12, /* Can be managed the config GUI */ //TODO ^^ Remove that one once debugging is done } diff --git a/src/transitionalpersonbackend.cpp b/src/transitionalpersonbackend.cpp index 32dbd5d1..9200b95e 100644 --- a/src/transitionalpersonbackend.cpp +++ b/src/transitionalpersonbackend.cpp @@ -138,7 +138,7 @@ bool TransitionalPersonBackend::isEnabled() const return false; } -CollectionInterface::SupportedFeatures TransitionalPersonBackend::supportedFeatures() const +FlagPack<CollectionInterface::SupportedFeatures> TransitionalPersonBackend::supportedFeatures() const { return CollectionInterface::SupportedFeatures::NONE; } diff --git a/src/transitionalpersonbackend.h b/src/transitionalpersonbackend.h index 3f854011..4a7301f3 100644 --- a/src/transitionalpersonbackend.h +++ b/src/transitionalpersonbackend.h @@ -55,7 +55,7 @@ public: virtual QString name () const override; virtual QString category () const override; virtual QVariant icon () const override; - virtual SupportedFeatures supportedFeatures() const override; + virtual FlagPack<SupportedFeatures> supportedFeatures() const override; //Mutators virtual bool load ( ) override; diff --git a/src/typedefs.h b/src/typedefs.h index ba28af33..09ca00d5 100644 --- a/src/typedefs.h +++ b/src/typedefs.h @@ -61,5 +61,73 @@ template<typename A> constexpr int enum_class_size() { #define IGNORE_NULL(content) content #endif //ENABLE_IGNORE_NULL +/** + * Create a safe pack of flags from an enum class. + * + * This class exist to ensure all sources come from the same enum and that it is + * never accidentally accidentally into an integer. + * + * This assume that the enum has been setup as flags. + */ +template<class T> +class LIB_EXPORT FlagPack +{ +public: + FlagPack(const T& base) : m_Flags(static_cast<int>(base)) {} + + //Operator + FlagPack<T>& operator|(const T& other) { + m_Flags |= static_cast<int>(other); + return *this; + } + + FlagPack<T>& operator|(const FlagPack<T>& other) { + m_Flags |= other.m_Flags; + return *this; + } + + FlagPack<T> operator&(const T& other) { + return FlagPack<T>(m_Flags & static_cast<int>(other)); + } + + FlagPack<T> operator&(const FlagPack<T>& other) { + return FlagPack<T>(m_Flags & other.m_Flags); + } + + bool operator!=(const T& other) { + return m_Flags != static_cast<int>(other); + } + + bool operator==(const T& other) { + return m_Flags == static_cast<int>(other); + } + + bool operator==(const FlagPack<T>& other) { + return m_Flags == other.m_Flags; + } + + bool operator!() { + return !m_Flags; + } + + operator bool() { + return m_Flags != 0; + } + + int value() const { + return m_Flags; + } + +private: + FlagPack(int base) : m_Flags(base) {} + int m_Flags; +}; + +#define DECLARE_ENUM_FLAGS(T)\ +static FlagPack<T> operator|(const T& first, const T& second) { \ + FlagPack<T> p (first); \ + return p | second; \ +} + #endif //TYPEDEFS_H -- GitLab