Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
database::core::backend_registry Class Reference

Singleton registry for database backend plugins. More...

#include <backend_registry.h>

Collaboration diagram for database::core::backend_registry:
Collaboration graph

Public Member Functions

 backend_registry (const backend_registry &)=delete
 
backend_registryoperator= (const backend_registry &)=delete
 
 backend_registry (backend_registry &&)=delete
 
backend_registryoperator= (backend_registry &&)=delete
 
kcenon::common::VoidResult register_backend (const std::string &name, backend_factory_fn factory)
 Register a backend factory function.
 
kcenon::common::VoidResult unregister_backend (const std::string &name)
 Unregister a backend (for testing or dynamic unloading)
 
std::unique_ptr< database_backendcreate (const std::string &name) const
 Create a backend instance by name.
 
bool has_backend (const std::string &name) const
 Check if a backend is registered.
 
std::vector< std::string > available_backends () const
 Get list of all registered backend names.
 
size_t backend_count () const
 Get number of registered backends.
 
void clear ()
 Clear all registered backends (for testing)
 

Static Public Member Functions

static backend_registryinstance ()
 Get the singleton instance.
 

Private Member Functions

 backend_registry ()=default
 
 ~backend_registry ()=default
 

Private Attributes

std::mutex mutex_
 
std::map< std::string, backend_factory_fnfactories_
 

Detailed Description

Singleton registry for database backend plugins.

Thread Safety:

  • All public methods are thread-safe
  • Uses internal mutex for synchronization
  • Safe for concurrent registration and creation

Singleton Pattern:

  • Uses Meyer's singleton (thread-safe in C++11+)
  • No manual memory management required
  • Automatic cleanup on program exit

Definition at line 72 of file backend_registry.h.

Constructor & Destructor Documentation

◆ backend_registry() [1/3]

database::core::backend_registry::backend_registry ( const backend_registry & )
delete

◆ backend_registry() [2/3]

database::core::backend_registry::backend_registry ( backend_registry && )
delete

◆ backend_registry() [3/3]

database::core::backend_registry::backend_registry ( )
privatedefault

◆ ~backend_registry()

database::core::backend_registry::~backend_registry ( )
privatedefault

Member Function Documentation

◆ available_backends()

std::vector< std::string > database::core::backend_registry::available_backends ( ) const

Get list of all registered backend names.

Returns
Vector of backend names

Thread Safety: This method is thread-safe

Example:

for (const auto& name : backends) {
std::cout << "Available: " << name << std::endl;
}
std::vector< std::string > available_backends() const
Get list of all registered backend names.
static backend_registry & instance()
Get the singleton instance.

Definition at line 86 of file backend_registry.cpp.

87{
88 std::lock_guard<std::mutex> lock(mutex_);
89
90 std::vector<std::string> names;
91 names.reserve(factories_.size());
92
93 for (const auto& pair : factories_)
94 {
95 names.push_back(pair.first);
96 }
97
98 // Sort for consistent output
99 std::sort(names.begin(), names.end());
100
101 return names;
102}
std::map< std::string, backend_factory_fn > factories_

References factories_, and mutex_.

Referenced by database::core::available_backends(), TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ backend_count()

size_t database::core::backend_registry::backend_count ( ) const

Get number of registered backends.

Returns
Count of registered backends

Thread Safety: This method is thread-safe

Definition at line 104 of file backend_registry.cpp.

105{
106 std::lock_guard<std::mutex> lock(mutex_);
107 return factories_.size();
108}

References factories_, and mutex_.

Referenced by TEST_F().

Here is the caller graph for this function:

◆ clear()

void database::core::backend_registry::clear ( )

Clear all registered backends (for testing)

Thread Safety: This method is thread-safe

Warning: This should only be used in test code. It will remove all registered backends including built-in ones.

Definition at line 110 of file backend_registry.cpp.

111{
112 std::lock_guard<std::mutex> lock(mutex_);
113 factories_.clear();
114}

References factories_, and mutex_.

Referenced by BackendRegistryTest::SetUp(), BackendRegistryTest::TearDown(), TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ create()

std::unique_ptr< database_backend > database::core::backend_registry::create ( const std::string & name) const

