Add example usages and tests to scoped_ptr.h and tuple.h.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1305 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
@@ -254,6 +254,7 @@ test_files = [
|
|||||||
'pr_time_test.cc',
|
'pr_time_test.cc',
|
||||||
'ref_counted_unittest.cc',
|
'ref_counted_unittest.cc',
|
||||||
'run_all_unittests.cc',
|
'run_all_unittests.cc',
|
||||||
|
'scoped_ptr_unittest.cc',
|
||||||
'sha2_unittest.cc',
|
'sha2_unittest.cc',
|
||||||
'singleton_unittest.cc',
|
'singleton_unittest.cc',
|
||||||
'stack_container_unittest.cc',
|
'stack_container_unittest.cc',
|
||||||
@@ -264,6 +265,7 @@ test_files = [
|
|||||||
'thread_unittest.cc',
|
'thread_unittest.cc',
|
||||||
'time_unittest.cc',
|
'time_unittest.cc',
|
||||||
'tracked_objects_test.cc',
|
'tracked_objects_test.cc',
|
||||||
|
'tuple_unittest.cc',
|
||||||
'values_unittest.cc',
|
'values_unittest.cc',
|
||||||
'waitable_event_unittest.cc',
|
'waitable_event_unittest.cc',
|
||||||
'word_iterator_unittest.cc',
|
'word_iterator_unittest.cc',
|
||||||
|
@@ -251,6 +251,10 @@
|
|||||||
RelativePath="..\ref_counted_unittest.cc"
|
RelativePath="..\ref_counted_unittest.cc"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath="..\scoped_ptr_unittest.cc"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath="..\sha2_unittest.cc"
|
RelativePath="..\sha2_unittest.cc"
|
||||||
>
|
>
|
||||||
@@ -315,6 +319,10 @@
|
|||||||
RelativePath="..\tracked_objects_test.cc"
|
RelativePath="..\tracked_objects_test.cc"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath="..\tuple_unittest.cc"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath="..\values_unittest.cc"
|
RelativePath="..\values_unittest.cc"
|
||||||
>
|
>
|
||||||
|
@@ -1,10 +1,40 @@
|
|||||||
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
|
||||||
// Use of this source code is governed by a BSD-style license that can be
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
// All Rights Reserved.
|
|
||||||
|
|
||||||
#ifndef BASE_SCOPED_PTR_H__
|
// Scopers help you manage ownership of a pointer, helping you easily manage the
|
||||||
#define BASE_SCOPED_PTR_H__
|
// a pointer within a scope, and automatically destroying the pointer at the
|
||||||
|
// end of a scope. There are two main classes you will use, which coorespond
|
||||||
|
// to the operators new/delete and new[]/delete[].
|
||||||
|
//
|
||||||
|
// Example usage (scoped_ptr):
|
||||||
|
// {
|
||||||
|
// scoped_ptr<Foo> foo(new Foo("wee"));
|
||||||
|
// } // foo goes out of scope, releasing the pointer with it.
|
||||||
|
//
|
||||||
|
// {
|
||||||
|
// scoped_ptr<Foo> foo; // No pointer managed.
|
||||||
|
// foo.reset(new Foo("wee")); // Now a pointer is managed.
|
||||||
|
// foo.reset(new Foo("wee2")); // Foo("wee") was destroyed.
|
||||||
|
// foo.reset(new Foo("wee3")); // Foo("wee2") was destroyed.
|
||||||
|
// foo->Method(); // Foo::Method() called.
|
||||||
|
// foo.get()->Method(); // Foo::Method() called.
|
||||||
|
// SomeFunc(foo.Release()); // SomeFunc takes owernship, foo no longer
|
||||||
|
// // manages a pointer.
|
||||||
|
// foo.reset(new Foo("wee4")); // foo manages a pointer again.
|
||||||
|
// foo.reset(); // Foo("wee4") destroyed, foo no longer
|
||||||
|
// // manages a pointer.
|
||||||
|
// } // foo wasn't managing a pointer, so nothing was destroyed.
|
||||||
|
//
|
||||||
|
// Example usage (scoped_array):
|
||||||
|
// {
|
||||||
|
// scoped_array<Foo> foo(new Foo[100]);
|
||||||
|
// foo.get()->Method(); // Foo::Method on the 0th element.
|
||||||
|
// foo[10].Method(); // Foo::Method on the 10th element.
|
||||||
|
// }
|
||||||
|
|
||||||
|
#ifndef BASE_SCOPED_PTR_H_
|
||||||
|
#define BASE_SCOPED_PTR_H_
|
||||||
|
|
||||||
// This is an implementation designed to match the anticipated future TR2
|
// This is an implementation designed to match the anticipated future TR2
|
||||||
// implementation of the scoped_ptr class, and its closely-related brethren,
|
// implementation of the scoped_ptr class, and its closely-related brethren,
|
||||||
@@ -347,5 +377,4 @@ bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) {
|
|||||||
return p != b.get();
|
return p != b.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // BASE_SCOPED_PTR_H__
|
#endif // BASE_SCOPED_PTR_H_
|
||||||
|
|
||||||
|
169
base/scoped_ptr_unittest.cc
Normal file
169
base/scoped_ptr_unittest.cc
Normal file
@@ -0,0 +1,169 @@
|
|||||||
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#include "base/basictypes.h"
|
||||||
|
#include "base/scoped_ptr.h"
|
||||||
|
#include "testing/gtest/include/gtest/gtest.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
class ConDecLogger {
|
||||||
|
public:
|
||||||
|
ConDecLogger() : ptr_(NULL) { }
|
||||||
|
explicit ConDecLogger(int* ptr) { set_ptr(ptr); }
|
||||||
|
~ConDecLogger() { --*ptr_; }
|
||||||
|
|
||||||
|
void set_ptr(int* ptr) { ptr_ = ptr; ++*ptr_; }
|
||||||
|
|
||||||
|
int SomeMeth(int x) { return x; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
int* ptr_;
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(ConDecLogger);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST(ScopedPtrTest, ScopedPtr) {
|
||||||
|
int constructed = 0;
|
||||||
|
|
||||||
|
{
|
||||||
|
scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed));
|
||||||
|
EXPECT_EQ(1, constructed);
|
||||||
|
EXPECT_TRUE(scoper.get());
|
||||||
|
|
||||||
|
EXPECT_EQ(10, scoper->SomeMeth(10));
|
||||||
|
EXPECT_EQ(10, scoper.get()->SomeMeth(10));
|
||||||
|
EXPECT_EQ(10, (*scoper).SomeMeth(10));
|
||||||
|
}
|
||||||
|
EXPECT_EQ(0, constructed);
|
||||||
|
|
||||||
|
// Test reset() and release()
|
||||||
|
{
|
||||||
|
scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed));
|
||||||
|
EXPECT_EQ(1, constructed);
|
||||||
|
EXPECT_TRUE(scoper.get());
|
||||||
|
|
||||||
|
scoper.reset(new ConDecLogger(&constructed));
|
||||||
|
EXPECT_EQ(1, constructed);
|
||||||
|
EXPECT_TRUE(scoper.get());
|
||||||
|
|
||||||
|
scoper.reset();
|
||||||
|
EXPECT_EQ(0, constructed);
|
||||||
|
EXPECT_FALSE(scoper.get());
|
||||||
|
|
||||||
|
scoper.reset(new ConDecLogger(&constructed));
|
||||||
|
EXPECT_EQ(1, constructed);
|
||||||
|
EXPECT_TRUE(scoper.get());
|
||||||
|
|
||||||
|
ConDecLogger* take = scoper.release();
|
||||||
|
EXPECT_EQ(1, constructed);
|
||||||
|
EXPECT_FALSE(scoper.get());
|
||||||
|
delete take;
|
||||||
|
EXPECT_EQ(0, constructed);
|
||||||
|
|
||||||
|
scoper.reset(new ConDecLogger(&constructed));
|
||||||
|
EXPECT_EQ(1, constructed);
|
||||||
|
EXPECT_TRUE(scoper.get());
|
||||||
|
}
|
||||||
|
EXPECT_EQ(0, constructed);
|
||||||
|
|
||||||
|
// Test swap(), == and !=
|
||||||
|
{
|
||||||
|
scoped_ptr<ConDecLogger> scoper1;
|
||||||
|
scoped_ptr<ConDecLogger> scoper2;
|
||||||
|
EXPECT_TRUE(scoper1 == scoper2.get());
|
||||||
|
EXPECT_FALSE(scoper1 != scoper2.get());
|
||||||
|
|
||||||
|
ConDecLogger* logger = new ConDecLogger(&constructed);
|
||||||
|
scoper1.reset(logger);
|
||||||
|
EXPECT_EQ(logger, scoper1.get());
|
||||||
|
EXPECT_FALSE(scoper2.get());
|
||||||
|
EXPECT_FALSE(scoper1 == scoper2.get());
|
||||||
|
EXPECT_TRUE(scoper1 != scoper2.get());
|
||||||
|
|
||||||
|
scoper2.swap(scoper1);
|
||||||
|
EXPECT_EQ(logger, scoper2.get());
|
||||||
|
EXPECT_FALSE(scoper1.get());
|
||||||
|
EXPECT_FALSE(scoper1 == scoper2.get());
|
||||||
|
EXPECT_TRUE(scoper1 != scoper2.get());
|
||||||
|
}
|
||||||
|
EXPECT_EQ(0, constructed);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ScopedPtrTest, ScopedArray) {
|
||||||
|
static const int kNumLoggers = 12;
|
||||||
|
|
||||||
|
int constructed = 0;
|
||||||
|
|
||||||
|
{
|
||||||
|
scoped_array<ConDecLogger> scoper(new ConDecLogger[kNumLoggers]);
|
||||||
|
EXPECT_TRUE(scoper.get());
|
||||||
|
EXPECT_EQ(&scoper[0], scoper.get());
|
||||||
|
for (int i = 0; i < kNumLoggers; ++i) {
|
||||||
|
scoper[i].set_ptr(&constructed);
|
||||||
|
}
|
||||||
|
EXPECT_EQ(12, constructed);
|
||||||
|
|
||||||
|
EXPECT_EQ(10, scoper.get()->SomeMeth(10));
|
||||||
|
EXPECT_EQ(10, scoper[2].SomeMeth(10));
|
||||||
|
}
|
||||||
|
EXPECT_EQ(0, constructed);
|
||||||
|
|
||||||
|
// Test reset() and release()
|
||||||
|
{
|
||||||
|
scoped_array<ConDecLogger> scoper;
|
||||||
|
EXPECT_FALSE(scoper.get());
|
||||||
|
scoper.release();
|
||||||
|
EXPECT_FALSE(scoper.get());
|
||||||
|
scoper.reset();
|
||||||
|
EXPECT_FALSE(scoper.get());
|
||||||
|
|
||||||
|
scoper.reset(new ConDecLogger[kNumLoggers]);
|
||||||
|
for (int i = 0; i < kNumLoggers; ++i) {
|
||||||
|
scoper[i].set_ptr(&constructed);
|
||||||
|
}
|
||||||
|
EXPECT_EQ(12, constructed);
|
||||||
|
scoper.reset();
|
||||||
|
EXPECT_EQ(0, constructed);
|
||||||
|
|
||||||
|
scoper.reset(new ConDecLogger[kNumLoggers]);
|
||||||
|
for (int i = 0; i < kNumLoggers; ++i) {
|
||||||
|
scoper[i].set_ptr(&constructed);
|
||||||
|
}
|
||||||
|
EXPECT_EQ(12, constructed);
|
||||||
|
ConDecLogger* ptr = scoper.release();
|
||||||
|
EXPECT_EQ(12, constructed);
|
||||||
|
delete[] ptr;
|
||||||
|
EXPECT_EQ(0, constructed);
|
||||||
|
}
|
||||||
|
EXPECT_EQ(0, constructed);
|
||||||
|
|
||||||
|
// Test swap(), == and !=
|
||||||
|
{
|
||||||
|
scoped_array<ConDecLogger> scoper1;
|
||||||
|
scoped_array<ConDecLogger> scoper2;
|
||||||
|
EXPECT_TRUE(scoper1 == scoper2.get());
|
||||||
|
EXPECT_FALSE(scoper1 != scoper2.get());
|
||||||
|
|
||||||
|
ConDecLogger* loggers = new ConDecLogger[kNumLoggers];
|
||||||
|
for (int i = 0; i < kNumLoggers; ++i) {
|
||||||
|
loggers[i].set_ptr(&constructed);
|
||||||
|
}
|
||||||
|
scoper1.reset(loggers);
|
||||||
|
EXPECT_EQ(loggers, scoper1.get());
|
||||||
|
EXPECT_FALSE(scoper2.get());
|
||||||
|
EXPECT_FALSE(scoper1 == scoper2.get());
|
||||||
|
EXPECT_TRUE(scoper1 != scoper2.get());
|
||||||
|
|
||||||
|
scoper2.swap(scoper1);
|
||||||
|
EXPECT_EQ(loggers, scoper2.get());
|
||||||
|
EXPECT_FALSE(scoper1.get());
|
||||||
|
EXPECT_FALSE(scoper1 == scoper2.get());
|
||||||
|
EXPECT_TRUE(scoper1 != scoper2.get());
|
||||||
|
}
|
||||||
|
EXPECT_EQ(0, constructed);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO scoped_ptr_malloc
|
24
base/tuple.h
24
base/tuple.h
@@ -2,6 +2,30 @@
|
|||||||
// Use of this source code is governed by a BSD-style license that can be
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
// A Tuple is a generic templatized container, similar in concept to std::pair.
|
||||||
|
// There are classes Tuple0 to Tuple5, cooresponding to the number of elements
|
||||||
|
// it contains. The convenient MakeTuple() function takes 0 to 5 arguments,
|
||||||
|
// and will construct and return the appropriate Tuple object. The functions
|
||||||
|
// DispatchToMethod and DispatchToFunction take a function pointer or instance
|
||||||
|
// and method pointer, and unpack a tuple into arguments to the call.
|
||||||
|
//
|
||||||
|
// Tuple elements are copied by value, and stored in the tuple. See the unit
|
||||||
|
// tests for more details of how/when the values are copied.
|
||||||
|
//
|
||||||
|
// Example usage:
|
||||||
|
// // These two methods of creating a Tuple are identical.
|
||||||
|
// Tuple2<int, const char*> tuple_a(1, "wee");
|
||||||
|
// Tuple2<int, const char*> tuple_b = MakeTuple(1, "wee");
|
||||||
|
//
|
||||||
|
// void SomeFunc(int a, const char* b) { }
|
||||||
|
// DispatchToFunction(&SomeFunc, tuple_a); // SomeFunc(1, "wee")
|
||||||
|
// DispatchToFunction(
|
||||||
|
// &SomeFunc, MakeTuple(10, "foo")); // SomeFunc(10, "foo")
|
||||||
|
//
|
||||||
|
// struct { void SomeMeth(int a, int b, int c) { } } foo;
|
||||||
|
// DispatchToMethod(&foo, &Foo::SomeMeth, MakeTuple(1, 2, 3));
|
||||||
|
// // foo->SomeMeth(1, 2, 3);
|
||||||
|
|
||||||
#ifndef BASE_TUPLE_H__
|
#ifndef BASE_TUPLE_H__
|
||||||
#define BASE_TUPLE_H__
|
#define BASE_TUPLE_H__
|
||||||
|
|
||||||
|
109
base/tuple_unittest.cc
Normal file
109
base/tuple_unittest.cc
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
#include "base/tuple.h"
|
||||||
|
#include "testing/gtest/include/gtest/gtest.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
void DoAdd(int a, int b, int c, int* res) {
|
||||||
|
*res = a + b + c;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Addy {
|
||||||
|
Addy() { }
|
||||||
|
void DoAdd(int a, int b, int c, int d, int* res) {
|
||||||
|
*res = a + b + c + d;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST(TupleTest, Basic) {
|
||||||
|
Tuple0 t0 = MakeTuple();
|
||||||
|
Tuple1<int> t1(1);
|
||||||
|
Tuple2<int, const char*> t2 = MakeTuple(1, static_cast<const char*>("wee"));
|
||||||
|
Tuple3<int, int, int> t3(1, 2, 3);
|
||||||
|
Tuple4<int, int, int, int*> t4(1, 2, 3, &t1.a);
|
||||||
|
Tuple5<int, int, int, int, int*> t5(1, 2, 3, 4, &t4.a);
|
||||||
|
|
||||||
|
EXPECT_EQ(1, t1.a);
|
||||||
|
EXPECT_EQ(1, t2.a);
|
||||||
|
EXPECT_EQ(1, t3.a);
|
||||||
|
EXPECT_EQ(2, t3.b);
|
||||||
|
EXPECT_EQ(3, t3.c);
|
||||||
|
EXPECT_EQ(1, t4.a);
|
||||||
|
EXPECT_EQ(2, t4.b);
|
||||||
|
EXPECT_EQ(3, t4.c);
|
||||||
|
EXPECT_EQ(1, t5.a);
|
||||||
|
EXPECT_EQ(2, t5.b);
|
||||||
|
EXPECT_EQ(3, t5.c);
|
||||||
|
EXPECT_EQ(4, t5.d);
|
||||||
|
|
||||||
|
|
||||||
|
EXPECT_EQ(1, t1.a);
|
||||||
|
DispatchToFunction(&DoAdd, t4);
|
||||||
|
EXPECT_EQ(6, t1.a);
|
||||||
|
|
||||||
|
int res = 0;
|
||||||
|
DispatchToFunction(&DoAdd, MakeTuple(9, 8, 7, &res));
|
||||||
|
EXPECT_EQ(24, res);
|
||||||
|
|
||||||
|
Addy addy;
|
||||||
|
EXPECT_EQ(1, t4.a);
|
||||||
|
DispatchToMethod(&addy, &Addy::DoAdd, t5);
|
||||||
|
EXPECT_EQ(10, t4.a);
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
struct CopyLogger {
|
||||||
|
CopyLogger() { ++TimesConstructed; }
|
||||||
|
CopyLogger(const CopyLogger& tocopy) { ++TimesConstructed; ++TimesCopied; }
|
||||||
|
~CopyLogger() { }
|
||||||
|
|
||||||
|
static int TimesCopied;
|
||||||
|
static int TimesConstructed;
|
||||||
|
};
|
||||||
|
|
||||||
|
void SomeLoggerMethRef(const CopyLogger& logy, const CopyLogger* ptr, bool* b) {
|
||||||
|
*b = &logy == ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SomeLoggerMethCopy(CopyLogger logy, const CopyLogger* ptr, bool* b) {
|
||||||
|
*b = &logy == ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
int CopyLogger::TimesCopied = 0;
|
||||||
|
int CopyLogger::TimesConstructed = 0;
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST(TupleTest, Copying) {
|
||||||
|
CopyLogger logger;
|
||||||
|
EXPECT_EQ(0, CopyLogger::TimesCopied);
|
||||||
|
EXPECT_EQ(1, CopyLogger::TimesConstructed);
|
||||||
|
|
||||||
|
bool res = false;
|
||||||
|
|
||||||
|
// Creating the tuple should copy the class to store internally in the tuple.
|
||||||
|
Tuple3<CopyLogger, CopyLogger*, bool*> tuple(logger, &logger, &res);
|
||||||
|
tuple.b = &tuple.a;
|
||||||
|
EXPECT_EQ(2, CopyLogger::TimesConstructed);
|
||||||
|
EXPECT_EQ(1, CopyLogger::TimesCopied);
|
||||||
|
|
||||||
|
// Our internal Logger and the one passed to the function should be the same.
|
||||||
|
res = false;
|
||||||
|
DispatchToFunction(&SomeLoggerMethRef, tuple);
|
||||||
|
EXPECT_TRUE(res);
|
||||||
|
EXPECT_EQ(2, CopyLogger::TimesConstructed);
|
||||||
|
EXPECT_EQ(1, CopyLogger::TimesCopied);
|
||||||
|
|
||||||
|
// Now they should be different, since the function call will make a copy.
|
||||||
|
res = false;
|
||||||
|
DispatchToFunction(&SomeLoggerMethCopy, tuple);
|
||||||
|
EXPECT_FALSE(res);
|
||||||
|
EXPECT_EQ(3, CopyLogger::TimesConstructed);
|
||||||
|
EXPECT_EQ(2, CopyLogger::TimesCopied);
|
||||||
|
}
|
Reference in New Issue
Block a user