|
| 1 | +/** |
| 2 | + * (C) 2016 - 2021 KISTLER INSTRUMENTE AG, Winterthur, Switzerland |
| 3 | + * (C) 2016 - 2024 Stanislav Angelovic <stanislav.angelovic@protonmail.com> |
| 4 | + * |
| 5 | + * @file AdaptorInterfaces.h |
| 6 | + * |
| 7 | + * Created on: Nov 8, 2016 |
| 8 | + * Project: sdbus-c++ |
| 9 | + * Description: High-level D-Bus IPC C++ library based on sd-bus |
| 10 | + * |
| 11 | + * This file is part of sdbus-c++. |
| 12 | + * |
| 13 | + * sdbus-c++ is free software; you can redistribute it and/or modify it |
| 14 | + * under the terms of the GNU Lesser General Public License as published by |
| 15 | + * the Free Software Foundation, either version 2.1 of the License, or |
| 16 | + * (at your option) any later version. |
| 17 | + * |
| 18 | + * sdbus-c++ is distributed in the hope that it will be useful, |
| 19 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | + * GNU Lesser General Public License for more details. |
| 22 | + * |
| 23 | + * You should have received a copy of the GNU Lesser General Public License |
| 24 | + * along with sdbus-c++. If not, see <http://www.gnu.org/licenses/>. |
| 25 | + */ |
| 26 | + |
| 27 | +#ifndef SDBUS_CXX_ADAPTORINTERFACES_H_ |
| 28 | +#define SDBUS_CXX_ADAPTORINTERFACES_H_ |
| 29 | + |
| 30 | +#include <sdbus-c++/IObject.h> |
| 31 | +#include <cassert> |
| 32 | +#include <string> |
| 33 | +#include <memory> |
| 34 | + |
| 35 | +// Forward declarations |
| 36 | +namespace sdbus { |
| 37 | + class IConnection; |
| 38 | +} |
| 39 | + |
| 40 | +namespace sdbus { |
| 41 | + |
| 42 | + /********************************************//** |
| 43 | + * @class ObjectHolder |
| 44 | + * |
| 45 | + * ObjectHolder is a helper that simply owns and provides |
| 46 | + * access to an object to other classes in the inheritance |
| 47 | + * hierarchy of an object based on generated interface classes. |
| 48 | + * |
| 49 | + ***********************************************/ |
| 50 | + class ObjectHolder |
| 51 | + { |
| 52 | + protected: |
| 53 | + ObjectHolder(std::unique_ptr<IObject>&& object) |
| 54 | + : object_(std::move(object)) |
| 55 | + { |
| 56 | + } |
| 57 | + |
| 58 | + const IObject& getObject() const |
| 59 | + { |
| 60 | + assert(object_ != nullptr); |
| 61 | + return *object_; |
| 62 | + } |
| 63 | + |
| 64 | + IObject& getObject() |
| 65 | + { |
| 66 | + assert(object_ != nullptr); |
| 67 | + return *object_; |
| 68 | + } |
| 69 | + |
| 70 | + private: |
| 71 | + std::unique_ptr<IObject> object_; |
| 72 | + }; |
| 73 | + |
| 74 | + /********************************************//** |
| 75 | + * @class AdaptorInterfaces |
| 76 | + * |
| 77 | + * AdaptorInterfaces is a helper template class that joins all interface classes of a remote |
| 78 | + * D-Bus object generated by sdbus-c++-xml2cpp to be used on the server (the adaptor) side, |
| 79 | + * including some auxiliary classes. AdaptorInterfaces is the class that native-like object |
| 80 | + * implementation classes written by users should inherit from and implement all pure virtual |
| 81 | + * methods. So the _Interfaces template parameter is a list of sdbus-c++-xml2cpp-generated |
| 82 | + * adaptor-side interface classes representing interfaces (with methods, signals and properties) |
| 83 | + * of the D-Bus object. |
| 84 | + * |
| 85 | + * In the final adaptor class inherited from AdaptorInterfaces, one needs to make sure: |
| 86 | + * 1. to call `registerAdaptor();` in the class constructor, and, conversely, |
| 87 | + * 2. to call `unregisterAdaptor();` in the class destructor, |
| 88 | + * so that the object API vtable is registered and unregistered at the proper time. |
| 89 | + * |
| 90 | + ***********************************************/ |
| 91 | + template <typename... _Interfaces> |
| 92 | + class AdaptorInterfaces |
| 93 | + : protected ObjectHolder |
| 94 | + , public _Interfaces... |
| 95 | + { |
| 96 | + public: |
| 97 | + /*! |
| 98 | + * @brief Creates object instance |
| 99 | + * |
| 100 | + * @param[in] connection D-Bus connection where the object will publish itself |
| 101 | + * @param[in] objectPath Path of the D-Bus object |
| 102 | + * |
| 103 | + * For more information, consult @ref createObject(sdbus::IConnection&,std::string) |
| 104 | + */ |
| 105 | + AdaptorInterfaces(IConnection& connection, ObjectPath objectPath) |
| 106 | + : ObjectHolder(createObject(connection, std::move(objectPath))) |
| 107 | + , _Interfaces(getObject())... |
| 108 | + { |
| 109 | + } |
| 110 | + |
| 111 | + /*! |
| 112 | + * @brief Adds object vtable (i.e. D-Bus API) definitions for all interfaces it implements |
| 113 | + * |
| 114 | + * This function must be called in the constructor of the final adaptor class that implements AdaptorInterfaces. |
| 115 | + * |
| 116 | + * See also @ref IObject::addVTable() |
| 117 | + */ |
| 118 | + void registerAdaptor() |
| 119 | + { |
| 120 | + (_Interfaces::registerAdaptor(), ...); |
| 121 | + } |
| 122 | + |
| 123 | + /*! |
| 124 | + * @brief Unregisters adaptors's API and removes it from the bus |
| 125 | + * |
| 126 | + * This function must be called in the destructor of the final adaptor class that implements AdaptorInterfaces. |
| 127 | + * |
| 128 | + * For more information, see underlying @ref IObject::unregister() |
| 129 | + */ |
| 130 | + void unregisterAdaptor() |
| 131 | + { |
| 132 | + getObject().unregister(); |
| 133 | + } |
| 134 | + |
| 135 | + /*! |
| 136 | + * @brief Returns reference to the underlying IObject instance |
| 137 | + */ |
| 138 | + using ObjectHolder::getObject; |
| 139 | + |
| 140 | + protected: |
| 141 | + using base_type = AdaptorInterfaces; |
| 142 | + |
| 143 | + AdaptorInterfaces(const AdaptorInterfaces&) = delete; |
| 144 | + AdaptorInterfaces& operator=(const AdaptorInterfaces&) = delete; |
| 145 | + AdaptorInterfaces(AdaptorInterfaces&&) = delete; |
| 146 | + AdaptorInterfaces& operator=(AdaptorInterfaces&&) = delete; |
| 147 | + ~AdaptorInterfaces() = default; |
| 148 | + }; |
| 149 | + |
| 150 | +} |
| 151 | + |
| 152 | +#endif /* SDBUS_CXX_ADAPTORINTERFACES_H_ */ |
0 commit comments