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, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 **************************************************************************/
20
26#include <algorithm>
27#include <memory>
28
29#include "model/comm_driver.h"
31
32void CommDriverRegistry::Activate(DriverPtr driver) {
33 if (!driver) return;
34 AbstractCommDriver* found = nullptr;
35 for (auto& d : drivers)
36 if (d->iface == driver->iface && d->bus == driver->bus) found = d.get();
37 if (found) return;
38 drivers.push_back(std::move(driver));
40};
41
42void CommDriverRegistry::Deactivate(DriverPtr& driver) {
43 auto found = std::find(drivers.begin(), drivers.end(), driver);
44 if (found == drivers.end()) return;
45 drivers.erase(found);
47}
48
49const std::vector<DriverPtr>& CommDriverRegistry::GetDrivers() const {
50 return drivers;
51};
52
54 while (drivers.size()) {
55 Deactivate(drivers[0]);
56 }
57}
58
59CommDriverRegistry& CommDriverRegistry::GetInstance() {
60 static CommDriverRegistry instance;
61 return instance;
62}
63
64std::unique_ptr<AbstractCommDriver> kNoDriver(nullptr);
65
66DriverPtr& FindDriver(const std::vector<DriverPtr>& drivers,
67 const std::string& iface, const NavAddr::Bus _bus) {
68 if (_bus != NavAddr::Bus::Undef) {
69 for (const DriverPtr& d : drivers) {
70 if (d->iface == iface && d->bus == _bus) {
71 return const_cast<DriverPtr&>(d);
72 }
73 }
74 return kNoDriver;
75 } else {
76 for (auto&& d : drivers)
77 if (d->iface == iface) return const_cast<DriverPtr&>(d);
78 return kNoDriver;
79 }
80}
Common interface for all drivers.
Definition comm_driver.h:58
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.
const void Notify()
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.