-
Notifications
You must be signed in to change notification settings - Fork 1
/
SusieMgr.hpp
321 lines (255 loc) · 8.21 KB
/
SusieMgr.hpp
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#pragma once
//---------------------------------------------------------------------------//
//
// SusieMgr.hpp
// Susie プラグイン管理クラス (C++17)
// Copyright (C) 2013-2017 tapetums
//
//---------------------------------------------------------------------------//
#if __cplusplus > 201700L
#include <optional>
#elif 1 // __cplusplus >= 201402L
#include <experimental/optional>
namespace std { template<typename T> using optional = experimental::optional<T>; }
#else
#error No library <optional>
#endif
#include <vector>
#include <stdexcept>
#include <string>
namespace std { using tstring = basic_string<TCHAR>; }
#include "Susie.hpp"
//---------------------------------------------------------------------------//
namespace tapetums
{
class SusieMgr;
}
//---------------------------------------------------------------------------//
// Susie プラグイン管理クラス
class tapetums::SusieMgr final
{
public:
using value_type = Susie;
using ref_type = std::optional<value_type&>;
using const_ref_type = std::optional<const value_type&>;
using container_type = std::vector<value_type>;
using iterator = container_type::iterator;
using const_iterator = container_type::const_iterator;
using reverse_iterator = container_type::reverse_iterator;
using const_reverse_iterator = container_type::const_reverse_iterator;
private:
container_type plugins;
public:
SusieMgr() = default;
~SusieMgr() = default;
SusieMgr(const SusieMgr&) = delete;
SusieMgr& operator =(const SusieMgr&) = delete;
SusieMgr(SusieMgr&&) noexcept = default;
SusieMgr& operator =(SusieMgr&&) noexcept = default;
template <typename... Args>
SusieMgr(Susie&& susie, Args&&... args) { init(susie, args...); }
template <typename... Args>
SusieMgr(LPCTSTR path, Args... args) { init(path, args...); }
public:
size_t size() const noexcept;
const_ref_type at(size_t index) const noexcept;
ref_type at(size_t index) noexcept;
public:
const_ref_type operator[](size_t index) const noexcept { return at(index); }
ref_type operator[](size_t index) noexcept { return at(index); }
public:
void append(Susie&& susie);
bool append(LPCTSTR path);
void collect_susie(LPCTSTR dir_path, bool serach_in_subdir);
ref_type acquire(LPCSTR filename, uint8_t* dw);
ref_type acquire(LPCWSTR filename, uint8_t* dw);
public:
iterator begin() noexcept { return plugins.begin(); }
iterator end() noexcept { return plugins.end(); }
const_iterator begin() const noexcept { return plugins.begin(); }
const_iterator end() const noexcept { return plugins.end(); }
const_iterator cbegin() const noexcept { return plugins.cbegin(); }
const_iterator cend() const noexcept { return plugins.cend(); }
reverse_iterator rbegin() noexcept { return plugins.rbegin(); }
reverse_iterator rend() noexcept { return plugins.rend(); }
const_reverse_iterator rbegin() const noexcept { return plugins.rbegin(); }
const_reverse_iterator rend() const noexcept { return plugins.rend(); }
const_reverse_iterator crbegin() const noexcept { return plugins.crbegin(); }
const_reverse_iterator crend() const noexcept { return plugins.crend(); }
private:
void init(Susie&& susie);
void init(LPCTSTR path);
template <typename... Args>
void init(Susie&& susie, Args&&... args);
template <typename... Args>
void init(LPCTSTR path, Args... args);
};
//---------------------------------------------------------------------------//
// ctor
//---------------------------------------------------------------------------//
inline void tapetums::SusieMgr::init(Susie&& susie)
{
append(std::move(susie));
}
//---------------------------------------------------------------------------//
inline void tapetums::SusieMgr::init(LPCTSTR path)
{
append(path);
}
//---------------------------------------------------------------------------//
template<typename... Args>
inline void tapetums::SusieMgr::init(Susie&& susie, Args&&... args)
{
append(std::move(susie));
init(args...);
}
//---------------------------------------------------------------------------//
template<typename... Args>
inline void tapetums::SusieMgr::init(LPCTSTR path, Args... args)
{
append(path);
init(args...);
}
//---------------------------------------------------------------------------//
// Accessors
//---------------------------------------------------------------------------//
inline size_t tapetums::SusieMgr::size() const noexcept
{
return plugins.size();
}
//---------------------------------------------------------------------------//
inline tapetums::SusieMgr::const_ref_type tapetums::SusieMgr::at(size_t index) const noexcept
{
try
{
return const_ref_type(plugins.at(index));
}
catch ( std::out_of_range& )
{
return const_ref_type();
}
}
//---------------------------------------------------------------------------//
inline tapetums::SusieMgr::ref_type tapetums::SusieMgr::at(size_t index) noexcept
{
try
{
return ref_type(plugins.at(index));
}
catch ( std::out_of_range& )
{
return ref_type();
}
}
//---------------------------------------------------------------------------//
// Methids
//---------------------------------------------------------------------------//
inline void tapetums::SusieMgr::append
(
Susie&& susie
)
{
plugins.push_back(std::move(susie));
}
//---------------------------------------------------------------------------//
inline bool tapetums::SusieMgr::append
(
LPCTSTR path
)
{
Susie susie;
if ( susie.Load(path) )
{
plugins.push_back(std::move(susie));
return true;
}
else
{
return false;
}
}
//---------------------------------------------------------------------------//
inline void tapetums::SusieMgr::collect_susie
(
LPCTSTR dir_path, bool serach_in_subdir
)
{
std::tstring path { dir_path };
path += TEXT(R"(\*)");
WIN32_FIND_DATA fd { };
const auto hFindFile = ::FindFirstFile(path.c_str(), &fd);
if ( INVALID_HANDLE_VALUE == hFindFile )
{
// 指定されたフォルダが見つからなかった
return;
}
// フォルダ内にあるものを列挙する
do
{
// 隠しファイルは飛ばす
if ( fd.cFileName[0] == '.' )
{
continue;
}
if ( fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN )
{
continue;
}
// フルパスを合成
path = dir_path + std::tstring{ TEXT(R"(\)") } + fd.cFileName;
if ( fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
// フォルダだったら
if ( serach_in_subdir )
{
// サブフォルダを検索
collect_susie(path.c_str(), serach_in_subdir);
}
}
else
{
// DLLファイルだったら
const auto pos = path.rfind(TEXT("."));
if ( path.substr(pos) == SPI_EXT )
{
// コンポーネントデータベースに記憶
append(path.c_str());
}
}
}
while ( ::FindNextFile(hFindFile, &fd) );
::FindClose( hFindFile );
return;
}
//---------------------------------------------------------------------------//
inline tapetums::SusieMgr::ref_type tapetums::SusieMgr::acquire
(
LPCSTR filename, uint8_t* dw
)
{
for ( auto&& susie : plugins )
{
if ( susie.IsSupportedA(filename, dw) == SPI_SUPPORTED )
{
return ref_type(std::ref(susie));
}
}
return ref_type();
}
//---------------------------------------------------------------------------//
inline tapetums::SusieMgr::ref_type tapetums::SusieMgr::acquire
(
LPCWSTR filename, uint8_t* dw
)
{
for ( auto&& susie : plugins )
{
if ( susie.IsSupportedW(filename, dw) == SPI_SUPPORTED )
{
return ref_type(std::ref(susie));
}
}
return ref_type();
}
//---------------------------------------------------------------------------//
// Susie.hpp