File: AdaptorInterfaces.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 (149 lines) | stat: -rw-r--r-- 4,981 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
/**
 * (C) 2016 - 2017 KISTLER INSTRUMENTE AG, Winterthur, Switzerland
 * (C) 2016 - 2019 Stanislav Angelovic <angelovic.s@gmail.com>
 *
 * @file AdaptorInterfaces.h
 *
 * Created on: Nov 8, 2016
 * 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_ADAPTORINTERFACES_H_
#define SDBUS_CXX_ADAPTORINTERFACES_H_

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

// Forward declarations
namespace sdbus {
    class IConnection;
}

namespace sdbus {

    /********************************************//**
     * @class ObjectHolder
     *
     * ObjectHolder is a helper that simply owns and provides
     * access to an object to other classes in the inheritance
     * hierarchy of an object based on generated interface classes.
     *
     ***********************************************/
    class ObjectHolder
    {
    protected:
        ObjectHolder(std::unique_ptr<IObject>&& object)
            : object_(std::move(object))
        {
        }

        const IObject& getObject() const
        {
            assert(object_ != nullptr);
            return *object_;
        }

        IObject& getObject()
        {
            assert(object_ != nullptr);
            return *object_;
        }

    private:
        std::unique_ptr<IObject> object_;
    };

    /********************************************//**
     * @class AdaptorInterfaces
     *
     * AdaptorInterfaces 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 server (the adaptor) side,
     * including some auxiliary classes. AdaptorInterfaces is the class that native-like object
     * 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
     * adaptor-side interface classes representing interfaces (with methods, signals and properties)
     * of the D-Bus object.
     *
     * In the final adaptor class inherited from AdaptorInterfaces, it is necessary to finish
     * adaptor registration in class constructor (finishRegistration();`), and, conversely,
     * unregister the adaptor in class destructor (`unregister();`).
     *
     ***********************************************/
    template <typename... _Interfaces>
    class AdaptorInterfaces
        : protected ObjectHolder
        , public _Interfaces...
    {
    public:
        /*!
         * @brief Creates object instance
         *
         * @param[in] connection D-Bus connection where the object will publish itself
         * @param[in] objectPath Path of the D-Bus object
         *
         * For more information, consult @ref createObject(sdbus::IConnection&,std::string)
         */
        AdaptorInterfaces(IConnection& connection, std::string objectPath)
            : ObjectHolder(createObject(connection, std::move(objectPath)))
            , _Interfaces(getObject())...
        {
        }

        /*!
         * @brief Finishes adaptor API registration and publishes the adaptor on the bus
         *
         * This function must be called in the constructor of the final adaptor class that implements AdaptorInterfaces.
         *
         * For more information, see underlying @ref IObject::finishRegistration()
         */
        void registerAdaptor()
        {
            getObject().finishRegistration();
        }

        /*!
         * @brief Unregisters adaptors's API and removes it from the bus
         *
         * This function must be called in the destructor of the final adaptor class that implements AdaptorInterfaces.
         *
         * For more information, see underlying @ref IObject::unregister()
         */
        void unregisterAdaptor()
        {
            getObject().unregister();
        }

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

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

}

#endif /* SDBUS_CXX_ADAPTORINTERFACES_H_ */