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_rtp_headers",
"//third_party/abseil-cpp/absl/algorithm:container",
"//third_party/abseil-cpp/absl/memory",
"//third_party/abseil-cpp/absl/strings",
"//third_party/abseil-cpp/absl/types:optional",

View file

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

View file

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