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

Classes

class  adapter
 Unified adapter template for wrapping values or smart pointers. More...
 
class  adapter_base
 Base class for adapter interface to eliminate RTTI dependency. More...
 
class  adapter_factory
 Smart adapter factory that avoids unnecessary wrapping. More...
 
struct  adapter_traits
 Primary adapter_traits template for value types. More...
 
struct  adapter_traits< std::shared_ptr< T > >
 Specialization for std::shared_ptr. More...
 
struct  adapter_traits< std::unique_ptr< T, Deleter > >
 Specialization for std::unique_ptr. More...
 
class  interface_adapter
 Interface adapter with type safety and depth tracking. More...
 
class  smart_adapter_factory
 Smart adapter factory that avoids unnecessary wrapping. More...
 

Functions

template<typename T >
 adapter (T) -> adapter< T >
 
template<typename T >
 adapter (std::shared_ptr< T >) -> adapter< std::shared_ptr< T > >
 
template<typename T , typename D >
 adapter (std::unique_ptr< T, D >) -> adapter< std::unique_ptr< T, D > >
 
template<typename T , typename... Args>
auto make_adapter (Args &&... args)
 Factory function to create adapter with in-place construction.
 
template<typename T , typename... Args>
auto make_shared_adapter (Args &&... args)
 Factory function to create adapter wrapping shared_ptr.
 
template<typename T , typename... Args>
auto make_unique_adapter (Args &&... args)
 Factory function to create adapter wrapping unique_ptr.
 
template<typename T , typename Interface >
std::shared_ptr< T > safe_unwrap (std::shared_ptr< Interface > ptr)
 Helper function to safely unwrap an adapter.
 
template<typename Interface >
bool is_adapter (std::shared_ptr< Interface > ptr)
 Check if an interface pointer is actually an adapter.
 
template<typename Interface , typename Impl >
std::shared_ptr< Interface > make_interface_adapter (std::shared_ptr< Impl > impl)
 Convenience function for creating smart adapters.
 
template<typename T , typename Interface >
std::shared_ptr< T > unwrap_adapter (std::shared_ptr< Interface > ptr)
 Convenience function for unwrapping adapters.
 
template<typename Interface , typename Impl >
std::shared_ptr< Interface > make_smart_adapter (std::shared_ptr< Impl > impl)
 Convenience function for creating smart adapters.
 

Variables

template<typename T , typename Interface >
constexpr bool implements_interface_v
 Type trait to check if a type implements an interface.
 

Function Documentation

◆ adapter() [1/3]

template<typename T >
kcenon::common::adapters::adapter ( std::shared_ptr< T > ) -> adapter< std::shared_ptr< T > >

◆ adapter() [2/3]

template<typename T , typename D >
kcenon::common::adapters::adapter ( std::unique_ptr< T, D > ) -> adapter< std::unique_ptr< T, D > >

◆ adapter() [3/3]

template<typename T >
kcenon::common::adapters::adapter ( T ) -> adapter< T >

Referenced by main(), make_adapter(), make_shared_adapter(), and make_unique_adapter().

Here is the caller graph for this function:

◆ is_adapter()

template<typename Interface >
bool kcenon::common::adapters::is_adapter ( std::shared_ptr< Interface > ptr)

Check if an interface pointer is actually an adapter.

Template Parameters
InterfaceThe interface type
Parameters
ptrShared pointer to check
Returns
true if ptr is an adapter, false otherwise

Definition at line 541 of file adapter.h.

541 {
542 if (!ptr) {
543 return false;
544 }
545
546 if constexpr (std::is_base_of_v<adapter_base, Interface>) {
547 auto* base = static_cast<adapter_base*>(ptr.get());
548 return base->is_adapter();
549 }
550
551 return false;
552}

◆ make_adapter()

template<typename T , typename... Args>
auto kcenon::common::adapters::make_adapter ( Args &&... args)

Factory function to create adapter with in-place construction.

Template Parameters
TThe type to construct
ArgsConstructor argument types
Parameters
argsArguments to forward to T's constructor
Returns
adapter<T>

Definition at line 263 of file adapter.h.

263 {
264 return adapter<T>(T(std::forward<Args>(args)...));
265}
Unified adapter template for wrapping values or smart pointers.
Definition adapter.h:118

References adapter().

Here is the call graph for this function:

◆ make_interface_adapter()

template<typename Interface , typename Impl >
std::shared_ptr< Interface > kcenon::common::adapters::make_interface_adapter ( std::shared_ptr< Impl > impl)

Convenience function for creating smart adapters.

Template Parameters
InterfaceThe target interface type
ImplThe implementation type (deduced)
Parameters
implShared pointer to implementation
Returns
Shared pointer to Interface

Definition at line 563 of file adapter.h.

