Skip to content

Commit ecea654

Browse files
committed
Fixed/Improved signal/slot registrations.
1 parent e2b763e commit ecea654

8 files changed

Lines changed: 1756 additions & 1847 deletions

File tree

README.md

Lines changed: 67 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -518,15 +518,15 @@ using namespace std::chrono_literals;
518518
class IAccounts
519519
{
520520
public:
521-
virtual ~IAccounts() = default;
521+
virtual ~IAccounts() = default;
522522

523523
public: // Methods
524-
virtual auto CreateUser(std::string const& name, std::string const& fullname, int32_t accountType) -> object_path = 0;
525-
virtual auto DeleteUser(int64_t id, bool removeFiles) -> void = 0;
524+
virtual auto CreateUser(std::string const& name, std::string const& fullname, int32_t accountType) -> object_path = 0;
525+
virtual auto DeleteUser(int64_t id, bool removeFiles) -> void = 0;
526526
public: // Properties
527527
public: // signals
528-
DBusGlue::signal <void(object_path)> UserAdded;
529-
DBusGlue::signal <void(object_path)> UserDeleted;
528+
DBusGlue::signal <void(object_path)> UserAdded;
529+
DBusGlue::signal <void(object_path)> UserDeleted;
530530
};
531531

532532
//----------------------------------------------------------------------------------------
@@ -545,70 +545,67 @@ DBUS_DECLARE
545545

546546
int main()
547547
{
548-
auto bus = open_system_bus();
549-
550-
try
551-
{
552-
bus.install_event_loop(std::unique_ptr <event_loop> (new busy_loop(&bus, 50ms)));
553-
554-
// wrapped interface for creating / deleting accounts.
555-
auto accountControl = create_interface <IAccounts>(
556-
bus,
557-
"org.freedesktop.Accounts",
558-
"/org/freedesktop/Accounts",
559-
"org.freedesktop.Accounts"
560-
);
561-
562-
accountControl.UserAdded.listen(
563-
[](object_path const& p) {
564-
// success callback
565-
std::cout << "callback - create: " << p << std::endl;
566-
},
567-
[](message&, std::string const& str) {
568-
// failure callback
569-
std::cerr << "oh no something gone wrong: " << str << "\n";
570-
}
571-
);
572-
573-
// WARNING! Passing "release_slot" forces you to manage the slots lifetime yourself!
574-
// You can use this variation to manage lifetimes of your observed signals. With a unique_ptr for example.
575-
auto* slot = accountControl.UserDeleted.listen(
576-
[&](object_path const& p) {
577-
// this is called from the dbus system.
578-
std::cout << "callback - delete: " << p << std::endl;
579-
580-
// create a user from here.
581-
auto path = accountControl.CreateUser("tempus", "tempus", 0);
582-
},
583-
[](message&, std::string const& str) {
584-
// this is called when an error got signaled into our callback.
585-
std::cerr << "oh no something gone wrong: " << str << "\n";
586-
},
587-
DBusGlue::release_slot
588-
);
589-
590-
// try to delete a user with id 1001. WARNING, DONT JUST DELETE SOME USER ON YOUR SYSTEM. obviously...
591-
try {
592-
// commented out in case you just run this example
593-
// you should get the id from the name first.
594-
//accountControl.DeleteUser(1001, false);
595-
} catch (std::exception const& exc) {
596-
// Create the user if he doesn't exist
597-
accountControl.CreateUser("tempus", "tempus", 0);
598-
std::cout << exc.what() << std::endl;
599-
}
600-
601-
// just wait here so we dont exit directly
602-
std::cin.get();
603-
604-
// cleanup. IF!!! you passed "release_slot"
605-
delete slot;
606-
}
607-
catch (std::exception const& exc)
608-
{
609-
std::cout << exc.what() << "\n";
610-
}
611-
612-
return 0;
548+
auto bus = open_system_bus();
549+
550+
try
551+
{
552+
bus.install_event_loop(std::unique_ptr <event_loop> (new busy_loop(&bus, 50ms)));
553+
554+
// wrapped interface for creating / deleting accounts.
555+
auto accountControl = create_interface <IAccounts>(
556+
bus,
557+
"org.freedesktop.Accounts",
558+
"/org/freedesktop/Accounts",
559+
"org.freedesktop.Accounts"
560+
);
561+
562+
accountControl.UserAdded.listen(
563+
[](object_path const& p) {
564+
// success callback
565+
std::cout << "callback - create: " << p << std::endl;
566+
},
567+
[](message&, std::string const& str) {
568+
// failure callback
569+
std::cerr << "oh no something gone wrong: " << str << "\n";
570+
}
571+
);
572+
573+
// WARNING! Passing "release_slot" forces you to manage the slots lifetime yourself!
574+
// You can use this variation to manage lifetimes of your observed signals.
575+
std::unique_ptr<void, void(*)(void*)> slot = accountControl.UserDeleted.listen(
576+
[&](object_path const& p) {
577+
// this is called from the dbus system.
578+
std::cout << "callback - delete: " << p << std::endl;
579+
580+
// create a user from here.
581+
auto path = accountControl.CreateUser("tempus", "tempus", 0);
582+
},
583+
[](message&, std::string const& str) {
584+
// this is called when an error got signaled into our callback.
585+
std::cerr << "oh no something gone wrong: " << str << "\n";
586+
},
587+
DBusGlue::release_slot
588+
);
589+
590+
// try to delete a user with id 1001. WARNING, DONT JUST DELETE SOME USER ON YOUR SYSTEM. obviously...
591+
try {
592+
// commented out in case you just run this example
593+
// you should get the id from the name first.
594+
//accountControl.DeleteUser(1001, false);
595+
} catch (std::exception const& exc) {
596+
// Create the user if he doesn't exist
597+
accountControl.CreateUser("tempus", "tempus", 0);
598+
std::cout << exc.what() << std::endl;
599+
}
600+
601+
// just wait here so we dont exit directly
602+
std::cin.get();
603+
}
604+
catch (std::exception const& exc)
605+
{
606+
std::cout << exc.what() << "\n";
607+
}
608+
609+
return 0;
613610
}
614611
```

0 commit comments

Comments
 (0)