// 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 can be included directly, this file may disappear or change contents." #endif #ifndef COMMONAPI_DBUS_DBUSERROREVENT_HPP_ #define COMMONAPI_DBUS_DBUSERROREVENT_HPP_ #include #include namespace CommonAPI { namespace DBus { template class DBusErrorEvent; template<> class DBusErrorEvent<> : public Event { 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 In_, class... InArgs_, template class DeplIn_, class... DeplIn_Args> class DBusErrorEvent< In_, DeplIn_> : public Event { public: DBusErrorEvent(const std::string &_errorName, const std::tuple &_in) : errorName_(_errorName) { initialize(typename make_sequence_range::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::type()); } private: template inline void initialize(index_sequence, const std::tuple &_in) { in_ = std::make_tuple(std::get(_in)...); } template void deserialize(const DBusMessage &_reply, index_sequence) { if (sizeof...(InArgs_) > 0) { DBusInputStream dbusInputStream(_reply); const bool success = DBusSerializableArguments...>::deserialize(dbusInputStream, std::get(in_)...); if (!success) { COMMONAPI_ERROR("DBusErrorEvent::", __func__, "(", errorName_, "): deserialization failed!"); return; } this->notifyListeners(errorName_, std::move(std::get(in_).getValue())...); } } std::string errorName_; std::tuple...> in_; }; class DBusErrorEventHelper { public: template static void notifyListeners(const DBusMessage &_reply, const std::string &_errorName, index_sequence, const std::tuple &_errorEvents) { notifyListeners(_reply, _errorName, std::get(_errorEvents)...); } template static void notifyListeners(const DBusMessage &_reply, const std::string &_errorName, ErrorEvent_ *_errorEvent) { if(_errorEvent->getErrorName() == _errorName) _errorEvent->notifyErrorEventListeners(_reply); } template 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_ */