Skip to content

Commit 2e80c50

Browse files
authored
impl(common): make ErrorContext a collection (#10275)
1 parent a2c9082 commit 2e80c50

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

google/cloud/internal/error_metadata.h

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ namespace internal {
7474
class ErrorContext {
7575
public:
7676
using Container = std::vector<std::pair<std::string, std::string>>;
77+
using value_type = Container::value_type;
78+
using reference = Container::reference;
79+
using const_reference = Container::const_reference;
80+
using difference_type = Container::difference_type;
81+
using size_type = Container::size_type;
82+
using const_iterator = Container::const_iterator;
7783

7884
ErrorContext() = default;
7985
explicit ErrorContext(Container m) : metadata_(std::move(m)) {}
@@ -83,20 +89,32 @@ class ErrorContext {
8389
ErrorContext(ErrorContext&&) = default;
8490
ErrorContext& operator=(ErrorContext&&) = default;
8591

86-
template <typename... A>
87-
Container::reference emplace_back(A&&... a) {
88-
return metadata_.emplace_back(std::forward<A>(a)...);
92+
friend bool operator==(ErrorContext const& lhs, ErrorContext const& rhs) {
93+
return lhs.metadata_ == rhs.metadata_;
94+
}
95+
96+
friend bool operator!=(ErrorContext const& lhs, ErrorContext const& rhs) {
97+
return !(lhs == rhs);
8998
}
9099

91-
void push_back(Container::value_type p) {
92-
return metadata_.push_back(std::move(p));
100+
void swap(ErrorContext& rhs) { metadata_.swap(rhs.metadata_); }
101+
102+
template <typename... A>
103+
void emplace_back(A&&... a) {
104+
metadata_.emplace_back(std::forward<A>(a)...);
93105
}
94106

107+
void push_back(Container::value_type p) { metadata_.push_back(std::move(p)); }
108+
109+
size_type size() const { return metadata_.size(); }
110+
111+
size_type max_size() const { return metadata_.max_size(); }
112+
95113
bool empty() const { return metadata_.empty(); }
96114

97-
Container::const_iterator begin() const { return metadata_.begin(); }
115+
const_iterator begin() const { return metadata_.begin(); }
98116

99-
Container::const_iterator end() const { return metadata_.end(); }
117+
const_iterator end() const { return metadata_.end(); }
100118

101119
private:
102120
Container metadata_;

google/cloud/internal/error_metadata_test.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,22 @@ namespace internal {
2222
namespace {
2323

2424
using ::testing::HasSubstr;
25+
using ::testing::Pair;
2526
using ::testing::StartsWith;
27+
using ::testing::UnorderedElementsAre;
28+
29+
TEST(ErrorContext, Basic) {
30+
auto const other =
31+
ErrorContext{{{"key", "value"}, {"filename", "the-filename"}}};
32+
auto actual = ErrorContext{other};
33+
EXPECT_EQ(other, actual);
34+
actual.push_back(std::make_pair("k0", "v0"));
35+
actual.emplace_back("k1", "v1");
36+
EXPECT_NE(other, actual);
37+
EXPECT_THAT(actual, UnorderedElementsAre(Pair("key", "value"),
38+
Pair("filename", "the-filename"),
39+
Pair("k0", "v0"), Pair("k1", "v1")));
40+
}
2641

2742
TEST(ErrorContext, FormatEmpty) {
2843
EXPECT_EQ("error message", Format("error message", ErrorContext{}));

0 commit comments

Comments
 (0)