MMKVPredef.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.2.6";
  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 "%ws"
  76. using MMKVFileHandle_t = HANDLE;
  77. using MMKVPath_t = std::wstring;
  78. extern MMKVPath_t string2MMKVPath_t(const std::string &str);
  79. # ifndef MMKV_EMBED_ZLIB
  80. # define MMKV_EMBED_ZLIB 1
  81. # endif
  82. #else // MMKV_WIN32
  83. constexpr auto MMKV_PATH_SLASH = "/";
  84. # define MMKV_PATH_FORMAT "%s"
  85. using MMKVFileHandle_t = int;
  86. using MMKVPath_t = std::string;
  87. # define string2MMKVPath_t(str) (str)
  88. # ifndef MMKV_EMBED_ZLIB
  89. # define MMKV_EMBED_ZLIB 0
  90. # endif
  91. #endif // MMKV_WIN32
  92. #ifdef MMKV_APPLE
  93. # import <Foundation/Foundation.h>
  94. # define MMKV_NAMESPACE_BEGIN namespace mmkv {
  95. # define MMKV_NAMESPACE_END }
  96. # define MMKV_NAMESPACE_PREFIX mmkv
  97. using MMKVLog_t = NSString *;
  98. #else
  99. # define MMKV_NAMESPACE_BEGIN
  100. # define MMKV_NAMESPACE_END
  101. # define MMKV_NAMESPACE_PREFIX
  102. using MMKVLog_t = const std::string &;
  103. #endif // MMKV_APPLE
  104. MMKV_NAMESPACE_BEGIN
  105. enum MMKVLogLevel : int {
  106. MMKVLogDebug = 0, // not available for release/product build
  107. MMKVLogInfo = 1, // default level
  108. MMKVLogWarning,
  109. MMKVLogError,
  110. MMKVLogNone, // special level used to disable all log messages
  111. };
  112. enum MMKVRecoverStrategic : int {
  113. OnErrorDiscard = 0,
  114. OnErrorRecover,
  115. };
  116. enum MMKVErrorType : int {
  117. MMKVCRCCheckFail = 0,
  118. MMKVFileLength,
  119. };
  120. enum SyncFlag : bool { MMKV_SYNC = true, MMKV_ASYNC = false };
  121. MMKV_NAMESPACE_END
  122. namespace mmkv {
  123. typedef void (*LogHandler)(MMKVLogLevel level, const char *file, int line, const char *function, MMKVLog_t message);
  124. // by default MMKV will discard all datas on failure
  125. // return `OnErrorRecover` to recover any data from file
  126. typedef MMKVRecoverStrategic (*ErrorHandler)(const std::string &mmapID, MMKVErrorType errorType);
  127. // called when content is changed by other process
  128. // doesn't guarantee real-time notification
  129. typedef void (*ContentChangeHandler)(const std::string &mmapID);
  130. extern size_t DEFAULT_MMAP_SIZE;
  131. #define DEFAULT_MMAP_ID "mmkv.default"
  132. class MMBuffer;
  133. struct KeyValueHolder;
  134. #ifdef MMKV_DISABLE_CRYPT
  135. using KeyValueHolderCrypt = KeyValueHolder;
  136. #else
  137. struct KeyValueHolderCrypt;
  138. #endif
  139. #ifdef MMKV_APPLE
  140. struct KeyHasher {
  141. size_t operator()(NSString *key) const { return key.hash; }
  142. };
  143. struct KeyEqualer {
  144. bool operator()(NSString *left, NSString *right) const {
  145. if (left == right) {
  146. return true;
  147. }
  148. return ([left isEqualToString:right] == YES);
  149. }
  150. };
  151. using MMKVVector = std::vector<std::pair<NSString *, mmkv::MMBuffer>>;
  152. using MMKVMap = std::unordered_map<NSString *, mmkv::KeyValueHolder, KeyHasher, KeyEqualer>;
  153. using MMKVMapCrypt = std::unordered_map<NSString *, mmkv::KeyValueHolderCrypt, KeyHasher, KeyEqualer>;
  154. #else
  155. using MMKVVector = std::vector<std::pair<std::string, mmkv::MMBuffer>>;
  156. using MMKVMap = std::unordered_map<std::string, mmkv::KeyValueHolder>;
  157. using MMKVMapCrypt = std::unordered_map<std::string, mmkv::KeyValueHolderCrypt>;
  158. #endif // MMKV_APPLE
  159. template <typename T>
  160. void unused(const T &) {}
  161. constexpr size_t AES_KEY_LEN = 16;
  162. constexpr size_t AES_KEY_BITSET_LEN = 128;
  163. } // namespace mmkv
  164. #ifdef MMKV_DEBUG
  165. # include <cassert>
  166. # define MMKV_ASSERT(var) assert(var)
  167. #else
  168. # define MMKV_ASSERT(var) mmkv::unused(var)
  169. #endif
  170. #endif //cplus-plus
  171. #endif //MMKV_SRC_MMKVPREDEF_H