2
* Copyright © 2012 Canonical Ltd.
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.
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.
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/>.
16
* Authored by: Thomas Voß <thomas.voss@canonical.com>
18
#ifndef CORE_DBUS_OBJECT_H_
19
#define CORE_DBUS_OBJECT_H_
21
#include <core/dbus/bus.h>
22
#include <core/dbus/service.h>
34
* @brief Template specialization of std::hash for a std::tuple<std::string, std::string>.
37
struct hash<std::tuple<std::string, std::string>>
39
size_t operator()(const std::tuple<std::string, std::string>& key) const
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));
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.
53
inline std::ostream& operator<<(std::ostream& out,
54
const std::tuple<std::string, std::string>& tuple)
56
out << "(" << std::get<0>(tuple) << "," << std::get<1>(tuple) << ")";
77
template<typename T, typename U>
81
* @brief The Object class models a DBus object living on the bus.
83
class Object : public std::enable_shared_from_this<Object>
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;
91
typedef std::shared_ptr<Object> Ptr;
92
typedef std::function<void(const Message::Ptr&)> MethodHandler;
95
* @brief Emits a signal with arguments for this object.
97
template<typename Signal, typename... Args>
98
inline void emit_signal(const Args& ... args);
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.
108
template<typename Method, typename ResultType, typename... Args>
109
inline Result<ResultType> invoke_method_synchronously(const Args& ... args);
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.
119
template<typename Method, typename ResultType, typename... Args>
120
inline std::future<Result<ResultType>> invoke_method_asynchronously(const Args& ... args);
123
* @brief Accesses a property of the object.
124
* @return An instance of the property or nullptr in case of errors.
126
template<typename PropertyDescription>
127
std::shared_ptr<Property<PropertyDescription>>
128
inline get_property();
131
* @brief Queries all properties in one go for the object.
133
template<typename Interface>
134
std::map<std::string, types::Variant<types::Any>>
135
inline get_all_properties();
138
* @brief Accesses a signal of the object.
139
* @return An instance of the signal or nullptr in case of errors.
141
template<typename SignalDescription>
142
const std::shared_ptr<Signal<SignalDescription, typename SignalDescription::ArgumentType>>
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.
150
std::shared_ptr<Object>
151
inline add_object_for_path(const types::ObjectPath& path);
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.
158
template<typename Method>
159
inline void install_method_handler(const MethodHandler& handler);
162
* @brief Uninstalls an implementation for a specific method of this object instance.
163
* @tparam Method The method to uninstall the implementation for.
165
template<typename Method>
166
inline void uninstall_method_handler();
169
* @brief Queries whether this object is a stub instance.
170
* @return true if this object is a stub instance, false otherwise.
172
inline bool is_stub() const;
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.
179
inline bool on_new_message(const Message::Ptr& msg);
182
friend class Service;
183
template<typename T, typename U> friend class Signal;
184
template<typename T> friend class Property;
186
Object(const std::shared_ptr<Service> parent, const types::ObjectPath& path);
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&);
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;
200
std::tuple<std::string, std::string>,
201
std::function<void(const Message::Ptr&)>
202
> property_changed_vtable;
205
interfaces::Properties::Signals::PropertiesChanged,
206
interfaces::Properties::Signals::PropertiesChanged::ArgumentType
208
> signal_properties_changed;
213
#include "impl/object.h"
215
#endif // CORE_DBUS_OBJECT_H_