Skip to content

Commit a927497

Browse files
committed
Make type of pointer the template argument.
1 parent 56cf4c7 commit a927497

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

Firestore/core/src/firebase/firestore/util/vector_of_ptr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ namespace util {
4141
* for some reason, usually this is to enable polymorphism or because copying
4242
* values of T is expensive.
4343
*/
44-
template <typename T, typename P = std::shared_ptr<T>>
44+
template <typename P>
4545
class vector_of_ptr {
4646
public:
47-
using value_type = T;
4847
using pointer_type = P;
48+
using value_type = decltype(*std::declval<P>());
4949
using vector_type = std::vector<P>;
5050

5151
vector_of_ptr() = default;

Firestore/core/test/firebase/firestore/util/vector_of_ptr_test.cc

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,52 @@
1616

1717
#include "Firestore/core/src/firebase/firestore/util/vector_of_ptr.h"
1818

19+
#include "absl/memory/memory.h"
1920
#include "gtest/gtest.h"
2021

2122
namespace firebase {
2223
namespace firestore {
2324
namespace util {
2425

2526
TEST(VectorOfPtrTest, DefaultConstructor) {
26-
vector_of_ptr<int> values;
27+
vector_of_ptr<std::shared_ptr<int>> values;
2728
EXPECT_EQ(0, values.size());
2829
}
2930

3031
TEST(VectorOfPtrTest, PushBack) {
31-
vector_of_ptr<int> values;
32+
vector_of_ptr<std::shared_ptr<int>> values;
3233
values.push_back(std::make_shared<int>(0));
3334
values.push_back(std::make_shared<int>(42));
3435
EXPECT_EQ(2, values.size());
3536
}
3637

3738
TEST(VectorOfPtrTest, BracedInitialization) {
38-
vector_of_ptr<int> brace_initialized_ints{std::make_shared<int>(0),
39-
std::make_shared<int>(1)};
39+
vector_of_ptr<std::shared_ptr<int>> brace_initialized_ints{
40+
std::make_shared<int>(0), std::make_shared<int>(1)};
4041

4142
EXPECT_EQ(2, brace_initialized_ints.size());
4243

4344
brace_initialized_ints = {};
4445
EXPECT_EQ(0, brace_initialized_ints.size());
4546
}
4647

48+
TEST(VectorOfPtrTest, WorksWithUniquePtr) {
49+
vector_of_ptr<std::unique_ptr<int>> values;
50+
values.push_back(absl::make_unique<int>(42));
51+
52+
auto pointer = absl::make_unique<int>(0);
53+
values.push_back(std::move(pointer));
54+
55+
ASSERT_EQ(2, values.size());
56+
}
57+
4758
TEST(VectorOfPtrTest, EqualityIsValueEquality) {
48-
vector_of_ptr<int> lhs = {std::make_shared<int>(0), std::make_shared<int>(1)};
49-
vector_of_ptr<int> rhs = {std::make_shared<int>(0), std::make_shared<int>(1)};
50-
vector_of_ptr<int> other = {std::make_shared<int>(1),
51-
std::make_shared<int>(0)};
52-
vector_of_ptr<int> contains_nulls = {nullptr, nullptr};
53-
vector_of_ptr<int> empty;
59+
using int_ptr_vector = vector_of_ptr<std::shared_ptr<int>>;
60+
int_ptr_vector lhs = {std::make_shared<int>(0), std::make_shared<int>(1)};
61+
int_ptr_vector rhs = {std::make_shared<int>(0), std::make_shared<int>(1)};
62+
int_ptr_vector other = {std::make_shared<int>(1), std::make_shared<int>(0)};
63+
int_ptr_vector contains_nulls = {nullptr, nullptr};
64+
int_ptr_vector empty;
5465

5566
EXPECT_EQ(lhs, lhs);
5667
EXPECT_EQ(lhs, rhs);
@@ -62,7 +73,7 @@ TEST(VectorOfPtrTest, EqualityIsValueEquality) {
6273
TEST(VectorOfPtrTest, IterationIsOnPointers) {
6374
std::shared_ptr<int> pointers[] = {std::make_shared<int>(-1),
6475
std::make_shared<int>(42)};
65-
vector_of_ptr<int> vector = {pointers[0], pointers[1]};
76+
vector_of_ptr<std::shared_ptr<int>> vector = {pointers[0], pointers[1]};
6677

6778
size_t pos = 0;
6879
for (const std::shared_ptr<int>& element : vector) {

0 commit comments

Comments
 (0)