Skip to content

Commit

Permalink
Small refactor update.
Browse files Browse the repository at this point in the history
  • Loading branch information
Madman10K committed Oct 26, 2024
1 parent 43bafb3 commit d3a8a04
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 122 deletions.
52 changes: 26 additions & 26 deletions ArrayBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,62 +16,62 @@ UDBus::ArrayBuilder::~ArrayBuilder() noexcept
message = nullptr;
}

UDBus::Message& UDBus::ArrayBuilder::getMessage() noexcept
UDBus::Message& UDBus::ArrayBuilder::getMessage() const noexcept
{
return *message;
}

#define WRAP_AS_PROCEDURE(x) [&]() -> void { x; }

UDBus::ArrayBuilder& UDBus::operator<<(UDBus::ArrayBuilder& builder, UDBus::MessageManipulators manipulators) noexcept
UDBus::ArrayBuilder& UDBus::operator<<(UDBus::ArrayBuilder& message, const UDBus::MessageManipulators manipulators) noexcept
{
std::function<void(void)> f = WRAP_AS_PROCEDURE();

ArrayBuilder* tmpBuilderPointer = &builder; // Temporary pointer to the builder argument because lambda captures complicate the program flow
std::string tmpVariantSignature = builder.variantSignature; // Temporary string that will be copied in the lambda
ArrayBuilder* tmpBuilderPointer = &message; // Temporary pointer to the builder argument because lambda captures complicate the program flow
std::string tmpVariantSignature = message.variantSignature; // Temporary string that will be copied in the lambda
switch (manipulators)
{
case BeginStruct:
if (builder.bInitialising && !builder.bInVariant)
builder.signature += DBUS_STRUCT_BEGIN_CHAR_AS_STRING;
else if (builder.bInVariant)
builder.variantSignature += DBUS_STRUCT_BEGIN_CHAR_AS_STRING;
f = WRAP_AS_PROCEDURE(Message::BeginStruct(builder.getMessage()));
if (message.bInitialising && !message.bInVariant)
message.signature += DBUS_STRUCT_BEGIN_CHAR_AS_STRING;
else if (message.bInVariant)
message.variantSignature += DBUS_STRUCT_BEGIN_CHAR_AS_STRING;
f = WRAP_AS_PROCEDURE(Message::BeginStruct(message.getMessage()));
break;
case EndStruct:
if (builder.bInitialising && !builder.bInVariant)
builder.signature += DBUS_STRUCT_END_CHAR_AS_STRING;
else if (builder.bInVariant)
builder.variantSignature += DBUS_STRUCT_END_CHAR_AS_STRING;
f = WRAP_AS_PROCEDURE(Message::EndStruct(builder.getMessage()));
if (message.bInitialising && !message.bInVariant)
message.signature += DBUS_STRUCT_END_CHAR_AS_STRING;
else if (message.bInVariant)
message.variantSignature += DBUS_STRUCT_END_CHAR_AS_STRING;
f = WRAP_AS_PROCEDURE(Message::EndStruct(message.getMessage()));
break;
case BeginVariant:
if (builder.bInitialising)
builder.signature += DBUS_TYPE_VARIANT_AS_STRING;
builder.bInVariant = true;
if (message.bInitialising)
message.signature += DBUS_TYPE_VARIANT_AS_STRING;
message.bInVariant = true;
// Pushing f() will make the last item the current size and insert will insert at the position before that index, so we have to add one
// This is done because the first child callback is placed before the actual variant iterator creation callback. This results in a mix-up like this:
// a (uivu) -> v a(uivu) where in reality it should be the opposite. The variant type should be pushed before the array iterator.
builder.variantIndex = builder.eventList.size() + 1;
f = WRAP_AS_PROCEDURE(Message::BeginVariant(builder.getMessage()));
message.variantIndex = message.eventList.size() + 1;
f = WRAP_AS_PROCEDURE(Message::BeginVariant(message.getMessage()));
break;
case EndVariant:
builder.bInVariant = false;
message.bInVariant = false;
f = [tmpBuilderPointer, tmpVariantSignature]() -> void {
Message::EndVariant(tmpBuilderPointer->getMessage(), tmpVariantSignature);
};
// Insert variant callback before the first child, fixing a mix-up, detailed in the above case's comments
builder.eventList.insert(builder.eventList.begin() + static_cast<decltype(builder.eventList)::difference_type>(builder.variantIndex), f);
builder.variantSignature.clear(); // Clear signature because we might have multiple variants
return builder; // We've already inserted so we can now return
message.eventList.insert(message.eventList.begin() + static_cast<decltype(message.eventList)::difference_type>(message.variantIndex), f);
message.variantSignature.clear(); // Clear signature because we might have multiple variants
return message; // We've already inserted so we can now return
default:
break;
}
builder.eventList.push_back(f);
return builder;
message.eventList.push_back(f);
return message;
}

