| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "appregistry.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <utility> |
| 8 | |
| 9 | void AppRegistry::add(std::string const &app) |
| 10 | { |
| 11 | std::lock_guard<std::mutex> lock(regMutex_); |
| 12 | registry_.insert(app); |
| 13 | notifyRegisteredApp(app); |
| 14 | } |
| 15 | |
| 16 | void AppRegistry::remove(std::string const &app) |
| 17 | { |
| 18 | std::lock_guard<std::mutex> lock(regMutex_); |
| 19 | if (registry_.erase(app) > 0) |
| 20 | notifyUnregisteredApp(app); |
| 21 | } |
| 22 | |
| 23 | bool AppRegistry::registered(std::string const &app) |
| 24 | { |
| 25 | std::lock_guard<std::mutex> lock(regMutex_); |
| 26 | return registry_.find(app) != registry_.end(); |
| 27 | } |
| 28 | |
| 29 | void AppRegistry::addObserver(std::shared_ptr<IAppRegistry::Observer> observer) |
| 30 | { |
| 31 | std::lock_guard<std::mutex> lock(obMutex_); |
| 32 | |
| 33 | auto const it = std::find(observers_.begin(), observers_.end(), observer); |
| 34 | if (it == observers_.end()) |
| 35 | observers_.emplace_back(std::move(observer)); |
| 36 | } |
| 37 | |
| 38 | void AppRegistry::removeObserver( |
| 39 | std::shared_ptr<IAppRegistry::Observer> const &observer) |
| 40 | { |
| 41 | std::lock_guard<std::mutex> lock(obMutex_); |
| 42 | std::erase(observers_, observer); |
| 43 | } |
| 44 | |
| 45 | void AppRegistry::notifyRegisteredApp(std::string const &app) |
| 46 | { |
| 47 | std::lock_guard<std::mutex> lock(obMutex_); |
| 48 | for (auto &o : observers_) |
| 49 | o->registeredApp(app); |
| 50 | } |
| 51 | |
| 52 | void AppRegistry::notifyUnregisteredApp(std::string const &app) |
| 53 | { |
| 54 | std::lock_guard<std::mutex> lock(obMutex_); |
| 55 | for (auto &o : observers_) |
| 56 | o->unregisteredApp(app); |
| 57 | } |