Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Unified Diff: base/values.h

Issue 2771923005: Implement comparison operators for base::Value (Closed)
Patch Set: Fix compilation error Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/values.cc » ('j') | chrome/browser/ui/webui/options/preferences_browsertest.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/values.h
diff --git a/base/values.h b/base/values.h
index b7ca355799898bb86329cb584a55d2891440880f..c338203dc33f38c02fd751950cf7fe01619b292b 100644
--- a/base/values.h
+++ b/base/values.h
@@ -161,11 +161,116 @@ class BASE_EXPORT Value {
// Preferred version of DeepCopy. TODO(estade): remove the above.
std::unique_ptr<Value> CreateDeepCopy() const;
+ // Comparison operators.
+ friend bool operator==(const Value& lhs, const Value& rhs) {
+ if (lhs.type_ != rhs.type_)
+ return false;
+
+ switch (lhs.type_) {
+ case Type::NONE:
+ return true;
+ case Type::BOOLEAN:
+ return lhs.bool_value_ == rhs.bool_value_;
+ case Type::INTEGER:
+ return lhs.int_value_ == rhs.int_value_;
+ case Type::DOUBLE:
+ return lhs.double_value_ == rhs.double_value_;
+ case Type::STRING:
+ return *lhs.string_value_ == *rhs.string_value_;
+ case Type::BINARY:
+ return *lhs.binary_value_ == *rhs.binary_value_;
+ // TODO(crbug.com/646113): Clean this up when DictionaryValue and
+ // ListValue are completely inlined.
+ case Type::DICTIONARY:
+ if ((*lhs.dict_ptr_)->size() != (*rhs.dict_ptr_)->size())
+ return false;
+ return std::equal(std::begin(**lhs.dict_ptr_),
+ std::end(**lhs.dict_ptr_),
+ std::begin(**rhs.dict_ptr_),
+ [](const DictStorage::value_type& u,
+ const DictStorage::value_type& v) {
+ return std::tie(u.first, *u.second) ==
+ std::tie(v.first, *v.second);
+ });
+ case Type::LIST:
+ if (lhs.list_->size() != rhs.list_->size())
+ return false;
+ return std::equal(
+ std::begin(*lhs.list_), std::end(*lhs.list_),
+ std::begin(*rhs.list_),
+ [](const ListStorage::value_type& u,
+ const ListStorage::value_type& v) { return *u == *v; });
+ }
+
+ NOTREACHED();
+ return false;
+ }
+
+ friend bool operator!=(const Value& lhs, const Value& rhs) {
+ return !(lhs == rhs);
+ }
+
+ friend bool operator<(const Value& lhs, const Value& rhs) {
+ if (lhs.type_ != rhs.type_)
+ return lhs.type_ < rhs.type_;
+
+ switch (lhs.type_) {
+ case Type::NONE:
+ return false;
+ case Type::BOOLEAN:
+ return lhs.bool_value_ < rhs.bool_value_;
+ case Type::INTEGER:
+ return lhs.int_value_ < rhs.int_value_;
+ case Type::DOUBLE:
+ return lhs.double_value_ < rhs.double_value_;
+ case Type::STRING:
+ return *lhs.string_value_ < *rhs.string_value_;
+ case Type::BINARY:
+ return *lhs.binary_value_ < *rhs.binary_value_;
+ // TODO(crbug.com/646113): Clean this up when DictionaryValue and
+ // ListValue are completely inlined.
+ case Type::DICTIONARY:
+ return std::lexicographical_compare(
+ std::begin(**lhs.dict_ptr_), std::end(**lhs.dict_ptr_),
+ std::begin(**rhs.dict_ptr_), std::end(**rhs.dict_ptr_),
+ [](const DictStorage::value_type& u,
+ const DictStorage::value_type& v) {
+ return std::tie(u.first, *u.second) <
+ std::tie(v.first, *v.second);
+ });
+ case Type::LIST:
+ return std::lexicographical_compare(
+ std::begin(*lhs.list_), std::end(*lhs.list_),
+ std::begin(*rhs.list_), std::end(*rhs.list_),
+ [](const ListStorage::value_type& u,
+ const ListStorage::value_type& v) { return *u < *v; });
+ }
+
+ NOTREACHED();
+ return false;
+ }
+
+ friend bool operator>(const Value& lhs, const Value& rhs) {
+ return rhs < lhs;
+ }
+
+ friend bool operator<=(const Value& lhs, const Value& rhs) {
+ return !(rhs < lhs);
+ }
+
+ friend bool operator>=(const Value& lhs, const Value& rhs) {
+ return !(lhs < rhs);
+ }
+
// Compares if two Value objects have equal contents.
+ // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead.
+ // TODO(crbug.com/646113): Delete this and migrate callsites.
bool Equals(const Value* other) const;
// Compares if two Value objects have equal contents. Can handle NULLs.
// NULLs are considered equal but different from Value::CreateNullValue().
+ // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead.
+ // TODO(crbug.com/646113): Delete this and migrate callsites.
static bool Equals(const Value* a, const Value* b);
protected:
« no previous file with comments | « no previous file | base/values.cc » ('j') | chrome/browser/ui/webui/options/preferences_browsertest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698