Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
backend_registry.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
42#pragma once
43
44#include "database_backend.h"
45
46#include <string>
47#include <memory>
48#include <vector>
49#include <map>
50#include <functional>
51#include <mutex>
52
53namespace database
54{
55namespace core
56{
57
73{
74public:
79 static backend_registry& instance();
80
81 // Prevent copying and moving
86
103 kcenon::common::VoidResult register_backend(const std::string& name, backend_factory_fn factory);
104
112 kcenon::common::VoidResult unregister_backend(const std::string& name);
113
129 std::unique_ptr<database_backend> create(const std::string& name) const;
130
138 bool has_backend(const std::string& name) const;
139
154 std::vector<std::string> available_backends() const;
155
162 size_t backend_count() const;
163
172 void clear();
173
174private:
175 backend_registry() = default;
176 ~backend_registry() = default;
177
178 mutable std::mutex mutex_;
179 std::map<std::string, backend_factory_fn> factories_;
180};
181
201template<typename BackendType>
203{
204public:
209 explicit backend_registrar(const std::string& name)
210 {
211 backend_registry::instance().register_backend(name, &BackendType::create);
212 }
213};
214
228inline std::unique_ptr<database_backend> create_backend(const std::string& name)
229{
230 return backend_registry::instance().create(name);
231}
232
238inline bool has_backend(const std::string& name)
239{
241}
242
247inline std::vector<std::string> available_backends()
248{
250}
251
252} // namespace core
253} // namespace database
Helper class for automatic backend registration.
backend_registrar(const std::string &name)
Constructor that registers the backend.
Singleton registry for database backend plugins.
std::vector< std::string > available_backends() const
Get list of all registered backend names.
backend_registry(const backend_registry &)=delete
void clear()
Clear all registered backends (for testing)
backend_registry & operator=(backend_registry &&)=delete
backend_registry & operator=(const backend_registry &)=delete
kcenon::common::VoidResult unregister_backend(const std::string &name)
Unregister a backend (for testing or dynamic unloading)
static backend_registry & instance()
Get the singleton instance.
bool has_backend(const std::string &name) const
Check if a backend is registered.
std::unique_ptr< database_backend > create(const std::string &name) const
Create a backend instance by name.
std::map< std::string, backend_factory_fn > factories_
kcenon::common::VoidResult register_backend(const std::string &name, backend_factory_fn factory)
Register a backend factory function.
size_t backend_count() const
Get number of registered backends.
backend_registry(backend_registry &&)=delete
Abstract interface for database backends.
std::unique_ptr< database_backend >(*)() backend_factory_fn
Factory function type for creating database backends.
std::unique_ptr< database_backend > create_backend(const std::string &name)
Convenience function for creating backends (static method style)
bool has_backend(const std::string &name)
Convenience function for checking backend availability.
std::vector< std::string > available_backends()
Convenience function for getting available backends.