PBEncodeItem.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #ifndef MMKV_APPLE
  33. PBEncodeItemType_String,
  34. #else
  35. PBEncodeItemType_NSString,
  36. PBEncodeItemType_NSData,
  37. PBEncodeItemType_NSDate,
  38. #endif
  39. };
  40. struct PBEncodeItem {
  41. PBEncodeItemType type;
  42. uint32_t compiledSize;
  43. uint32_t valueSize;
  44. union {
  45. const MMBuffer *bufferValue;
  46. #ifndef MMKV_APPLE
  47. const std::string *strValue;
  48. #else
  49. void *objectValue;
  50. void *tmpObjectValue; // this object should be released on dealloc
  51. #endif
  52. } value;
  53. PBEncodeItem() : type(PBEncodeItemType_None), compiledSize(0), valueSize(0) { memset(&value, 0, sizeof(value)); }
  54. #ifndef MMKV_APPLE
  55. // opt std::vector.push_back() on slow_path
  56. PBEncodeItem(PBEncodeItem &&other) = default;
  57. #else
  58. // opt std::vector.push_back() on slow_path
  59. PBEncodeItem(PBEncodeItem &&other)
  60. : type(other.type), compiledSize(other.compiledSize), valueSize(other.valueSize), value(other.value) {
  61. // omit unnecessary CFRetain() & CFRelease()
  62. other.type = PBEncodeItemType_None;
  63. }
  64. ~PBEncodeItem() {
  65. if (type == PBEncodeItemType_NSString) {
  66. if (value.tmpObjectValue) {
  67. CFRelease(value.tmpObjectValue);
  68. }
  69. }
  70. }
  71. #endif // MMKV_APPLE
  72. };
  73. } // namespace mmkv
  74. #endif
  75. #endif //MMKV_PBENCODEITEM_HPP