Create a backend instance by name.

Parameters
nameBackend name (e.g., "postgresql", "sqlite")
Returns
Unique pointer to backend, or nullptr if not found

Thread Safety: This method is thread-safe

Example:

auto backend = backend_registry::instance().create("postgresql");
if (!backend) {
std::cerr << "PostgreSQL backend not available" << std::endl;
}
std::unique_ptr< database_backend > create(const std::string &name) const
Create a backend instance by name.

Definition at line 59 of file backend_registry.cpp.

60{
61 std::lock_guard<std::mutex> lock(mutex_);
62
63 auto it = factories_.find(name);
64 if (it == factories_.end())
65 {
66 return nullptr;
67 }
68
69 try
70 {
71 return it->second();
72 }
73 catch (...)
74 {
75 // Factory function threw an exception
76 return nullptr;
77 }
78}

References factories_, and mutex_.

Referenced by database::core::create_backend(), database::integrated::create_backend(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ has_backend()

bool database::core::backend_registry::has_backend ( const std::string & name) const

Check if a backend is registered.

Parameters
nameBackend name to check
Returns
true if backend is registered

Thread Safety: This method is thread-safe

Definition at line 80 of file backend_registry.cpp.

81{
82 std::lock_guard<std::mutex> lock(mutex_);
83 return factories_.find(name) != factories_.end();
84}

References factories_, and mutex_.

Referenced by database::core::has_backend().

Here is the caller graph for this function:

◆ instance()

◆ operator=() [1/2]

backend_registry & database::core::backend_registry::operator= ( backend_registry && )
delete

◆ operator=() [2/2]

backend_registry & database::core::backend_registry::operator= ( const backend_registry & )
delete

◆ register_backend()

kcenon::common::VoidResult database::core::backend_registry::register_backend ( const std::string & name,
backend_factory_fn factory )

Register a backend factory function.

Parameters
nameBackend name (e.g., "postgresql", "sqlite")
factoryFactory function that creates backend instances
Returns
VoidResult::ok() on success, error if name already registered

Thread Safety: This method is thread-safe

Example:

"postgresql",
&postgresql_backend::create
);
kcenon::common::VoidResult register_backend(const std::string &name, backend_factory_fn factory)
Register a backend factory function.

Definition at line 20 of file backend_registry.cpp.

22{
23 if (name.empty())
24 {
25 return kcenon::common::error_info{1, "Backend name cannot be empty", "backend_registry"};
26 }
27
28 if (!factory)
29 {
30 return kcenon::common::error_info{2, "Factory function cannot be null", "backend_registry"};
31 }
32
33 std::lock_guard<std::mutex> lock(mutex_);
34
35 // Check if backend already registered
36 if (factories_.find(name) != factories_.end())
37 {
38 return kcenon::common::error_info{3, "Backend '" + name + "' is already registered", "backend_registry"};
39 }
40
41 factories_[name] = factory;
42 return kcenon::common::VoidResult::ok(std::monostate{});
43}

References factories_, and mutex_.

Referenced by database::core::backend_registrar< BackendType >::backend_registrar(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ unregister_backend()

kcenon::common::VoidResult database::core::backend_registry::unregister_backend ( const std::string & name)

Unregister a backend (for testing or dynamic unloading)

Parameters
nameBackend name to unregister
Returns
VoidResult::ok() on success, error if not found

Thread Safety: This method is thread-safe

Definition at line 45 of file backend_registry.cpp.

46{
47 std::lock_guard<std::mutex> lock(mutex_);
48
49 auto it = factories_.find(name);
50 if (it == factories_.end())
51 {
52 return kcenon::common::error_info{4, "Backend '" + name + "' not found", "backend_registry"};
53 }
54
55 factories_.erase(it);
56 return kcenon::common::VoidResult::ok(std::monostate{});
57}

References factories_, and mutex_.

Referenced by TEST_F(), TEST_F(), and TEST_F().

Here is the caller graph for this function:

Member Data Documentation

◆ factories_

std::map<std::string, backend_factory_fn> database::core::backend_registry::factories_
private

◆ mutex_

std::mutex database::core::backend_registry::mutex_
mutableprivate

The documentation for this class was generated from the following files: