Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
backend_base.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
36#pragma once
37
38#include "database_backend.h"
39#include "result.h"
40#include "../database_types.h"
41
42#include <kcenon/common/patterns/result.h>
43
44#include <memory>
45#include <atomic>
46
47namespace database
48{
49namespace core
50{
51
72template<typename Derived, database_types Type>
74{
75public:
81 backend_base() = default;
82
88 ~backend_base() override
89 {
90 shutdown();
91 }
92
93 // Prevent copying (backends own unique resources)
94 backend_base(const backend_base&) = delete;
96
97 // Prevent moving (std::atomic members are not moveable)
98 backend_base(backend_base&&) noexcept = delete;
99 backend_base& operator=(backend_base&&) noexcept = delete;
100
105 static std::unique_ptr<database_backend> create()
106 {
107 return std::make_unique<Derived>();
108 }
109
114 database_types type() const override
115 {
116 return Type;
117 }
118
127 kcenon::common::VoidResult initialize(const connection_config& config) override
128 {
129 if (initialized_) {
130 return kcenon::common::error_info{
131 static_cast<int>(database::error_code::invalid_state),
132 "Backend already initialized",
133 Derived::backend_name()
134 };
135 }
136
137 auto result = static_cast<Derived*>(this)->do_initialize(config);
138 if (result.is_ok()) {
139 initialized_ = true;
140 }
141 return result;
142 }
143
151 kcenon::common::VoidResult shutdown() override
152 {
153 if (!initialized_) {
154 return kcenon::common::ok();
155 }
156
157 auto result = static_cast<Derived*>(this)->do_shutdown();
158 initialized_ = false;
159 return result;
160 }
161
166 bool is_initialized() const override
167 {
168 return initialized_;
169 }
170
171protected:
172 std::atomic<bool> initialized_{false};
173};
174
175} // namespace core
176} // namespace database
CRTP template base class for database backends.
database_types type() const override
Get the database type of this backend.
kcenon::common::VoidResult shutdown() override
Shutdown the database backend gracefully.
std::atomic< bool > initialized_
Initialization state.
backend_base & operator=(const backend_base &)=delete
backend_base(backend_base &&) noexcept=delete
~backend_base() override
Virtual destructor.
bool is_initialized() const override
Check if backend is initialized and ready.
backend_base(const backend_base &)=delete
static std::unique_ptr< database_backend > create()
Factory method for backend_registry.
kcenon::common::VoidResult initialize(const connection_config &config) override
Initialize the database backend.
backend_base()=default
Default constructor.
Abstract base class for database backends.
Abstract interface for database backends.
Defines the enumeration of supported database types.
database_types
Represents various database backends or modes.
Result<T> type for database_system error handling.
Configuration for database connection.