Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
kcenon::common::adapters::interface_adapter< Interface, Implementation > Class Template Reference

Interface adapter with type safety and depth tracking. More...

#include <adapter.h>

Inheritance diagram for kcenon::common::adapters::interface_adapter< Interface, Implementation >:
Inheritance graph
Collaboration diagram for kcenon::common::adapters::interface_adapter< Interface, Implementation >:
Collaboration graph

Public Member Functions

 interface_adapter (std::shared_ptr< Implementation > impl)
 Construct adapter with existing implementation.
 
virtual ~interface_adapter ()=default
 
std::shared_ptr< Implementation > unwrap () const
 Get the underlying implementation.
 
bool is_wrapped_adapter () const
 Check if this adapter wraps another adapter.
 
size_t get_wrapper_depth () const
 Get the current wrapper depth.
 
size_t get_adapter_depth () const override
 Get the adapter depth (implements adapter_base)
 
size_t get_type_id () const override
 Get unique type ID for this adapter type.
 
- Public Member Functions inherited from kcenon::common::adapters::adapter_base
virtual ~adapter_base ()=default
 
virtual bool is_adapter () const
 Check if this is an adapter (always true for adapter_base)
 

Static Public Member Functions

static std::string adapter_type_name ()
 Get type name for debugging.
 
static constexpr size_t max_depth ()
 Get the maximum allowed wrapper depth.
 
static size_t get_static_type_id ()
 Get static type ID for this adapter type.
 

Protected Attributes

std::shared_ptr< Implementation > impl_
 

Static Private Member Functions

static size_t generate_type_id ()
 Generate a unique type ID.
 
static size_t calculate_depth (std::shared_ptr< Implementation > impl)
 Calculate the depth of adapter wrapping.
 

Private Attributes

size_t wrapper_depth_
 

Static Private Attributes

static constexpr size_t max_wrapper_depth_ = 2
 

Detailed Description

template<typename Interface, typename Implementation>
class kcenon::common::adapters::interface_adapter< Interface, Implementation >

Interface adapter with type safety and depth tracking.

This template provides:

  • Type identification via type ID system (no RTTI)
  • Wrapper depth tracking to prevent infinite chains
  • Unwrap functionality to access underlying implementation
  • Maximum depth limit (default: 2) to prevent performance issues
Template Parameters
InterfaceThe interface type being adapted to
ImplementationThe concrete implementation being wrapped

Definition at line 306 of file adapter.h.

Constructor & Destructor Documentation

◆ interface_adapter()

template<typename Interface , typename Implementation >
kcenon::common::adapters::interface_adapter< Interface, Implementation >::interface_adapter ( std::shared_ptr< Implementation > impl)
inlineexplicit

Construct adapter with existing implementation.

Parameters
implShared pointer to the implementation
Exceptions
std::runtime_errorif wrapper depth exceeds maximum

Definition at line 313 of file adapter.h.

314 : impl_(impl), wrapper_depth_(calculate_depth(impl)) {
316 throw std::runtime_error(
317 "Adapter chain too deep (" + std::to_string(wrapper_depth_) +
318 " levels, max: " + std::to_string(max_wrapper_depth_) + ")");
319 }
320 }
std::shared_ptr< Implementation > impl_
Definition adapter.h:378
static size_t calculate_depth(std::shared_ptr< Implementation > impl)
Calculate the depth of adapter wrapping.
Definition adapter.h:398
static constexpr size_t max_wrapper_depth_
Definition adapter.h:382

References kcenon::common::adapters::interface_adapter< Interface, Implementation >::max_wrapper_depth_, and kcenon::common::adapters::interface_adapter< Interface, Implementation >::wrapper_depth_.

◆ ~interface_adapter()

template<typename Interface , typename Implementation >
virtual kcenon::common::adapters::interface_adapter< Interface, Implementation >::~interface_adapter ( )
virtualdefault

Member Function Documentation

◆ adapter_type_name()

template<typename Interface , typename Implementation >
static std::string kcenon::common::adapters::interface_adapter< Interface, Implementation >::adapter_type_name ( )
inlinestatic

Get type name for debugging.

Returns
Type name string

Definition at line 352 of file adapter.h.

352 {
353 return "interface_adapter<Interface, Implementation>";
354 }

◆ calculate_depth()

template<typename Interface , typename Implementation >
static size_t kcenon::common::adapters::interface_adapter< Interface, Implementation >::calculate_depth ( std::shared_ptr< Implementation > impl)
inlinestaticprivate

Calculate the depth of adapter wrapping.

Parameters
implThe implementation to check
Returns
Depth level (0 for direct implementation, 1+ for wrapped)

Definition at line 398 of file adapter.h.

