summaryrefslogtreecommitdiff
path: root/include/CommonAPI/DBus/DBusAttribute.hpp
blob: 50dea3b27f01468bf062fa0865bd1cdf7a381ffd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (C) 2013-2015 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#if !defined (COMMONAPI_INTERNAL_COMPILATION)
#error "Only <CommonAPI/CommonAPI.hpp> can be included directly, this file may disappear or change contents."
#endif

#ifndef COMMONAPI_DBUS_DBUS_ATTRIBUTE_HPP_
#define COMMONAPI_DBUS_DBUS_ATTRIBUTE_HPP_

#include <cassert>
#include <cstdint>
#include <tuple>

#include <CommonAPI/DBus/DBusConfig.hpp>
#include <CommonAPI/DBus/DBusEvent.hpp>
#include <CommonAPI/DBus/DBusProxyHelper.hpp>

namespace CommonAPI {
namespace DBus {

template <typename _AttributeType, typename _AttributeDepl = EmptyDeployment>
class DBusReadonlyAttribute: public _AttributeType {
public:
    typedef typename _AttributeType::ValueType ValueType;
    typedef _AttributeDepl ValueTypeDepl;
    typedef typename _AttributeType::AttributeAsyncCallback AttributeAsyncCallback;

    DBusReadonlyAttribute(DBusProxy &_proxy,
    					  const char *setMethodSignature, const char *getMethodName,
    					  _AttributeDepl *_depl = nullptr)
    	: proxy_(_proxy),
          getMethodName_(getMethodName),
          setMethodSignature_(setMethodSignature),
          depl_(_depl)	{
        assert(getMethodName);
    }

    void getValue(CommonAPI::CallStatus &_status, ValueType &_value, const CommonAPI::CallInfo *_info) const {
    	CommonAPI::Deployable<ValueType, _AttributeDepl> deployedValue(depl_);
        DBusProxyHelper<
        	DBusSerializableArguments<
			>,
            DBusSerializableArguments<
				CommonAPI::Deployable<
					ValueType,
					_AttributeDepl
				>
        	>
        >::callMethodWithReply(proxy_, getMethodName_, "", (_info ? _info : &defaultCallInfo), _status, deployedValue);
        _value = deployedValue.getValue();
    }

    std::future<CallStatus> getValueAsync(AttributeAsyncCallback _callback, const CommonAPI::CallInfo *_info) {
    	CommonAPI::Deployable<ValueType, _AttributeDepl> deployedValue(depl_);
        return DBusProxyHelper<
        			DBusSerializableArguments<>,
                    DBusSerializableArguments<CommonAPI::Deployable<ValueType, _AttributeDepl>>
        	   >::callMethodAsync(proxy_, getMethodName_, "", (_info ? _info : &defaultCallInfo),
        			[_callback](CommonAPI::CallStatus _status, CommonAPI::Deployable<ValueType, _AttributeDepl> _response) {
        				_callback(_status, _response.getValue());
        			},
        			std::make_tuple(deployedValue));
    }

 protected:
    DBusProxy &proxy_;
    const char *getMethodName_;
    const char *setMethodSignature_;
    _AttributeDepl *depl_;
};

template <typename _AttributeType, typename _AttributeDepl = EmptyDeployment>
class DBusAttribute: public DBusReadonlyAttribute<_AttributeType, _AttributeDepl> {
public:
    typedef typename _AttributeType::ValueType ValueType;
    typedef typename _AttributeType::AttributeAsyncCallback AttributeAsyncCallback;

    DBusAttribute(DBusProxy &_proxy,
    			  const char *_setMethodName, const char *_setMethodSignature, const char *_getMethodName,
    			  _AttributeDepl *_depl = nullptr)
    	: DBusReadonlyAttribute<_AttributeType, _AttributeDepl>(_proxy, _setMethodSignature, _getMethodName, _depl),
            setMethodName_(_setMethodName),
            setMethodSignature_(_setMethodSignature) {
        assert(_setMethodName);
        assert(_setMethodSignature);
    }

    void setValue(const ValueType &_request, CommonAPI::CallStatus &_status, ValueType &_response, const CommonAPI::CallInfo *_info) {
    	CommonAPI::Deployable<ValueType, _AttributeDepl> deployedRequest(_request, this->depl_);
    	CommonAPI::Deployable<ValueType, _AttributeDepl> deployedResponse(this->depl_);
    	DBusProxyHelper<DBusSerializableArguments<CommonAPI::Deployable<ValueType, _AttributeDepl>>,
                        DBusSerializableArguments<CommonAPI::Deployable<ValueType, _AttributeDepl>> >::callMethodWithReply(
                                this->proxy_,
                                setMethodName_,
                                setMethodSignature_,
								(_info ? _info : &defaultCallInfo),
                                deployedRequest,
								_status,
                                deployedResponse);
    	_response = deployedResponse.getValue();
    }


    std::future<CallStatus> setValueAsync(const ValueType &_request, AttributeAsyncCallback _callback, const CommonAPI::CallInfo *_info) {
    	CommonAPI::Deployable<ValueType, _AttributeDepl> deployedRequest(_request, this->depl_);
    	CommonAPI::Deployable<ValueType, _AttributeDepl> deployedResponse(this->depl_);
    	return DBusProxyHelper<DBusSerializableArguments<CommonAPI::Deployable<ValueType, _AttributeDepl>>,
                               DBusSerializableArguments<CommonAPI::Deployable<ValueType, _AttributeDepl>> >::callMethodAsync(
                                       this->proxy_,
                                       setMethodName_,
                                       setMethodSignature_,
									   (_info ? _info : &defaultCallInfo),
                                       deployedRequest,
                                       [_callback](CommonAPI::CallStatus _status, CommonAPI::Deployable<ValueType, _AttributeDepl> _response) {
    										_callback(_status, _response.getValue());
    								   },
                                       std::make_tuple(deployedResponse));
    }

 protected:
    const char* setMethodName_;
    const char* setMethodSignature_;
};

template <typename _AttributeType>
class DBusObservableAttribute: public _AttributeType {
public:
    typedef typename _AttributeType::ValueType ValueType;
    typedef typename _AttributeType::ValueTypeDepl ValueTypeDepl;
    typedef typename _AttributeType::AttributeAsyncCallback AttributeAsyncCallback;
    typedef typename _AttributeType::ChangedEvent ChangedEvent;

    template <typename... _AttributeTypeArguments>
    DBusObservableAttribute(DBusProxy &_proxy,
    						const char *_changedEventName,
    						_AttributeTypeArguments... arguments)
    	 : _AttributeType(_proxy, arguments...),
    	   changedEvent_(_proxy, _changedEventName, this->setMethodSignature_, this->getMethodName_,
    			   	   	 std::make_tuple(CommonAPI::Deployable<ValueType, ValueTypeDepl>(this->depl_))) {
    }

    ChangedEvent &getChangedEvent() {
        return changedEvent_;
    }

 protected:
    DBusEvent<ChangedEvent, CommonAPI::Deployable<ValueType, ValueTypeDepl> > changedEvent_;
};

} // namespace DBus
} // namespace CommonAPI

#endif // COMMONAPI_DBUS_DBUS_ATTRIBUTE_HPP_