Default noexcept move operations for MOQT property classes. `KeyValuePairList`, `MoqtProperties`, `TrackProperties`, and `ObjectProperties` declared copy operations (and a virtual destructor on `MoqtProperties`) without defaulting move operations, suppressing implicit move constructors and making `PublishedObject` non-nothrow-move-constructible. In libstdc++, `std::vector<PublishedObject>::push_back` falls back to copying `PublishedObject` during reallocation when `PublishedObject` is not nothrow-move-constructible, which fails to compile because `quiche::QuicheMemSlice` is move-only. Also move `MoqtProperties`'s constructors and assignment operators to `protected:` so polymorphic base references cannot accidentally slice derived property objects. PiperOrigin-RevId: 1023241130
diff --git a/quiche/quic/moqt/moqt_key_value_pair.h b/quiche/quic/moqt/moqt_key_value_pair.h index 362de76..64b0a39 100644 --- a/quiche/quic/moqt/moqt_key_value_pair.h +++ b/quiche/quic/moqt/moqt_key_value_pair.h
@@ -69,6 +69,10 @@ class QUICHE_EXPORT KeyValuePairList { public: KeyValuePairList() = default; + KeyValuePairList(const KeyValuePairList&) = default; + KeyValuePairList(KeyValuePairList&&) noexcept = default; + KeyValuePairList& operator=(const KeyValuePairList& other) = default; + KeyValuePairList& operator=(KeyValuePairList&& other) noexcept = default; size_t size() const { return map_.size(); } void insert(KeyValuePair pair); @@ -85,7 +89,6 @@ ValueVector Get(uint64_t key) const; void clear() { map_.clear(); } bool operator==(const KeyValuePairList& other) const = default; - KeyValuePairList& operator=(const KeyValuePairList& other) = default; // Returns the first key in the range [min_key, max_key], or nullopt if // there is no such key. @@ -381,13 +384,6 @@ inline constexpr uint64_t kMaxMandatoryTrackProperty = 0x7FFF; class QUICHE_EXPORT MoqtProperties : public KeyValuePairList { public: - MoqtProperties() = default; - MoqtProperties(const MoqtProperties&) = default; - explicit MoqtProperties(std::initializer_list<KeyValuePair> list) { - for (const auto& pair : list) { - insert(pair); - } - } virtual ~MoqtProperties() = default; void clear() { @@ -433,9 +429,19 @@ "Unknown mandatory property: 0x", absl::Hex(*key))); } bool operator==(const MoqtProperties& other) const = default; - MoqtProperties& operator=(const MoqtProperties& other) = default; protected: + MoqtProperties() = default; + explicit MoqtProperties(std::initializer_list<KeyValuePair> list) { + for (const auto& pair : list) { + insert(pair); + } + } + MoqtProperties(const MoqtProperties&) = default; + MoqtProperties(MoqtProperties&&) noexcept = default; + MoqtProperties& operator=(const MoqtProperties& other) = default; + MoqtProperties& operator=(MoqtProperties&& other) noexcept = default; + size_t TotalCount(PropertyType header) const { uint64_t key = static_cast<uint64_t>(header); return count(key) + immutable_properties_.count(key); @@ -455,13 +461,14 @@ class QUICHE_EXPORT TrackProperties : public MoqtProperties { public: - using MoqtProperties::MoqtProperties; TrackProperties() = default; explicit TrackProperties(std::initializer_list<KeyValuePair> list) : MoqtProperties(list) {} TrackProperties(const TrackProperties&) = default; - bool operator==(const TrackProperties& other) const = default; + TrackProperties(TrackProperties&&) noexcept = default; TrackProperties& operator=(const TrackProperties& other) = default; + TrackProperties& operator=(TrackProperties&& other) noexcept = default; + bool operator==(const TrackProperties& other) const = default; // Returns true if the property list renders the track malformed (e.g., it // contains object-only properties, duplicate known properties, or malformed @@ -471,13 +478,14 @@ class QUICHE_EXPORT ObjectProperties : public MoqtProperties { public: - using MoqtProperties::MoqtProperties; ObjectProperties() = default; explicit ObjectProperties(std::initializer_list<KeyValuePair> list) : MoqtProperties(list) {} ObjectProperties(const ObjectProperties&) = default; - bool operator==(const ObjectProperties& other) const = default; + ObjectProperties(ObjectProperties&&) noexcept = default; ObjectProperties& operator=(const ObjectProperties& other) = default; + ObjectProperties& operator=(ObjectProperties&& other) noexcept = default; + bool operator==(const ObjectProperties& other) const = default; // Returns true if the property list renders the track malformed (e.g., it // contains track-only properties, duplicate known properties, or malformed
diff --git a/quiche/quic/moqt/moqt_key_value_pair_test.cc b/quiche/quic/moqt/moqt_key_value_pair_test.cc index 802d597..a6664eb 100644 --- a/quiche/quic/moqt/moqt_key_value_pair_test.cc +++ b/quiche/quic/moqt/moqt_key_value_pair_test.cc
@@ -6,6 +6,8 @@ #include <cstdint> #include <optional> +#include <type_traits> +#include <utility> #include <variant> #include <vector> @@ -21,6 +23,17 @@ namespace moqt::test { +static_assert(std::is_nothrow_move_constructible_v<KeyValuePairList>); +static_assert(std::is_nothrow_move_assignable_v<KeyValuePairList>); +static_assert(!std::is_copy_constructible_v<MoqtProperties>); +static_assert(!std::is_move_constructible_v<MoqtProperties>); +static_assert(!std::is_copy_assignable_v<MoqtProperties>); +static_assert(!std::is_move_assignable_v<MoqtProperties>); +static_assert(std::is_nothrow_move_constructible_v<TrackProperties>); +static_assert(std::is_nothrow_move_assignable_v<TrackProperties>); +static_assert(std::is_nothrow_move_constructible_v<ObjectProperties>); +static_assert(std::is_nothrow_move_assignable_v<ObjectProperties>); + class LocationTest : public quic::test::QuicTest {}; TEST_F(LocationTest, LocationTests) { @@ -632,4 +645,44 @@ } } +TEST_F(ObjectPropertiesTest, MoveOperations) { + TrackProperties track_props({ + {PropertyType::kObjectDeliveryTimeout, + quic::QuicTimeDelta::FromMilliseconds(10)}, + }); + KeyValuePairList track_immutable; + track_immutable.insert({PropertyType::kDynamicGroups, 1ULL}); + track_props.AddImmutablePropertyList(track_immutable); + TrackProperties expected_track_props = track_props; + + TrackProperties moved_track_props(std::move(track_props)); + EXPECT_EQ(moved_track_props, expected_track_props); + EXPECT_TRUE(moved_track_props.dynamic_groups()); + + TrackProperties assigned_track_props; + assigned_track_props = std::move(moved_track_props); + EXPECT_EQ(assigned_track_props, expected_track_props); + EXPECT_TRUE(assigned_track_props.dynamic_groups()); + + ObjectProperties object_props({ + {PropertyType::kPriorGroupIdGap, 2ULL}, + {PropertyType::kPriorObjectIdGap, 3ULL}, + }); + KeyValuePairList object_immutable; + object_immutable.insert({PropertyType::kObjectDeliveryTimeout, 15ULL}); + object_props.AddImmutablePropertyList(object_immutable); + ObjectProperties expected_object_props = object_props; + + ObjectProperties moved_object_props(std::move(object_props)); + EXPECT_EQ(moved_object_props, expected_object_props); + EXPECT_EQ(moved_object_props.object_delivery_timeout(), + quic::QuicTimeDelta::FromMilliseconds(15)); + + ObjectProperties assigned_object_props; + assigned_object_props = std::move(moved_object_props); + EXPECT_EQ(assigned_object_props, expected_object_props); + EXPECT_EQ(assigned_object_props.object_delivery_timeout(), + quic::QuicTimeDelta::FromMilliseconds(15)); +} + } // namespace moqt::test
diff --git a/quiche/quic/moqt/moqt_object_test.cc b/quiche/quic/moqt/moqt_object_test.cc index cf96772..b6e3b26 100644 --- a/quiche/quic/moqt/moqt_object_test.cc +++ b/quiche/quic/moqt/moqt_object_test.cc
@@ -6,6 +6,7 @@ #include <cstring> #include <string> +#include <type_traits> #include <utility> #include <vector> @@ -20,6 +21,9 @@ namespace test { namespace { +static_assert(std::is_nothrow_move_constructible_v<PublishedObject>); +static_assert(std::is_move_assignable_v<PublishedObject>); + class CachedObjectTest : public quiche::test::QuicheTest { public: PublishedObjectMetadata DefaultMetadata() {