OpenCPN Partial API docs
comm_drv_registry.cpp
1 /***************************************************************************
2  *
3  * Project: OpenCPN
4  * Purpose: Implements comm_drv_registry.h
5  * Author: David Register, Alec Leamas
6  *
7  ***************************************************************************
8  * Copyright (C) 2022 by David Register, Alec Leamas *
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  * This program is distributed in the hope that it will be useful, *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18  * GNU General Public License for more details. *
19  * *
20  * You should have received a copy of the GNU General Public License *
21  * along with this program; if not, write to the *
22  * Free Software Foundation, Inc., *
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24  **************************************************************************/
25 // For compilers that support precompilation, includes "wx.h".
26 #include <wx/wxprec.h>
27 
28 #ifndef WX_PRECOMP
29 #include <wx/wx.h>
30 #endif // precompiled headers
31 
32 #include <algorithm>
33 #include <memory>
34 
35 #include "model/comm_driver.h"
36 #include "model/comm_drv_registry.h"
37 
38 void CommDriverRegistry::Activate(DriverPtr driver) {
39  auto found = std::find(drivers.begin(), drivers.end(), driver);
40  if (found != drivers.end()) return;
41  drivers.push_back(driver);
43 };
44 
45 void CommDriverRegistry::Deactivate(DriverPtr driver) {
46  auto found = std::find(drivers.begin(), drivers.end(), driver);
47  if (found == drivers.end()) return;
48  drivers.erase(found);
50 }
51 
52 const std::vector<DriverPtr>& CommDriverRegistry::GetDrivers() {
53  return drivers;
54 };
55 
57  while (drivers.size()) {
58  Deactivate(drivers[0]);
59  }
60 }
61 
62 CommDriverRegistry& CommDriverRegistry::GetInstance() {
63  static CommDriverRegistry instance;
64  return instance;
65 }
66 
67 const std::shared_ptr<AbstractCommDriver> kNoDriver(nullptr);
68 
69 const DriverPtr FindDriver(const std::vector<DriverPtr>& drivers,
70  const std::string& iface, const NavAddr::Bus _bus) {
71  if (_bus != NavAddr::Bus::Undef){
72  auto func = [iface, _bus](const DriverPtr d) {
73  return ((d->iface == iface) && (d->bus == _bus));
74  };
75  auto found = std::find_if(drivers.begin(), drivers.end(), func);
76  return found != drivers.end() ? *found : kNoDriver;
77  }
78  else{
79  auto func = [iface](const DriverPtr d) { return d->iface == iface; };
80  auto found = std::find_if(drivers.begin(), drivers.end(), func);
81  return found != drivers.end() ? *found : kNoDriver;
82  }
83 }
84 
85 const DriverPtr FindDriver(const std::vector<DriverPtr>& drivers,
86  const std::string& key) {
87  auto func = [key](const DriverPtr d) { return d->Key() == key; };
88  auto found = std::find_if(drivers.begin(), drivers.end(), func);
89  return found != drivers.end() ? *found : kNoDriver;
90 }
The global driver registry, a singleton.
void Activate(DriverPtr driver)
Add driver to list of active drivers.
EventVar evt_driverlist_change
Notified by all driverlist updates.
void Deactivate(DriverPtr driver)
Remove driver from list of active drivers.
void CloseAllDrivers()
Close and destroy all drivers completely.
const std::vector< DriverPtr > & GetDrivers()
const void Notify()
Notify all listeners, no data supplied.