| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/values.h" | 5 #include "base/values.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <cmath> | 10 #include <cmath> |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 "string", "binary", "dictionary", "list"}; | 25 "string", "binary", "dictionary", "list"}; |
| 26 static_assert(arraysize(kTypeNames) == | 26 static_assert(arraysize(kTypeNames) == |
| 27 static_cast<size_t>(Value::Type::LIST) + 1, | 27 static_cast<size_t>(Value::Type::LIST) + 1, |
| 28 "kTypeNames Has Wrong Size"); | 28 "kTypeNames Has Wrong Size"); |
| 29 | 29 |
| 30 std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node); | 30 std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node); |
| 31 | 31 |
| 32 // Make a deep copy of |node|, but don't include empty lists or dictionaries | 32 // Make a deep copy of |node|, but don't include empty lists or dictionaries |
| 33 // in the copy. It's possible for this function to return NULL and it | 33 // in the copy. It's possible for this function to return NULL and it |
| 34 // expects |node| to always be non-NULL. | 34 // expects |node| to always be non-NULL. |
| 35 std::unique_ptr<ListValue> CopyListWithoutEmptyChildren(const ListValue& list) { | 35 std::unique_ptr<Value> CopyListWithoutEmptyChildren(const Value& list) { |
| 36 std::unique_ptr<ListValue> copy; | 36 Value copy(Value::Type::LIST); |
| 37 for (const auto& entry : list) { | 37 for (const auto& entry : list.GetList()) { |
| 38 std::unique_ptr<Value> child_copy = CopyWithoutEmptyChildren(entry); | 38 std::unique_ptr<Value> child_copy = CopyWithoutEmptyChildren(entry); |
| 39 if (child_copy) { | 39 if (child_copy) |
| 40 if (!copy) | 40 copy.GetList().push_back(std::move(*child_copy)); |
| 41 copy.reset(new ListValue); | |
| 42 copy->Append(std::move(child_copy)); | |
| 43 } | |
| 44 } | 41 } |
| 45 return copy; | 42 return copy.GetList().empty() ? nullptr : MakeUnique<Value>(std::move(copy)); |
| 46 } | 43 } |
| 47 | 44 |
| 48 std::unique_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren( | 45 std::unique_ptr<DictionaryValue> CopyDictionaryWithoutEmptyChildren( |
| 49 const DictionaryValue& dict) { | 46 const DictionaryValue& dict) { |
| 50 std::unique_ptr<DictionaryValue> copy; | 47 std::unique_ptr<DictionaryValue> copy; |
| 51 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { | 48 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
| 52 std::unique_ptr<Value> child_copy = CopyWithoutEmptyChildren(it.value()); | 49 std::unique_ptr<Value> child_copy = CopyWithoutEmptyChildren(it.value()); |
| 53 if (child_copy) { | 50 if (child_copy) { |
| 54 if (!copy) | 51 if (!copy) |
| 55 copy.reset(new DictionaryValue); | 52 copy.reset(new DictionaryValue); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 } | 160 } |
| 164 | 161 |
| 165 Value::Value(BlobStorage&& in_blob) noexcept : type_(Type::BINARY) { | 162 Value::Value(BlobStorage&& in_blob) noexcept : type_(Type::BINARY) { |
| 166 binary_value_.Init(std::move(in_blob)); | 163 binary_value_.Init(std::move(in_blob)); |
| 167 } | 164 } |
| 168 | 165 |
| 169 Value::Value(DictStorage&& in_dict) noexcept : type_(Type::DICTIONARY) { | 166 Value::Value(DictStorage&& in_dict) noexcept : type_(Type::DICTIONARY) { |
| 170 dict_.Init(std::move(in_dict)); | 167 dict_.Init(std::move(in_dict)); |
| 171 } | 168 } |
| 172 | 169 |
| 170 Value::Value(const ListStorage& in_list) : type_(Type::LIST) { |
| 171 list_.Init(in_list); |
| 172 } |
| 173 |
| 174 Value::Value(ListStorage&& in_list) noexcept : type_(Type::LIST) { |
| 175 list_.Init(std::move(in_list)); |
| 176 } |
| 177 |
| 173 Value& Value::operator=(const Value& that) { | 178 Value& Value::operator=(const Value& that) { |
| 174 if (type_ == that.type_) { | 179 if (type_ == that.type_) { |
| 175 InternalCopyAssignFromSameType(that); | 180 InternalCopyAssignFromSameType(that); |
| 176 } else { | 181 } else { |
| 177 // This is not a self assignment because the type_ doesn't match. | 182 // This is not a self assignment because the type_ doesn't match. |
| 178 InternalCleanup(); | 183 InternalCleanup(); |
| 179 InternalCopyConstructFrom(that); | 184 InternalCopyConstructFrom(that); |
| 180 } | 185 } |
| 181 | 186 |
| 182 return *this; | 187 return *this; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 const std::string& Value::GetString() const { | 227 const std::string& Value::GetString() const { |
| 223 CHECK(is_string()); | 228 CHECK(is_string()); |
| 224 return *string_value_; | 229 return *string_value_; |
| 225 } | 230 } |
| 226 | 231 |
| 227 const Value::BlobStorage& Value::GetBlob() const { | 232 const Value::BlobStorage& Value::GetBlob() const { |
| 228 CHECK(is_blob()); | 233 CHECK(is_blob()); |
| 229 return *binary_value_; | 234 return *binary_value_; |
| 230 } | 235 } |
| 231 | 236 |
| 237 Value::ListStorage& Value::GetList() { |
| 238 CHECK(is_list()); |
| 239 return *list_; |
| 240 } |
| 241 |
| 242 const Value::ListStorage& Value::GetList() const { |
| 243 CHECK(is_list()); |
| 244 return *list_; |
| 245 } |
| 246 |
| 232 size_t Value::GetSize() const { | 247 size_t Value::GetSize() const { |
| 233 return GetBlob().size(); | 248 return GetBlob().size(); |
| 234 } | 249 } |
| 235 | 250 |
| 236 const char* Value::GetBuffer() const { | 251 const char* Value::GetBuffer() const { |
| 237 return GetBlob().data(); | 252 return GetBlob().data(); |
| 238 } | 253 } |
| 239 | 254 |
| 240 bool Value::GetAsBoolean(bool* out_value) const { | 255 bool Value::GetAsBoolean(bool* out_value) const { |
| 241 if (out_value && is_bool()) { | 256 if (out_value && is_bool()) { |
| (...skipping 1067 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1309 } | 1324 } |
| 1310 | 1325 |
| 1311 std::ostream& operator<<(std::ostream& out, const Value::Type& type) { | 1326 std::ostream& operator<<(std::ostream& out, const Value::Type& type) { |
| 1312 if (static_cast<int>(type) < 0 || | 1327 if (static_cast<int>(type) < 0 || |
| 1313 static_cast<size_t>(type) >= arraysize(kTypeNames)) | 1328 static_cast<size_t>(type) >= arraysize(kTypeNames)) |
| 1314 return out << "Invalid Type (index = " << static_cast<int>(type) << ")"; | 1329 return out << "Invalid Type (index = " << static_cast<int>(type) << ")"; |
| 1315 return out << Value::GetTypeName(type); | 1330 return out << Value::GetTypeName(type); |
| 1316 } | 1331 } |
| 1317 | 1332 |
| 1318 } // namespace base | 1333 } // namespace base |
| OLD | NEW |