PBUtility.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_PBUTILITY_H
  21. #define MMKV_PBUTILITY_H
  22. #ifdef __cplusplus
  23. #include "MMKVPredef.h"
  24. #include <cstdint>
  25. namespace mmkv {
  26. template <typename T, typename P>
  27. union Converter {
  28. static_assert(sizeof(T) == sizeof(P), "size not match");
  29. T first;
  30. P second;
  31. };
  32. static inline int64_t Float64ToInt64(double v) {
  33. Converter<double, int64_t> converter;
  34. converter.first = v;
  35. return converter.second;
  36. }
  37. static inline int32_t Float32ToInt32(float v) {
  38. Converter<float, int32_t> converter;
  39. converter.first = v;
  40. return converter.second;
  41. }
  42. static inline double Int64ToFloat64(int64_t v) {
  43. Converter<double, int64_t> converter;
  44. converter.second = v;
  45. return converter.first;
  46. }
  47. static inline float Int32ToFloat32(int32_t v) {
  48. Converter<float, int32_t> converter;
  49. converter.second = v;
  50. return converter.first;
  51. }
  52. static inline uint64_t Int64ToUInt64(int64_t v) {
  53. Converter<int64_t, uint64_t> converter;
  54. converter.first = v;
  55. return converter.second;
  56. }
  57. static inline int64_t UInt64ToInt64(uint64_t v) {
  58. Converter<int64_t, uint64_t> converter;
  59. converter.second = v;
  60. return converter.first;
  61. }
  62. static inline uint32_t Int32ToUInt32(int32_t v) {
  63. Converter<int32_t, uint32_t> converter;
  64. converter.first = v;
  65. return converter.second;
  66. }
  67. static inline int32_t UInt32ToInt32(uint32_t v) {
  68. Converter<int32_t, uint32_t> converter;
  69. converter.second = v;
  70. return converter.first;
  71. }
  72. static inline int32_t logicalRightShift32(int32_t value, uint32_t spaces) {
  73. return UInt32ToInt32((Int32ToUInt32(value) >> spaces));
  74. }
  75. static inline int64_t logicalRightShift64(int64_t value, uint32_t spaces) {
  76. return UInt64ToInt64((Int64ToUInt64(value) >> spaces));
  77. }
  78. constexpr uint32_t LittleEdian32Size = 4;
  79. constexpr uint32_t pbFloatSize() {
  80. return LittleEdian32Size;
  81. }
  82. constexpr uint32_t pbFixed32Size() {
  83. return LittleEdian32Size;
  84. }
  85. constexpr uint32_t LittleEdian64Size = 8;
  86. constexpr uint32_t pbDoubleSize() {
  87. return LittleEdian64Size;
  88. }
  89. constexpr uint32_t pbBoolSize() {
  90. return 1;
  91. }
  92. extern uint32_t pbRawVarint32Size(uint32_t value);
  93. static inline uint32_t pbRawVarint32Size(int32_t value) {
  94. return pbRawVarint32Size(Int32ToUInt32(value));
  95. }
  96. extern uint32_t pbUInt64Size(uint64_t value);
  97. static inline uint32_t pbInt64Size(int64_t value) {
  98. return pbUInt64Size(Int64ToUInt64(value));
  99. }
  100. static inline uint32_t pbInt32Size(int32_t value) {
  101. if (value >= 0) {
  102. return pbRawVarint32Size(value);
  103. } else {
  104. return 10;
  105. }
  106. }
  107. static inline uint32_t pbUInt32Size(uint32_t value) {
  108. return pbRawVarint32Size(value);
  109. }
  110. static inline uint32_t pbMMBufferSize(const MMBuffer &data) {
  111. auto valueLength = static_cast<uint32_t>(data.length());
  112. return valueLength + pbUInt32Size(valueLength);
  113. }
  114. constexpr uint32_t Fixed32Size = pbFixed32Size();
  115. } // namespace mmkv
  116. #endif
  117. #endif //MMKV_PBUTILITY_H