~thomas-voss/dbus-cpp/add_support_for_get_connection_app_armor_security_context

« back to all changes in this revision

Viewing changes to include/core/dbus/object.h

  • Committer: Tarmac
  • Author(s): thomas-voss, Thomas Voß
  • Date: 2013-12-12 13:25:20 UTC
  • mfrom: (11.2.42 dbus-cpp-refactoring)
  • Revision ID: tarmac-20131212132520-gi9pig97oo57tke5
 * Add introspect function to autogenerated protocol header files.
 * Add a code generator for protocol header files.
 * Split up files and move the boost::asio executor implementation into src.
 * Force definitions inline.

Approved by Timo Jyrinki, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2012 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 
17
 */
 
18
#ifndef CORE_DBUS_OBJECT_H_
 
19
#define CORE_DBUS_OBJECT_H_
 
20
 
 
21
#include <core/dbus/bus.h>
 
22
#include <core/dbus/service.h>
 
23
 
 
24
#include <functional>
 
25
#include <future>
 
26
#include <map>
 
27
#include <memory>
 
28
#include <ostream>
 
29
#include <string>
 
30
 
 
31
namespace std
 
32
{
 
33
/**
 
34
 * @brief Template specialization of std::hash for a std::tuple<std::string, std::string>.
 
35
 */
 
36
template<>
 
37
struct hash<std::tuple<std::string, std::string>>
 
38
{
 
39
    size_t operator()(const std::tuple<std::string, std::string>& key) const
 
40
    {
 
41
        static const std::hash<std::string> h {};
 
42
        // Using XOR as we do not expect first and second to be equal.
 
43
        return h(std::get<0>(key)) ^ h(std::get<1>(key));
 
44
    }
 
45
};
 
46
 
 
47
/**
 
48
 * @brief Pretty prints a std::tuple<std::string, std::string>.
 
49
 * @param out The output stream to write to.
 
50
 * @param tuple The tuple to print.
 
51
 * @return Returns a reference to the output stream.
 
52
 */
 
53
inline std::ostream& operator<<(std::ostream& out,
 
54
                                const std::tuple<std::string, std::string>& tuple)
 
55
{
 
56
    out << "(" << std::get<0>(tuple) << "," << std::get<1>(tuple) << ")";
 
57
    return out;
 
58
}
 
59
}
 
60
 
 
61
namespace core
 
