diff options
Diffstat (limited to 'common/resourcecontext.h')
-rw-r--r-- | common/resourcecontext.h | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/common/resourcecontext.h b/common/resourcecontext.h new file mode 100644 index 0000000..6058ac7 --- /dev/null +++ b/common/resourcecontext.h | |||
@@ -0,0 +1,77 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2016 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | * | ||
4 | * This library is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||
8 | * later version accepted by the membership of KDE e.V. (or its | ||
9 | * successor approved by the membership of KDE e.V.), which shall | ||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||
11 | * | ||
12 | * This library is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | ||
20 | #pragma once | ||
21 | |||
22 | #include "domaintypeadaptorfactoryinterface.h" | ||
23 | #include "applicationdomaintype.h" | ||
24 | #include "resourceaccess.h" | ||
25 | #include <QByteArray> | ||
26 | #include <QMap> | ||
27 | |||
28 | namespace Sink { | ||
29 | |||
30 | /* | ||
31 | * A context object that can be passed around so each part of the system knows in what context it works. | ||
32 | * | ||
33 | * This is necessary because we can't rely on a singleton or thread-local storage since multiple resources can be accessed from the same thread/process. | ||
34 | */ | ||
35 | struct ResourceContext { | ||
36 | const QByteArray resourceInstanceIdentifier; | ||
37 | const QByteArray resourceType; | ||
38 | QMap<QByteArray, DomainTypeAdaptorFactoryInterface::Ptr> adaptorFactories; | ||
39 | //TODO prehaps use a weak pointer to not mess up lifetime management | ||
40 | ResourceAccessInterface::Ptr mResourceAccess; | ||
41 | |||
42 | |||
43 | ResourceContext(const QByteArray &identifier, const QByteArray &resourceType_, const QMap<QByteArray, DomainTypeAdaptorFactoryInterface::Ptr> &factories = QMap<QByteArray, DomainTypeAdaptorFactoryInterface::Ptr>()) | ||
44 | : resourceInstanceIdentifier(identifier), | ||
45 | resourceType(resourceType_), | ||
46 | adaptorFactories(factories) | ||
47 | { | ||
48 | } | ||
49 | |||
50 | QByteArray instanceId() const | ||
51 | { | ||
52 | return resourceInstanceIdentifier; | ||
53 | } | ||
54 | |||
55 | DomainTypeAdaptorFactoryInterface &adaptorFactory(const QByteArray &type) const | ||
56 | { | ||
57 | auto factory = adaptorFactories.value(type); | ||
58 | Q_ASSERT(factory); | ||
59 | return *factory; | ||
60 | } | ||
61 | |||
62 | template<typename DomainType> | ||
63 | DomainTypeAdaptorFactoryInterface &adaptorFactory() | ||
64 | { | ||
65 | return adaptorFactory(ApplicationDomain::getTypeName<DomainType>()); | ||
66 | } | ||
67 | |||
68 | ResourceAccessInterface::Ptr resourceAccess() | ||
69 | { | ||
70 | if (!mResourceAccess) { | ||
71 | mResourceAccess = ResourceAccessFactory::instance().getAccess(resourceInstanceIdentifier, resourceType); | ||
72 | } | ||
73 | return mResourceAccess; | ||
74 | } | ||
75 | }; | ||
76 | |||
77 | } | ||