OpenCPN Partial API docs
Loading...
Searching...
No Matches
comm_drv_registry.cpp
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2022 by David Register *
3 * Copyright (C) 2022 Alec Leamas *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
25#include <algorithm>
26#include <memory>
27
28#include "model/comm_driver.h"
30
31void CommDriverRegistry::Activate(DriverPtr driver) {
32 if (!driver) return;
33 AbstractCommDriver* found = nullptr;
34 for (auto& d : drivers)
35 if (d->iface == driver->iface && d->bus == driver->bus) found = d.get();
36 if (found) return;
37 drivers.push_back(std::move(driver));
39};
40
41void CommDriverRegistry::Deactivate(DriverPtr& driver) {
42 auto found = std::find(drivers.begin(), drivers.end(), driver);
43 if (found == drivers.end()) return;
44 drivers.erase(found);
46}
47
48const std::vector<DriverPtr>& CommDriverRegistry::GetDrivers() const {
49 return drivers;
50};
51
53 while (drivers.size()) {
54 Deactivate(drivers[0]);
55 }
56}
57
58CommDriverRegistry& CommDriverRegistry::GetInstance() {
59 static CommDriverRegistry instance;
60 return instance;
61}
62
63std::unique_ptr<AbstractCommDriver> kNoDriver(nullptr);
64
65DriverPtr& FindDriver(const std::vector<DriverPtr>& drivers,
66 const std::string& iface, const NavAddr::Bus _bus) {
67 if (_bus != NavAddr::Bus::Undef) {
68 for (const DriverPtr& d : drivers) {
69 if (d->iface == iface && d->bus == _bus) {
70 return const_cast<DriverPtr&>(d);
71 }
72 }
73 return kNoDriver;
74 } else {
75 for (auto&& d : drivers)
76 if (d->iface == iface) return const_cast<DriverPtr&>(d);
77 return kNoDriver;
78 }
79}
Common interface for all drivers.
Definition comm_driver.h:65
The global driver registry, a singleton.
void Deactivate(DriverPtr &driver)
Remove driver from list of active drivers.
void Activate(DriverPtr driver)
Add driver to list of active drivers.
const std::vector< DriverPtr > & GetDrivers() const
EventVar evt_driverlist_change
Notified by all driverlist updates.
void CloseAllDrivers()
Close and destroy all drivers completely.
void Notify() override
Notify all listeners, no data supplied.
Communication driver layer.
DriverPtr & FindDriver(const std::vector< DriverPtr > &drivers, const std::string &iface, const NavAddr::Bus _bus)
Search list of drivers for a driver with given interface string.
Driver registration container, a singleton.