struct.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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_STRUCT_H_
  17. #define FLATBUFFERS_STRUCT_H_
  18. #include "flatbuffers/base.h"
  19. namespace flatbuffers {
  20. // "structs" are flat structures that do not have an offset table, thus
  21. // always have all members present and do not support forwards/backwards
  22. // compatible extensions.
  23. class Struct FLATBUFFERS_FINAL_CLASS {
  24. public:
  25. template<typename T> T GetField(uoffset_t o) const {
  26. return ReadScalar<T>(&data_[o]);
  27. }
  28. template<typename T> T GetStruct(uoffset_t o) const {
  29. return reinterpret_cast<T>(&data_[o]);
  30. }
  31. const uint8_t *GetAddressOf(uoffset_t o) const { return &data_[o]; }
  32. uint8_t *GetAddressOf(uoffset_t o) { return &data_[o]; }
  33. private:
  34. // private constructor & copy constructor: you obtain instances of this
  35. // class by pointing to existing data only
  36. Struct();
  37. Struct(const Struct &);
  38. Struct &operator=(const Struct &);
  39. uint8_t data_[1];
  40. };
  41. } // namespace flatbuffers
  42. #endif // FLATBUFFERS_STRUCT_H_