Akonadi

attributefactory.h
1 /*
2  SPDX-FileCopyrightText: 2007-2008 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #pragma once
8 
9 #include "akonadicore_export.h"
10 #include "attribute.h"
11 
12 #include <memory>
13 
14 namespace Akonadi
15 {
16 class AttributeFactoryPrivate;
17 
18 /**
19  * @short Provides the functionality of registering and creating arbitrary
20  * entity attributes.
21  *
22  * This class provides the functionality of registering and creating arbitrary Attributes for Entity
23  * and its subclasses (e.g. Item and Collection).
24  *
25  * @code
26  *
27  * // register the type first
28  * Akonadi::AttributeFactory::registerAttribute<SecrecyAttribute>();
29  *
30  * ...
31  *
32  * // use it anywhere else in the application
33  * SecrecyAttribute *attr = Akonadi::AttributeFactory::createAttribute( "secrecy" );
34  *
35  * @endcode
36  *
37  * @author Volker Krause <[email protected]>
38  */
39 class AKONADICORE_EXPORT AttributeFactory
40 {
41 public:
42  /// @cond PRIVATE
44  /// @endcond
45 
46  /**
47  * Registers a custom attribute of type T.
48  * The same attribute cannot be registered more than once.
49  */
50  template<typename T> inline static void registerAttribute()
51  {
52  static_assert(std::is_default_constructible<T>::value, "An Attribute must be default-constructible.");
53  AttributeFactory::self()->registerAttribute(std::unique_ptr<T>{new T{}});
54  }
55 
56  /**
57  * Creates an entity attribute object of the given type.
58  * If the type has not been registered, creates a DefaultAttribute.
59  *
60  * @param type The attribute type.
61  */
62  static Attribute *createAttribute(const QByteArray &type);
63 
64 protected:
65  /// @cond PRIVATE
66  explicit AttributeFactory();
67 
68 private:
69  Q_DISABLE_COPY(AttributeFactory)
70  static AttributeFactory *self();
71  void registerAttribute(std::unique_ptr<Attribute> attribute);
72 
73  const std::unique_ptr<AttributeFactoryPrivate> d;
74  /// @endcond
75 };
76 
77 } // namespace Akonadi
78 
Provides interface for custom attributes for Entity.
Definition: attribute.h:124
static void registerAttribute()
Registers a custom attribute of type T.
Helper integration between Akonadi and Qt.
Provides the functionality of registering and creating arbitrary entity attributes.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jul 2 2022 06:41:47 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.