Use unique_ptr in JsepCandidateCollection

Bug: None
Change-Id: I80ffacf3a355879b56a03b5cb59bffa32114dac1
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/147601
Commit-Queue: Steve Anton <steveanton@webrtc.org>
Reviewed-by: Seth Hampson <shampson@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28712}
This commit is contained in:
Steve Anton 2019-07-30 18:07:40 -07:00 committed by Commit Bot
parent e08ca23ec9
commit 8e967dfdfc
3 changed files with 17 additions and 26 deletions

View file

@ -183,6 +183,7 @@ rtc_static_library("libjingle_peerconnection_api") {
"video:video_frame", "video:video_frame",
"video:video_rtp_headers", "video:video_rtp_headers",
"//third_party/abseil-cpp/absl/algorithm:container", "//third_party/abseil-cpp/absl/algorithm:container",
"//third_party/abseil-cpp/absl/memory",
"//third_party/abseil-cpp/absl/strings", "//third_party/abseil-cpp/absl/strings",
"//third_party/abseil-cpp/absl/types:optional", "//third_party/abseil-cpp/absl/types:optional",

View file

@ -10,9 +10,11 @@
#include "api/jsep_ice_candidate.h" #include "api/jsep_ice_candidate.h"
#include <memory>
#include <utility> #include <utility>
#include "absl/algorithm/container.h" #include "absl/algorithm/container.h"
#include "absl/memory/memory.h"
namespace webrtc { namespace webrtc {
@ -42,41 +44,29 @@ size_t JsepCandidateCollection::count() const {
} }
void JsepCandidateCollection::add(JsepIceCandidate* candidate) { void JsepCandidateCollection::add(JsepIceCandidate* candidate) {
candidates_.push_back(candidate); candidates_.push_back(absl::WrapUnique(candidate));
} }
const IceCandidateInterface* JsepCandidateCollection::at(size_t index) const { const IceCandidateInterface* JsepCandidateCollection::at(size_t index) const {
return candidates_[index]; return candidates_[index].get();
}
JsepCandidateCollection::~JsepCandidateCollection() {
for (std::vector<JsepIceCandidate*>::iterator it = candidates_.begin();
it != candidates_.end(); ++it) {
delete *it;
}
} }
bool JsepCandidateCollection::HasCandidate( bool JsepCandidateCollection::HasCandidate(
const IceCandidateInterface* candidate) const { const IceCandidateInterface* candidate) const {
bool ret = false; return absl::c_any_of(
for (std::vector<JsepIceCandidate*>::const_iterator it = candidates_.begin(); candidates_, [&](const std::unique_ptr<JsepIceCandidate>& entry) {
it != candidates_.end(); ++it) { return entry->sdp_mid() == candidate->sdp_mid() &&
if ((*it)->sdp_mid() == candidate->sdp_mid() && entry->sdp_mline_index() == candidate->sdp_mline_index() &&
(*it)->sdp_mline_index() == candidate->sdp_mline_index() && entry->candidate().IsEquivalent(candidate->candidate());
(*it)->candidate().IsEquivalent(candidate->candidate())) { });
ret = true;
break;
}
}
return ret;
} }
size_t JsepCandidateCollection::remove(const cricket::Candidate& candidate) { size_t JsepCandidateCollection::remove(const cricket::Candidate& candidate) {
auto iter = absl::c_find_if(candidates_, [&](JsepIceCandidate* c) { auto iter = absl::c_find_if(
return candidate.MatchesForRemoval(c->candidate()); candidates_, [&](const std::unique_ptr<JsepIceCandidate>& c) {
}); return candidate.MatchesForRemoval(c->candidate());
});
if (iter != candidates_.end()) { if (iter != candidates_.end()) {
delete *iter;
candidates_.erase(iter); candidates_.erase(iter);
return 1; return 1;
} }

View file

@ -16,6 +16,7 @@
#include <stddef.h> #include <stddef.h>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -63,7 +64,6 @@ class JsepCandidateCollection : public IceCandidateCollection {
// Move constructor is defined so that a vector of JsepCandidateCollections // Move constructor is defined so that a vector of JsepCandidateCollections
// can be resized. // can be resized.
JsepCandidateCollection(JsepCandidateCollection&& o); JsepCandidateCollection(JsepCandidateCollection&& o);
~JsepCandidateCollection() override;
size_t count() const override; size_t count() const override;
bool HasCandidate(const IceCandidateInterface* candidate) const override; bool HasCandidate(const IceCandidateInterface* candidate) const override;
// Adds and takes ownership of the JsepIceCandidate. // Adds and takes ownership of the JsepIceCandidate.
@ -77,7 +77,7 @@ class JsepCandidateCollection : public IceCandidateCollection {
size_t remove(const cricket::Candidate& candidate); size_t remove(const cricket::Candidate& candidate);
private: private:
std::vector<JsepIceCandidate*> candidates_; std::vector<std::unique_ptr<JsepIceCandidate>> candidates_;
RTC_DISALLOW_COPY_AND_ASSIGN(JsepCandidateCollection); RTC_DISALLOW_COPY_AND_ASSIGN(JsepCandidateCollection);
}; };