60template<
typename T,
typename Deleter>
117template<
typename T,
typename Traits = adapter_traits<T>>
140 auto get() const noexcept {
141 if constexpr (Traits::is_smart_pointer) {
144 return std::addressof(
value_);
152 decltype(
auto)
operator*()
const {
153 if constexpr (Traits::is_smart_pointer) {
163 decltype(
auto)
operator*() {
164 if constexpr (Traits::is_smart_pointer) {
187 return typename Traits::weak_type(
value_);
194 explicit operator bool() const noexcept {
195 if constexpr (Traits::is_smart_pointer) {
196 return static_cast<bool>(
value_);
229 return Traits::is_smart_pointer;
237 return Traits::supports_weak;
251template<
typename T,
typename D>
262template<
typename T,
typename... Args>
264 return adapter<T>(T(std::forward<Args>(args)...));
275template<
typename T,
typename... Args>
277 return adapter(std::make_shared<T>(std::forward<Args>(args)...));
288template<
typename T,
typename... Args>
290 return adapter(std::make_unique<T>(std::forward<Args>(args)...));
305template<
typename Interface,
typename Implementation>
316 throw std::runtime_error(
353 return "interface_adapter<Interface, Implementation>";
378 std::shared_ptr<Implementation>
impl_;
389 static std::atomic<size_t> counter{0};
403 if constexpr (std::is_base_of_v<adapter_base, Implementation>) {
405 if (
auto* base_adapter =
407 return 1 + base_adapter->get_adapter_depth();
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();
428template<
typename T,
typename Interface>
430 std::is_base_of_v<Interface, T> || std::is_convertible_v<T*, Interface*>;
453 template<
typename Interface,
typename Impl>
454 static std::shared_ptr<Interface>
457 return std::static_pointer_cast<Interface>(impl);
459 return std::make_shared<interface_adapter<Interface, Impl>>(impl);
471 template<
typename AdapterType,
typename... Args>
473 return std::make_shared<AdapterType>(std::forward<Args>(args)...);
484 template<
typename T,
typename Interface>
485 static std::shared_ptr<T>
try_unwrap(std::shared_ptr<Interface> ptr) {
490 if constexpr (std::is_base_of_v<adapter_base, Interface>) {
498 if (base && base->get_type_id() ==
516 template<
typename Interface,
typename Impl>
529template<
typename T,
typename Interface>
540template<
typename Interface>
546 if constexpr (std::is_base_of_v<adapter_base, Interface>) {
548 return base->is_adapter();
562template<
typename Interface,
typename Impl>
575template<
typename T,
typename Interface>
Base class for adapter interface to eliminate RTTI dependency.
virtual ~adapter_base()=default
virtual size_t get_type_id() const =0
Get unique type ID for this adapter type.
virtual size_t get_adapter_depth() const =0
Get the wrapper depth for this adapter.
virtual bool is_adapter() const
Check if this is an adapter (always true for adapter_base)
Smart adapter factory that avoids unnecessary wrapping.
static std::shared_ptr< AdapterType > create_explicit(Args &&... args)
Create an adapter with explicit adapter type.
static std::shared_ptr< Interface > create(std::shared_ptr< Impl > impl)
Create an adapter with automatic optimization.
static constexpr bool is_zero_cost()
Check if zero-cost adaptation is possible.
static std::shared_ptr< T > try_unwrap(std::shared_ptr< Interface > ptr)
Try to unwrap an interface to get underlying implementation.
Unified adapter template for wrapping values or smart pointers.
static constexpr bool is_smart_pointer() noexcept
Check if adapter is holding a smart pointer type.
T release() noexcept
Release ownership and return the underlying value.
typename Traits::value_type value_type
auto weak() const
Get weak reference (only for shared_ptr)
typename Traits::pointer_type pointer_type
typename Traits::reference_type reference_type
adapter()
Default constructor (for default-constructible types)
adapter(T value)
Construct adapter from value.
auto operator->() const noexcept
Arrow operator.
T & value() &noexcept
Get the underlying storage (mutable)
T value() &&noexcept
Move out the underlying storage.
auto operator->() noexcept
Arrow operator (mutable)
typename Traits::const_reference_type const_reference_type
auto get() const noexcept
Access raw pointer to the stored value.
const T & value() const &noexcept
Get the underlying storage.
static constexpr bool supports_weak() noexcept
Check if weak references are supported.
Interface adapter with type safety and depth tracking.
bool is_wrapped_adapter() const
Check if this adapter wraps another adapter.
static std::string adapter_type_name()
Get type name for debugging.
size_t get_type_id() const override
Get unique type ID for this adapter type.
std::shared_ptr< Implementation > impl_
interface_adapter(std::shared_ptr< Implementation > impl)
Construct adapter with existing implementation.
static constexpr size_t max_depth()
Get the maximum allowed wrapper depth.
static size_t calculate_depth(std::shared_ptr< Implementation > impl)
Calculate the depth of adapter wrapping.
static size_t generate_type_id()
Generate a unique type ID.
size_t get_adapter_depth() const override
Get the adapter depth (implements adapter_base)
virtual ~interface_adapter()=default
static constexpr size_t max_wrapper_depth_
size_t get_wrapper_depth() const
Get the current wrapper depth.
static size_t get_static_type_id()
Get static type ID for this adapter type.
std::shared_ptr< Implementation > unwrap() const
Get the underlying implementation.
auto make_adapter(Args &&... args)
Factory function to create adapter with in-place construction.
auto make_unique_adapter(Args &&... args)
Factory function to create adapter wrapping unique_ptr.
std::shared_ptr< Interface > make_interface_adapter(std::shared_ptr< Impl > impl)
Convenience function for creating smart adapters.
std::shared_ptr< T > unwrap_adapter(std::shared_ptr< Interface > ptr)
Convenience function for unwrapping adapters.
adapter(T) -> adapter< T >
std::shared_ptr< T > safe_unwrap(std::shared_ptr< Interface > ptr)
Helper function to safely unwrap an adapter.
bool is_adapter(std::shared_ptr< Interface > ptr)
Check if an interface pointer is actually an adapter.
constexpr bool implements_interface_v
Type trait to check if a type implements an interface.
auto make_shared_adapter(Args &&... args)
Factory function to create adapter wrapping shared_ptr.
const T & const_reference_type
std::shared_ptr< T > pointer_type
std::weak_ptr< T > weak_type
std::unique_ptr< T, Deleter > pointer_type
const T & const_reference_type
Primary adapter_traits template for value types.
static constexpr bool is_smart_pointer
const T & const_reference_type
static constexpr bool supports_weak