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

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

#include <adapter.h>

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

Static Public Member Functions

template<typename Interface , typename Impl >
static std::shared_ptr< Interface > create (std::shared_ptr< Impl > impl)
 Create an adapter with automatic optimization.
 
template<typename AdapterType , typename... Args>
static std::shared_ptr< AdapterType > create_explicit (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

Definition at line 440 of file adapter.h.

Member Function Documentation

◆ create()

template<typename Interface , typename Impl >
static std::shared_ptr< Interface > kcenon::common::adapters::adapter_factory::create ( 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 an interface_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 455 of file adapter.h.

455 {
457 return std::static_pointer_cast<Interface>(impl);
458 } else {
459 return std::make_shared<interface_adapter<Interface, Impl>>(impl);
460 }
461 }
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.

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

Here is the caller graph for this function:

◆ create_explicit()

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

Create an adapter with explicit adapter type.

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 472 of file adapter.h.

472 {
473 return std::make_shared<AdapterType>(std::forward<Args>(args)...);
474 }

◆ is_zero_cost()

template<typename Interface , typename Impl >
static constexpr bool kcenon::common::adapters::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 517 of file adapter.h.

517 {
519 }

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

◆ try_unwrap()

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

Try to unwrap an interface to get underlying implementation.

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 485 of file adapter.h.

485 {
486 if (!ptr) {
487 return nullptr;
488 }
489
490 if constexpr (std::is_base_of_v<adapter_base, Interface>) {
491#ifdef __cpp_rtti
492 if (auto* adapter =
493 dynamic_cast<interface_adapter<Interface, T>*>(ptr.get())) {
494 return adapter->unwrap();
495 }
496#else
497 auto* base = static_cast<adapter_base*>(ptr.get());
498 if (base && base->get_type_id() ==
500 auto* adapter =
501 static_cast<interface_adapter<Interface, T>*>(ptr.get());
502 return adapter->unwrap();
503 }
504#endif
505 }
506
507 return nullptr;
508 }
static size_t get_static_type_id()
Get static type ID for this adapter type.
Definition adapter.h:372
adapter(T) -> adapter< T >

Referenced by kcenon::common::adapters::safe_unwrap(), and kcenon::common::adapters::unwrap_adapter().

Here is the caller graph for this function:

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