Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::utils::startable_base< Derived > Class Template Reference

CRTP base class providing unified start/stop lifecycle management. More...

#include <startable_base.h>

Inheritance diagram for kcenon::network::utils::startable_base< Derived >:
Inheritance graph
Collaboration diagram for kcenon::network::utils::startable_base< Derived >:
Collaboration graph

Public Member Functions

auto is_running () const noexcept -> bool
 Checks if the component is currently running.
 
auto wait_for_stop () -> void
 Blocks until stop is called.
 

Protected Member Functions

 startable_base ()=default
 Default constructor.
 
 ~startable_base ()=default
 Destructor.
 
 startable_base (const startable_base &)=delete
 
startable_baseoperator= (const startable_base &)=delete
 
 startable_base (startable_base &&) noexcept=default
 
startable_baseoperator= (startable_base &&) noexcept=default
 
template<typename... Args>
auto do_start (Args &&... args) -> VoidResult
 Unified start operation with 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 get_lifecycle () -> lifecycle_manager &
 Gets the lifecycle manager for derived class access.
 
auto get_lifecycle () const -> const lifecycle_manager &
 Gets the lifecycle manager (const version).
 

Private Member Functions

auto reset_connection_state () -> void
 Resets connection state flags for a fresh start.
 
auto derived () -> Derived &
 CRTP accessor for derived class.
 
auto derived () const -> const Derived &
 CRTP accessor for derived class (const version).
 

Private Attributes

lifecycle_manager lifecycle_
 
std::atomic< bool > stop_initiated_ {false}
 

Detailed Description

template<typename Derived>
class kcenon::network::utils::startable_base< Derived >

CRTP base class providing unified start/stop lifecycle management.

This template extracts the common start/stop lifecycle pattern from client and server implementations. It handles:

  • Running state checks and transitions
  • Atomic flag management for stop prevention
  • Connection state reset
  • Error handling with state rollback
Template Parameters
DerivedThe derived class using CRTP pattern.

Required Derived Class Interface

The derived class must implement:

  • component_name() - Returns component identifier for error messages
  • do_start_impl(args...) - Protocol-specific start implementation
  • do_stop_impl() - Protocol-specific stop implementation
  • on_stopped() - Called after successful stop (optional callbacks)

Thread Safety

All lifecycle methods are thread-safe using atomic operations.

Usage Example

class my_client : public startable_base<my_client> {
public:
static constexpr std::string_view component_name() { return "MyClient"; }
auto start_client(std::string_view host, unsigned short port) -> VoidResult {
return do_start(host, port);
}
auto stop_client() -> VoidResult {
return do_stop();
}
protected:
friend class startable_base<my_client>;
auto do_start_impl(std::string_view host, unsigned short port) -> VoidResult {
// TCP-specific implementation
}
auto do_stop_impl() -> VoidResult {
// TCP-specific cleanup
}
auto on_stopped() -> void {
// Invoke disconnected callback
}
};
CRTP base class providing unified start/stop lifecycle management.
auto do_stop() -> VoidResult
Unified stop operation with lifecycle management.
auto do_start(Args &&... args) -> VoidResult
Unified start operation with lifecycle management.
startable_base()=default
Default constructor.
Result< std::monostate > VoidResult

Definition at line 89 of file startable_base.h.

Constructor & Destructor Documentation

◆ startable_base() [1/3]

template<typename Derived >
kcenon::network::utils::startable_base< Derived >::startable_base ( )
protecteddefault

Default constructor.

◆ ~startable_base()

template<typename Derived >
kcenon::network::utils::startable_base< Derived >::~startable_base ( )
protecteddefault

Destructor.

◆ startable_base() [2/3]

template<typename Derived >
kcenon::network::utils::startable_base< Derived >::startable_base ( const startable_base< Derived > & )
protecteddelete

◆ startable_base() [3/3]

template<typename Derived >
kcenon::network::utils::startable_base< Derived >::startable_base ( startable_base< Derived > && )
protecteddefaultnoexcept

Member Function Documentation

◆ derived() [1/2]

template<typename Derived >
auto kcenon::network::utils::startable_base< Derived >::derived ( ) -> Derived&
inlinenodiscardprivate

CRTP accessor for derived class.

Returns
Reference to the derived class instance.

Definition at line 242 of file startable_base.h.

243 {
244 return static_cast<Derived&>(*this);
245 }

Referenced by kcenon::network::utils::startable_base< Derived >::do_start(), and kcenon::network::utils::startable_base< Derived >::do_stop().

Here is the caller graph for this function:

◆ derived() [2/2]

template<typename Derived >
auto kcenon::network::utils::startable_base< Derived >::derived ( ) const -> const Derived&
inlinenodiscardprivate

CRTP accessor for derived class (const version).

Returns
Const reference to the derived class instance.

Definition at line 251 of file startable_base.h.

252 {
253 return static_cast<const Derived&>(*this);
254 }

◆ do_start()

template<typename Derived >
template<typename... Args>
auto kcenon::network::utils::startable_base< Derived >::do_start ( Args &&... args) -> VoidResult
inlinenodiscardprotected

Unified start operation with lifecycle management.

Template Parameters
ArgsThe argument types for the start operation.
Parameters
argsThe arguments to pass to do_start_impl.
Returns
Result<void> - Success if started, or error with code:

