table.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_TABLE_H_
  17. #define FLATBUFFERS_TABLE_H_
  18. #include "flatbuffers/base.h"
  19. #include "flatbuffers/verifier.h"
  20. namespace flatbuffers {
  21. // "tables" use an offset table (possibly shared) that allows fields to be
  22. // omitted and added at will, but uses an extra indirection to read.
  23. class Table {
  24. public:
  25. const uint8_t *GetVTable() const {
  26. return data_ - ReadScalar<soffset_t>(data_);
  27. }
  28. // This gets the field offset for any of the functions below it, or 0
  29. // if the field was not present.
  30. voffset_t GetOptionalFieldOffset(voffset_t field) const {
  31. // The vtable offset is always at the start.
  32. auto vtable = GetVTable();
  33. // The first element is the size of the vtable (fields + type id + itself).
  34. auto vtsize = ReadScalar<voffset_t>(vtable);
  35. // If the field we're accessing is outside the vtable, we're reading older
  36. // data, so it's the same as if the offset was 0 (not present).
  37. return field < vtsize ? ReadScalar<voffset_t>(vtable + field) : 0;
  38. }
  39. template<typename T> T GetField(voffset_t field, T defaultval) const {
  40. auto field_offset = GetOptionalFieldOffset(field);
  41. return field_offset ? ReadScalar<T>(data_ + field_offset) : defaultval;
  42. }
  43. template<typename P> P GetPointer(voffset_t field) {
  44. auto field_offset = GetOptionalFieldOffset(field);
  45. auto p = data_ + field_offset;
  46. return field_offset ? reinterpret_cast<P>(p + ReadScalar<uoffset_t>(p))
  47. : nullptr;
  48. }
  49. template<typename P> P GetPointer(voffset_t field) const {
  50. return const_cast<Table *>(this)->GetPointer<P>(field);
  51. }
  52. template<typename P> P GetStruct(voffset_t field) const {
  53. auto field_offset = GetOptionalFieldOffset(field);
  54. auto p = const_cast<uint8_t *>(data_ + field_offset);
  55. return field_offset ? reinterpret_cast<P>(p) : nullptr;
  56. }
  57. template<typename Raw, typename Face>
  58. flatbuffers::Optional<Face> GetOptional(voffset_t field) const {
  59. auto field_offset = GetOptionalFieldOffset(field);
  60. auto p = data_ + field_offset;
  61. return field_offset ? Optional<Face>(static_cast<Face>(ReadScalar<Raw>(p)))
  62. : Optional<Face>();
  63. }
  64. template<typename T> bool SetField(voffset_t field, T val, T def) {
  65. auto field_offset = GetOptionalFieldOffset(field);
  66. if (!field_offset) return IsTheSameAs(val, def);
  67. WriteScalar(data_ + field_offset, val);
  68. return true;
  69. }
  70. template<typename T> bool SetField(voffset_t field, T val) {
  71. auto field_offset = GetOptionalFieldOffset(field);
  72. if (!field_offset) return false;
  73. WriteScalar(data_ + field_offset, val);
  74. return true;
  75. }
  76. bool SetPointer(voffset_t field, const uint8_t *val) {
  77. auto field_offset = GetOptionalFieldOffset(field);
  78. if (!field_offset) return false;
  79. WriteScalar(data_ + field_offset,
  80. static_cast<uoffset_t>(val - (data_ + field_offset)));
  81. return true;
  82. }
  83. uint8_t *GetAddressOf(voffset_t field) {
  84. auto field_offset = GetOptionalFieldOffset(field);
  85. return field_offset ? data_ + field_offset : nullptr;
  86. }
  87. const uint8_t *GetAddressOf(voffset_t field) const {
  88. return const_cast<Table *>(this)->GetAddressOf(field);
  89. }
  90. bool CheckField(voffset_t field) const {
  91. return GetOptionalFieldOffset(field) != 0;
  92. }
  93. // Verify the vtable of this table.
  94. // Call this once per table, followed by VerifyField once per field.
  95. bool VerifyTableStart(Verifier &verifier) const {
  96. return verifier.VerifyTableStart(data_);
  97. }
  98. // Verify a particular field.
  99. template<typename T>
  100. bool VerifyField(const Verifier &verifier, voffset_t field) const {
  101. // Calling GetOptionalFieldOffset should be safe now thanks to
  102. // VerifyTable().
  103. auto field_offset = GetOptionalFieldOffset(field);
  104. // Check the actual field.
  105. return !field_offset || verifier.Verify<T>(data_, field_offset);
  106. }
  107. // VerifyField for required fields.
  108. template<typename T>
  109. bool VerifyFieldRequired(const Verifier &verifier, voffset_t field) const {
  110. auto field_offset = GetOptionalFieldOffset(field);
  111. return verifier.Check(field_offset != 0) &&
  112. verifier.Verify<T>(data_, field_offset);
  113. }
  114. // Versions for offsets.
  115. bool VerifyOffset(const Verifier &verifier, voffset_t field) const {
  116. auto field_offset = GetOptionalFieldOffset(field);
  117. return !field_offset || verifier.VerifyOffset(data_, field_offset);
  118. }
  119. bool VerifyOffsetRequired(const Verifier &verifier, voffset_t field) const {
  120. auto field_offset = GetOptionalFieldOffset(field);
  121. return verifier.Check(field_offset != 0) &&
  122. verifier.VerifyOffset(data_, field_offset);
  123. }
  124. private:
  125. // private constructor & copy constructor: you obtain instances of this
  126. // class by pointing to existing data only
  127. Table();
  128. Table(const Table &other);
  129. Table &operator=(const Table &);
  130. uint8_t data_[1];
  131. };
  132. // This specialization allows avoiding warnings like:
  133. // MSVC C4800: type: forcing value to bool 'true' or 'false'.
  134. template<>
  135. inline flatbuffers::Optional<bool> Table::GetOptional<uint8_t, bool>(
  136. voffset_t field) const {
  137. auto field_offset = GetOptionalFieldOffset(field);
  138. auto p = data_ + field_offset;
  139. return field_offset ? Optional<bool>(ReadScalar<uint8_t>(p) != 0)
  140. : Optional<bool>();
  141. }
  142. } // namespace flatbuffers
  143. #endif // FLATBUFFERS_TABLE_H_