PBEncodeItem.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2018 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_PBENCODEITEM_HPP
  21. #define MMKV_PBENCODEITEM_HPP
  22. #ifdef __cplusplus
  23. #include "MMKVPredef.h"
  24. #include "MMBuffer.h"
  25. #include <cstdint>
  26. #include <memory.h>
  27. namespace mmkv {
  28. enum PBEncodeItemType {
  29. PBEncodeItemType_None,
  30. PBEncodeItemType_Data,
  31. PBEncodeItemType_Container,
  32. PBEncodeItemType_String,
  33. #ifdef MMKV_HAS_CPP20
  34. PBEncodeItemType_Int32,
  35. PBEncodeItemType_UInt32,
  36. PBEncodeItemType_Int64,
  37. PBEncodeItemType_UInt64,
  38. // PBEncodeItemType_Bool,
  39. // PBEncodeItemType_Float,
  40. // PBEncodeItemType_Double,
  41. #endif // MMKV_HAS_CPP20
  42. #ifdef MMKV_APPLE
  43. PBEncodeItemType_NSString,
  44. PBEncodeItemType_NSData,
  45. PBEncodeItemType_NSDate,
  46. #endif
  47. };
  48. struct PBEncodeItem {
  49. PBEncodeItemType type;
  50. uint32_t compiledSize;
  51. uint32_t valueSize;
  52. union {
  53. const MMBuffer *bufferValue;
  54. #ifdef MMKV_HAS_CPP20
  55. // bool boolValue;
  56. int32_t int32Value;
  57. int64_t int64Value;
  58. uint32_t uint32Value;
  59. uint64_t uint64Value;
  60. #endif // MMKV_HAS_CPP20
  61. // float floatValue;
  62. // double doubleValue;
  63. const std::string *strValue;
  64. #ifdef MMKV_APPLE
  65. void *objectValue;
  66. void *tmpObjectValue; // this object should be released on dealloc
  67. #endif
  68. } value;
  69. PBEncodeItem() : type(PBEncodeItemType_None), compiledSize(0), valueSize(0) { memset(&value, 0, sizeof(value)); }
  70. #ifndef MMKV_APPLE
  71. // opt std::vector.push_back() on slow_path
  72. PBEncodeItem(PBEncodeItem &&other) = default;
  73. #else
  74. // opt std::vector.push_back() on slow_path
  75. PBEncodeItem(PBEncodeItem &&other)
  76. : type(other.type), compiledSize(other.compiledSize), valueSize(other.valueSize), value(other.value) {
  77. // omit unnecessary CFRetain() & CFRelease()
  78. other.type = PBEncodeItemType_None;
  79. }
  80. ~PBEncodeItem() {
  81. if (type == PBEncodeItemType_NSString) {
  82. if (value.tmpObjectValue) {
  83. CFRelease(value.tmpObjectValue);
  84. }
  85. }
  86. }
  87. #endif // MMKV_APPLE
  88. };
  89. } // namespace mmkv
  90. #endif
  91. #endif //MMKV_PBENCODEITEM_HPP