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
|
/*
* 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_IMPL_PROPERTY_H_
#define CORE_DBUS_IMPL_PROPERTY_H_
namespace core
{
namespace dbus
{
template<typename PropertyType>
const typename Property<PropertyType>::ValueType&
Property<PropertyType>::get() const
{
if (parent->is_stub())
{
Super::mutable_get() = parent->invoke_method_synchronously<
interfaces::Properties::Get,
types::TypedVariant<typename Property<PropertyType>::ValueType>
>(interface, name).value().get();
}
return Super::get();
}
template<typename PropertyType>
void
Property<PropertyType>::set(const typename Property<PropertyType>::ValueType& new_value)
{
if (parent->is_stub())
{
if (!writable)
{
throw std::runtime_error("Property is not writable");
}
parent->invoke_method_synchronously<
interfaces::Properties::Set,
void
>(interface, name, types::TypedVariant<ValueType>(new_value));
}
Super::set(new_value);
}
template<typename PropertyType>
bool
Property<PropertyType>::is_writable() const
{
return writable;
}
template<typename PropertyType>
const core::Signal<void>&
Property<PropertyType>::about_to_be_destroyed() const
{
return signal_about_to_be_destroyed;
}
template<typename PropertyType>
std::shared_ptr<Property<PropertyType>>
Property<PropertyType>::make_property(const std::shared_ptr<Object>& parent)
{
return std::shared_ptr<Property<PropertyType>>(
new Property<PropertyType>(
parent,
traits::Service<typename PropertyType::Interface>::interface_name(),
PropertyType::name(),
PropertyType::writable));
}
template<typename PropertyType>
Property<PropertyType>::Property(
const std::shared_ptr<Object>& parent,
const std::string& interface,
const std::string& name,
bool writable)
: parent(parent),
interface(interface),
name(name),
writable(writable)
{
if (!parent->is_stub())
{
parent->get_property_router.install_route(
Object::PropertyKey
{
traits::Service<typename PropertyType::Interface>::interface_name(),
PropertyType::name()
},
std::bind(&Property::handle_get, this, std::placeholders::_1));
parent->set_property_router.install_route(
Object::PropertyKey
{
traits::Service<typename PropertyType::Interface>::interface_name(),
PropertyType::name()
},
std::bind(
&Property::handle_set,
this,
std::placeholders::_1));
}
}
template<typename PropertyType>
Property<PropertyType>::~Property()
{
try
{
signal_about_to_be_destroyed();
} catch(...)
{
// Consciously dropping all exceptions here.
// There is hardly anything we can do about it while
// tearing down the object anyway.
}
}
template<typename PropertyType>
void
Property<PropertyType>::handle_get(const Message::Ptr& msg)
{
auto reply = Message::make_method_return(msg);
reply->writer() << types::TypedVariant<ValueType>(Super::get());
parent->parent->get_connection()->send(reply);
}
template<typename PropertyType>
void
Property<PropertyType>::handle_set(const Message::Ptr& msg)
{
if (!writable)
{
auto error = Message::make_error(
msg,
traits::Service<interfaces::Properties>::interface_name() + ".NotWritableError",
name + "is not writable");
parent->parent->get_connection()->send(error);
return;
}
std::string s; types::TypedVariant<ValueType> value;
try
{
msg->reader() >> s >> s >> value;
Super::set(value.get());
}
catch (...)
{
auto error = Message::make_error(
msg,
traits::Service<interfaces::Properties>::interface_name() + ".NotWritableError",
name + "is not writable");
parent->parent->get_connection()->send(error);
return;
}
auto reply = Message::make_method_return(msg);
parent->parent->get_connection()->send(reply);
}
template<typename PropertyType>
void
Property<PropertyType>::handle_changed(const types::Variant& arg)
{
try
{
auto value = arg.as<typename PropertyType::ValueType>();
Super::set(value);
}
catch (const std::exception &e){
std::cout << __PRETTY_FUNCTION__ << ": " << e.what() << std::endl;
}
catch (...)
{
std::cout << __PRETTY_FUNCTION__ << ": " << "Unknown exception." << std::endl;
}
}
}
}
#endif // CORE_DBUS_IMPL_PROPERTY_H_
|