-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompileTimeDI.cppm
More file actions
145 lines (122 loc) · 5.74 KB
/
Copy pathCompileTimeDI.cppm
File metadata and controls
145 lines (122 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (C) 2026 mxreal64
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://gnu.org>.
export module CompileTimeDI;
// Clean, macro-free header unit imports for immediate toolchain compatibility
import <meta>;
import <type_traits>;
import <tuple>;
import <utility>;
import <cstddef>;
namespace ctdi {
// Compile-time type collection wrapper
template <typename... Ts>
struct TypeList {
static constexpr std::size_t size = sizeof...(Ts);
};
// Internal metadata collector to append types to a TypeList sequence
template <typename List, typename T> struct AppendToTypeList;
template <typename... Ts, typename T>
struct AppendToTypeList<TypeList<Ts...>, T> {
using type = TypeList<Ts..., T>;
};
// Extracts dependencies and applies structural memory safety audits
template <typename T>
consteval auto ExtractDependencies() {
using CurrentList = TypeList<>;
// Pure, finalized C++26 syntax: direct iteration over the reflection sequence
// utilizing the explicit unchecked visibility context required by your local compiler build
template for (constexpr std::meta::info field : std::meta::members_of(^^std::decay_t<T>, std::meta::access_context::unchecked())) {
// 🛡️ The Raw Pointer Audit: Halt compilation if an unmanaged raw pointer is found
constexpr bool is_raw_ptr = std::is_pointer_v<typename [: std::meta::type_of(field) :]>;
if constexpr (is_raw_ptr) {
static_assert(!is_raw_ptr, " HARD DISMISSAL: Secure architecture violation! Raw pointers are forbidden in registered services.");
}
// Unroll the structural field type and expand it into our template collection
using FieldType = typename [: std::meta::type_of(field) :];
using CurrentList = typename AppendToTypeList<CurrentList, FieldType>::type;
}
return CurrentList{};
}
template <typename T>
using GetDependencies_t = decltype(ExtractDependencies<T>());
export enum class Lifetime { Transient, Singleton };
export template <typename T, Lifetime L>
struct ServiceDescriptor {
using ServiceType = T;
static constexpr Lifetime lifetime = L;
};
// Metaprogramming graph resolution helper traits
template <typename T, typename List> struct Contains;
template <typename T, typename... Ts>
struct Contains<T, TypeList<Ts...>> : std::bool_constant<(std::is_same_v<std::decay_t<T>, std::decay_t<Ts>> || ...)> {};
template <typename T, typename List> constexpr bool Contains_v = Contains<T, List>::value;
template <typename T, typename List> struct Append;
template <typename T, typename... Ts> struct Append<T, TypeList<Ts...>> { using type = TypeList<Ts..., T>; };
// Deep recursive validation pass for tracking circular dependency loops
template <typename Target, typename ContainerList, typename PathList>
constexpr bool ValidateDependencyGraph() {
using CleanTarget = std::decay_t<Target>;
if constexpr (Contains_v<CleanTarget, PathList>) {
static_assert(!Contains_v<CleanTarget, PathList>, " COMPILE-TIME ERROR: Circular Dependency Loop Detected!");
return false;
}
else if constexpr (!Contains_v<CleanTarget, ContainerList>) {
static_assert(Contains_v<CleanTarget, ContainerList>, " COMPILE-TIME ERROR: Required Dependency missing from registration!");
return false;
}
else {
using Deps = GetDependencies_t<CleanTarget>;
return []<typename... Ds>(TypeList<Ds...>) {
using NewPath = typename Append<CleanTarget, PathList>::type;
return (ValidateDependencyGraph<Ds, ContainerList, NewPath>() && ...);
}(Deps{});
}
}
export template <typename... Registrations>
class CompileTimeDI {
private:
using RegisteredTypes = TypeList<typename Registrations::ServiceType...>;
template <typename T> struct Wrapper { T instance; };
using SingletonStorageTuple = std::tuple<Wrapper<typename Registrations::ServiceType>...>;
mutable SingletonStorageTuple mutable_storage;
static constexpr bool ValidateAll() {
return (ValidateDependencyGraph<typename Registrations::ServiceType, RegisteredTypes, TypeList<>>() && ...);
}
// Core structural verification boundary
static_assert(ValidateAll(), "DI Tree validation failed.");
template <typename T>
static constexpr Lifetime GetLifetime() {
Lifetime found = Lifetime::Transient;
((std::is_same_v<std::decay_t<T>, std::decay_t<typename Registrations::ServiceType>> ? (found = Registrations::lifetime) : found), ...);
return found;
}
// cristiannoooo
public:
constexpr CompileTimeDI() noexcept = default;
template <typename T>
[[nodiscard]] constexpr decltype(auto) resolve() const {
static_assert(Contains_v<T, RegisteredTypes>, " Requested root type is not registered.");
constexpr Lifetime L = GetLifetime<T>();
if constexpr (L == Lifetime::Singleton) {
return (std::get<Wrapper<std::decay_t<T>>>(mutable_storage).instance);
} else {
using Deps = GetDependencies_t<std::decay_t<T>>;
return []<typename... Ds>(TypeList<Ds...>, const auto& self) {
return std::decay_t<T>{ self.template resolve<std::decay_t<Ds>>()... };
}(Deps{}, *this);
}
}
};
} // namespace ctdi