summaryrefslogtreecommitdiff
path: root/include/CommonAPI/DBus/DBusErrorEvent.hpp
blob: 0ab53aaaad0cdfc05c6a93d29eda4b29fad7370e (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
// Copyright (C) 2013-2020 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_DBUSERROREVENT_HPP_
#define COMMONAPI_DBUS_DBUSERROREVENT_HPP_

#include <CommonAPI/Event.hpp>

#include <CommonAPI/DBus/DBusInputStream.hpp>

namespace CommonAPI {
namespace DBus {

template <class...>
class DBusErrorEvent;

template<>
class DBusErrorEvent<> : public Event<std::string> {
public:
    DBusErrorEvent(const std::string &_errorName) :
        errorName_(_errorName) {}

    DBusErrorEvent(const DBusErrorEvent &_source) :
        errorName_(_source.errorName_) {}

    virtual ~DBusErrorEvent() {}

    inline const std::string & getErrorName() const {
        return errorName_;
    }

    void notifyErrorEventListeners(const DBusMessage &_reply) {
        (void)_reply;
        this->notifyListeners(errorName_);
    }

private:
    std::string errorName_;
};

template <
    template <class...> class In_, class... InArgs_,
    template <class...> class DeplIn_, class... DeplIn_Args>
class DBusErrorEvent<
        In_<InArgs_...>,
        DeplIn_<DeplIn_Args...>> : public Event<std::string, InArgs_...> {
public:
    DBusErrorEvent(const std::string &_errorName,
                   const std::tuple<DeplIn_Args*...> &_in) :
        errorName_(_errorName) {
        initialize(typename make_sequence_range<sizeof...(DeplIn_Args), 0>::type(), _in);
    }

    DBusErrorEvent(const DBusErrorEvent &_source) :
        errorName_(_source.errorName_),
        in_(_source.in_) {}

    virtual ~DBusErrorEvent() {}

    inline const std::string & getErrorName() const {
        return errorName_;
    }

    void notifyErrorEventListeners(const DBusMessage &_reply) {
        deserialize(_reply, typename make_sequence_range<sizeof...(InArgs_), 0>::type());
    }

private:

    template <size_t... DeplIn_ArgIndices>
    inline void initialize(index_sequence<DeplIn_ArgIndices...>, const std::tuple<DeplIn_Args*...> &_in) {
        in_ = std::make_tuple(std::get<DeplIn_ArgIndices>(_in)...);
    }

    template <size_t... InArgIndices_>
    void deserialize(const DBusMessage &_reply,
                     index_sequence<InArgIndices_...>) {
        if (sizeof...(InArgs_) > 0) {
            DBusInputStream dbusInputStream(_reply);
            const bool success = DBusSerializableArguments<CommonAPI::Deployable<InArgs_, DeplIn_Args>...>::deserialize(dbusInputStream, std::get<InArgIndices_>(in_)...);
            if (!success) {
                COMMONAPI_ERROR("DBusErrorEvent::", __func__, "(", errorName_, "): deserialization failed!");
                return;
            }
            this->notifyListeners(errorName_, std::move(std::get<InArgIndices_>(in_).getValue())...);
        }
    }

    std::string errorName_;
    std::tuple<CommonAPI::Deployable<InArgs_, DeplIn_Args>...> in_;
};

class DBusErrorEventHelper {
public:
    template <size_t... ErrorEventsIndices_, class... ErrorEvents_>
    static void notifyListeners(const DBusMessage &_reply,
                                const std::string &_errorName,
                                index_sequence<ErrorEventsIndices_...>,
                                const std::tuple<ErrorEvents_*...> &_errorEvents) {

        notifyListeners(_reply, _errorName, std::get<ErrorEventsIndices_>(_errorEvents)...);
    }

    template <class ErrorEvent_>
    static void notifyListeners(const DBusMessage &_reply,
                                const std::string &_errorName,
                                ErrorEvent_ *_errorEvent) {
        if(_errorEvent->getErrorName() == _errorName)
            _errorEvent->notifyErrorEventListeners(_reply);

    }

    template <class ErrorEvent_, class... Rest_>
    static void notifyListeners(const DBusMessage &_reply,
                                const std::string &_errorName,
                                ErrorEvent_ *_errorEvent,
                                Rest_&... _rest) {
        if(_errorEvent->getErrorName() == _errorName)
            _errorEvent->notifyErrorEventListeners(_reply);
        notifyListeners(_reply, _errorName, _rest...);
    }
};

} // namespace DBus
} // namespace CommonAPI

#endif /* COMMONAPI_DBUS_DBUSERROREVENT_HPP_ */