State Transitions

  1. Check if already running -> return error
  2. Set running state
  3. Reset connection state flags
  4. Call derived's do_start_impl
  5. On failure: rollback to stopped state

Definition at line 144 of file startable_base.h.

145 {
146 if (!lifecycle_.try_start())
147 {
148 return error_void(
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 }
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 derived() -> Derived &
CRTP accessor for derived class.
auto reset_connection_state() -> void
Resets connection state flags for a fresh start.
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")

References kcenon::network::utils::startable_base< Derived >::derived(), kcenon::network::utils::startable_base< Derived >::lifecycle_, kcenon::network::utils::lifecycle_manager::mark_stopped(), kcenon::network::utils::startable_base< Derived >::reset_connection_state(), and kcenon::network::utils::lifecycle_manager::try_start().

Here is the call graph for this function:

◆ do_stop()

template<typename Derived >
auto kcenon::network::utils::startable_base< Derived >::do_stop ( ) -> VoidResult
inlinenodiscardprotected

Unified stop operation with lifecycle management.

Returns
Result<void> - Success if stopped (or already stopped).

State Transitions

  1. If not running -> return ok (idempotent)
  2. Prevent multiple concurrent stops via atomic flag
  3. Call derived's do_stop_impl
  4. Mark as stopped
  5. Call derived's on_stopped for cleanup callbacks

Definition at line 178 of file startable_base.h.

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 }
auto is_running() const -> bool
Checks if the component is currently running.
VoidResult ok()

References kcenon::network::utils::startable_base< Derived >::derived(), kcenon::network::utils::lifecycle_manager::is_running(), kcenon::network::utils::startable_base< Derived >::lifecycle_, kcenon::network::utils::lifecycle_manager::mark_stopped(), kcenon::network::ok(), and kcenon::network::utils::startable_base< Derived >::stop_initiated_.

Here is the call graph for this function:

◆ get_lifecycle() [1/2]

template<typename Derived >
auto kcenon::network::utils::startable_base< Derived >::get_lifecycle ( ) -> lifecycle_manager&
inlinenodiscardprotected

Gets the lifecycle manager for derived class access.

Returns
Reference to the lifecycle manager.

Definition at line 215 of file startable_base.h.

216 {
217 return lifecycle_;
218 }

References kcenon::network::utils::startable_base< Derived >::lifecycle_.

◆ get_lifecycle() [2/2]

template<typename Derived >
auto kcenon::network::utils::startable_base< Derived >::get_lifecycle ( ) const -> const lifecycle_manager&
inlinenodiscardprotected

Gets the lifecycle manager (const version).

Returns
Const reference to the lifecycle manager.

Definition at line 224 of file startable_base.h.

225 {
226 return lifecycle_;
227 }

References kcenon::network::utils::startable_base< Derived >::lifecycle_.

◆ is_running()

template<typename Derived >
auto kcenon::network::utils::startable_base< Derived >::is_running ( ) const -> bool
inlinenodiscardnoexcept

Checks if the component is currently running.

Returns
true if running, false otherwise.

Definition at line 96 of file startable_base.h.

97 {
98 return lifecycle_.is_running();
99 }

References kcenon::network::utils::lifecycle_manager::is_running(), and kcenon::network::utils::startable_base< Derived >::lifecycle_.

Referenced by kcenon::network::core::messaging_client::is_running().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_stop_initiated()

template<typename Derived >
auto kcenon::network::utils::startable_base< Derived >::is_stop_initiated ( ) const -> bool
inlinenodiscardprotectednoexcept

Checks if stop has been initiated.

Returns
true if stop is in progress, false otherwise.

Definition at line 206 of file startable_base.h.

207 {
208 return stop_initiated_.load(std::memory_order_acquire);
209 }

References kcenon::network::utils::startable_base< Derived >::stop_initiated_.

◆ operator=() [1/2]

template<typename Derived >
startable_base & kcenon::network::utils::startable_base< Derived >::operator= ( const startable_base< Derived > & )
protecteddelete

◆ operator=() [2/2]

template<typename Derived >
startable_base & kcenon::network::utils::startable_base< Derived >::operator= ( startable_base< Derived > && )
protecteddefaultnoexcept

◆ reset_connection_state()

template<typename Derived >
auto kcenon::network::utils::startable_base< Derived >::reset_connection_state ( ) -> void
inlineprivate

Resets connection state flags for a fresh start.

Definition at line 233 of file startable_base.h.

234 {
235 stop_initiated_.store(false, std::memory_order_release);
236 }

References kcenon::network::utils::startable_base< Derived >::stop_initiated_.

Referenced by kcenon::network::utils::startable_base< Derived >::do_start().

Here is the caller graph for this function:

◆ wait_for_stop()

template<typename Derived >
auto kcenon::network::utils::startable_base< Derived >::wait_for_stop ( ) -> void
inline

Blocks until stop is called.

Definition at line 104 of file startable_base.h.

105 {
107 }
auto wait_for_stop() -> void
Blocks until the component has stopped.

References kcenon::network::utils::startable_base< Derived >::lifecycle_, and kcenon::network::utils::lifecycle_manager::wait_for_stop().

Referenced by kcenon::network::core::messaging_client::wait_for_stop().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ lifecycle_

◆ stop_initiated_

template<typename Derived >
std::atomic<bool> kcenon::network::utils::startable_base< Derived >::stop_initiated_ {false}
private

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