-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiocpp.h
264 lines (240 loc) · 7.29 KB
/
iocpp.h
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#pragma once
/*=================================================
* Copyright © 2020-2024 ChivenZhang.
* All Rights Reserved.
* =====================Note=========================
*
*
*=====================History========================
* Created by [email protected].
*
* =================================================*/
#include <any>
#include <map>
#include <memory>
#include <string>
#include <cstdint>
#include <functional>
namespace iocpp
{
inline constexpr uint32_t Hash32(const char* const first, const size_t count) noexcept
{
// These FNV-1a utility functions are extremely performance sensitive,
// check examples like that in VSO-653642 before making changes.
constexpr uint32_t _FNV_offset_basis = 2166136261U;
constexpr uint32_t _FNV_prime = 16777619U;
auto result = _FNV_offset_basis;
// accumulate range [_First, _First + _Count) into partial FNV-1a hash _Val
for (size_t i = 0; i < count; ++i)
{
result ^= (uint32_t)first[i];
result *= _FNV_prime;
}
return result;
}
inline constexpr uint64_t Hash64(const char* const first, const size_t count) noexcept
{
// These FNV-1a utility functions are extremely performance sensitive,
// check examples like that in VSO-653642 before making changes.
constexpr uint64_t _FNV_offset_basis = 14695981039346656037ULL;
constexpr uint64_t _FNV_prime = 1099511628211ULL;
auto result = _FNV_offset_basis;
// accumulate range [_First, _First + _Count) into partial FNV-1a hash _Val
for (size_t i = 0; i < count; ++i)
{
result ^= (uint64_t)first[i];
result *= _FNV_prime;
}
return result;
}
inline constexpr uint32_t Hash(const char* value) noexcept
{
size_t count = 0; for (size_t i = 0; value[i]; ++i) ++count;
return Hash32(value, count);
}
inline const uint32_t Hash(std::string const& value) noexcept
{
return Hash32(value.c_str(), value.size());
}
class Bean
{
public:
std::any Data;
std::function<void(std::any& src, std::any& dst)> Cast;
};
class BeanManager
{
public:
static BeanManager& Instance();
template<class T, class I, std::enable_if_t<std::is_same_v<T, I>, int> = 0>
std::weak_ptr<std::shared_ptr<Bean>> create(uint32_t N = 0)
{
auto name = ((uint64_t)Hash(typeid(I).name()) << 32) | N;
auto result = m_Beans[name];
if (result == nullptr)
{
result = m_Beans[name] = std::make_shared<std::shared_ptr<Bean>>();
}
if (result->get() == nullptr)
{
auto bean = std::make_shared<Bean>();
bean->Data = std::make_shared<T>();
bean->Cast = [](std::any& src, std::any& dst) {
if (src.type() == dst.type()) dst = src;
else dst = std::shared_ptr<T>();
};
(*result) = bean;
}
return result;
}
template<class T, class I, std::enable_if_t<std::is_same_v<T, I> == false && std::is_base_of_v<I, T>, int> = 0>
std::weak_ptr<std::shared_ptr<Bean>> create(uint32_t N = 0)
{
auto name1 = ((uint64_t)Hash(typeid(I).name()) << 32) | N;
auto name2 = ((uint64_t)Hash(typeid(T).name()) << 32) | N;
auto name3 = ((uint64_t)Hash(typeid(I).name()) << 32) | 0;
auto result1 = m_Beans[name1];
auto result2 = m_Beans[name2];
auto result3 = m_Beans[name3];
if (result1 || result2)
{
if (result1 == nullptr) result1 = m_Beans[name1] = result2;
if (result2 == nullptr) result2 = m_Beans[name2] = result1;
if (result3 == nullptr) result3 = m_Beans[name3] = result1;
}
else
{
result1 = result2 = m_Beans[name1] = m_Beans[name2] = std::make_shared<std::shared_ptr<Bean>>();
if (result3 == nullptr) result3 = m_Beans[name3] = result1;
}
auto result = result1;
if (result->get() == nullptr)
{
auto bean = std::make_shared<Bean>();
bean->Data = std::make_shared<T>();
bean->Cast = [](std::any& src, std::any& dst) {
if (src.type() == dst.type()) dst = src;
else dst = std::dynamic_pointer_cast<I>(std::any_cast<std::shared_ptr<T>>(src));
};
(*result) = bean;
if (N) (*m_Beans[name3]) = (*result);
}
return result;
}
template<class T>
std::weak_ptr<std::shared_ptr<Bean>> depend(uint32_t N = 0)
{
auto name = ((uint64_t)Hash(typeid(T).name()) << 32) | N;
auto result = m_Beans[name];
if (result == nullptr)
{
result = m_Beans[name] = std::make_shared<std::shared_ptr<Bean>>();
}
return result;
}
public:
std::map<uint64_t, std::shared_ptr<std::shared_ptr<Bean>>> m_Beans;
};
template<class T, class I, uint32_t N = 0>
class Resource
{
public:
Resource()
{
m_Bean = BeanManager::Instance().create<T, I>(N);
}
protected:
T* bean() const
{
if (m_Bean.lock() == nullptr) return nullptr;
if (m_Bean.lock()->get() == nullptr) return nullptr;
auto result = std::any_cast<std::shared_ptr<T>> (m_Bean.lock()->get()->Data);
return result.get();
}
private:
std::weak_ptr<std::shared_ptr<Bean>> m_Bean;
};
template<class T, uint32_t N = 0>
class Autowire
{
public:
Autowire()
{
m_Bean = BeanManager::Instance().depend<T>(N);
}
protected:
T* bean() const
{
std::any dst = std::shared_ptr<T>();
if (m_Bean.lock() == nullptr) return nullptr;
if (m_Bean.lock()->get() == nullptr) return nullptr;
m_Bean.lock()->get()->Cast(m_Bean.lock()->get()->Data, dst);
auto result = std::any_cast<std::shared_ptr<T>> (dst);
return result.get();
}
private:
std::weak_ptr<std::shared_ptr<Bean>> m_Bean;
};
template<class T, class I>
class ResourceThis
{
public:
ResourceThis(uint32_t N = 0)
{
m_Bean = BeanManager::Instance().create<T, I>(N);
}
T* bean() const
{
if (m_Bean.lock() == nullptr) return nullptr;
if (m_Bean.lock()->get() == nullptr) return nullptr;
auto result = std::any_cast<std::shared_ptr<T>> (m_Bean.lock()->get()->Data);
return result.get();
}
private:
std::weak_ptr<std::shared_ptr<Bean>> m_Bean;
};
template<class T>
class AutowireThis
{
public:
AutowireThis(uint32_t N = 0)
{
m_Bean = BeanManager::Instance().depend<T>(N);
}
T* bean() const
{
std::any dst = std::shared_ptr<T>();
if (m_Bean.lock() == nullptr) return nullptr;
if (m_Bean.lock()->get() == nullptr) return nullptr;
m_Bean.lock()->get()->Cast(m_Bean.lock()->get()->Data, dst);
auto result = std::any_cast<std::shared_ptr<T>> (dst);
return result.get();
}
private:
std::weak_ptr<std::shared_ptr<Bean>> m_Bean;
};
}
// Autowire(Type or Interface)
#define AUTOWIRE(T) iocpp::Autowire<T, 0>
// Autowire(Type or Interface)
#define AUTOWIRE_DATA(T) iocpp::AutowireThis<T>(0).bean()
// Autowire(Type or Interface, Name)
#define AUTOWIREN(T, N) iocpp::Autowire<T, iocpp::Hash(N)>
// Autowire(Type or Interface, Name)
#define AUTOWIREN_DATA(T, N) iocpp::AutowireThis<T>(iocpp::Hash(N)).bean()
// Resource(Type)
#define RESOURCE(T) iocpp::Resource<T, T, 0>
// Resource(Type, Interface)
#define RESOURCE2(T, I) iocpp::Resource<T, I, 0>
// Resource(Type, Name)
#define RESOURCEN(T, N) iocpp::Resource<T, T, iocpp::Hash(N)>
// Resource(Type, Interface, Name)
#define RESOURCE2N(T, I, N) iocpp::Resource<T, I, iocpp::Hash(N)>
// Resource(Type)
#define RESOURCE_DATA(T) iocpp::ResourceThis<T, T>(0).bean()
// Resource(Type, Interface)
#define RESOURCE2_DATA(T, I) iocpp::ResourceThis<T, I>(0).bean()
// Resource(Type, Name)
#define RESOURCEN_DATA(T, N) iocpp::ResourceThis<T, T>(iocpp::Hash(N)).bean()
// Resource(Type, Interface, Name)
#define RESOURCE2N_DATA(T, I, N) iocpp::ResourceThis<T, I>(iocpp::Hash(N)).bean()