UDBus::ArrayBuilder& UDBus::operator<<(UDBus::ArrayBuilder& builder, ArrayBuilderManipulators manipulators) noexcept
UDBus::ArrayBuilder& UDBus::operator<<(UDBus::ArrayBuilder& builder, const ArrayBuilderManipulators manipulators) noexcept
{
switch (manipulators)
{
Expand Down
28 changes: 14 additions & 14 deletions Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ UDBus::Connection::~Connection() noexcept
unref();
}

UDBus::Connection::operator DBusConnection*() noexcept
UDBus::Connection::operator DBusConnection*() const noexcept
{
return connection;
}

void UDBus::Connection::unref() noexcept
void UDBus::Connection::unref() const noexcept
{
dbus_connection_unref(connection);
}

void UDBus::Connection::close() noexcept
void UDBus::Connection::close() const noexcept
{
dbus_connection_close(connection);
}

void UDBus::Connection::bus_get(DBusBusType type, UDBus::Error& error) noexcept
void UDBus::Connection::bus_get(const DBusBusType type, UDBus::Error& error) noexcept
{
connection = dbus_bus_get(type, error);
}

void UDBus::Connection::bus_get_private(DBusBusType type, UDBus::Error& error) noexcept
void UDBus::Connection::bus_get_private(const DBusBusType type, UDBus::Error& error) noexcept
{
connection = dbus_bus_get_private(type, error);
}
Expand All @@ -40,7 +40,7 @@ void UDBus::Connection::open_private(const char* address, UDBus::Error& error) n
connection = dbus_connection_open_private(address, error);
}

void UDBus::Connection::ref(UDBus::Connection& conn) noexcept
void UDBus::Connection::ref(const UDBus::Connection& conn) noexcept
{
connection = dbus_connection_ref(conn);
}
Expand All @@ -55,42 +55,42 @@ UDBus::Connection::Connection(DBusConnection* conn) noexcept
connection = conn;
}

void UDBus::Connection::flush() noexcept
void UDBus::Connection::flush() const noexcept
{
dbus_connection_flush(connection);
}

udbus_bool_t UDBus::Connection::send(UDBus::Message& message, dbus_uint32_t* client_serial) noexcept
udbus_bool_t UDBus::Connection::send(UDBus::Message& message, dbus_uint32_t* client_serial) const noexcept
{
return dbus_connection_send(connection, message, client_serial);
}

udbus_bool_t UDBus::Connection::send_with_reply(UDBus::Message& message, UDBus::PendingCall& pending_return, int timeout_milliseconds) noexcept
udbus_bool_t UDBus::Connection::send_with_reply(UDBus::Message& message, UDBus::PendingCall& pending_return, const int timeout_milliseconds) const noexcept
{
return dbus_connection_send_with_reply(connection, message, pending_return, timeout_milliseconds);
}

UDBus::Message UDBus::Connection::send_with_reply_and_block(UDBus::Message& message, int timeout_milliseconds, UDBus::Error& error) noexcept
UDBus::Message UDBus::Connection::send_with_reply_and_block(UDBus::Message& message, const int timeout_milliseconds, UDBus::Error& error) const noexcept
{
return Message(dbus_connection_send_with_reply_and_block(connection, message, timeout_milliseconds, error));
}

int UDBus::Connection::request_name(const char* name, unsigned int flags, UDBus::Error& error) noexcept
int UDBus::Connection::request_name(const char* name, const unsigned int flags, UDBus::Error& error) const noexcept
{
return dbus_bus_request_name(connection, name, flags, error);
}

udbus_bool_t UDBus::Connection::read_write(int timeout_milliseconds) noexcept
udbus_bool_t UDBus::Connection::read_write(const int timeout_milliseconds) const noexcept
{
return dbus_connection_read_write(connection, timeout_milliseconds);
}

udbus_bool_t UDBus::Connection::read_write_dispatch(int timeout_milliseconds) noexcept
udbus_bool_t UDBus::Connection::read_write_dispatch(const int timeout_milliseconds) const noexcept
{
return dbus_connection_read_write_dispatch(connection, timeout_milliseconds);
}

UDBus::Message UDBus::Connection::pop_message() noexcept
UDBus::Message UDBus::Connection::pop_message() const noexcept
{
return Message(dbus_connection_pop_message(connection));
}
Expand Down
4 changes: 2 additions & 2 deletions DBusUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "DBusUtils.hpp"

udbus_bool_t::udbus_bool_t(dbus_bool_t dbus) noexcept
udbus_bool_t::udbus_bool_t(const dbus_bool_t dbus) noexcept
{
b = dbus;
}

udbus_bool_t::operator bool() noexcept
udbus_bool_t::operator bool() const noexcept
{
return b;
}
Loading

0 comments on commit d3a8a04

Please sign in to comment.