@@ -1356,8 +1356,9 @@ class FlatBufferBuilder {
1356
1356
// Write a single aligned scalar to the buffer
1357
1357
template <typename T> uoffset_t PushElement (T element) {
1358
1358
AssertScalarT<T>();
1359
+ T litle_endian_element = EndianScalar (element);
1359
1360
Align (sizeof (T));
1360
- buf_.push_small (EndianScalar (element) );
1361
+ buf_.push_small (litle_endian_element );
1361
1362
return GetSize ();
1362
1363
}
1363
1364
@@ -1600,13 +1601,11 @@ class FlatBufferBuilder {
1600
1601
1601
1602
// / @brief Store a string in the buffer, which can contain any binary data.
1602
1603
// / If a string with this exact contents has already been serialized before,
1603
- // / instead simply returns the offset of the existing string. This uses a map
1604
- // / stored on the heap, but only stores the numerical offsets.
1604
+ // / instead simply returns the offset of the existing string.
1605
1605
// / @param[in] str A const char pointer to the data to be stored as a string.
1606
1606
// / @param[in] len The number of bytes that should be stored from `str`.
1607
1607
// / @return Returns the offset in the buffer where the string starts.
1608
1608
Offset<String> CreateSharedString (const char *str, size_t len) {
1609
- FLATBUFFERS_ASSERT (FLATBUFFERS_GENERAL_HEAP_ALLOC_OK);
1610
1609
if (!string_pool)
1611
1610
string_pool = new StringOffsetMap (StringOffsetCompare (buf_));
1612
1611
auto size_before_string = buf_.size ();
@@ -1628,8 +1627,7 @@ class FlatBufferBuilder {
1628
1627
#ifdef FLATBUFFERS_HAS_STRING_VIEW
1629
1628
// / @brief Store a string in the buffer, which can contain any binary data.
1630
1629
// / If a string with this exact contents has already been serialized before,
1631
- // / instead simply returns the offset of the existing string. This uses a map
1632
- // / stored on the heap, but only stores the numerical offsets.
1630
+ // / instead simply returns the offset of the existing string.
1633
1631
// / @param[in] str A const std::string_view to store in the buffer.
1634
1632
// / @return Returns the offset in the buffer where the string starts
1635
1633
Offset<String> CreateSharedString (const flatbuffers::string_view str) {
@@ -1638,8 +1636,7 @@ class FlatBufferBuilder {
1638
1636
#else
1639
1637
// / @brief Store a string in the buffer, which null-terminated.
1640
1638
// / If a string with this exact contents has already been serialized before,
1641
- // / instead simply returns the offset of the existing string. This uses a map
1642
- // / stored on the heap, but only stores the numerical offsets.
1639
+ // / instead simply returns the offset of the existing string.
1643
1640
// / @param[in] str A const char pointer to a C-string to add to the buffer.
1644
1641
// / @return Returns the offset in the buffer where the string starts.
1645
1642
Offset<String> CreateSharedString (const char *str) {
@@ -1648,8 +1645,7 @@ class FlatBufferBuilder {
1648
1645
1649
1646
// / @brief Store a string in the buffer, which can contain any binary data.
1650
1647
// / If a string with this exact contents has already been serialized before,
1651
- // / instead simply returns the offset of the existing string. This uses a map
1652
- // / stored on the heap, but only stores the numerical offsets.
1648
+ // / instead simply returns the offset of the existing string.
1653
1649
// / @param[in] str A const reference to a std::string to store in the buffer.
1654
1650
// / @return Returns the offset in the buffer where the string starts.
1655
1651
Offset<String> CreateSharedString (const std::string &str) {
@@ -1659,8 +1655,7 @@ class FlatBufferBuilder {
1659
1655
1660
1656
// / @brief Store a string in the buffer, which can contain any binary data.
1661
1657
// / If a string with this exact contents has already been serialized before,
1662
- // / instead simply returns the offset of the existing string. This uses a map
1663
- // / stored on the heap, but only stores the numerical offsets.
1658
+ // / instead simply returns the offset of the existing string.
1664
1659
// / @param[in] str A const pointer to a `String` struct to add to the buffer.
1665
1660
// / @return Returns the offset in the buffer where the string starts
1666
1661
Offset<String> CreateSharedString (const String *str) {
@@ -1767,18 +1762,15 @@ class FlatBufferBuilder {
1767
1762
// / where the vector is stored.
1768
1763
template <typename T> Offset<Vector<T>> CreateVector (size_t vector_size,
1769
1764
const std::function<T (size_t i)> &f) {
1770
- FLATBUFFERS_ASSERT (FLATBUFFERS_GENERAL_HEAP_ALLOC_OK);
1771
1765
std::vector<T> elems (vector_size);
1772
1766
for (size_t i = 0 ; i < vector_size; i++) elems[i] = f (i);
1773
1767
return CreateVector (elems);
1774
1768
}
1775
- #endif // FLATBUFFERS_CPP98_STL
1769
+ #endif
1776
1770
// clang-format on
1777
1771
1778
1772
// / @brief Serialize values returned by a function into a FlatBuffer `vector`.
1779
- // / This is a convenience function that takes care of iteration for you. This
1780
- // / uses a vector stored on the heap to store the intermediate results of the
1781
- // / iteration.
1773
+ // / This is a convenience function that takes care of iteration for you.
1782
1774
// / @tparam T The data type of the `std::vector` elements.
1783
1775
// / @param f A function that takes the current iteration 0..vector_size-1,
1784
1776
// / and the state parameter returning any type that you can construct a
@@ -1788,7 +1780,6 @@ class FlatBufferBuilder {
1788
1780
// / where the vector is stored.
1789
1781
template <typename T, typename F, typename S>
1790
1782
Offset<Vector<T>> CreateVector (size_t vector_size, F f, S *state) {
1791
- FLATBUFFERS_ASSERT (FLATBUFFERS_GENERAL_HEAP_ALLOC_OK);
1792
1783
std::vector<T> elems (vector_size);
1793
1784
for (size_t i = 0 ; i < vector_size; i++) elems[i] = f (i, state);
1794
1785
return CreateVector (elems);
@@ -1802,34 +1793,9 @@ class FlatBufferBuilder {
1802
1793
// / where the vector is stored.
1803
1794
Offset<Vector<Offset<String>>> CreateVectorOfStrings (
1804
1795
const std::vector<std::string> &v) {
1805
- return CreateVectorOfStrings (v.cbegin (), v.cend ());
1806
- }
1807
-
1808
- // / @brief Serialize a collection of Strings into a FlatBuffer `vector`.
1809
- // / This is a convenience function for a common case.
1810
- // / @param begin The begining iterator of the collection
1811
- // / @param end The ending iterator of the collection
1812
- // / @return Returns a typed `Offset` into the serialized data indicating
1813
- // / where the vector is stored.
1814
- template <class It >
1815
- Offset<Vector<Offset<String>>> CreateVectorOfStrings (It begin, It end) {
1816
- auto size = std::distance (begin, end);
1817
- auto scratch_buffer_usage = size * sizeof (Offset<String>);
1818
- // If there is not enough space to store the offsets, there definitely won't
1819
- // be enough space to store all the strings. So ensuring space for the
1820
- // scratch region is OK, for it it fails, it would have failed later.
1821
- buf_.ensure_space (scratch_buffer_usage);
1822
- for (auto it = begin; it != end; ++it) {
1823
- buf_.scratch_push_small (CreateString (*it));
1824
- }
1825
- StartVector (size, sizeof (Offset<String>));
1826
- for (auto it = buf_.scratch_end ();
1827
- it > buf_.scratch_end () - scratch_buffer_usage;) {
1828
- it -= sizeof (Offset<String>);
1829
- PushElement (*reinterpret_cast <Offset<String> *>(it));
1830
- }
1831
- buf_.scratch_pop (scratch_buffer_usage);
1832
- return Offset<Vector<Offset<String>>>(EndVector (size));
1796
+ std::vector<Offset<String>> offsets (v.size ());
1797
+ for (size_t i = 0 ; i < v.size (); i++) offsets[i] = CreateString (v[i]);
1798
+ return CreateVector (offsets);
1833
1799
}
1834
1800
1835
1801
// / @brief Serialize an array of structs into a FlatBuffer `vector`.
@@ -1860,9 +1826,9 @@ class FlatBufferBuilder {
1860
1826
Offset<Vector<const T *>> CreateVectorOfNativeStructs (
1861
1827
const S *v, size_t len, T((*const pack_func)(const S &))) {
1862
1828
FLATBUFFERS_ASSERT (pack_func);
1863
- auto structs = StartVectorOfStructs <T>(len);
1864
- for ( size_t i = 0 ; i < len; i++) { structs[i] = pack_func (v[i]); }
1865
- return EndVectorOfStructs <T>(len );
1829
+ std::vector <T> vv (len);
1830
+ std::transform (v, v + len, vv. begin (), pack_func);
1831
+ return CreateVectorOfStructs <T>(data (vv), vv. size () );
1866
1832
}
1867
1833
1868
1834
// / @brief Serialize an array of native structs into a FlatBuffer `vector`.
@@ -2028,10 +1994,10 @@ class FlatBufferBuilder {
2028
1994
Offset<Vector<const T *>> CreateVectorOfSortedNativeStructs (S *v,
2029
1995
size_t len) {
2030
1996
extern T Pack (const S &);
2031
- auto structs = StartVectorOfStructs<T>(len );
2032
- for ( size_t i = 0 ; i < len; i++) { structs[i] = Pack (v[i]); }
2033
- std::sort (structs, structs + len, StructKeyComparator<T>( ));
2034
- return EndVectorOfStructs <T>(len);
1997
+ typedef T (*Pack_t)( const S & );
1998
+ std::vector<T> vv ( len);
1999
+ std::transform (v, v + len, vv. begin (), static_cast <Pack_t &>(Pack ));
2000
+ return CreateVectorOfSortedStructs <T>(vv, len);
2035
2001
}
2036
2002
2037
2003
// / @cond FLATBUFFERS_INTERNAL
0 commit comments