File indexing completed on 2024-06-16 04:50:11
0001 /* 0002 SPDX-FileCopyrightText: 2007-2008 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #pragma once 0008 0009 #include "akonadicore_export.h" 0010 #include "attribute.h" 0011 0012 #include <memory> 0013 0014 namespace Akonadi 0015 { 0016 class AttributeFactoryPrivate; 0017 0018 /** 0019 * @short Provides the functionality of registering and creating arbitrary 0020 * entity attributes. 0021 * 0022 * This class provides the functionality of registering and creating arbitrary Attributes for Entity 0023 * and its subclasses (e.g. Item and Collection). 0024 * 0025 * @code 0026 * 0027 * // register the type first 0028 * Akonadi::AttributeFactory::registerAttribute<SecrecyAttribute>(); 0029 * 0030 * ... 0031 * 0032 * // use it anywhere else in the application 0033 * SecrecyAttribute *attr = Akonadi::AttributeFactory::createAttribute( "secrecy" ); 0034 * 0035 * @endcode 0036 * 0037 * @author Volker Krause <vkrause@kde.org> 0038 */ 0039 class AKONADICORE_EXPORT AttributeFactory 0040 { 0041 public: 0042 /// @cond PRIVATE 0043 ~AttributeFactory(); 0044 /// @endcond 0045 0046 /** 0047 * Registers a custom attribute of type T. 0048 * The same attribute cannot be registered more than once. 0049 */ 0050 template<typename T> 0051 inline static void registerAttribute() 0052 { 0053 static_assert(std::is_default_constructible<T>::value, "An Attribute must be default-constructible."); 0054 AttributeFactory::self()->registerAttribute(std::unique_ptr<T>{new T{}}); 0055 } 0056 0057 /** 0058 * Creates an entity attribute object of the given type. 0059 * If the type has not been registered, creates a DefaultAttribute. 0060 * 0061 * @param type The attribute type. 0062 */ 0063 static Attribute *createAttribute(const QByteArray &type); 0064 0065 protected: 0066 /// @cond PRIVATE 0067 explicit AttributeFactory(); 0068 0069 private: 0070 Q_DISABLE_COPY(AttributeFactory) 0071 static AttributeFactory *self(); 0072 void registerAttribute(std::unique_ptr<Attribute> attribute); 0073 0074 const std::unique_ptr<AttributeFactoryPrivate> d; 0075 /// @endcond 0076 }; 0077 0078 } // namespace Akonadi