Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
kcenon::common::adapters::smart_adapter_factory Class Reference

Smart adapter factory that avoids unnecessary wrapping. More...

#include <smart_adapter.h>

Collaboration diagram for kcenon::common::adapters::smart_adapter_factory:
Collaboration graph

Static Public Member Functions

template<typename Interface , typename Impl >
static std::shared_ptr< Interface > make_adapter (std::shared_ptr< Impl > impl)
 Create an adapter with automatic optimization.
 
template<typename AdapterType , typename... Args>
static std::shared_ptr< AdapterType > make_explicit_adapter (Args &&... args)
 Create an adapter with explicit adapter type.
 
template<typename T , typename Interface >
static std::shared_ptr< T > try_unwrap (std::shared_ptr< Interface > ptr)
 Try to unwrap an interface to get underlying implementation.
 
template<typename Interface , typename Impl >
static constexpr bool is_zero_cost ()
 Check if zero-cost adaptation is possible.
 

Detailed Description

Smart adapter factory that avoids unnecessary wrapping.

This factory uses compile-time checks to determine if adaptation is needed:

  • If the implementation already implements the interface, direct cast (zero-cost)
  • Otherwise, create an interface_adapter wrapper

This provides zero-cost abstraction when possible.

Note
Consider using adapter_factory from adapter.h for new code

Definition at line 46 of file smart_adapter.h.

Member Function Documentation

◆ is_zero_cost()

template<typename Interface , typename Impl >
static constexpr bool kcenon::common::adapters::smart_adapter_factory::is_zero_cost ( )
inlinestaticconstexpr

Check if zero-cost adaptation is possible.

Template Parameters
InterfaceThe target interface
ImplThe implementation type
Returns
true if direct cast is possible (zero-cost)

Definition at line 120 of file smart_adapter.h.

120 {
122 }
constexpr bool implements_interface_v
Type trait to check if a type implements an interface.
Definition adapter.h:429

References kcenon::common::adapters::implements_interface_v.

◆ make_adapter()

template<typename Interface , typename Impl >
static std::shared_ptr< Interface > kcenon::common::adapters::smart_adapter_factory::make_adapter ( std::shared_ptr< Impl > impl)
inlinestatic

Create an adapter with automatic optimization.

If Impl already implements Interface, returns a static_pointer_cast (zero-cost). Otherwise, creates a typed_adapter wrapper.

Template Parameters
InterfaceThe target interface type
ImplThe implementation type
Parameters
implShared pointer to the implementation
Returns
Shared pointer to Interface (either direct cast or adapter)

Definition at line 60 of file smart_adapter.h.

60 {
62 // Zero-cost: Direct cast, no wrapper needed
63 return std::static_pointer_cast<Interface>(impl);
64 } else {
65 // Create adapter wrapper
66 return std::make_shared<interface_adapter<Interface, Impl>>(impl);
67 }
68 }

References kcenon::common::adapters::implements_interface_v.

Referenced by kcenon::common::adapters::make_smart_adapter().

Here is the caller graph for this function:

◆ make_explicit_adapter()

template<typename AdapterType , typename... Args>
static std::shared_ptr< AdapterType > kcenon::common::adapters::smart_adapter_factory::make_explicit_adapter ( Args &&... args)
inlinestatic

Create an adapter with explicit adapter type.

Always creates the specified adapter type, even if zero-cost cast is possible. Use this when you need specific adapter behavior beyond simple interface conversion.

Template Parameters
AdapterTypeThe specific adapter class to use
ArgsConstructor argument types
Parameters
argsArguments to forward to adapter constructor
Returns
Shared pointer to the created adapter

Definition at line 82 of file smart_adapter.h.

82 {
83 return std::make_shared<AdapterType>(std::forward<Args>(args)...);
84 }

◆ try_unwrap()

template<typename T , typename Interface >
static std::shared_ptr< T > kcenon::common::adapters::smart_adapter_factory::try_unwrap ( std::shared_ptr< Interface > ptr)
inlinestatic

Try to unwrap an interface to get underlying implementation.

If the interface is a interface_adapter, unwraps it. Otherwise, returns nullptr (strict type safety without RTTI).

Template Parameters
TThe expected underlying type
InterfaceThe interface type
Parameters
ptrShared pointer to the interface
Returns
Shared pointer to T, or nullptr if conversion fails

Definition at line 98 of file smart_adapter.h.

98 {
99 if (!ptr) {
100 return nullptr;
101 }
102
103 // Try safe_unwrap (for interface_adapter)
104 if (auto unwrapped = safe_unwrap<T>(ptr)) {
105 return unwrapped;
106 }
107
108 // Not an adapter - return nullptr for strict type safety
109 // (removed dynamic_pointer_cast to eliminate RTTI dependency)
110 return nullptr;
111 }
std::shared_ptr< T > safe_unwrap(std::shared_ptr< Interface > ptr)
Helper function to safely unwrap an adapter.
Definition adapter.h:530

References kcenon::common::adapters::safe_unwrap().

Here is the call graph for this function:

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