mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-29 13:42:00 +00:00
[C++] Support C++ object copies and moves (#5988)
Augment the C++ generator to emit a C++ copy constructor and a by-value copy assignment operator. This is enabled by default when the C++ standard is C++11 or later. These additional functions are only emitted for objects that need it, typically tables containing other tables. These new functions are declared in the object table type and are defined as inline functions after table declarations. When these new functions are declared, a user-defined explicitly-defaulted default constructor and move constructor are also emitted. The copy assignment operator uses the copy-and-swap idiom to provide strong exception safety, at the expense of keeping 2 full table copies in memory temporarily. fixes #5783
This commit is contained in:
committed by
GitHub
parent
5993338ee3
commit
43203984f7
@@ -66,6 +66,10 @@ struct TableInFirstNST : public flatbuffers::NativeTable {
|
||||
NamespaceA::NamespaceB::EnumInNestedNS foo_enum = NamespaceA::NamespaceB::EnumInNestedNS_A;
|
||||
NamespaceA::NamespaceB::UnionInNestedNSUnion foo_union{};
|
||||
flatbuffers::unique_ptr<NamespaceA::NamespaceB::StructInNestedNS> foo_struct{};
|
||||
TableInFirstNST() = default;
|
||||
TableInFirstNST(const TableInFirstNST &o);
|
||||
TableInFirstNST(TableInFirstNST&&) FLATBUFFERS_NOEXCEPT = default;
|
||||
TableInFirstNST &operator=(TableInFirstNST o) FLATBUFFERS_NOEXCEPT;
|
||||
};
|
||||
|
||||
struct TableInFirstNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
@@ -194,6 +198,10 @@ struct TableInCT : public flatbuffers::NativeTable {
|
||||
}
|
||||
flatbuffers::unique_ptr<NamespaceA::TableInFirstNST> refer_to_a1{};
|
||||
flatbuffers::unique_ptr<NamespaceA::SecondTableInAT> refer_to_a2{};
|
||||
TableInCT() = default;
|
||||
TableInCT(const TableInCT &o);
|
||||
TableInCT(TableInCT&&) FLATBUFFERS_NOEXCEPT = default;
|
||||
TableInCT &operator=(TableInCT o) FLATBUFFERS_NOEXCEPT;
|
||||
};
|
||||
|
||||
struct TableInC FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
@@ -277,6 +285,10 @@ struct SecondTableInAT : public flatbuffers::NativeTable {
|
||||
return "NamespaceA.SecondTableInAT";
|
||||
}
|
||||
flatbuffers::unique_ptr<NamespaceC::TableInCT> refer_to_c{};
|
||||
SecondTableInAT() = default;
|
||||
SecondTableInAT(const SecondTableInAT &o);
|
||||
SecondTableInAT(SecondTableInAT&&) FLATBUFFERS_NOEXCEPT = default;
|
||||
SecondTableInAT &operator=(SecondTableInAT o) FLATBUFFERS_NOEXCEPT;
|
||||
};
|
||||
|
||||
struct SecondTableInA FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
@@ -350,6 +362,21 @@ inline bool operator!=(const TableInFirstNST &lhs, const TableInFirstNST &rhs) {
|
||||
}
|
||||
|
||||
|
||||
inline TableInFirstNST::TableInFirstNST(const TableInFirstNST &o)
|
||||
: foo_table((o.foo_table) ? new NamespaceA::NamespaceB::TableInNestedNST(*o.foo_table) : nullptr),
|
||||
foo_enum(o.foo_enum),
|
||||
foo_union(o.foo_union),
|
||||
foo_struct((o.foo_struct) ? new NamespaceA::NamespaceB::StructInNestedNS(*o.foo_struct) : nullptr) {
|
||||
}
|
||||
|
||||
inline TableInFirstNST &TableInFirstNST::operator=(TableInFirstNST o) FLATBUFFERS_NOEXCEPT {
|
||||
std::swap(foo_table, o.foo_table);
|
||||
std::swap(foo_enum, o.foo_enum);
|
||||
std::swap(foo_union, o.foo_union);
|
||||
std::swap(foo_struct, o.foo_struct);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline TableInFirstNST *TableInFirstNS::UnPack(const flatbuffers::resolver_function_t *_resolver) const {
|
||||
auto _o = std::unique_ptr<TableInFirstNST>(new TableInFirstNST());
|
||||
UnPackTo(_o.get(), _resolver);
|
||||
@@ -404,6 +431,17 @@ inline bool operator!=(const TableInCT &lhs, const TableInCT &rhs) {
|
||||
}
|
||||
|
||||
|
||||
inline TableInCT::TableInCT(const TableInCT &o)
|
||||
: refer_to_a1((o.refer_to_a1) ? new NamespaceA::TableInFirstNST(*o.refer_to_a1) : nullptr),
|
||||
refer_to_a2((o.refer_to_a2) ? new NamespaceA::SecondTableInAT(*o.refer_to_a2) : nullptr) {
|
||||
}
|
||||
|
||||
inline TableInCT &TableInCT::operator=(TableInCT o) FLATBUFFERS_NOEXCEPT {
|
||||
std::swap(refer_to_a1, o.refer_to_a1);
|
||||
std::swap(refer_to_a2, o.refer_to_a2);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline TableInCT *TableInC::UnPack(const flatbuffers::resolver_function_t *_resolver) const {
|
||||
auto _o = std::unique_ptr<TableInCT>(new TableInCT());
|
||||
UnPackTo(_o.get(), _resolver);
|
||||
@@ -448,6 +486,15 @@ inline bool operator!=(const SecondTableInAT &lhs, const SecondTableInAT &rhs) {
|
||||
}
|
||||
|
||||
|
||||
inline SecondTableInAT::SecondTableInAT(const SecondTableInAT &o)
|
||||
: refer_to_c((o.refer_to_c) ? new NamespaceC::TableInCT(*o.refer_to_c) : nullptr) {
|
||||
}
|
||||
|
||||
inline SecondTableInAT &SecondTableInAT::operator=(SecondTableInAT o) FLATBUFFERS_NOEXCEPT {
|
||||
std::swap(refer_to_c, o.refer_to_c);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline SecondTableInAT *SecondTableInA::UnPack(const flatbuffers::resolver_function_t *_resolver) const {
|
||||
auto _o = std::unique_ptr<SecondTableInAT>(new SecondTableInAT());
|
||||
UnPackTo(_o.get(), _resolver);
|
||||
|
||||
Reference in New Issue
Block a user