62
{
 
63
namespace dbus
 
64
{
 
65
namespace types
 
66
{
 
67
class Any;
 
68
class ObjectPath;
 
69
template<typename T>
 
70
class Variant;
 
71
}
 
72
class MatchRule;
 
73
template<typename T>
 
74
class Property;
 
75
template<typename T>
 
76
class Result;
 
77
template<typename T, typename U>
 
78
class Signal;
 
79
 
 
80
/**
 
81
 * @brief The Object class models a DBus object living on the bus.
 
82
 */
 
83
class Object : public std::enable_shared_from_this<Object>
 
84
{
 
85
  private:
 
86
    typedef std::tuple<std::string, std::string> MethodKey;
 
87
    typedef std::tuple<std::string, std::string> PropertyKey;
 
88
    typedef std::tuple<std::string, std::string> SignalKey;
 
89
 
 
90
  public:
 
91
    typedef std::shared_ptr<Object> Ptr;
 
92
    typedef std::function<void(const Message::Ptr&)> MethodHandler;
 
93
 
 
94
    /**
 
95
     * @brief Emits a signal with arguments for this object.
 
96
     */
 
97
    template<typename Signal, typename... Args>
 
98
    inline void emit_signal(const Args& ... args);
 
99
 
 
100
    /**
 
101
     * @brief Invokes a method of a remote object blocking while waiting for the result.
 
102
     * @tparam Method The method to invoke.
 
103
     * @tparam ResultType The expected type of the result.
 
104
     * @tparam Args Parameter pack of arguments passed to the invocation.
 
105
     * @param [in] args Argument instances passed to the invocation.
 
106
     * @return An invocation result, either signalling an error or containing the result of the invocation.
 
107
     */
 
108
    template<typename Method, typename ResultType, typename... Args>
 
109
    inline Result<ResultType> invoke_method_synchronously(const Args& ... args);
 
110
 
 
111
    /**
 
112
     * @brief Invokes a method of a remote object returning a std::future to synchronize with the result
 
113
     * @tparam Method The method to invoke.
 
114
     * @tparam ResultType The expected type of the result.
 
115
     * @tparam Args Parameter pack of arguments passed to the invocation.
 
116
     * @param [in] args Argument instances passed to the invocation.
 
117
     * @return A future wrapping an invocation result, either signalling an error or containing the result of the invocation.
 
118
     */
 
119
    template<typename Method, typename ResultType, typename... Args>
 
120
    inline std::future<Result<ResultType>> invoke_method_asynchronously(const Args& ... args);
 
121
 
 
122
    /**
 
123
     * @brief Accesses a property of the object.
 
124
     * @return An instance of the property or nullptr in case of errors.
 
125
     */
 
126
    template<typename PropertyDescription>
 
127
    std::shared_ptr<Property<PropertyDescription>>
 
128
    inline get_property();
 
129
 
 
130
    /**
 
131
     * @brief Queries all properties in one go for the object.
 
132
     */
 
133
    template<typename Interface>
 
134
    std::map<std::string, types::Variant<types::Any>>
 
135
    inline get_all_properties();
 
136
 
 
137
    /**
 
138
     * @brief Accesses a signal of the object.
 
139
     * @return An instance of the signal or nullptr in case of errors.
 
140
     */
 
141
    template<typename SignalDescription>
 
142
    const std::shared_ptr<Signal<SignalDescription, typename SignalDescription::ArgumentType>>
 
143
    inline get_signal();
 
144
 
 
145
    /**
 
146
     * @brief Adds an object as a child of this object.
 
147
     * @param [in] path The path to associate the object with.
 
148
     * @return An object instance or nullptr in case of errors.
 
149
     */
 
150
    std::shared_ptr<Object> 
 
151
    inline add_object_for_path(const types::ObjectPath& path);
 
152
 
 
153
    /**
 
154
     * @brief Installs an implementation for a specific method of this object instance.
 
155
     * @tparam Method The method to install the implementation for.
 
156
     * @param [in] handler The implementation.
 
157
     */
 
158
    template<typename Method>
 
159
    inline void install_method_handler(const MethodHandler& handler);
 
160
 
 
161
    /**
 
162
     * @brief Uninstalls an implementation for a specific method of this object instance.
 
163
     * @tparam Method The method to uninstall the implementation for.
 
164
     */
 
165
    template<typename Method>
 
166
    inline void uninstall_method_handler();
 
167
 
 
168
    /**
 
169
     * @brief Queries whether this object is a stub instance.
 
170
     * @return true if this object is a stub instance, false otherwise.
 
171
     */
 
172
    inline bool is_stub() const;
 
173
 
 
174
    /**
 
175
     * @brief Requests the object to process a message
 
176
     * @param msg The message to be processed.
 
177
     * @return true iff the msg has been handled.
 
178
     */
 
179
    inline bool on_new_message(const Message::Ptr& msg);
 
180
 
 
181
  private:
 
182
    friend class Service;
 
183
    template<typename T, typename U> friend class Signal;
 
184
    template<typename T> friend class Property;
 
185
 
 
186
    Object(const std::shared_ptr<Service> parent, const types::ObjectPath& path);
 
187
 
 
188
    void add_match(const MatchRule& rule);
 
189
    void remove_match(const MatchRule& rule);
 
190
    void on_properties_changed(
 
191
            const interfaces::Properties::Signals::PropertiesChanged::ArgumentType&);
 
192
 
 
193
    std::shared_ptr<Service> parent;
 
194
    types::ObjectPath object_path;    
 
195
    MessageRouter<SignalKey> signal_router;
 
196
    MessageRouter<MethodKey> method_router;
 
197
    MessageRouter<PropertyKey> get_property_router;
 
198
    MessageRouter<PropertyKey> set_property_router;
 
199
    std::map<
 
200
        std::tuple<std::string, std::string>,
 
201
        std::function<void(const Message::Ptr&)>
 
202
    > property_changed_vtable;
 
203
    std::shared_ptr<
 
204
        Signal<
 
205
            interfaces::Properties::Signals::PropertiesChanged,
 
206
            interfaces::Properties::Signals::PropertiesChanged::ArgumentType
 
207
        >
 
208
    > signal_properties_changed;
 
209
};
 
210
}
 
211
}
 
212
 
 
213
#include "impl/object.h"
 
214
 
 
215
#endif // CORE_DBUS_OBJECT_H_