Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
service_registration.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
15#pragma once
16
18
19#include <memory>
20
21#if KCENON_WITH_COMMON_SYSTEM
22
23#include <kcenon/common/di/service_container.h>
24
25#include "../core/network_context.h"
26
27namespace kcenon::network::di {
28
32struct network_registration_config {
34 size_t thread_count = 0;
35
37 bool initialize_on_register = true;
38
40 common::di::service_lifetime lifetime = common::di::service_lifetime::singleton;
41};
42
67 });
68 * @endcode
69 */
70inline common::VoidResult register_network_services(
71 common::di::IServiceContainer& container,
72 const network_registration_config& config = {}) {
73
74 // Check if already registered
75 if (container.is_registered<kcenon::network::core::network_context>()) {
76 return common::make_error<std::monostate>(
77 common::di::di_error_codes::already_registered,
78 "network_context is already registered",
79 "kcenon::network::di"
80 );
81 }
82
83 // Register network_context factory
84 // Note: network_context is a singleton, so we return a reference wrapper
85 return container.register_factory<kcenon::network::core::network_context>(
86 [config](common::di::IServiceContainer&) -> std::shared_ptr<kcenon::network::core::network_context> {
87 // Get the singleton instance
89
90 // Initialize if requested and not already initialized
91 if (config.initialize_on_register && !ctx.is_initialized()) {
92 ctx.initialize(config.thread_count);
93 }
94
95 // Return a non-owning shared_ptr (aliasing constructor)
96 // This allows the context to be managed via DI without changing ownership
97 return std::shared_ptr<kcenon::network::core::network_context>(
98 std::shared_ptr<void>{}, &ctx);
99 },
100 config.lifetime
101 );
102}
103
113inline common::VoidResult unregister_network_services(
114 common::di::IServiceContainer& container) {
115
116 return container.unregister<kcenon::network::core::network_context>();
117}
118
128inline common::VoidResult shutdown_network_services(
129 common::di::IServiceContainer& container) {
130
131 // Shutdown the singleton
133
134 // Unregister from container
135 return unregister_network_services(container);
136}
137
146inline kcenon::network::core::network_context& get_network_context() {
148}
149
159inline common::VoidResult register_all_network_services(
160 common::di::IServiceContainer& container,
161 const network_registration_config& config = {}) {
162
163 // Register network_context
164 auto result = register_network_services(container, config);
165 if (result.is_err()) {
166 return result;
167 }
168
169 return common::VoidResult::ok({});
170}
171
172} // namespace kcenon::network::di
173
174#endif // KCENON_WITH_COMMON_SYSTEM
Global context for shared network system resources.
Definition core.cppm:252
static network_context & instance()
Get the singleton instance.
Public header for compile-time feature flags.
tracing_config config
Definition exporters.cpp:29