MMKVPredef.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2019 THL A29 Limited, a Tencent company.
  6. * All rights reserved.
  7. *
  8. * Licensed under the BSD 3-Clause License (the "License"); you may not use
  9. * this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * https://opensource.org/licenses/BSD-3-Clause
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #ifndef MMKV_SRC_MMKVPREDEF_H
  21. #define MMKV_SRC_MMKVPREDEF_H
  22. // disable encryption & decryption to reduce some code
  23. //#define MMKV_DISABLE_CRYPT
  24. //#define MMKV_DISABLE_FLUTTER
  25. // using POSIX implementation
  26. //#define FORCE_POSIX
  27. #ifdef __cplusplus
  28. #include <string>
  29. #include <vector>
  30. #include <unordered_map>
  31. constexpr auto MMKV_VERSION = "v1.3.1";
  32. #ifdef DEBUG
  33. # define MMKV_DEBUG
  34. #endif
  35. #ifdef NDEBUG
  36. # undef MMKV_DEBUG
  37. #endif
  38. #ifdef __ANDROID__
  39. # ifdef FORCE_POSIX
  40. # define MMKV_POSIX
  41. # else
  42. # define MMKV_ANDROID
  43. # endif
  44. #elif __APPLE__
  45. # ifdef FORCE_POSIX
  46. # define MMKV_POSIX
  47. # else
  48. # define MMKV_APPLE
  49. # ifdef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
  50. # define MMKV_IOS
  51. # elif __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__
  52. # define MMKV_WATCH
  53. # else
  54. # define MMKV_MAC
  55. # endif
  56. # endif // FORCE_POSIX
  57. #elif __linux__ || __unix__
  58. # define MMKV_POSIX
  59. # if __linux__
  60. # define MMKV_LINUX
  61. # endif
  62. #elif _WIN32
  63. # define MMKV_WIN32
  64. #endif
  65. #ifdef MMKV_WIN32
  66. # if !defined(_WIN32_WINNT)
  67. # define _WIN32_WINNT _WIN32_WINNT_WINXP
  68. # endif
  69. # include <SDKDDKVer.h>
  70. // Exclude rarely-used stuff from Windows headers
  71. # define WIN32_LEAN_AND_MEAN
  72. // Windows Header Files
  73. # include <windows.h>
  74. constexpr auto MMKV_PATH_SLASH = L"\\";
  75. # define MMKV_PATH_FORMAT "%ls"
  76. using MMKVFileHandle_t = HANDLE;
  77. using MMKVPath_t = std::wstring;
  78. extern MMKVPath_t string2MMKVPath_t(const std::string &str);
  79. extern std::string MMKVPath_t2String(const MMKVPath_t &str);
  80. # ifndef MMKV_EMBED_ZLIB
  81. # define MMKV_EMBED_ZLIB 1
  82. # endif
  83. #else // MMKV_WIN32
  84. constexpr auto MMKV_PATH_SLASH = "/";
  85. # define MMKV_PATH_FORMAT "%s"
  86. using MMKVFileHandle_t = int;
  87. using MMKVPath_t = std::string;
  88. # define string2MMKVPath_t(str) (str)
  89. # define MMKVPath_t2String(str) (str)
  90. # ifndef MMKV_EMBED_ZLIB
  91. # define MMKV_EMBED_ZLIB 0
  92. # endif
  93. #endif // MMKV_WIN32
  94. #ifdef MMKV_APPLE
  95. # import <Foundation/Foundation.h>
  96. # define MMKV_NAMESPACE_BEGIN namespace mmkv {
  97. # define MMKV_NAMESPACE_END }
  98. # define MMKV_NAMESPACE_PREFIX mmkv
  99. using MMKVLog_t = NSString *;
  100. #else
  101. # define MMKV_NAMESPACE_BEGIN
  102. # define MMKV_NAMESPACE_END
  103. # define MMKV_NAMESPACE_PREFIX
  104. using MMKVLog_t = const std::string &;
  105. #endif // MMKV_APPLE
  106. MMKV_NAMESPACE_BEGIN
  107. enum MMKVLogLevel : int {
  108. MMKVLogDebug = 0, // not available for release/product build
  109. MMKVLogInfo = 1, // default level
  110. MMKVLogWarning,
  111. MMKVLogError,
  112. MMKVLogNone, // special level used to disable all log messages
  113. };
  114. enum MMKVRecoverStrategic : int {
  115. OnErrorDiscard = 0,
  116. OnErrorRecover,
  117. };
  118. enum MMKVErrorType : int {
  119. MMKVCRCCheckFail = 0,
  120. MMKVFileLength,
  121. };
  122. enum SyncFlag : bool { MMKV_SYNC = true, MMKV_ASYNC = false };
  123. MMKV_NAMESPACE_END
  124. namespace mmkv {
  125. typedef void (*LogHandler)(MMKVLogLevel level, const char *file, int line, const char *function, MMKVLog_t message);
  126. // by default MMKV will discard all datas on failure
  127. // return `OnErrorRecover` to recover any data from file
  128. typedef MMKVRecoverStrategic (*ErrorHandler)(const std::string &mmapID, MMKVErrorType errorType);
  129. // called when content is changed by other process
  130. // doesn't guarantee real-time notification
  131. typedef void (*ContentChangeHandler)(const std::string &mmapID);
  132. extern size_t DEFAULT_MMAP_SIZE;
  133. #define DEFAULT_MMAP_ID "mmkv.default"
  134. class MMBuffer;
  135. struct KeyValueHolder;
  136. #ifdef MMKV_DISABLE_CRYPT
  137. using KeyValueHolderCrypt = KeyValueHolder;
  138. #else
  139. struct KeyValueHolderCrypt;
  140. #endif
  141. #ifdef MMKV_APPLE
  142. struct KeyHasher {
  143. size_t operator()(NSString *key) const { return key.hash; }
  144. };
  145. struct KeyEqualer {
  146. bool operator()(NSString *left, NSString *right) const {
  147. if (left == right) {
  148. return true;
  149. }
  150. return ([left isEqualToString:right] == YES);
  151. }
  152. };
  153. using MMKVVector = std::vector<std::pair<NSString *, mmkv::MMBuffer>>;
  154. using MMKVMap = std::unordered_map<NSString *, mmkv::KeyValueHolder, KeyHasher, KeyEqualer>;
  155. using MMKVMapCrypt = std::unordered_map<NSString *, mmkv::KeyValueHolderCrypt, KeyHasher, KeyEqualer>;
  156. #else
  157. using MMKVVector = std::vector<std::pair<std::string, mmkv::MMBuffer>>;
  158. using MMKVMap = std::unordered_map<std::string, mmkv::KeyValueHolder>;
  159. using MMKVMapCrypt = std::unordered_map<std::string, mmkv::KeyValueHolderCrypt>;
  160. #endif // MMKV_APPLE
  161. template <typename T>
  162. void unused(const T &) {}
  163. constexpr size_t AES_KEY_LEN = 16;
  164. constexpr size_t AES_KEY_BITSET_LEN = 128;
  165. } // namespace mmkv
  166. #ifdef MMKV_DEBUG
  167. # include <cassert>
  168. # define MMKV_ASSERT(var) assert(var)
  169. #else
  170. # define MMKV_ASSERT(var) mmkv::unused(var)
  171. #endif
  172. #endif //cplus-plus
  173. #if defined(__arm__)
  174. #if defined(__ARM_ARCH_7A__)
  175. #if defined(__ARM_NEON__)
  176. #if defined(__ARM_PCS_VFP)
  177. #define MMKV_ABI "armeabi-v7a/NEON (hard-float)"
  178. #else
  179. #define MMKV_ABI "armeabi-v7a/NEON"
  180. #endif
  181. #else
  182. #if defined(__ARM_PCS_VFP)
  183. #define MMKV_ABI "armeabi-v7a (hard-float)"
  184. #else
  185. #define MMKV_ABI "armeabi-v7a"
  186. #endif
  187. #endif
  188. #else
  189. #define MMKV_ABI "armeabi"
  190. #endif
  191. #elif defined(__i386__) || defined(_M_IX86)
  192. #define MMKV_ABI "x86"
  193. #elif defined(__x86_64__) || defined(_M_X64)
  194. #define MMKV_ABI "x86_64"
  195. #elif defined(__mips64)
  196. #define MMKV_ABI "mips64"
  197. #elif defined(__mips__)
  198. #define MMKV_ABI "mips"
  199. #elif defined(__aarch64__) || defined(_M_ARM64)
  200. #define MMKV_ABI "arm64-v8a"
  201. #else
  202. #define MMKV_ABI "unknown"
  203. #endif
  204. #endif //MMKV_SRC_MMKVPREDEF_H