398 {
399 if (!impl) {
400 return 0;
401 }
402
403 if constexpr (std::is_base_of_v<adapter_base, Implementation>) {
404#ifdef __cpp_rtti
405 if (auto* base_adapter =
406 dynamic_cast<adapter_base*>(impl.get())) {
407 return 1 + base_adapter->get_adapter_depth();
408 }
409 return 0;
410#else
411 auto* base_adapter = static_cast<adapter_base*>(impl.get());
412 if (base_adapter && base_adapter->get_type_id() != 0) {
413 return 1 + base_adapter->get_adapter_depth();
414 }
415 return 0;
416#endif
417 }
418
419 return 0;
420 }

◆ generate_type_id()

template<typename Interface , typename Implementation >
static size_t kcenon::common::adapters::interface_adapter< Interface, Implementation >::generate_type_id ( )
inlinestaticprivate

Generate a unique type ID.

Returns
Unique ID

Definition at line 388 of file adapter.h.

388 {
389 static std::atomic<size_t> counter{0};
390 return ++counter;
391 }

Referenced by kcenon::common::adapters::interface_adapter< Interface, Implementation >::get_static_type_id().

Here is the caller graph for this function:

◆ get_adapter_depth()

template<typename Interface , typename Implementation >
size_t kcenon::common::adapters::interface_adapter< Interface, Implementation >::get_adapter_depth ( ) const
inlineoverridevirtual

Get the adapter depth (implements adapter_base)

Returns
Wrapper depth

Implements kcenon::common::adapters::adapter_base.

Definition at line 346 of file adapter.h.

346{ return wrapper_depth_; }

References kcenon::common::adapters::interface_adapter< Interface, Implementation >::wrapper_depth_.

◆ get_static_type_id()

template<typename Interface , typename Implementation >
static size_t kcenon::common::adapters::interface_adapter< Interface, Implementation >::get_static_type_id ( )
inlinestatic

Get static type ID for this adapter type.

Returns
Type ID

Definition at line 372 of file adapter.h.

372 {
373 static const size_t id = generate_type_id();
374 return id;
375 }
static size_t generate_type_id()
Generate a unique type ID.
Definition adapter.h:388

References kcenon::common::adapters::interface_adapter< Interface, Implementation >::generate_type_id().

Referenced by kcenon::common::adapters::interface_adapter< Interface, Implementation >::get_type_id().

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

◆ get_type_id()

template<typename Interface , typename Implementation >
size_t kcenon::common::adapters::interface_adapter< Interface, Implementation >::get_type_id ( ) const
inlineoverridevirtual

Get unique type ID for this adapter type.

Returns
Type ID

Implements kcenon::common::adapters::adapter_base.

Definition at line 366 of file adapter.h.

366{ return get_static_type_id(); }
static size_t get_static_type_id()
Get static type ID for this adapter type.
Definition adapter.h:372

References kcenon::common::adapters::interface_adapter< Interface, Implementation >::get_static_type_id().

Here is the call graph for this function:

◆ get_wrapper_depth()

template<typename Interface , typename Implementation >
size_t kcenon::common::adapters::interface_adapter< Interface, Implementation >::get_wrapper_depth ( ) const
inline

Get the current wrapper depth.

Returns
Number of adapter layers (0 for direct implementation)

Definition at line 340 of file adapter.h.

340{ return wrapper_depth_; }

References kcenon::common::adapters::interface_adapter< Interface, Implementation >::wrapper_depth_.

◆ is_wrapped_adapter()

template<typename Interface , typename Implementation >
bool kcenon::common::adapters::interface_adapter< Interface, Implementation >::is_wrapped_adapter ( ) const
inline

Check if this adapter wraps another adapter.

Returns
true if wrapping another adapter, false otherwise

Definition at line 334 of file adapter.h.

334{ return wrapper_depth_ > 0; }

References kcenon::common::adapters::interface_adapter< Interface, Implementation >::wrapper_depth_.

◆ max_depth()

template<typename Interface , typename Implementation >
static constexpr size_t kcenon::common::adapters::interface_adapter< Interface, Implementation >::max_depth ( )
inlinestaticconstexpr

Get the maximum allowed wrapper depth.

Returns
Maximum depth value

Definition at line 360 of file adapter.h.

360{ return max_wrapper_depth_; }

References kcenon::common::adapters::interface_adapter< Interface, Implementation >::max_wrapper_depth_.

◆ unwrap()

template<typename Interface , typename Implementation >
std::shared_ptr< Implementation > kcenon::common::adapters::interface_adapter< Interface, Implementation >::unwrap ( ) const
inline

Get the underlying implementation.

Returns
Shared pointer to the wrapped implementation

Definition at line 328 of file adapter.h.

328{ return impl_; }

References kcenon::common::adapters::interface_adapter< Interface, Implementation >::impl_.

Member Data Documentation

◆ impl_

template<typename Interface , typename Implementation >
std::shared_ptr<Implementation> kcenon::common::adapters::interface_adapter< Interface, Implementation >::impl_
protected

◆ max_wrapper_depth_

template<typename Interface , typename Implementation >
size_t kcenon::common::adapters::interface_adapter< Interface, Implementation >::max_wrapper_depth_ = 2
staticconstexprprivate

◆ wrapper_depth_


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