Skip to content

Commit

Permalink
Misc bug fixes and enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
PixlOne committed Feb 14, 2022
1 parent e86f198 commit 200df3c
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/include/ipcgull/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ namespace ipcgull {
const std::array<std::string, sizeof...(R)>& return_names) :
_f (_fn_generator<std::tuple<R...>>::make_fn(f)),
_return_names (return_names.begin(), return_names.end()),
_return_types (make_variant_type<R>()...) { }
_return_types ({make_variant_type<R>()...}) { }

template <typename... R>
function(std::tuple<R...>(*f)(),
Expand Down
4 changes: 4 additions & 0 deletions src/include/ipcgull/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace ipcgull {
typedef std::map<std::string, function> function_table;
typedef std::map<std::string, base_property> property_table;
typedef std::map<std::string, signal> signal_table;
typedef std::tuple<function_table, property_table, signal_table> tables;
private:
const std::string _name;
const function_table _functions;
Expand All @@ -57,6 +58,9 @@ namespace ipcgull {
property_table p,
signal_table s);

explicit interface(std::string name,
tables t={});

~interface();

// Moving or copying the interface detaches from owner
Expand Down
8 changes: 4 additions & 4 deletions src/include/ipcgull/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace ipcgull {
class server;

class node {
std::map<std::string, std::shared_ptr<interface>> _interfaces;
std::map<std::string, std::weak_ptr<interface>> _interfaces;
std::list<std::weak_ptr<server>> _servers;
std::string _name;

Expand Down Expand Up @@ -69,10 +69,10 @@ namespace ipcgull {
std::shared_ptr<node> make_child(const std::string& name) const;

template <typename T, typename... Args>
[[maybe_unused]] std::shared_ptr<T> make_interface(Args... args) {
[[maybe_unused]] std::shared_ptr<T> make_interface(Args&&... args) {
static_assert(std::is_base_of<interface, T>::value,
"T must be an interface");
auto ptr = std::make_shared<T>(std::forward<Args>(args)...);
auto ptr = std::make_shared<T>(std::forward<Args&&>(args)...);

if(_interfaces.count(ptr->name()))
throw std::invalid_argument("duplicate interface");
Expand Down Expand Up @@ -112,7 +112,7 @@ namespace ipcgull {
void manage(const std::weak_ptr<object>& obj);
[[nodiscard]] const std::weak_ptr<object>& managed() const;

[[nodiscard]] const std::map<std::string, std::shared_ptr<interface>>&
[[nodiscard]] const std::map<std::string, std::weak_ptr<interface>>&
interfaces() const;

[[nodiscard]] const std::string& name() const;
Expand Down
4 changes: 2 additions & 2 deletions src/include/ipcgull/property.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,14 @@ namespace ipcgull {

property& operator=(const property& o) {
if(this != &o) {
operator=(o._data);
operator=(*o._data);
}

return *this;
}
property& operator=(property&& o) {
if(this != &o) {
operator=(std::move(o._data));
operator=(std::move(*o._data));
o.notify_change();
}

Expand Down
11 changes: 10 additions & 1 deletion src/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@ interface::interface(std::string name,
_signals (std::move(s)) {
}

interface::interface(std::string name,
tables t) :
_name (std::move(name)),
_functions (std::move(std::get<0>(t))),
_properties (std::move(std::get<1>(t))),
_signals (std::move(std::get<2>(t))) {
}

interface::~interface() {
assert(_owner.expired());
if(auto owner = _owner.lock())
owner->drop_interface(_name);
}

interface::interface(interface&& o) noexcept :
Expand Down
11 changes: 7 additions & 4 deletions src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ std::shared_ptr<node> node::make_child(const std::string &name) const {
if(auto server = s.lock())
server->drop_interface(full_name(*server), name);
}
if_it->second->_owner.reset();
if(auto lock = if_it->second.lock())
lock->_owner.reset();
_interfaces.erase(if_it);

return true;
Expand All @@ -120,8 +121,10 @@ void node::add_server(const std::weak_ptr<server>& s) {
const std::string node_path = full_name(*server);
for(auto& x : _interfaces) {
try {
server->add_interface(self, *x.second);
interfaces.insert(x.second);
if(auto iface = x.second.lock()) {
server->add_interface(self, *iface);
interfaces.insert(iface);
}
} catch(std::exception& e) {
for(auto& iface : interfaces)
server->drop_interface(node_path, iface->name());
Expand Down Expand Up @@ -177,7 +180,7 @@ void node::emit_signal(const std::string& iface,
}
}

const std::map<std::string, std::shared_ptr<interface>>&
const std::map<std::string, std::weak_ptr<interface>>&
node::interfaces() const {
return _interfaces;
}
Expand Down
34 changes: 30 additions & 4 deletions src/server_gdbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ struct server::internal {
const char* c_str = g_variant_get_string(v, &length);
return std::string(c_str, length);
} else if(g_variant_type_is_subtype_of(type, G_VARIANT_TYPE_BOOLEAN)) {
return {g_variant_get_boolean(v)};
return {static_cast<bool>(g_variant_get_boolean(v)) };
} else if(g_variant_type_is_subtype_of(type,
G_VARIANT_TYPE_DICTIONARY)) {
const gsize length = g_variant_n_children(v);
Expand Down Expand Up @@ -326,7 +326,17 @@ struct server::internal {
"Unknown interface");
return;
}
const auto& functions = iface_it->second->functions();

auto iface = iface_it->second.lock();
if(!iface) {
g_dbus_method_invocation_return_error(
invocation, G_DBUS_ERROR,
G_DBUS_ERROR_UNKNOWN_INTERFACE,
"Interface expired");
return;
}

const auto& functions = iface->functions();
auto f_it = functions.find(method_name);

if(f_it == functions.end()) {
Expand Down Expand Up @@ -430,8 +440,16 @@ struct server::internal {
"Unknown interface");
return nullptr;
}
auto iface = iface_it->second.lock();
if(!iface) {
g_set_error(error, G_DBUS_ERROR,
G_DBUS_ERROR_UNKNOWN_INTERFACE,
"Interface expired");
return nullptr;
}

try {
const auto& property = iface_it->second->get_property(
const auto& property = iface->get_property(
property_name);

return i->to_gvariant(property.get_variant(),
Expand Down Expand Up @@ -491,8 +509,16 @@ struct server::internal {
return false;
}

auto iface = iface_it->second.lock();
if(!iface) {
g_set_error(error, G_DBUS_ERROR,
G_DBUS_ERROR_UNKNOWN_INTERFACE,
"Interface expired");
return false;
}

try {
auto& p = iface_it->second->get_property(
auto& p = iface->get_property(
property_name);

try {
Expand Down

0 comments on commit 200df3c

Please sign in to comment.