Skip to content

Commit

Permalink
Cleanup: Use ContainsValue() instead of std::find() in base/
Browse files Browse the repository at this point in the history
BUG=561800

Review URL: https://codereview.chromium.org/1473983004

Cr-Commit-Position: refs/heads/master@{#363519}
  • Loading branch information
leizleiz authored and Commit bot committed Dec 7, 2015
1 parent 073cd45 commit 6c335d4
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 47 deletions.
4 changes: 2 additions & 2 deletions base/ios/crb_protocol_observers.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "base/logging.h"
#include "base/mac/scoped_nsobject.h"
#include "base/stl_util.h"

@interface CRBProtocolObservers () {
base::scoped_nsobject<Protocol> _protocol;
Expand Down Expand Up @@ -102,8 +103,7 @@ - (void)addObserver:(id)observer {
DCHECK(observer);
DCHECK([observer conformsToProtocol:self.protocol]);

if (std::find(_observers.begin(), _observers.end(), observer) !=
_observers.end())
if (ContainsValue(_observers, observer))
return;

_observers.push_back(observer);
Expand Down
4 changes: 2 additions & 2 deletions base/observer_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/stl_util.h"

///////////////////////////////////////////////////////////////////////////////
//
Expand Down Expand Up @@ -157,8 +158,7 @@ ObserverType* ObserverListBase<ObserverType>::Iterator::GetNext() {
template <class ObserverType>
void ObserverListBase<ObserverType>::AddObserver(ObserverType* obs) {
DCHECK(obs);
if (std::find(observers_.begin(), observers_.end(), obs)
!= observers_.end()) {
if (ContainsValue(observers_, obs)) {
NOTREACHED() << "Observers can only be added once!";
return;
}
Expand Down
4 changes: 2 additions & 2 deletions base/scoped_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "base/basictypes.h"
#include "base/logging.h"
#include "base/stl_util.h"

// ScopedObserver is used to keep track of the set of sources an object has
// attached itself to as an observer. When ScopedObserver is destroyed it
Expand Down Expand Up @@ -44,8 +45,7 @@ class ScopedObserver {
}

bool IsObserving(Source* source) const {
return std::find(sources_.begin(), sources_.end(), source) !=
sources_.end();
return ContainsValue(sources_, source);
}

bool IsObservingSources() const { return !sources_.empty(); }
Expand Down
24 changes: 10 additions & 14 deletions base/threading/sequenced_worker_pool_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/stl_util.h"
#include "base/synchronization/condition_variable.h"
#include "base/synchronization/lock.h"
#include "base/test/sequenced_task_runner_test_template.h"
Expand Down Expand Up @@ -69,7 +70,8 @@ class ThreadBlocker {
class DestructionDeadlockChecker
: public base::RefCountedThreadSafe<DestructionDeadlockChecker> {
public:
DestructionDeadlockChecker(const scoped_refptr<SequencedWorkerPool>& pool)
explicit DestructionDeadlockChecker(
const scoped_refptr<SequencedWorkerPool>& pool)
: pool_(pool) {}

protected:
Expand Down Expand Up @@ -519,10 +521,8 @@ TEST_F(SequencedWorkerPoolTest, DISABLED_IgnoresAfterShutdown) {

// The kNumWorkerThread items should have completed, in no particular order.
ASSERT_EQ(kNumWorkerThreads, result.size());
for (size_t i = 0; i < kNumWorkerThreads; i++) {
EXPECT_TRUE(std::find(result.begin(), result.end(), static_cast<int>(i)) !=
result.end());
}
for (size_t i = 0; i < kNumWorkerThreads; i++)
EXPECT_TRUE(ContainsValue(result, static_cast<int>(i)));

// No further tasks, regardless of shutdown mode, should be allowed.
EXPECT_FALSE(pool()->PostWorkerTaskWithShutdownBehavior(
Expand Down Expand Up @@ -654,11 +654,9 @@ TEST_F(SequencedWorkerPoolTest, DiscardOnShutdown) {
// The kNumWorkerThread items should have completed, plus the BLOCK_SHUTDOWN
// one, in no particular order.
ASSERT_EQ(kNumWorkerThreads + 1, result.size());
for (size_t i = 0; i < kNumWorkerThreads; i++) {
EXPECT_TRUE(std::find(result.begin(), result.end(), static_cast<int>(i)) !=
result.end());
}
EXPECT_TRUE(std::find(result.begin(), result.end(), 102) != result.end());
for (size_t i = 0; i < kNumWorkerThreads; i++)
EXPECT_TRUE(ContainsValue(result, static_cast<int>(i)));
EXPECT_TRUE(ContainsValue(result, 102));
}

// Tests that CONTINUE_ON_SHUTDOWN tasks don't block shutdown.
Expand Down Expand Up @@ -754,10 +752,8 @@ TEST_F(SequencedWorkerPoolTest, SkipOnShutdown) {
// allowed to complete. No additional non-blocking tasks should have been
// started.
ASSERT_EQ(kNumWorkerThreads, result.size());
for (size_t i = 0; i < kNumWorkerThreads; i++) {
EXPECT_TRUE(std::find(result.begin(), result.end(), static_cast<int>(i)) !=
result.end());
}
for (size_t i = 0; i < kNumWorkerThreads; i++)
EXPECT_TRUE(ContainsValue(result, static_cast<int>(i)));
}

// Ensure all worker threads are created, and then trigger a spurious
Expand Down
28 changes: 10 additions & 18 deletions base/trace_event/trace_event_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "base/memory/singleton.h"
#include "base/process/process_handle.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/strings/pattern.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/waitable_event.h"
Expand Down Expand Up @@ -1132,7 +1133,7 @@ TEST_F(TraceEventTestFixture, AddMetadataEvent) {

class Convertable : public ConvertableToTraceFormat {
public:
Convertable(int* num_calls) : num_calls_(num_calls) {}
explicit Convertable(int* num_calls) : num_calls_(num_calls) {}
void AppendAsTraceFormat(std::string* out) const override {
(*num_calls_)++;
out->append("\"metadata_value\"");
Expand Down Expand Up @@ -1202,24 +1203,15 @@ TEST_F(TraceEventTestFixture, Categories) {
EndTraceAndFlush();
std::vector<std::string> cat_groups;
TraceLog::GetInstance()->GetKnownCategoryGroups(&cat_groups);
EXPECT_TRUE(std::find(cat_groups.begin(),
cat_groups.end(), "c1") != cat_groups.end());
EXPECT_TRUE(std::find(cat_groups.begin(),
cat_groups.end(), "c2") != cat_groups.end());
EXPECT_TRUE(std::find(cat_groups.begin(),
cat_groups.end(), "c3") != cat_groups.end());
EXPECT_TRUE(std::find(cat_groups.begin(),
cat_groups.end(), "c4") != cat_groups.end());
EXPECT_TRUE(std::find(cat_groups.begin(),
cat_groups.end(), "c5,c6") != cat_groups.end());
EXPECT_TRUE(std::find(cat_groups.begin(),
cat_groups.end(), "c7,c8") != cat_groups.end());
EXPECT_TRUE(std::find(cat_groups.begin(),
cat_groups.end(),
"disabled-by-default-c9") != cat_groups.end());
EXPECT_TRUE(ContainsValue(cat_groups, "c1"));
EXPECT_TRUE(ContainsValue(cat_groups, "c2"));
EXPECT_TRUE(ContainsValue(cat_groups, "c3"));
EXPECT_TRUE(ContainsValue(cat_groups, "c4"));
EXPECT_TRUE(ContainsValue(cat_groups, "c5,c6"));
EXPECT_TRUE(ContainsValue(cat_groups, "c7,c8"));
EXPECT_TRUE(ContainsValue(cat_groups, "disabled-by-default-c9"));
// Make sure metadata isn't returned.
EXPECT_TRUE(std::find(cat_groups.begin(),
cat_groups.end(), "__metadata") == cat_groups.end());
EXPECT_FALSE(ContainsValue(cat_groups, "__metadata"));

const std::vector<std::string> empty_categories;
std::vector<std::string> included_categories;
Expand Down
16 changes: 7 additions & 9 deletions base/trace_event/trace_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "base/memory/scoped_ptr.h"
#include "base/memory/singleton.h"
#include "base/process/process_metrics.h"
#include "base/stl_util.h"
#include "base/strings/string_split.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/stringprintf.h"
Expand All @@ -34,7 +35,6 @@
#include "base/trace_event/trace_buffer.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/trace_event_synthetic_delay.h"
#include "base/trace_event/trace_log.h"
#include "base/trace_event/trace_sampling_thread.h"

#if defined(OS_WIN)
Expand Down Expand Up @@ -212,15 +212,16 @@ class TraceLog::ThreadLocalEventBuffer
: public MessageLoop::DestructionObserver,
public MemoryDumpProvider {
public:
ThreadLocalEventBuffer(TraceLog* trace_log);
explicit ThreadLocalEventBuffer(TraceLog* trace_log);
~ThreadLocalEventBuffer() override;

TraceEvent* AddTraceEvent(TraceEventHandle* handle);

TraceEvent* GetEventByHandle(TraceEventHandle handle) {
if (!chunk_ || handle.chunk_seq != chunk_->seq() ||
handle.chunk_index != chunk_index_)
return NULL;
handle.chunk_index != chunk_index_) {
return nullptr;
}

return chunk_->GetEventAt(handle.event_index);
}
Expand Down Expand Up @@ -770,10 +771,7 @@ void TraceLog::RemoveEnabledStateObserver(EnabledStateObserver* listener) {

bool TraceLog::HasEnabledStateObserver(EnabledStateObserver* listener) const {
AutoLock lock(lock_);
std::vector<EnabledStateObserver*>::const_iterator it =
std::find(enabled_state_observer_list_.begin(),
enabled_state_observer_list_.end(), listener);
return it != enabled_state_observer_list_.end();
return ContainsValue(enabled_state_observer_list_, listener);
}

TraceLogStatus TraceLog::GetStatus() const {
Expand Down Expand Up @@ -834,7 +832,7 @@ void TraceLog::SetEventCallbackEnabled(const TraceConfig& trace_config,
reinterpret_cast<subtle::AtomicWord>(cb));
event_callback_trace_config_ = trace_config;
UpdateCategoryGroupEnabledFlags();
};
}

void TraceLog::SetEventCallbackDisabled() {
AutoLock lock(lock_);
Expand Down

0 comments on commit 6c335d4

Please sign in to comment.