-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig_manager.h.mako
354 lines (287 loc) · 12.7 KB
/
config_manager.h.mako
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
## -*- coding: utf-8 -*-
<%!
import time
%><%
cpp_include_prefix = pb_set.get_custom_variable("cpp_include_prefix", "config/excel/")
xresloader_include_prefix = pb_set.get_custom_variable("xresloader_include_prefix", pb_set.pb_include_prefix)
%><%namespace name="pb_loader" module="pb_loader"/>
// Copyright ${time.strftime("%Y")} xresloader. All rights reserved.
// Generated by xres-code-generator, please don't edit it
//
#pragma once
#include <atomic>
#include <stdint.h>
#include <cstddef>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <string>
#include <memory>
#include <cstring>
#include <type_traits>
#include <unordered_map>
#if defined(_MSC_VER)
# pragma warning(push)
# if ((defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L))
# pragma warning(disable : 4996)
# pragma warning(disable : 4309)
# if _MSC_VER >= 1922
# pragma warning(disable : 5054)
# endif
# endif
# pragma warning(disable : 4100)
# pragma warning(disable : 4244)
# pragma warning(disable : 4251)
# pragma warning(disable : 4267)
# pragma warning(disable : 4668)
# pragma warning(disable : 4715)
# pragma warning(disable : 4800)
# pragma warning(disable : 4946)
# pragma warning(disable : 6001)
# pragma warning(disable : 6244)
# pragma warning(disable : 6246)
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <Windows.h>
#endif
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
#if defined(__GNUC__) && !defined(__clang__) && !defined(__apple_build_version__)
# if (__GNUC__ * 100 + __GNUC_MINOR__ * 10) >= 460
# pragma GCC diagnostic push
# endif
# pragma GCC diagnostic ignored "-Wunused-parameter"
# pragma GCC diagnostic ignored "-Wtype-limits"
# pragma GCC diagnostic ignored "-Wsign-compare"
# pragma GCC diagnostic ignored "-Wsign-conversion"
# pragma GCC diagnostic ignored "-Wshadow"
# pragma GCC diagnostic ignored "-Wuninitialized"
# pragma GCC diagnostic ignored "-Wconversion"
# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 409
# pragma GCC diagnostic ignored "-Wfloat-conversion"
# endif
# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 501
# pragma GCC diagnostic ignored "-Wsuggest-override"
# endif
#elif defined(__clang__) || defined(__apple_build_version__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunused-parameter"
# pragma clang diagnostic ignored "-Wtype-limits"
# pragma clang diagnostic ignored "-Wsign-compare"
# pragma clang diagnostic ignored "-Wsign-conversion"
# pragma clang diagnostic ignored "-Wshadow"
# pragma clang diagnostic ignored "-Wuninitialized"
# pragma clang diagnostic ignored "-Wconversion"
# if ((__clang_major__ * 100) + __clang_minor__) >= 305
# pragma clang diagnostic ignored "-Wfloat-conversion"
# endif
# if ((__clang_major__ * 100) + __clang_minor__) >= 306
# pragma clang diagnostic ignored "-Winconsistent-missing-override"
# endif
# if ((__clang_major__ * 100) + __clang_minor__) >= 1100
# pragma clang diagnostic ignored "-Wsuggest-override"
# endif
#endif
#include <google/protobuf/descriptor.h>
#include <google/protobuf/message.h>
#include <${xresloader_include_prefix}pb_header_v3.pb.h>
% for pb_msg in pb_set.generate_message:
% for loader in pb_msg.loaders:
#include "${loader.get_cpp_header_path()}"
% endfor
% endfor
#if defined(__GNUC__) && !defined(__clang__) && !defined(__apple_build_version__)
# if (__GNUC__ * 100 + __GNUC_MINOR__ * 10) >= 460
# pragma GCC diagnostic pop
# endif
#elif defined(__clang__) || defined(__apple_build_version__)
# pragma clang diagnostic pop
#endif
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
% for block_file in pb_set.get_custom_blocks("custom_config_include"):
// include custom_config_include: ${block_file}
<%include file="${block_file}" />
% endfor
#include "${cpp_include_prefix}config_traits.h"
#include "${cpp_include_prefix}spin_rw_lock.h"
#ifndef EXCEL_CONFIG_LOADER_API
# define EXCEL_CONFIG_LOADER_API
#endif
#ifndef EXCEL_CONFIG_SYMBOL_VISIBLE
# define EXCEL_CONFIG_SYMBOL_VISIBLE
#endif
${pb_loader.CppNamespaceBegin(global_package)}
#ifndef EXCEL_CONFIG_LOADER_TRAITS
# define EXCEL_CONFIG_LOADER_TRAITS
using excel_config_type_traits = ::excel::traits::config_traits<::excel::traits::type_guard>;
#endif
struct config_group_t {
std::string version;
% for pb_msg in pb_set.generate_message:
% for loader in pb_msg.loaders:
${pb_loader.CppFullPath(global_package)}${loader.get_cpp_class_full_name()} ${loader.get_cpp_public_var_name()} ;
% endfor
% endfor
% for block_file in pb_set.get_custom_blocks("custom_config_group"):
<%include file="${block_file}" />
% endfor
} ;
class config_manager {
public:
using read_buffer_func_t = std::function<bool(std::string&, const char* path)>;
using read_version_func_t = std::function<bool(std::string&)>;
using config_group_ptr_t = excel_config_type_traits::shared_ptr<config_group_t>;
using on_load_func_t = std::function<void(config_group_ptr_t)>;
using on_filter_func_t = std::function<bool(org::xresloader::pb::xresloader_datablocks&, const ::google::protobuf::Descriptor*, const std::string&)>;
using on_group_filter_func_t = std::function<int(config_group_ptr_t)>;
struct on_not_found_event_data_t {
const std::list<org::xresloader::pb::xresloader_data_source>* data_source;
const ::google::protobuf::Descriptor* message_descriptor;
const char* index_name;
const char* keys;
bool is_list;
size_t list_index;
};
using on_not_found_func_t = std::function<void(const on_not_found_event_data_t&)>;
struct log_level_t {
enum type {
LOG_LW_DISABLED = 0, // 关闭日志
LOG_LW_ERROR, // 错误
LOG_LW_WARNING,
LOG_LW_INFO,
LOG_LW_DEBUG,
};
};
struct log_caller_info_t {
log_level_t::type level_id;
const char * level_name;
const char * file_path;
uint32_t line_number;
const char * func_name;
EXCEL_CONFIG_LOADER_API log_caller_info_t();
EXCEL_CONFIG_LOADER_API log_caller_info_t(log_level_t::type lid, const char *lname, const char *fpath, uint32_t lnum, const char *fnname);
EXCEL_CONFIG_LOADER_API ~log_caller_info_t();
};
using on_log_func_t = std::function<void(const log_caller_info_t& caller, const char* content)>;
private:
config_manager();
struct constructor_helper_t {};
public:
EXCEL_CONFIG_LOADER_API config_manager(constructor_helper_t&);
EXCEL_CONFIG_LOADER_API ~config_manager();
static EXCEL_CONFIG_LOADER_API excel_config_type_traits::shared_ptr<config_manager> me();
static inline excel_config_type_traits::shared_ptr<config_manager> instance() { return me(); };
EXCEL_CONFIG_LOADER_API int init(bool enable_multithread_lock = true);
EXCEL_CONFIG_LOADER_API int init_new_group();
EXCEL_CONFIG_LOADER_API void reset();
EXCEL_CONFIG_LOADER_API void clear();
EXCEL_CONFIG_LOADER_API bool load_file_data(std::string& write_to, const std::string& file_path);
/**
* @brief 执行reload,如果版本号变化则要重新加载文件
*/
EXCEL_CONFIG_LOADER_API int reload();
/**
* @brief 执行reload加载所有资源
* @param del_when_failed 如果失败是否删除分组
*/
EXCEL_CONFIG_LOADER_API int reload_all(bool del_when_failed = false);
EXCEL_CONFIG_LOADER_API read_buffer_func_t get_buffer_loader() const;
EXCEL_CONFIG_LOADER_API void set_buffer_loader(read_buffer_func_t fn);
EXCEL_CONFIG_LOADER_API read_version_func_t get_version_loader() const;
EXCEL_CONFIG_LOADER_API void set_version_loader(read_version_func_t fn);
EXCEL_CONFIG_LOADER_API const config_group_ptr_t& get_current_config_group();
EXCEL_CONFIG_LOADER_API void set_override_same_version(bool v);
EXCEL_CONFIG_LOADER_API bool get_override_same_version() const;
EXCEL_CONFIG_LOADER_API void set_group_number(size_t sz);
EXCEL_CONFIG_LOADER_API size_t get_group_number() const;
EXCEL_CONFIG_LOADER_API void set_on_group_created(on_load_func_t func);
EXCEL_CONFIG_LOADER_API const on_load_func_t& get_n_group_created() const;
EXCEL_CONFIG_LOADER_API void set_on_group_reload_all(on_load_func_t func);
EXCEL_CONFIG_LOADER_API const on_load_func_t& get_on_group_reload_all() const;
EXCEL_CONFIG_LOADER_API void set_on_group_destroyed(on_load_func_t func);
EXCEL_CONFIG_LOADER_API const on_load_func_t& get_on_group_destroyed() const;
EXCEL_CONFIG_LOADER_API void set_on_filter(on_filter_func_t func);
EXCEL_CONFIG_LOADER_API const on_filter_func_t& get_on_filter() const;
EXCEL_CONFIG_LOADER_API void set_on_group_filter(on_group_filter_func_t func);
EXCEL_CONFIG_LOADER_API const on_group_filter_func_t& get_on_group_filter() const;
EXCEL_CONFIG_LOADER_API void set_on_not_found(on_not_found_func_t func);
EXCEL_CONFIG_LOADER_API const on_not_found_func_t& get_on_not_found() const;
template <class INNER_MSG_TYPE,
typename std::enable_if<
std::is_base_of<::google::protobuf::Message, INNER_MSG_TYPE>::value,
int>::type = 0>
EXCEL_CONFIG_SYMBOL_VISIBLE bool filter(org::xresloader::pb::xresloader_datablocks& outer_msg, const std::string& file_path) const {
if (!on_filter_) {
return true;
}
return on_filter_(outer_msg, INNER_MSG_TYPE::descriptor(), file_path);
}
template <class INNER_MSG_TYPE,
typename std::enable_if<
!std::is_base_of<::google::protobuf::Message, INNER_MSG_TYPE>::value,
int>::type = 0>
EXCEL_CONFIG_SYMBOL_VISIBLE bool filter(org::xresloader::pb::xresloader_datablocks&, const std::string&) const {
return true;
}
EXCEL_CONFIG_LOADER_API void set_on_log(on_log_func_t func);
EXCEL_CONFIG_LOADER_API const on_log_func_t& get_on_log() const;
static EXCEL_CONFIG_LOADER_API void log(const log_caller_info_t &caller,
#ifdef _MSC_VER
_In_z_ _Printf_format_string_ const char *fmt, ...);
#elif (defined(__clang__) && __clang_major__ >= 3)
const char *fmt, ...) __attribute__((__format__(__printf__, 2, 3)));
#elif (defined(__GNUC__) && __GNUC__ >= 4)
// 格式检查(成员函数有个隐含的this参数)
#if defined(__MINGW32__) || defined(__MINGW64__)
const char *fmt, ...) __attribute__((format(__MINGW_PRINTF_FORMAT, 2, 3)));
#else
const char *fmt, ...) __attribute__((format(printf, 2, 3)));
#endif
#else
const char *fmt, ...);
#endif
EXCEL_CONFIG_LOADER_API static bool dump_table(std::ostream& out, config_group_ptr_t group, const std::string& table_name);
EXCEL_CONFIG_LOADER_API void register_event_on_reset(void*, std::function<void()> fn);
EXCEL_CONFIG_LOADER_API void unregister_event_on_reset(void *);
private:
static bool default_buffer_loader(std::string&, const char* path);
static bool default_version_loader(std::string&);
static void default_log_writer(const log_caller_info_t& caller, const char* content);
private:
static bool is_destroyed_;
std::atomic<int64_t> reload_version_;
bool override_same_version_;
bool enable_multithread_lock_;
size_t max_group_number_;
on_load_func_t on_group_created_;
on_load_func_t on_group_reload_all_;
on_load_func_t on_group_destroyed_;
on_log_func_t on_log_;
on_filter_func_t on_filter_;
on_group_filter_func_t on_group_filter_;
on_not_found_func_t on_not_found_;
read_buffer_func_t read_file_handle_;
read_version_func_t read_version_handle_;
mutable excel::lock::spin_rw_lock handle_lock_;
std::list<config_group_ptr_t> config_group_list_;
mutable excel::lock::spin_rw_lock config_group_lock_;
std::string log_buffer_;
excel::lock::spin_rw_lock evt_lock_;
std::unordered_map<void*, std::function<void()>> on_evt_reset_;
};
${pb_loader.CppNamespaceEnd(global_package)} // ${global_package}
#ifndef EXCEL_CONFIG_MANAGER_LOGFILENF
#define EXCEL_CONFIG_MANAGER_LOGFILENF(lv, name) ::excel::config_manager::log_caller_info_t(lv, name, __FILE__, __LINE__, __FUNCTION__)
#endif
#define EXCEL_CONFIG_MANAGER_LOGDEBUG(...) ::excel::config_manager::log(EXCEL_CONFIG_MANAGER_LOGFILENF(::excel::config_manager::log_level_t::LOG_LW_DEBUG, "DEBUG"), __VA_ARGS__)
#define EXCEL_CONFIG_MANAGER_LOGINFO(...) ::excel::config_manager::log(EXCEL_CONFIG_MANAGER_LOGFILENF(::excel::config_manager::log_level_t::LOG_LW_INFO, "INFO"), __VA_ARGS__)
#define EXCEL_CONFIG_MANAGER_LOGWARNING(...) ::excel::config_manager::log(EXCEL_CONFIG_MANAGER_LOGFILENF(::excel::config_manager::log_level_t::LOG_LW_WARNING, "WARNING"), __VA_ARGS__)
#define EXCEL_CONFIG_MANAGER_LOGERROR(...) ::excel::config_manager::log(EXCEL_CONFIG_MANAGER_LOGFILENF(::excel::config_manager::log_level_t::LOG_LW_ERROR, "ERROR"), __VA_ARGS__)