File: service.h

package info (click to toggle)
dbus-cpp 5.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,208 kB
  • sloc: cpp: 9,427; ansic: 2,012; xml: 1,156; makefile: 14; sh: 2
file content (186 lines) | stat: -rw-r--r-- 6,189 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
184
185
186
/*
 * Copyright © 2012 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 */
#ifndef CORE_DBUS_SERVICE_H_
#define CORE_DBUS_SERVICE_H_

#include <core/dbus/bus.h>
#include <core/dbus/codec.h>
#include <core/dbus/match_rule.h>
#include <core/dbus/message_router.h>
#include <core/dbus/result.h>
#include <core/dbus/visibility.h>

#include <core/dbus/interfaces/properties.h>
#include <core/dbus/traits/service.h>
#include <core/dbus/types/any.h>
#include <core/dbus/types/object_path.h>
#include <core/dbus/types/variant.h>
#include <core/dbus/types/stl/map.h>
#include <core/dbus/types/stl/string.h>

#include <bitset>
#include <future>
#include <memory>
#include <set>
#include <stdexcept>
#include <string>
#include <tuple>

namespace core
{
namespace dbus
{
class Object;

/**
 * \brief Represents a service available on the bus.
 * \example geoclue/main.cpp
 * Provides an example of accessing the Geoclue session service on the bus.
 * \example upower/main.cpp
 * Provides an example of access the UPower system service on the bus.
*/
class ORG_FREEDESKTOP_DBUS_DLL_PUBLIC Service : public std::enable_shared_from_this<Service>
{
public: 
    typedef std::shared_ptr<Service> Ptr;    

    /**
     * @brief Exposes a service on the bus, trying to acquire the given name.
     * @returns An instance of Service or nullptr in case of errors.
     * @tparam Interface Needs to a model of concept Service.
     * @param [in] connection Bus to expose the service upon.
     * @param [in] name The name to acquire for this service.
     * @param [in] flags Flags specifying behavior when requesting a name on the bus.
     */
    inline static Ptr add_service(
        const Bus::Ptr& connection,
        const std::string& name,
        const Bus::RequestNameFlag& flags =
            Bus::RequestNameFlag::do_not_queue |
            Bus::RequestNameFlag::replace_existing)
    {
        Ptr instance(
            new Service(
                connection,
                name,
                flags));
        return instance;
    }

    /**
     * @brief Exposes a service on the bus.
     * @returns An instance of Service or nullptr in case of errors.
     * @tparam Interface Needs to a model of concept Service.
     * @param [in] connection Bus to expose the service upon.
     * @param [in] flags Flags specifying behavior when requesting a name on the bus.
     */
    template<typename Interface>
    inline static Ptr add_service(
        const Bus::Ptr& connection,
        const Bus::RequestNameFlag& flags =
            Bus::RequestNameFlag::do_not_queue |
            Bus::RequestNameFlag::replace_existing)
    {
        static Ptr instance
        {
            add_service(connection,
                        traits::Service<Interface>::interface_name(),
                        flags)
        };

        return instance;
    }

    /**
     * @brief Provides access to a service on the bus via a proxy object.
     * @returns An instance of Service or nullptr in case of errors.
     * @tparam Interface Needs to be a model of concept Service.
     * @param [in] connection Bus to access the service upon.
     */
    template<typename Interface>
    inline static Ptr use_service(const Bus::Ptr& connection)
    {
        return use_service(connection, traits::Service<Interface>::interface_name());
    }

    /**
     * @brief Provides access to a service on the bus via a proxy object.
     * @returns An instance of Service or nullptr in case of errors.
     * @param [in] connection Bus to access the service upon.
     * @param [in] name Well-known name of the service on the bus.
     */
    static Ptr use_service(const Bus::Ptr& connection, const std::string& name);

    /**
     * @brief Provides access to a service on the bus via a proxy object.
     * @returns An instance of Service.
     * @throw std::runtime_error in case of errors.
     * @param [in] connection Bus to access the service upon.
     * @param [in] name Well-known name of the service on the bus.
     */
    static Ptr use_service_or_throw_if_not_available(const Bus::Ptr& connection, const std::string& name);

    /**
     * @brief Provides access to the root object of this service instance.
     */
    const std::shared_ptr<Object>& root_object();

    /**
     * @brief Access an existing object on the specified path.
     * @param [in] path The path to access the object upon.
     * @return An instance to the object or nullptr if no object on that path exists.
     */
    std::shared_ptr<Object> object_for_path(const types::ObjectPath& path);

    /**
     * @brief Adds a new object on the specified path.
     * @param [in] path The path to mount the object upon.
     * @return An instance to the object or nullptr if the object could not be added.
     */
    std::shared_ptr<Object> add_object_for_path(const types::ObjectPath& path);

    /**
     * @brief Non-mutable access to the name of the service.
     */
    const std::string& get_name() const;

protected:
    friend class Bus;
    friend class Object;
    template<typename T> friend class Property;

    Service(const Bus::Ptr& connection, const std::string& name);
    Service(const Bus::Ptr& connection, const std::string& name, const Bus::RequestNameFlag& flags);

    bool is_stub() const;

    const Bus::Ptr& get_connection() const;

    void add_match(const MatchRule& rule);
    void remove_match(const MatchRule& rule);

private:
    Bus::Ptr connection;
    std::string name;
    std::shared_ptr<Object> root;
    bool stub;
};
}
}

#endif // CORE_DBUS_SERVICE_H_