buffer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2021 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef FLATBUFFERS_BUFFER_H_
  17. #define FLATBUFFERS_BUFFER_H_
  18. #include "flatbuffers/base.h"
  19. namespace flatbuffers {
  20. // Wrapper for uoffset_t to allow safe template specialization.
  21. // Value is allowed to be 0 to indicate a null object (see e.g. AddOffset).
  22. template<typename T> struct Offset {
  23. uoffset_t o;
  24. Offset() : o(0) {}
  25. Offset(uoffset_t _o) : o(_o) {}
  26. Offset<void> Union() const { return Offset<void>(o); }
  27. bool IsNull() const { return !o; }
  28. };
  29. inline void EndianCheck() {
  30. int endiantest = 1;
  31. // If this fails, see FLATBUFFERS_LITTLEENDIAN above.
  32. FLATBUFFERS_ASSERT(*reinterpret_cast<char *>(&endiantest) ==
  33. FLATBUFFERS_LITTLEENDIAN);
  34. (void)endiantest;
  35. }
  36. template<typename T> FLATBUFFERS_CONSTEXPR size_t AlignOf() {
  37. // clang-format off
  38. #ifdef _MSC_VER
  39. return __alignof(T);
  40. #else
  41. #ifndef alignof
  42. return __alignof__(T);
  43. #else
  44. return alignof(T);
  45. #endif
  46. #endif
  47. // clang-format on
  48. }
  49. // Lexicographically compare two strings (possibly containing nulls), and
  50. // return true if the first is less than the second.
  51. static inline bool StringLessThan(const char *a_data, uoffset_t a_size,
  52. const char *b_data, uoffset_t b_size) {
  53. const auto cmp = memcmp(a_data, b_data, (std::min)(a_size, b_size));
  54. return cmp == 0 ? a_size < b_size : cmp < 0;
  55. }
  56. // When we read serialized data from memory, in the case of most scalars,
  57. // we want to just read T, but in the case of Offset, we want to actually
  58. // perform the indirection and return a pointer.
  59. // The template specialization below does just that.
  60. // It is wrapped in a struct since function templates can't overload on the
  61. // return type like this.
  62. // The typedef is for the convenience of callers of this function
  63. // (avoiding the need for a trailing return decltype)
  64. template<typename T> struct IndirectHelper {
  65. typedef T return_type;
  66. typedef T mutable_return_type;
  67. static const size_t element_stride = sizeof(T);
  68. static return_type Read(const uint8_t *p, uoffset_t i) {
  69. return EndianScalar((reinterpret_cast<const T *>(p))[i]);
  70. }
  71. };
  72. template<typename T> struct IndirectHelper<Offset<T>> {
  73. typedef const T *return_type;
  74. typedef T *mutable_return_type;
  75. static const size_t element_stride = sizeof(uoffset_t);
  76. static return_type Read(const uint8_t *p, uoffset_t i) {
  77. p += i * sizeof(uoffset_t);
  78. return reinterpret_cast<return_type>(p + ReadScalar<uoffset_t>(p));
  79. }
  80. };
  81. template<typename T> struct IndirectHelper<const T *> {
  82. typedef const T *return_type;
  83. typedef T *mutable_return_type;
  84. static const size_t element_stride = sizeof(T);
  85. static return_type Read(const uint8_t *p, uoffset_t i) {
  86. return reinterpret_cast<const T *>(p + i * sizeof(T));
  87. }
  88. };
  89. /// @brief Get a pointer to the the file_identifier section of the buffer.
  90. /// @return Returns a const char pointer to the start of the file_identifier
  91. /// characters in the buffer. The returned char * has length
  92. /// 'flatbuffers::FlatBufferBuilder::kFileIdentifierLength'.
  93. /// This function is UNDEFINED for FlatBuffers whose schema does not include
  94. /// a file_identifier (likely points at padding or the start of a the root
  95. /// vtable).
  96. inline const char *GetBufferIdentifier(const void *buf,
  97. bool size_prefixed = false) {
  98. return reinterpret_cast<const char *>(buf) +
  99. ((size_prefixed) ? 2 * sizeof(uoffset_t) : sizeof(uoffset_t));
  100. }
  101. // Helper to see if the identifier in a buffer has the expected value.
  102. inline bool BufferHasIdentifier(const void *buf, const char *identifier,
  103. bool size_prefixed = false) {
  104. return strncmp(GetBufferIdentifier(buf, size_prefixed), identifier,
  105. flatbuffers::kFileIdentifierLength) == 0;
  106. }
  107. /// @cond FLATBUFFERS_INTERNAL
  108. // Helpers to get a typed pointer to the root object contained in the buffer.
  109. template<typename T> T *GetMutableRoot(void *buf) {
  110. EndianCheck();
  111. return reinterpret_cast<T *>(
  112. reinterpret_cast<uint8_t *>(buf) +
  113. EndianScalar(*reinterpret_cast<uoffset_t *>(buf)));
  114. }
  115. template<typename T> T *GetMutableSizePrefixedRoot(void *buf) {
  116. return GetMutableRoot<T>(reinterpret_cast<uint8_t *>(buf) +
  117. sizeof(uoffset_t));
  118. }
  119. template<typename T> const T *GetRoot(const void *buf) {
  120. return GetMutableRoot<T>(const_cast<void *>(buf));
  121. }
  122. template<typename T> const T *GetSizePrefixedRoot(const void *buf) {
  123. return GetRoot<T>(reinterpret_cast<const uint8_t *>(buf) + sizeof(uoffset_t));
  124. }
  125. } // namespace flatbuffers
  126. #endif // FLATBUFFERS_BUFFER_H_