Skip to content

Commit ddd3f17

Browse files
authored
Add a vector of pointers type (#3249)
1 parent 6e38bec commit ddd3f17

File tree

6 files changed

+209
-0
lines changed

6 files changed

+209
-0
lines changed

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@
182182
535F51F2FF2AB52A6E629091 /* FSTLevelDBMutationQueueTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E0872021552A00B64F25 /* FSTLevelDBMutationQueueTests.mm */; };
183183
53AB47E44D897C81A94031F6 /* write.pb.cc in Sources */ = {isa = PBXBuildFile; fileRef = 544129D921C2DDC800EFB9CC /* write.pb.cc */; };
184184
54080260D85A6F583E61DA1D /* FSTLocalSerializerTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E08A2021552A00B64F25 /* FSTLocalSerializerTests.mm */; };
185+
540C379D22C1741000E70F15 /* vector_of_ptr_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 540C379C22C1741000E70F15 /* vector_of_ptr_test.cc */; };
186+
540C379E22C1741000E70F15 /* vector_of_ptr_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 540C379C22C1741000E70F15 /* vector_of_ptr_test.cc */; };
187+
540C379F22C1741000E70F15 /* vector_of_ptr_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 540C379C22C1741000E70F15 /* vector_of_ptr_test.cc */; };
185188
54131E9720ADE679001DF3FF /* string_format_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 54131E9620ADE678001DF3FF /* string_format_test.cc */; };
186189
544129DA21C2DDC800EFB9CC /* common.pb.cc in Sources */ = {isa = PBXBuildFile; fileRef = 544129D221C2DDC800EFB9CC /* common.pb.cc */; };
187190
544129DB21C2DDC800EFB9CC /* firestore.pb.cc in Sources */ = {isa = PBXBuildFile; fileRef = 544129D421C2DDC800EFB9CC /* firestore.pb.cc */; };
@@ -783,6 +786,7 @@
783786
444B7AB3F5A2929070CB1363 /* hard_assert_test.cc */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.cpp.cpp; path = hard_assert_test.cc; sourceTree = "<group>"; };
784787
4C73C0CC6F62A90D8573F383 /* string_apple_benchmark.mm */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.cpp.objcpp; path = string_apple_benchmark.mm; sourceTree = "<group>"; };
785788
5342CDDB137B4E93E2E85CCA /* byte_string_test.cc */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.cpp.cpp; name = byte_string_test.cc; path = nanopb/byte_string_test.cc; sourceTree = "<group>"; };
789+
540C379C22C1741000E70F15 /* vector_of_ptr_test.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = vector_of_ptr_test.cc; sourceTree = "<group>"; };
786790
54131E9620ADE678001DF3FF /* string_format_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = string_format_test.cc; sourceTree = "<group>"; };
787791
544129D021C2DDC800EFB9CC /* query.pb.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = query.pb.h; sourceTree = "<group>"; };
788792
544129D121C2DDC800EFB9CC /* common.pb.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = common.pb.h; sourceTree = "<group>"; };
@@ -1316,6 +1320,7 @@
13161320
79507DF8378D3C42F5B36268 /* string_win_test.cc */,
13171321
B68B1E002213A764008977EF /* to_string_apple_test.mm */,
13181322
B696858D2214B53900271095 /* to_string_test.cc */,
1323+
540C379C22C1741000E70F15 /* vector_of_ptr_test.cc */,
13191324
);
13201325
path = util;
13211326
sourceTree = "<group>";
@@ -3150,6 +3155,7 @@
31503155
8DA258092DD856D829D973B5 /* transform_operations_test.mm in Sources */,
31513156
5F19F66D8B01BA2B97579017 /* tree_sorted_map_test.cc in Sources */,
31523157
16F52ECC6FA8A0587CD779EB /* user_test.cc in Sources */,
3158+
540C379E22C1741000E70F15 /* vector_of_ptr_test.cc in Sources */,
31533159
E3C0E5F834A82EEE9F8C4519 /* watch_change_test.mm in Sources */,
31543160
53AB47E44D897C81A94031F6 /* write.pb.cc in Sources */,
31553161
59E6941008253D4B0F77C2BA /* writer_test.cc in Sources */,
@@ -3325,6 +3331,7 @@
33253331
5C7FAF228D0F52CFFE9E41B5 /* transform_operations_test.mm in Sources */,
33263332
627253FDEC6BB5549FE77F4E /* tree_sorted_map_test.cc in Sources */,
33273333
596C782EFB68131380F8EEF8 /* user_test.cc in Sources */,
3334+
540C379F22C1741000E70F15 /* vector_of_ptr_test.cc in Sources */,
33283335
178FE1E277C63B3E7120BE56 /* watch_change_test.mm in Sources */,
33293336
A5AB1815C45FFC762981E481 /* write.pb.cc in Sources */,
33303337
A21819C437C3C80450D7EEEE /* writer_test.cc in Sources */,
@@ -3582,6 +3589,7 @@
35823589
54A0352720A3AED0003E0143 /* transform_operations_test.mm in Sources */,
35833590
549CCA5120A36DBC00BCEB75 /* tree_sorted_map_test.cc in Sources */,
35843591
ABC1D7DE2023A05300BA84F0 /* user_test.cc in Sources */,
3592+
540C379D22C1741000E70F15 /* vector_of_ptr_test.cc in Sources */,
35853593
B68FC0E521F6848700A7055C /* watch_change_test.mm in Sources */,
35863594
544129DE21C2DDC800EFB9CC /* write.pb.cc in Sources */,
35873595
3BA4EEA6153B3833F86B8104 /* writer_test.cc in Sources */,

