Akonadi

agentfactory.h
1/*
2 This file is part of akonadiresources.
3
4 SPDX-FileCopyrightText: 2006 Till Adam <adam@kde.org>
5 SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10#pragma once
11
12#include "agentbase.h"
13#include "akonadiagentbase_export.h"
14
15#include <QObject>
16
17#include <memory>
18
19namespace Akonadi
20{
21class AgentFactoryBasePrivate;
22
23/**
24 * @short A factory base class for in-process agents.
25 *
26 * @see AKONADI_AGENT_FACTORY()
27 * @internal
28 * @since 4.6
29 */
30class AKONADIAGENTBASE_EXPORT AgentFactoryBase : public QObject
31{
32 Q_OBJECT
33
34public:
35 /**
36 * Creates a new agent factory.
37 * Executed in the main thread, performs KDE infrastructure setup.
38 *
39 * @param catalogName The translation catalog of this resource.
40 * @param parent The parent object.
41 */
42 explicit AgentFactoryBase(const char *catalogName, QObject *parent = nullptr);
43
44 ~AgentFactoryBase() override;
45
46public Q_SLOTS:
47 /**
48 * Creates a new agent instance with the given identifier.
49 */
50 virtual QObject *createInstance(const QString &identifier) const = 0;
51
52protected:
53 void createComponentData(const QString &identifier) const;
54
55private:
56 std::unique_ptr<AgentFactoryBasePrivate> const d;
57};
58
59/**
60 * @short A factory for in-process agents.
61 *
62 * @see AKONADI_AGENT_FACTORY()
63 * @internal
64 * @since 4.6
65 */
66template<typename T>
68{
69public:
70 /** reimplemented */
71 explicit AgentFactory(const char *catalogName, QObject *parent = nullptr)
72 : AgentFactoryBase(catalogName, parent)
73 {
74 }
75
76 QObject *createInstance(const QString &identifier) const override
77 {
78 createComponentData(identifier);
79 T *instance = new T(identifier);
80
81 // check if T also inherits AgentBase::Observer and
82 // if it does, automatically register it on itself
83 auto observer = dynamic_cast<Akonadi::AgentBase::Observer *>(instance);
84 if (observer != nullptr) {
85 instance->registerObserver(observer);
86 }
87
88 return instance;
89 }
90};
91
92}
The interface for reacting on monitored or replayed changes.
Definition agentbase.h:179
A factory base class for in-process agents.
virtual QObject * createInstance(const QString &identifier) const =0
Creates a new agent instance with the given identifier.
A factory for in-process agents.
AgentFactory(const char *catalogName, QObject *parent=nullptr)
reimplemented
QObject * createInstance(const QString &identifier) const override
Creates a new agent instance with the given identifier.
Helper integration between Akonadi and Qt.
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.