diff options
author | Andreas Rogge <andreas.rogge@bareos.com> | 2022-10-28 13:47:27 +0300 |
---|---|---|
committer | Andreas Rogge <andreas.rogge@bareos.com> | 2022-11-07 19:40:28 +0300 |
commit | 28d4311e3d795ae2f14750d5197dd947a50c6c2e (patch) | |
tree | d9a69fc774a0b7ee4ce8ee2de791df7cb14db48b | |
parent | bd1cbc9c58a72cc665a6e9aeeaa2e5d874f31965 (diff) |
lib: rename PluginRegistry
As we have so many kinds of plugins, we'll just call it
ImplementationFactory as it is a factory that returns implementations.
-rw-r--r-- | core/src/lib/implementation_factory.h | 67 | ||||
-rw-r--r-- | core/src/lib/plugin_registry.h | 75 | ||||
-rw-r--r-- | core/src/stored/backends/unix_file_device.cc | 1 | ||||
-rw-r--r-- | core/src/stored/dev.cc | 5 | ||||
-rw-r--r-- | core/src/stored/sd_backends.h | 10 | ||||
-rw-r--r-- | core/src/stored/sd_backends_dynamic.cc | 2 | ||||
-rw-r--r-- | core/src/stored/stored_conf.cc | 4 |
7 files changed, 78 insertions, 86 deletions
diff --git a/core/src/lib/implementation_factory.h b/core/src/lib/implementation_factory.h new file mode 100644 index 000000000..727f0e6b5 --- /dev/null +++ b/core/src/lib/implementation_factory.h @@ -0,0 +1,67 @@ +/* + BAREOSĀ® - Backup Archiving REcovery Open Sourced + + Copyright (C) 2022-2022 Bareos GmbH & Co. KG + + This program is Free Software; you can redistribute it and/or + modify it under the terms of version three of the GNU Affero General Public + License as published by the Free Software Foundation and included + in the file LICENSE. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +/* + * generic registry where you can register factory functions based on a name. + * The registry is templated on the implementation's interface, thus the + * names will be per interface. + */ + +#ifndef BAREOS_LIB_IMPLEMENTATION_FACTORY_H_ +#define BAREOS_LIB_IMPLEMENTATION_FACTORY_H_ + +#include <memory> +#include <functional> +#include <unordered_map> + +template <typename Interface> class ImplementationFactory { + using Factory = std::function<Interface*()>; + using Map = std::unordered_map<std::string, Factory>; + + static Map& GetMap() + { + // this ensures thread-safe initialization (Meyers' singleton) + static Map factory_map; + return factory_map; + } + + public: + // this retuns a bool, so you can initialize a global static variable from it + // to register your plugin when the program starts or during dl_open() + static bool Add(const std::string& implementation_name, Factory factory) + { + GetMap().insert({implementation_name, factory}); + return true; + } + + static bool IsRegistered(const std::string& implementation_name) + { + auto m = GetMap(); + return m.find(implementation_name) != m.end(); + } + + static Interface* Create(const std::string& implementation_name) + { + Dmsg0(100, "Creating Instance for '%s'\n", implementation_name.c_str()); + return GetMap().at(implementation_name)(); + } +}; +#endif // BAREOS_LIB_IMPLEMENTATION_FACTORY_H_ diff --git a/core/src/lib/plugin_registry.h b/core/src/lib/plugin_registry.h deleted file mode 100644 index 3b01a3c59..000000000 --- a/core/src/lib/plugin_registry.h +++ /dev/null @@ -1,75 +0,0 @@ -/* - BAREOSĀ® - Backup Archiving REcovery Open Sourced - - Copyright (C) 2022-2022 Bareos GmbH & Co. KG - - This program is Free Software; you can redistribute it and/or - modify it under the terms of version three of the GNU Affero General Public - License as published by the Free Software Foundation and included - in the file LICENSE. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. -*/ - -/* - * generic plugin registry where you can register factory functions based - * on a name. The registry is templated on the plugin's interface, thus the - * naming is automatically per plugin interface. - */ - -#ifndef BAREOS_LIB_PLUGIN_REGISTRY_H_ -#define BAREOS_LIB_PLUGIN_REGISTRY_H_ - -#include <memory> -#include <functional> -#include <unordered_map> - -template <typename T> class PluginRegistry { - using Factory = std::function<T*()>; - using Map = std::unordered_map<std::string, Factory>; - - static Map& GetMap() - { - // this ensures thread-safe initialization (Meyers' singleton) - static Map plugin_map; - return plugin_map; - } - - public: - static bool Add(const std::string& plugin_name, Factory plugin_factory) - { - GetMap().insert({plugin_name, plugin_factory}); - return true; - } - - static bool IsRegistered(const std::string& plugin_name) - { - auto m = GetMap(); - return m.find(plugin_name) != m.end(); - } - - static T* Create(const std::string& plugin_name) - { - Dmsg0(100, "Creating Instance for '%s'\n", plugin_name.c_str()); - return GetMap().at(plugin_name)(); - } - - static void DumpDbg() - { - Dmsg0(100, "Start plugin registry dump\n"); - for (auto const& [plugin_name, plugin_factory] : GetMap()) { - auto type = plugin_factory.target_type().name(); - Dmsg0(100, "Plugin name %s with factory %s\n", plugin_name.c_str(), type); - } - Dmsg0(100, "end plugin registry dump\n"); - } -}; -#endif // BAREOS_LIB_PLUGIN_REGISTRY_H_ diff --git a/core/src/stored/backends/unix_file_device.cc b/core/src/stored/backends/unix_file_device.cc index 2b3150dd1..3d7b4e160 100644 --- a/core/src/stored/backends/unix_file_device.cc +++ b/core/src/stored/backends/unix_file_device.cc @@ -37,7 +37,6 @@ #include "unix_file_device.h" #include "lib/berrno.h" #include "lib/util.h" -#include "lib/plugin_registry.h" namespace storagedaemon { diff --git a/core/src/stored/dev.cc b/core/src/stored/dev.cc index bb989634e..2a1205e14 100644 --- a/core/src/stored/dev.cc +++ b/core/src/stored/dev.cc @@ -122,14 +122,15 @@ Device* FactoryCreateDevice(JobControlRecord* jcr, Dmsg1(400, "max_block_size in device_resource res is %u\n", device_resource->max_block_size); - if (!PluginRegistry<Device>::IsRegistered(device_resource->dev_type)) { + if (!ImplementationFactory<Device>::IsRegistered(device_resource->dev_type)) { Jmsg2(jcr, M_ERROR, 0, _("%s has an unknown device type %s\n"), device_resource->archive_device_string, device_resource->dev_type.c_str()); return nullptr; } - Device* dev = PluginRegistry<Device>::Create(device_resource->dev_type); + Device* dev + = ImplementationFactory<Device>::Create(device_resource->dev_type); dev->device_resource = device_resource; device_resource->dev = dev; diff --git a/core/src/stored/sd_backends.h b/core/src/stored/sd_backends.h index f137137bf..722b82136 100644 --- a/core/src/stored/sd_backends.h +++ b/core/src/stored/sd_backends.h @@ -22,7 +22,7 @@ #ifndef BAREOS_STORED_SD_BACKENDS_H_ #define BAREOS_STORED_SD_BACKENDS_H_ -#include <lib/plugin_registry.h> +#include <lib/implementation_factory.h> namespace storagedaemon { @@ -34,10 +34,10 @@ bool LoadStorageBackend(const std::string& dev_type, const std::vector<std::string>& backend_directories); #endif -#define REGISTER_SD_BACKEND(backend_name, backend_class) \ - [[maybe_unused]] static bool backend_name##_backend_ \ - = PluginRegistry<Device>::Add(#backend_name, \ - DeviceFactory<backend_class>); +#define REGISTER_SD_BACKEND(backend_name, backend_class) \ + [[maybe_unused]] static bool backend_name##_backend_ \ + = ImplementationFactory<Device>::Add(#backend_name, \ + DeviceFactory<backend_class>); } // namespace storagedaemon diff --git a/core/src/stored/sd_backends_dynamic.cc b/core/src/stored/sd_backends_dynamic.cc index e73a0028c..9ff449828 100644 --- a/core/src/stored/sd_backends_dynamic.cc +++ b/core/src/stored/sd_backends_dynamic.cc @@ -65,7 +65,7 @@ bool LoadStorageBackend(const std::string& dev_type, return false; } - if (!PluginRegistry<Device>::IsRegistered(dev_type)) { + if (!ImplementationFactory<Device>::IsRegistered(dev_type)) { Jmsg(nullptr, M_ERROR_TERM, 0, "Loaded backend library for %s did not register its backend. This is " "probably a bug in the backend library.\n", diff --git a/core/src/stored/stored_conf.cc b/core/src/stored/stored_conf.cc index 8e6640e2a..4ad7eb0a7 100644 --- a/core/src/stored/stored_conf.cc +++ b/core/src/stored/stored_conf.cc @@ -43,7 +43,7 @@ #define NEED_JANSSON_NAMESPACE 1 #include "lib/output_formatter.h" #include "lib/output_formatter_resource.h" -#include "lib/plugin_registry.h" +#include "lib/implementation_factory.h" #include "include/auth_types.h" #include "include/jcr.h" @@ -557,7 +557,7 @@ static void CheckAndLoadDeviceBackends(ConfigurationParser& my_config) DeviceResource* d = dynamic_cast<DeviceResource*>(p); if (d) { to_lower(d->dev_type); - if (!PluginRegistry<Device>::IsRegistered(d->dev_type)) { + if (!ImplementationFactory<Device>::IsRegistered(d->dev_type)) { #if defined(HAVE_DYNAMIC_SD_BACKENDS) if (!storage_res || storage_res->backend_directories.empty()) { Jmsg2(nullptr, M_ERROR_TERM, 0, |