Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
smart_adapter.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
19#include <memory>
20#include <type_traits>
21
22#include "adapter.h"
23
25
31template<typename T, typename Interface>
32inline constexpr bool implements_interface_v = std::is_base_of_v<Interface, T> ||
33 std::is_convertible_v<T*, Interface*>;
34
47public:
59 template<typename Interface, typename Impl>
60 static std::shared_ptr<Interface> make_adapter(std::shared_ptr<Impl> impl) {
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 }
69
81 template<typename AdapterType, typename... Args>
82 static std::shared_ptr<AdapterType> make_explicit_adapter(Args&&... args) {
83 return std::make_shared<AdapterType>(std::forward<Args>(args)...);
84 }
85
97 template<typename T, typename Interface>
98 static std::shared_ptr<T> try_unwrap(std::shared_ptr<Interface> ptr) {
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 }
112
119 template<typename Interface, typename Impl>
120 static constexpr bool is_zero_cost() {
122 }
123};
124
139template<typename Interface, typename Impl>
140std::shared_ptr<Interface> make_smart_adapter(std::shared_ptr<Impl> impl) {
142}
143
156template<typename T, typename Interface>
157std::shared_ptr<T> unwrap_adapter(std::shared_ptr<Interface> ptr) {
159}
160
161} // namespace common::adapters
Adapter traits and base class for type-erased cross-system wrappers.
Smart adapter factory that avoids unnecessary wrapping.
static std::shared_ptr< Interface > make_adapter(std::shared_ptr< Impl > impl)
Create an adapter with automatic optimization.
static std::shared_ptr< AdapterType > make_explicit_adapter(Args &&... args)
Create an adapter with explicit adapter type.
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.
std::shared_ptr< T > unwrap_adapter(std::shared_ptr< Interface > ptr)
Convenience function for unwrapping adapters.
Definition adapter.h:576
std::shared_ptr< Interface > make_smart_adapter(std::shared_ptr< Impl > impl)
Convenience function for creating smart adapters.
std::shared_ptr< T > safe_unwrap(std::shared_ptr< Interface > ptr)
Helper function to safely unwrap an adapter.
Definition adapter.h:530
constexpr bool implements_interface_v
Type trait to check if a type implements an interface.
Definition adapter.h:429