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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
/*
* 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_OBJECT_H_
#define CORE_DBUS_OBJECT_H_
#include <core/dbus/bus.h>
#include <core/dbus/lifetime_constrained_cache.h>
#include <core/dbus/service.h>
#include <functional>
#include <future>
#include <map>
#include <memory>
#include <mutex>
#include <ostream>
#include <string>
namespace std
{
/**
* @brief Template specialization of std::hash for a std::tuple<std::string, std::string>.
*/
template<>
struct hash<std::tuple<std::string, std::string>>
{
size_t operator()(const std::tuple<std::string, std::string>& key) const
{
static const std::hash<std::string> h {};
// Using XOR as we do not expect first and second to be equal.
return h(std::get<0>(key)) ^ h(std::get<1>(key));
}
};
/**
* @brief Pretty prints a std::tuple<std::string, std::string>.
* @param out The output stream to write to.
* @param tuple The tuple to print.
* @return Returns a reference to the output stream.
*/
inline std::ostream& operator<<(std::ostream& out,
const std::tuple<std::string, std::string>& tuple)
{
out << "(" << std::get<0>(tuple) << "," << std::get<1>(tuple) << ")";
return out;
}
}
namespace core
{
namespace dbus
{
namespace types
{
class Any;
class ObjectPath;
class Variant;
}
class MatchRule;
template<typename T>
class Property;
template<typename T>
class Result;
template<typename T, typename U>
class Signal;
/**
* @brief The Object class models a DBus object living on the bus.
*/
class Object : public std::enable_shared_from_this<Object>
{
private:
typedef std::tuple<types::ObjectPath, std::string, std::string> CacheKey;
typedef std::tuple<std::string, std::string> MethodKey;
typedef std::tuple<std::string, std::string> PropertyKey;
typedef std::tuple<std::string, std::string> SignalKey;
template<typename PropertyDescription>
static ThreadSafeLifetimeConstrainedCache<CacheKey, Property<PropertyDescription>>& property_cache();
public:
typedef std::shared_ptr<Object> Ptr;
typedef std::function<void(const Message::Ptr&)> MethodHandler;
~Object();
/**
* @brief Emits a signal with arguments for this object.
*/
template<typename Signal, typename... Args>
inline void emit_signal(const Args& ... args);
/**
* @brief Invokes a method of a remote object blocking the bus instance while waiting for the result.
* @tparam Method The method to invoke.
* @tparam ResultType The expected type of the result.
* @tparam Args Parameter pack of arguments passed to the invocation.
* @param [in] args Argument instances passed to the invocation.
* @return An invocation result, either signalling an error or containing the result of the invocation.
*/
template<typename Method, typename ResultType, typename... Args>
inline Result<ResultType> invoke_method_synchronously(const Args& ... args);
/**
* @brief Invokes a method of a remote object blocking the current thread while waiting for the result.
* @tparam Method The method to invoke.
* @tparam ResultType The expected type of the result.
* @tparam Args Parameter pack of arguments passed to the invocation.
* @param [in] args Argument instances passed to the invocation.
* @return A future wrapping an invocation result, either signalling an error or containing the result of the invocation.
*/
template<typename Method, typename ResultType, typename... Args>
inline Result<ResultType> transact_method(const Args& ... args);
/**
* @brief Invokes a method of a remote object returning a std::future to synchronize with the result
* @tparam Method The method to invoke.
* @tparam ResultType The expected type of the result.
* @tparam Args Parameter pack of arguments passed to the invocation.
* @param [in] args Argument instances passed to the invocation.
* @return A future wrapping an invocation result, either signalling an error or containing the result of the invocation.
*/
template<typename Method, typename ResultType, typename... Args>
inline std::future<Result<ResultType>> invoke_method_asynchronously(const Args& ... args);
/**
* @brief Invokes a method of a remote object invoking the provided callback on completion or in case of errors.
* @tparam Method The method to invoke.
* @tparam ResultType The expected type of the result.
* @tparam Args Parameter pack of arguments passed to the invocation.
* @param [in] args Argument instances passed to the invocation.
* @param [in] cb The callback to be invoked on completion/on error.
* @return A future wrapping an invocation result, either signalling an error or containing the result of the invocation.
*/
template<typename Method, typename ResultType, typename... Args>
inline void invoke_method_asynchronously_with_callback(
std::function<void(const Result<ResultType>&)> cb,
const Args& ... args);
/**
* @brief Accesses a property of the object.
* @return An instance of the property or nullptr in case of errors.
*/
template<typename PropertyDescription>
std::shared_ptr<Property<PropertyDescription>>
inline get_property();
/**
* @brief Queries all properties in one go for the object.
*/
template<typename Interface>
inline std::map<std::string, types::Variant>
get_all_properties();
/**
* @brief Accesses a signal of the object.
* @return An instance of the signal or nullptr in case of errors.
*/
template<typename SignalDescription>
const std::shared_ptr<Signal<SignalDescription, typename SignalDescription::ArgumentType>>
inline get_signal();
/**
* @brief Adds an object as a child of this object.
* @param [in] path The path to associate the object with.
* @return An object instance or nullptr in case of errors.
*/
std::shared_ptr<Object>
inline add_object_for_path(const types::ObjectPath& path);
/**
* @brief Installs an implementation for a specific method of this object instance.
* @tparam Method The method to install the implementation for.
* @param [in] handler The implementation.
*/
template<typename Method>
inline void install_method_handler(const MethodHandler& handler);
/**
* @brief Uninstalls an implementation for a specific method of this object instance.
* @tparam Method The method to uninstall the implementation for.
*/
template<typename Method>
inline void uninstall_method_handler();
/**
* @brief Queries whether this object is a stub instance.
* @return true if this object is a stub instance, false otherwise.
*/
inline bool is_stub() const;
/**
* @brief Requests the object to process a message
* @param msg The message to be processed.
* @return true iff the msg has been handled.
*/
inline bool on_new_message(const Message::Ptr& msg);
/**
* @return object path of the Object
*/
inline const types::ObjectPath& path() const;
private:
friend class Service;
template<typename T, typename U> friend class Signal;
template<typename T> friend class Property;
Object(const std::shared_ptr<Service> parent, const types::ObjectPath& path);
void add_match(const MatchRule& rule);
void remove_match(const MatchRule& rule);
void on_properties_changed(
const interfaces::Properties::Signals::PropertiesChanged::ArgumentType&);
std::shared_ptr<Service> parent;
types::ObjectPath object_path;
MessageRouter<SignalKey> signal_router;
MessageRouter<MethodKey> method_router;
MessageRouter<PropertyKey> get_property_router;
MessageRouter<PropertyKey> set_property_router;
std::once_flag add_match_once;
std::map<
std::tuple<std::string, std::string>,
std::function<void(const types::Variant&)>
> property_changed_vtable;
};
}
}
#include "impl/object.h"
#endif // CORE_DBUS_OBJECT_H_
|