-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathMMKVPredef.h
executable file
·293 lines (245 loc) · 7.37 KB
/
MMKVPredef.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
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
/*
* Tencent is pleased to support the open source community by making
* MMKV available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MMKV_SRC_MMKVPREDEF_H
#define MMKV_SRC_MMKVPREDEF_H
// disable encryption & decryption to reduce some code
// #define MMKV_DISABLE_CRYPT
//#define MMKV_DISABLE_FLUTTER
// using POSIX implementation
//#define FORCE_POSIX
#ifdef __cplusplus
#include <string>
#include <vector>
#include <unordered_map>
constexpr auto MMKV_VERSION = "v2.0.2";
#ifdef DEBUG
# define MMKV_DEBUG
#endif
#ifdef NDEBUG
# undef MMKV_DEBUG
#endif
#if __cplusplus>=202002L
# define MMKV_HAS_CPP20
#endif
#ifdef __ANDROID__
# ifdef FORCE_POSIX
# define MMKV_POSIX
# else
# define MMKV_ANDROID
# endif
#elif __OHOS__
# ifdef FORCE_POSIX
# define MMKV_POSIX
# else
# define MMKV_ANDROID
# define MMKV_OHOS
#endif
#elif __APPLE__
# ifdef FORCE_POSIX
# define MMKV_POSIX
# else
# define MMKV_APPLE
# ifdef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
# define MMKV_IOS
# elif __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__
# define MMKV_WATCH
# else
# define MMKV_MAC
# endif
# endif // FORCE_POSIX
#elif __linux__ || __unix__
# define MMKV_POSIX
# if __linux__
# define MMKV_LINUX
# endif
#elif _WIN32
# define MMKV_WIN32
#endif
#ifdef MMKV_WIN32
# if !defined(_WIN32_WINNT)
# define _WIN32_WINNT _WIN32_WINNT_WINXP
# endif
# include <SDKDDKVer.h>
// Exclude rarely-used stuff from Windows headers
# define WIN32_LEAN_AND_MEAN
// Windows Header Files
# include <windows.h>
constexpr auto MMKV_PATH_SLASH = L"\\";
# define MMKV_PATH_FORMAT "%ls"
using MMKVFileHandle_t = HANDLE;
using MMKVPath_t = std::wstring;
extern MMKVPath_t string2MMKVPath_t(const std::string &str);
extern std::string MMKVPath_t2String(const MMKVPath_t &str);
# ifndef MMKV_EMBED_ZLIB
# define MMKV_EMBED_ZLIB 1
# endif
#else // MMKV_WIN32
constexpr auto MMKV_PATH_SLASH = "/";
# define MMKV_PATH_FORMAT "%s"
using MMKVFileHandle_t = int;
using MMKVPath_t = std::string;
# define string2MMKVPath_t(str) (str)
# define MMKVPath_t2String(str) (str)
# ifndef MMKV_EMBED_ZLIB
# define MMKV_EMBED_ZLIB 0
# endif
#endif // MMKV_WIN32
#ifdef MMKV_APPLE
# import <Foundation/Foundation.h>
# define MMKV_NAMESPACE_BEGIN namespace mmkv {
# define MMKV_NAMESPACE_END }
# define MMKV_NAMESPACE_PREFIX mmkv
using MMKVLog_t = NSString *;
#else
# define MMKV_NAMESPACE_BEGIN
# define MMKV_NAMESPACE_END
# define MMKV_NAMESPACE_PREFIX
using MMKVLog_t = const std::string &;
#endif // MMKV_APPLE
MMKV_NAMESPACE_BEGIN
enum MMKVLogLevel : int {
MMKVLogDebug = 0, // not available for release/product build
MMKVLogInfo = 1, // default level
MMKVLogWarning,
MMKVLogError,
MMKVLogNone, // special level used to disable all log messages
};
enum MMKVRecoverStrategic : int {
OnErrorDiscard = 0,
OnErrorRecover,
};
enum MMKVErrorType : int {
MMKVCRCCheckFail = 0,
MMKVFileLength,
};
enum SyncFlag : bool { MMKV_SYNC = true, MMKV_ASYNC = false };
MMKV_NAMESPACE_END
namespace mmkv {
typedef void (*LogHandler)(MMKVLogLevel level, const char *file, int line, const char *function, MMKVLog_t message);
// by default MMKV will discard all datas on failure
// return `OnErrorRecover` to recover any data from file
typedef MMKVRecoverStrategic (*ErrorHandler)(const std::string &mmapID, MMKVErrorType errorType);
// called when content is changed by other process
// doesn't guarantee real-time notification
typedef void (*ContentChangeHandler)(const std::string &mmapID);
extern size_t DEFAULT_MMAP_SIZE;
#define DEFAULT_MMAP_ID "mmkv.default"
class MMBuffer;
struct KeyValueHolder;
#ifdef MMKV_DISABLE_CRYPT
using KeyValueHolderCrypt = KeyValueHolder;
#else
struct KeyValueHolderCrypt;
#endif
#ifdef MMKV_APPLE
struct KeyHasher {
size_t operator()(NSString *key) const { return key.hash; }
};
struct KeyEqualer {
bool operator()(NSString *left, NSString *right) const {
if (left == right) {
return true;
}
return ([left isEqualToString:right] == YES);
}
};
using MMKVVector = std::vector<std::pair<NSString *, mmkv::MMBuffer>>;
using MMKVMap = std::unordered_map<NSString *, mmkv::KeyValueHolder, KeyHasher, KeyEqualer>;
using MMKVMapCrypt = std::unordered_map<NSString *, mmkv::KeyValueHolderCrypt, KeyHasher, KeyEqualer>;
#else
struct KeyHasher {
// enables heterogeneous lookup
using is_transparent = void;
std::size_t operator()(const std::string_view& str) const {
return std::hash<std::string_view>{}(str);
}
std::size_t operator()(const std::string& str) const {
return std::hash<std::string>{}(str);
}
};
struct KeyEqualer {
// enables heterogeneous lookup
using is_transparent = void;
bool operator()(const std::string_view& lhs, const std::string_view& rhs) const {
return lhs == rhs;
}
bool operator()(const std::string& lhs, const std::string& rhs) const {
return lhs == rhs;
}
};
using MMKVVector = std::vector<std::pair<std::string, mmkv::MMBuffer>>;
using MMKVMap = std::unordered_map<std::string, mmkv::KeyValueHolder, KeyHasher, KeyEqualer>;
using MMKVMapCrypt = std::unordered_map<std::string, mmkv::KeyValueHolderCrypt, KeyHasher, KeyEqualer>;
#endif // MMKV_APPLE
template <typename T>
void unused(const T &) {}
constexpr size_t AES_KEY_LEN = 16;
constexpr size_t AES_KEY_BITSET_LEN = 128;
} // namespace mmkv
#ifdef MMKV_DEBUG
# include <cassert>
# define MMKV_ASSERT(var) assert(var)
#else
# define MMKV_ASSERT(var) mmkv::unused(var)
#endif
#endif //cplus-plus
#ifndef MMKV_WIN32
# ifndef likely
# define mmkv_unlikely(x) (__builtin_expect(bool(x), 0))
# define mmkv_likely(x) (__builtin_expect(bool(x), 1))
# endif
#else
# ifndef likely
# define mmkv_unlikely(x) (x)
# define mmkv_likely(x) (x)
# endif
#endif
#if defined(__arm__)
#if defined(__ARM_ARCH_7A__)
#if defined(__ARM_NEON__)
#if defined(__ARM_PCS_VFP)
#define MMKV_ABI "armeabi-v7a/NEON (hard-float)"
#else
#define MMKV_ABI "armeabi-v7a/NEON"
#endif
#else
#if defined(__ARM_PCS_VFP)
#define MMKV_ABI "armeabi-v7a (hard-float)"
#else
#define MMKV_ABI "armeabi-v7a"
#endif
#endif
#else
#define MMKV_ABI "armeabi"
#endif
#elif defined(__i386__) || defined(_M_IX86)
#define MMKV_ABI "x86"
#elif defined(__x86_64__) || defined(_M_X64)
#define MMKV_ABI "x86_64"
#elif defined(__mips64)
#define MMKV_ABI "mips64"
#elif defined(__mips__)
#define MMKV_ABI "mips"
#elif defined(__aarch64__) || defined(_M_ARM64)
#define MMKV_ABI "arm64-v8a"
#else
#define MMKV_ABI "unknown"
#endif
#endif //MMKV_SRC_MMKVPREDEF_H