563 {
564 return adapter_factory::create<Interface, Impl>(impl);
565}

References kcenon::common::adapters::adapter_factory::create().

Here is the call graph for this function:

◆ make_shared_adapter()

template<typename T , typename... Args>
auto kcenon::common::adapters::make_shared_adapter ( Args &&... args)

Factory function to create adapter wrapping shared_ptr.

Template Parameters
TThe type to manage
ArgsConstructor argument types
Parameters
argsArguments to forward to T's constructor
Returns
adapter<std::shared_ptr<T>>

Definition at line 276 of file adapter.h.

276 {
277 return adapter(std::make_shared<T>(std::forward<Args>(args)...));
278}

References adapter().

Referenced by main().

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

◆ make_smart_adapter()

template<typename Interface , typename Impl >
std::shared_ptr< Interface > kcenon::common::adapters::make_smart_adapter ( std::shared_ptr< Impl > impl)

Convenience function for creating smart adapters.

Template Parameters
InterfaceThe target interface type
ImplThe implementation type (deduced)
Parameters
implShared pointer to implementation
Returns
Shared pointer to Interface
auto executor = make_smart_adapter<IExecutor>(thread_pool);
// If thread_pool implements IExecutor: zero-cost cast
// Otherwise: creates interface_adapter<IExecutor, thread_pool>
std::shared_ptr< Interface > make_smart_adapter(std::shared_ptr< Impl > impl)
Convenience function for creating smart adapters.

Definition at line 140 of file smart_adapter.h.

140 {
141 return smart_adapter_factory::make_adapter<Interface, Impl>(impl);
142}

References kcenon::common::adapters::smart_adapter_factory::make_adapter().

Here is the call graph for this function:

◆ make_unique_adapter()

template<typename T , typename... Args>
auto kcenon::common::adapters::make_unique_adapter ( Args &&... args)

Factory function to create adapter wrapping unique_ptr.

Template Parameters
TThe type to manage
ArgsConstructor argument types
Parameters
argsArguments to forward to T's constructor
Returns
adapter<std::unique_ptr<T>>

Definition at line 289 of file adapter.h.

289 {
290 return adapter(std::make_unique<T>(std::forward<Args>(args)...));
291}

References adapter().

Here is the call graph for this function:

◆ safe_unwrap()

template<typename T , typename Interface >
std::shared_ptr< T > kcenon::common::adapters::safe_unwrap ( std::shared_ptr< Interface > ptr)

Helper function to safely unwrap an adapter.

Template Parameters
TThe expected underlying type
InterfaceThe interface type
Parameters
ptrShared pointer to the interface
Returns
Shared pointer to unwrapped implementation, or nullptr

Definition at line 530 of file adapter.h.

530 {
531 return adapter_factory::try_unwrap<T, Interface>(ptr);
532}

References kcenon::common::adapters::adapter_factory::try_unwrap().

Referenced by kcenon::common::adapters::smart_adapter_factory::try_unwrap().

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

◆ unwrap_adapter()

template<typename T , typename Interface >
std::shared_ptr< T > kcenon::common::adapters::unwrap_adapter ( std::shared_ptr< Interface > ptr)

Convenience function for unwrapping adapters.

Template Parameters
TThe expected underlying type
InterfaceThe interface type (deduced)
Parameters
ptrShared pointer to interface
Returns
Shared pointer to underlying implementation
Template Parameters
TThe expected underlying type
InterfaceThe interface type (deduced)
Parameters
ptrShared pointer to interface
Returns
Shared pointer to underlying implementation
auto thread_pool = unwrap_adapter<thread_pool_type>(executor);
std::shared_ptr< T > unwrap_adapter(std::shared_ptr< Interface > ptr)
Convenience function for unwrapping adapters.
Definition adapter.h:576

Definition at line 576 of file adapter.h.

576 {
577 return adapter_factory::try_unwrap<T, Interface>(ptr);
578}

References kcenon::common::adapters::adapter_factory::try_unwrap().

Here is the call graph for this function:

Variable Documentation

◆ implements_interface_v

template<typename T , typename Interface >
bool kcenon::common::adapters::implements_interface_v
inlineconstexpr
Initial value:
=
std::is_base_of_v<Interface, T> || std::is_convertible_v<T*, Interface*>

Type trait to check if a type implements an interface.

Type trait to check if a type implements an interface (C++17 compatible)

Template Parameters
TThe type to check
InterfaceThe interface to check against

Definition at line 429 of file adapter.h.

Referenced by kcenon::common::adapters::adapter_factory::create(), kcenon::common::adapters::adapter_factory::is_zero_cost(), kcenon::common::adapters::smart_adapter_factory::is_zero_cost(), and kcenon::common::adapters::smart_adapter_factory::make_adapter().