File: ProxyInterfaces.h

package info (click to toggle)
sdbus-cpp 0.8.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,184 kB
  • sloc: cpp: 9,207; xml: 160; ansic: 115; makefile: 22
file content (183 lines) | stat: -rw-r--r-- 6,713 bytes parent folder | download | duplicates (3)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
 * (C) 2016 - 2017 KISTLER INSTRUMENTE AG, Winterthur, Switzerland
 * (C) 2016 - 2019 Stanislav Angelovic <angelovic.s@gmail.com>
 *
 * @file ProxyInterfaces.h
 *
 * Created on: Apr 8, 2019
 * Project: sdbus-c++
 * Description: High-level D-Bus IPC C++ library based on sd-bus
 *
 * This file is part of sdbus-c++.
 *
 * sdbus-c++ is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * sdbus-c++ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with sdbus-c++. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef SDBUS_CXX_PROXYINTERFACES_H_
#define SDBUS_CXX_PROXYINTERFACES_H_

#include <sdbus-c++/IProxy.h>
#include <cassert>
#include <string>
#include <memory>

// Forward declarations
namespace sdbus {
    class IConnection;
}

namespace sdbus {

    /********************************************//**
     * @class ProxyObjectHolder
     *
     * ProxyObjectHolder is a helper that simply owns and provides
     * access to a proxy object to other classes in the inheritance
     * hierarchy of a native-like proxy object based on generated
     * interface classes.
     *
     ***********************************************/
    class ProxyObjectHolder
    {
    protected:
        ProxyObjectHolder(std::unique_ptr<IProxy>&& proxy)
            : proxy_(std::move(proxy))
        {
            assert(proxy_ != nullptr);
        }

        const IProxy& getProxy() const
        {
            assert(proxy_ != nullptr);
            return *proxy_;
        }

        IProxy& getProxy()
        {
            assert(proxy_ != nullptr);
            return *proxy_;
        }

    private:
        std::unique_ptr<IProxy> proxy_;
    };

    /********************************************//**
     * @class ProxyInterfaces
     *
     * ProxyInterfaces is a helper template class that joins all interface classes of a remote
     * D-Bus object generated by sdbus-c++-xml2cpp to be used on the client (the proxy) side,
     * including some auxiliary classes. ProxyInterfaces is the class that native-like proxy
     * implementation classes written by users should inherit from and implement all pure virtual
     * methods. So the _Interfaces template parameter is a list of sdbus-c++-xml2cpp-generated
     * proxy-side interface classes representing interfaces of the corresponding remote D-Bus object.
     *
     * In the final proxy class inherited from ProxyInterfaces, it is necessary to finish proxy
     * registration in class constructor (`finishRegistration();`), and, conversely, unregister
     * the proxy in class destructor (`unregister();`).
     *
     ***********************************************/
    template <typename... _Interfaces>
    class ProxyInterfaces
        : protected ProxyObjectHolder
        , public _Interfaces...
    {
    public:
        /*!
         * @brief Creates native-like proxy object instance
         *
         * @param[in] destination Bus name that provides a D-Bus object
         * @param[in] objectPath Path of the D-Bus object
         *
         * This constructor overload creates a proxy that manages its own D-Bus connection(s).
         * For more information on its behavior, consult @ref createProxy(std::string,std::string)
         */
        ProxyInterfaces(std::string destination, std::string objectPath)
            : ProxyObjectHolder(createProxy(std::move(destination), std::move(objectPath)))
            , _Interfaces(getProxy())...
        {
        }

        /*!
         * @brief Creates native-like proxy object instance
         *
         * @param[in] connection D-Bus connection to be used by the proxy object
         * @param[in] destination Bus name that provides a D-Bus object
         * @param[in] objectPath Path of the D-Bus object
         *
         * The proxy created this way just references a D-Bus connection owned and managed by the user.
         * For more information on its behavior, consult @ref createProxy(IConnection&,std::string,std::string)
         */
        ProxyInterfaces(IConnection& connection, std::string destination, std::string objectPath)
            : ProxyObjectHolder(createProxy(connection, std::move(destination), std::move(objectPath)))
            , _Interfaces(getProxy())...
        {
        }

        /*!
         * @brief Creates native-like proxy object instance
         *
         * @param[in] connection D-Bus connection to be used by the proxy object
         * @param[in] destination Bus name that provides a D-Bus object
         * @param[in] objectPath Path of the D-Bus object
         *
         * The proxy created this way becomes an owner of the connection.
         * For more information on its behavior, consult @ref createProxy(std::unique_ptr<sdbus::IConnection>&&,std::string,std::string)
         */
        ProxyInterfaces(std::unique_ptr<sdbus::IConnection>&& connection, std::string destination, std::string objectPath)
            : ProxyObjectHolder(createProxy(std::move(connection), std::move(destination), std::move(objectPath)))
            , _Interfaces(getProxy())...
        {
        }

        /*!
         * @brief Finishes proxy registration and makes the proxy ready for use
         *
         * This function must be called in the constructor of the final proxy class that implements ProxyInterfaces.
         *
         * For more information, see underlying @ref IProxy::finishRegistration()
         */
        void registerProxy()
        {
            getProxy().finishRegistration();
        }

        /*!
         * @brief Unregisters the proxy so it no more receives signals and async call replies
         *
         * This function must be called in the destructor of the final proxy class that implements ProxyInterfaces.
         *
         * See underlying @ref IProxy::unregister()
         */
        void unregisterProxy()
        {
            getProxy().unregister();
        }

        /*!
         * @brief Returns object path of the underlying DBus object
         */
        const std::string& getObjectPath() const
        {
            return getProxy().getObjectPath();
        }

    protected:
        using base_type = ProxyInterfaces;
        ~ProxyInterfaces() = default;
    };

}

#endif /* SDBUS_CXX_INTERFACES_H_ */