Firestore/core/src/firebase/firestore/model/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ cc_library(
2222
document.h
2323
document_key.cc
2424
document_key.h
25+
document_key_set.h
2526
document_map.h
27+
document_map.mm
28+
document_set.h
29+
document_set.mm
2630
field_mask.h
2731
field_mask.cc
2832
field_path.cc

Firestore/core/src/firebase/firestore/util/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ cc_library(
210210
status_win.cc
211211
statusor.cc
212212
statusor.h
213+
statusor_callback.h
213214
statusor_internals.h
214215
strerror.cc
215216
strerror.h
@@ -253,10 +254,12 @@ cc_library(
253254
ordered_code.cc
254255
ordered_code.h
255256
range.h
257+
sanitizers.h
256258
string_util.cc
257259
string_util.h
258260
to_string.h
259261
type_traits.h
262+
vector_of_ptr.h
260263
warnings.h
261264
DEPENDS
262265
absl_base
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2019 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_VECTOR_OF_PTR_H_
18+
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_VECTOR_OF_PTR_H_
19+
20+
#include <initializer_list>
21+
#include <memory>
22+
#include <utility>
23+
#include <vector>
24+
25+
#include "absl/algorithm/container.h"
26+
27+
namespace firebase {
28+
namespace firestore {
29+
namespace util {
30+
31+
/**
32+
* A std::vector of some pointer type where equality and many other operations
33+
* are defined as operating on the value pointed to rather than on the pointers
34+
* themselves.
35+
*
36+
* Contrast with `std::vector<std::shared_ptr<T>>`, where `operator==` just
37+
* checks if the pointers in the collection are equal rather than checking if
38+
* the things the pointers point to are equal.
39+
*
40+
* This is useful in cases where values of type T need to be held by pointer
41+
* for some reason, usually this is to enable polymorphism or because copying
42+
* values of T is expensive.
43+
*/
44+
template <typename P>
45+
class vector_of_ptr {
46+
public:
47+
using pointer_type = P;
48+
using value_type = decltype(*P());
49+
using vector_type = std::vector<P>;
50+
51+
using iterator = typename vector_type::iterator;
52+
using const_iterator = typename vector_type::const_iterator;
53+
54+
vector_of_ptr() = default;
55+
vector_of_ptr(std::initializer_list<P> values) : values_(values) {
56+
}
57+
58+
size_t size() const {
59+
return values_.size();
60+
}
61+
62+
void push_back(P value) {
63+
values_.push_back(std::move(value));
64+
}
65+
66+
iterator begin() {
67+
return values_.begin();
68+
}
69+
const_iterator begin() const {
70+
return values_.begin();
71+
}
72+
73+
iterator end() {
74+
return values_.end();
75+
}
76+
const_iterator end() const {
77+
return values_.end();
78+
}
79+
80+
friend bool operator==(const vector_of_ptr& lhs, const vector_of_ptr& rhs) {
81+
return absl::c_equal(
82+
lhs.values_, rhs.values_, [](const P& left, const P& right) {
83+
return left == nullptr ? right == nullptr
84+
: right != nullptr && *left == *right;
85+
});
86+
}
87+
88+
friend bool operator!=(const vector_of_ptr& lhs, const vector_of_ptr& rhs) {
89+
return !(lhs == rhs);
90+
}
91+
92+
private:
93+
vector_type values_;
94+
};
95+
96+
} // namespace util
97+
} // namespace firestore
98+
} // namespace firebase
99+
100+
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_VECTOR_OF_PTR_H_

Firestore/core/test/firebase/firestore/util/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ cc_test(
163163
string_format_test.cc
164164
string_util_test.cc
165165
string_win_test.cc
166+
vector_of_ptr_test.cc
166167
DEPENDS
167168
absl_base
168169
absl_strings
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2019 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "Firestore/core/src/firebase/firestore/util/vector_of_ptr.h"
18+
19+
#include "absl/memory/memory.h"
20+
#include "gtest/gtest.h"
21+
22+
namespace firebase {
23+
namespace firestore {
24+
namespace util {
25+
26+
TEST(VectorOfPtrTest, DefaultConstructor) {
27+
vector_of_ptr<std::shared_ptr<int>> values;
28+
EXPECT_EQ(0, values.size());
29+
}
30+
31+
TEST(VectorOfPtrTest, PushBack) {
32+
vector_of_ptr<std::shared_ptr<int>> values;
33+
values.push_back(std::make_shared<int>(0));
34+
values.push_back(std::make_shared<int>(42));
35+
EXPECT_EQ(2, values.size());
36+
}
37+
38+
TEST(VectorOfPtrTest, BracedInitialization) {
39+
vector_of_ptr<std::shared_ptr<int>> brace_initialized_ints{
40+
std::make_shared<int>(0), std::make_shared<int>(1)};
41+
42+
EXPECT_EQ(2, brace_initialized_ints.size());
43+
44+
brace_initialized_ints = {};
45+
EXPECT_EQ(0, brace_initialized_ints.size());
46+
}
47+
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+
58+
TEST(VectorOfPtrTest, EqualityIsValueEquality) {
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;
65+
66+
EXPECT_EQ(empty, int_ptr_vector());
67+
68+
EXPECT_EQ(lhs, lhs);
69+
EXPECT_EQ(lhs, rhs);
70+
EXPECT_NE(lhs, other);
71+
EXPECT_NE(lhs, contains_nulls);
72+
EXPECT_NE(lhs, empty);
73+
74+
EXPECT_EQ(contains_nulls, contains_nulls);
75+
EXPECT_NE(contains_nulls, lhs);
76+
}
77+
78+
TEST(VectorOfPtrTest, IterationIsOnPointers) {
79+
std::shared_ptr<int> pointers[] = {std::make_shared<int>(-1),
80+
std::make_shared<int>(42)};
81+
vector_of_ptr<std::shared_ptr<int>> vector = {pointers[0], pointers[1]};
82+
83+
size_t pos = 0;
84+
for (const std::shared_ptr<int>& element : vector) {
85+
ASSERT_EQ(*pointers[pos], *element);
86+
++pos;
87+
}
88+
ASSERT_EQ(pos, sizeof(pointers) / sizeof(pointers[0]));
89+
}
90+
91+
} // namespace util
92+
} // namespace firestore
93+
} // namespace firebase

0 commit comments

Comments
 (0)