/* * Copyright 2021 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FLATBUFFERS_FLATBUFFER_BUILDER_H_ #define FLATBUFFERS_FLATBUFFER_BUILDER_H_ #include <algorithm> #include <cstdint> #include <functional> #include <initializer_list> #include <type_traits> #include "flatbuffers/allocator.h" #include "flatbuffers/array.h" #include "flatbuffers/base.h" #include "flatbuffers/buffer.h" #include "flatbuffers/buffer_ref.h" #include "flatbuffers/default_allocator.h" #include "flatbuffers/detached_buffer.h" #include "flatbuffers/stl_emulation.h" #include "flatbuffers/string.h" #include "flatbuffers/struct.h" #include "flatbuffers/table.h" #include "flatbuffers/vector.h" #include "flatbuffers/vector_downward.h" #include "flatbuffers/verifier.h" namespace flatbuffers { // Converts a Field ID to a virtual table offset. inline voffset_t FieldIndexToOffset(voffset_t field_id) { … } template<typename T, typename Alloc = std::allocator<T>> const T *data(const std::vector<T, Alloc> &v) { … } template<typename T, typename Alloc = std::allocator<T>> T *data(std::vector<T, Alloc> &v) { … } /// @addtogroup flatbuffers_cpp_api /// @{ /// @class FlatBufferBuilder /// @brief Helper class to hold data needed in creation of a FlatBuffer. /// To serialize data, you typically call one of the `Create*()` functions in /// the generated code, which in turn call a sequence of `StartTable`/ /// `PushElement`/`AddElement`/`EndTable`, or the builtin `CreateString`/ /// `CreateVector` functions. Do this is depth-first order to build up a tree to /// the root. `Finish()` wraps up the buffer ready for transport. template<bool Is64Aware = false> class FlatBufferBuilderImpl { … }; /// @} // Hack to `FlatBufferBuilder` mean `FlatBufferBuilder<false>` or // `FlatBufferBuilder<>`, where the template < > syntax is required. FlatBufferBuilder; FlatBufferBuilder64; // These are external due to GCC not allowing them in the class. // See: https://stackoverflow.com/q/8061456/868247 template<> template<> inline Offset64<String> FlatBufferBuilder64::CreateString(const char *str, size_t len) { … } // Used to distinguish from real Offsets. template<typename T = void> struct EmptyOffset { … }; // TODO(derekbailey): it would be nice to combine these two methods. template<> template<> inline void FlatBufferBuilder64::StartVector<Offset64, uint32_t>( size_t len, size_t elemsize, size_t alignment) { … } template<> template<> inline void FlatBufferBuilder64::StartVector<Offset64, uint64_t>( size_t len, size_t elemsize, size_t alignment) { … } /// Helpers to get a typed pointer to objects that are currently being built. /// @warning Creating new objects will lead to reallocations and invalidates /// the pointer! template<typename T> T *GetMutableTemporaryPointer(FlatBufferBuilder &fbb, Offset<T> offset) { … } template<typename T> const T *GetTemporaryPointer(const FlatBufferBuilder &fbb, Offset<T> offset) { … } } // namespace flatbuffers #endif // FLATBUFFERS_FLATBUFFER_BUILDER_H_