Initial import to Git
[profile/ivi/common-api-dbus-runtime.git] / src / CommonAPI / DBus / DBusAttribute.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 #ifndef COMMONAPI_DBUS_DBUS_ATTRIBUTE_H_
5 #define COMMONAPI_DBUS_DBUS_ATTRIBUTE_H_
6
7 #include "DBusProxyHelper.h"
8 #include "DBusEvent.h"
9
10 #include <cassert>
11
12 namespace CommonAPI {
13 namespace DBus {
14
15
16 class DBusProxy;
17
18
19 template <typename _AttributeType, typename _DBusProxyType = DBusProxy>
20 class DBusReadonlyAttribute: public _AttributeType {
21  public:
22         typedef typename _AttributeType::ValueType ValueType;
23         typedef typename _AttributeType::AttributeAsyncCallback AttributeAsyncCallback;
24
25         DBusReadonlyAttribute(_DBusProxyType& dbusProxy, const char* getMethodName):
26                         dbusProxy_(dbusProxy),
27                         getMethodName_(getMethodName) {
28                 assert(getMethodName);
29         }
30
31         CallStatus getValue(ValueType& value) const {
32                 CallStatus callStatus;
33                 DBusProxyHelper<DBusSerializableArguments<>,
34                                                 DBusSerializableArguments<ValueType> >::callMethodWithReply(dbusProxy_, getMethodName_, "", callStatus, value);
35                 return callStatus;
36         }
37
38         std::future<CallStatus> getValueAsync(AttributeAsyncCallback attributeAsyncCallback) {
39                 return DBusProxyHelper<DBusSerializableArguments<>,
40                                                            DBusSerializableArguments<ValueType> >::callMethodAsync(dbusProxy_, getMethodName_, "", std::move(attributeAsyncCallback));
41         }
42
43  protected:
44         _DBusProxyType& dbusProxy_;
45         const char* getMethodName_;
46 };
47
48
49 template <typename _AttributeType, typename _DBusProxyType = DBusProxy>
50 class DBusAttribute: public DBusReadonlyAttribute<_AttributeType> {
51  public:
52         typedef typename _AttributeType::ValueType ValueType;
53         typedef typename _AttributeType::AttributeAsyncCallback AttributeAsyncCallback;
54
55         DBusAttribute(_DBusProxyType& dbusProxy, const char* setMethodName, const char* setMethodSignature, const char* getMethodName):
56                 DBusReadonlyAttribute<_AttributeType>(dbusProxy, getMethodName),
57                         setMethodName_(setMethodName),
58                         setMethodSignature_(setMethodSignature) {
59                 assert(setMethodName);
60                 assert(setMethodSignature);
61         }
62
63         void setValue(const ValueType& requestValue, CallStatus& callStatus, ValueType& responseValue) {
64                 DBusProxyHelper<DBusSerializableArguments<ValueType>,
65                                                 DBusSerializableArguments<ValueType> >::callMethodWithReply(
66                                                                 this->dbusProxy_,
67                                                                 setMethodName_,
68                                                                 setMethodSignature_,
69                                                                 requestValue,
70                                                                 callStatus,
71                                                                 responseValue);
72         }
73
74         std::future<CallStatus> setValueAsync(const ValueType& requestValue, AttributeAsyncCallback attributeAsyncCallback) {
75                 return DBusProxyHelper<DBusSerializableArguments<ValueType>,
76                                                            DBusSerializableArguments<ValueType> >::callMethodAsync(
77                                                                            this->dbusProxy_,
78                                                                            setMethodName_,
79                                                                            setMethodSignature_,
80                                                                            requestValue,
81                                                                            attributeAsyncCallback);
82         }
83
84  protected:
85         const char* setMethodName_;
86         const char* setMethodSignature_;
87 };
88
89
90 template <typename _AttributeType, typename _DBusProxyType = DBusProxy>
91 class DBusObservableAttribute: public _AttributeType {
92  public:
93         typedef typename _AttributeType::ValueType ValueType;
94         typedef typename _AttributeType::AttributeAsyncCallback AttributeAsyncCallback;
95         typedef typename _AttributeType::ChangedEvent ChangedEvent;
96
97         template <typename... _AttributeTypeArguments>
98         DBusObservableAttribute(_DBusProxyType& dbusProxy, const char* changedEventName, _AttributeTypeArguments... arguments):
99                 _AttributeType(dbusProxy, arguments...),
100                 changedEvent_(dbusProxy, changedEventName, this->setMethodSignature_) {
101         }
102
103         ChangedEvent& getChangedEvent() {
104                 return changedEvent_;
105         }
106
107  protected:
108         DBusEvent<ChangedEvent> changedEvent_;
109 };
110
111 } // namespace DBus
112 } // namespace CommonAPI
113
114 #endif // COMMONAPI_DBUS_DBUS_ATTRIBUTE_H_