Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
startable_base.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
12#pragma once
13
22#include <atomic>
23#include <string>
24#include <string_view>
25#include <type_traits>
26#include <utility>
27
30
32{
33
88 template<typename Derived>
90 {
91 public:
96 [[nodiscard]] auto is_running() const noexcept -> bool
97 {
98 return lifecycle_.is_running();
99 }
100
104 auto wait_for_stop() -> void
105 {
107 }
108
109 protected:
113 startable_base() = default;
114
118 ~startable_base() = default;
119
120 // Non-copyable
123
124 // Movable
125 startable_base(startable_base&&) noexcept = default;
126 startable_base& operator=(startable_base&&) noexcept = default;
127
143 template<typename... Args>
144 [[nodiscard]] auto do_start(Args&&... args) -> VoidResult
145 {
146 if (!lifecycle_.try_start())
147 {
148 return error_void(
149 error_codes::common_errors::already_exists,
150 std::string(derived().component_name()) + " is already running",
151 "startable_base::do_start",
152 std::string(derived().component_name()));
153 }
154
156
157 auto result = derived().do_start_impl(std::forward<Args>(args)...);
158
159 if (result.is_err())
160 {
162 }
163
164 return result;
165 }
166
178 [[nodiscard]] auto do_stop() -> VoidResult
179 {
180 if (!lifecycle_.is_running())
181 {
182 return ok();
183 }
184
185 // Prevent multiple stop calls
186 bool expected = false;
187 if (!stop_initiated_.compare_exchange_strong(expected, true,
188 std::memory_order_acq_rel))
189 {
190 return ok();
191 }
192
193 auto result = derived().do_stop_impl();
195
196 // Call derived class's on_stopped hook for callbacks
197 derived().on_stopped();
198
199 return result;
200 }
201
206 [[nodiscard]] auto is_stop_initiated() const noexcept -> bool
207 {
208 return stop_initiated_.load(std::memory_order_acquire);
209 }
210
215 [[nodiscard]] auto get_lifecycle() -> lifecycle_manager&
216 {
217 return lifecycle_;
218 }
219
224 [[nodiscard]] auto get_lifecycle() const -> const lifecycle_manager&
225 {
226 return lifecycle_;
227 }
228
229 private:
234 {
235 stop_initiated_.store(false, std::memory_order_release);
236 }
237
242 [[nodiscard]] auto derived() -> Derived&
243 {
244 return static_cast<Derived&>(*this);
245 }
246
251 [[nodiscard]] auto derived() const -> const Derived&
252 {
253 return static_cast<const Derived&>(*this);
254 }
255
257 std::atomic<bool> stop_initiated_{false};
258 };
259
260} // namespace kcenon::network::utils
Thread-safe lifecycle state management for network components.
auto try_start() -> bool
Attempts to transition from stopped to running state.
auto mark_stopped() -> void
Marks the component as stopped and signals waiters.
auto is_running() const -> bool
Checks if the component is currently running.
auto wait_for_stop() -> void
Blocks until the component has stopped.
CRTP base class providing unified start/stop lifecycle management.
auto do_stop() -> VoidResult
Unified stop operation with lifecycle management.
auto is_stop_initiated() const noexcept -> bool
Checks if stop has been initiated.
auto is_running() const noexcept -> bool
Checks if the component is currently running.
auto do_start(Args &&... args) -> VoidResult
Unified start operation with lifecycle management.
auto get_lifecycle() const -> const lifecycle_manager &
Gets the lifecycle manager (const version).
auto derived() -> Derived &
CRTP accessor for derived class.
auto wait_for_stop() -> void
Blocks until stop is called.
auto derived() const -> const Derived &
CRTP accessor for derived class (const version).
startable_base(const startable_base &)=delete
startable_base()=default
Default constructor.
startable_base & operator=(const startable_base &)=delete
auto get_lifecycle() -> lifecycle_manager &
Gets the lifecycle manager for derived class access.
startable_base(startable_base &&) noexcept=default
auto reset_connection_state() -> void
Resets connection state flags for a fresh start.
Component lifecycle management (start, stop, restart).
Utility components for network_system.
VoidResult ok()
Network-specific error and result type definitions.