summaryrefslogtreecommitdiff
path: root/src/CommonAPI/Attribute.h
blob: d9c23918033857008945640472adb6c18223ed62 (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
/* Copyright (C) 2013 BMW Group
 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
 * Author: Juergen Gehring (juergen.gehring@bmw.de)
 * 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/. */
#ifndef COMMONAPI_ATTRIBUTE_H_
#define COMMONAPI_ATTRIBUTE_H_

#include "types.h"
#include "Event.h"

#include <cstdint>
#include <functional>
#include <future>
#include <memory>


namespace CommonAPI {

/**
 * \brief Class representing a read only attribute
 *
 * Class representing a read only attribute
 */
template <typename _ValueType>
class ReadonlyAttribute {
 public:
	typedef _ValueType ValueType;
	typedef std::function<void(const CallStatus&, _ValueType)> AttributeAsyncCallback;

	virtual ~ReadonlyAttribute() { }

	/**
	 * \brief Get value of attribute, usually from remote. Synchronous call.
	 *
	 * Get value of attribute, usually from remote. Synchronous call.
	 *
	 * @param value Reference to be filled with value.
	 * @return Call status of the operation.
	 */
	virtual void getValue(CallStatus& callStaus, _ValueType& value) const = 0;

	/**
     * \brief Get value of attribute, usually from remote. Asynchronous call.
     *
     * Get value of attribute, usually from remote. Asynchronous call.
     *
     * @param attributeAsyncCallback std::function object for the callback to be invoked.
     * @return std::future containing the call status of the operation.
     */
	virtual std::future<CallStatus> getValueAsync(AttributeAsyncCallback attributeAsyncCallback) = 0;
};

/**
 * \brief Class representing a read and writable attribute
 *
 * Class representing a read and writable attribute
 */
template <typename _ValueType>
class Attribute: public ReadonlyAttribute<_ValueType> {
 public:
	typedef typename ReadonlyAttribute<_ValueType>::ValueType ValueType;
	typedef typename ReadonlyAttribute<_ValueType>::AttributeAsyncCallback AttributeAsyncCallback;

	virtual ~Attribute() { }

    /**
     * \brief Set value of attribute, usually to remote. Synchronous call.
     *
     * Set value of attribute, usually to remote. Synchronous call.
     *
     * @param requestValue Value to be set
     * @param callStatus call status reference will be filled with status of the operation
     * @param responseValue Reference which will contain the actuall value set by the remote.
     */
	virtual void setValue(const _ValueType& requestValue, CallStatus& callStatus, _ValueType& responseValue) = 0;

	/**
     * \brief Set value of attribute, usually to remote. Asynchronous call.
     *
     * Set value of attribute, usually to remote. Asynchronous call.
     *
     * @param requestValue Value to be set
     * @param attributeAsyncCallback std::function object for the callback to be invoked.
     * @return std::future containing the call status of the operation.
     */
	virtual std::future<CallStatus> setValueAsync(const _ValueType& requestValue,
												  AttributeAsyncCallback attributeAsyncCallback) = 0;
};

/**
 * \brief Class representing an observable attribute
 *
 * Class representing an observable attribute
 */
template <typename _AttributeBaseClass>
class _ObservableAttributeImpl: public _AttributeBaseClass {
 public:
	typedef typename _AttributeBaseClass::ValueType ValueType;
	typedef typename _AttributeBaseClass::AttributeAsyncCallback AttributeAsyncCallback;
	typedef Event<ValueType> ChangedEvent;

	virtual ~_ObservableAttributeImpl() { }

	/**
	 * \brief Returns the event handler for the remote change notifiaction event
	 *
	 * Returns the event handler for the remote change notifiaction event
	 *
	 * @return The event handler object
	 */
	virtual ChangedEvent& getChangedEvent() = 0;
};

template <typename _ValueType>
struct ObservableReadonlyAttribute: _ObservableAttributeImpl< ReadonlyAttribute<_ValueType> > {
};

template <typename _ValueType>
struct ObservableAttribute: _ObservableAttributeImpl< Attribute<_ValueType> > {
};

} // namespace CommonAPI

#endif // COMMONAPI_ATTRIBUTE_H_