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 1058 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1069 } | 1069 } |
1070 | 1070 |
1071 bool ListValue::Set(size_t index, Value* in_value) { | 1071 bool ListValue::Set(size_t index, Value* in_value) { |
1072 return Set(index, WrapUnique(in_value)); | 1072 return Set(index, WrapUnique(in_value)); |
1073 } | 1073 } |
1074 | 1074 |
1075 bool ListValue::Set(size_t index, std::unique_ptr<Value> in_value) { | 1075 bool ListValue::Set(size_t index, std::unique_ptr<Value> in_value) { |
1076 if (!in_value) | 1076 if (!in_value) |
1077 return false; | 1077 return false; |
1078 | 1078 |
1079 if (index >= list_->size()) { | 1079 if (index >= list_->size()) |
1080 // Pad out any intermediate indexes with null settings | 1080 list_->resize(index + 1); |
1081 while (index > list_->size()) | 1081 |
1082 Append(MakeUnique<Value>()); | 1082 (*list_)[index] = std::move(*in_value); |
1083 Append(std::move(in_value)); | |
1084 } else { | |
1085 (*list_)[index] = std::move(*in_value); | |
1086 } | |
1087 return true; | 1083 return true; |
1088 } | 1084 } |
1089 | 1085 |
1090 bool ListValue::Get(size_t index, const Value** out_value) const { | 1086 bool ListValue::Get(size_t index, const Value** out_value) const { |
1091 if (index >= list_->size()) | 1087 if (index >= list_->size()) |
1092 return false; | 1088 return false; |
1093 | 1089 |
1094 if (out_value) | 1090 if (out_value) |
1095 *out_value = &(*list_)[index]; | 1091 *out_value = &(*list_)[index]; |
1096 | 1092 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1227 *out_value = MakeUnique<Value>(std::move(*iter)); | 1223 *out_value = MakeUnique<Value>(std::move(*iter)); |
1228 | 1224 |
1229 return list_->erase(iter); | 1225 return list_->erase(iter); |
1230 } | 1226 } |
1231 | 1227 |
1232 void ListValue::Append(std::unique_ptr<Value> in_value) { | 1228 void ListValue::Append(std::unique_ptr<Value> in_value) { |
1233 list_->push_back(std::move(*in_value)); | 1229 list_->push_back(std::move(*in_value)); |
1234 } | 1230 } |
1235 | 1231 |
1236 void ListValue::AppendBoolean(bool in_value) { | 1232 void ListValue::AppendBoolean(bool in_value) { |
1237 Append(MakeUnique<Value>(in_value)); | 1233 list_->emplace_back(in_value); |
1238 } | 1234 } |
1239 | 1235 |
1240 void ListValue::AppendInteger(int in_value) { | 1236 void ListValue::AppendInteger(int in_value) { |
1241 Append(MakeUnique<Value>(in_value)); | 1237 list_->emplace_back(in_value); |
1242 } | 1238 } |
1243 | 1239 |
1244 void ListValue::AppendDouble(double in_value) { | 1240 void ListValue::AppendDouble(double in_value) { |
1245 Append(MakeUnique<Value>(in_value)); | 1241 list_->emplace_back(in_value); |
1246 } | 1242 } |
1247 | 1243 |
1248 void ListValue::AppendString(StringPiece in_value) { | 1244 void ListValue::AppendString(StringPiece in_value) { |
1249 Append(MakeUnique<Value>(in_value)); | 1245 list_->emplace_back(in_value); |
1250 } | 1246 } |
1251 | 1247 |
1252 void ListValue::AppendString(const string16& in_value) { | 1248 void ListValue::AppendString(const string16& in_value) { |
1253 Append(MakeUnique<Value>(in_value)); | 1249 list_->emplace_back(in_value); |
1254 } | 1250 } |
1255 | 1251 |
1256 void ListValue::AppendStrings(const std::vector<std::string>& in_values) { | 1252 void ListValue::AppendStrings(const std::vector<std::string>& in_values) { |
1257 for (std::vector<std::string>::const_iterator it = in_values.begin(); | 1253 list_->reserve(list_->size() + in_values.size()); |
1258 it != in_values.end(); ++it) { | 1254 for (const auto& in_value : in_values) |
1259 AppendString(*it); | 1255 list_->emplace_back(in_value); |
1260 } | |
1261 } | 1256 } |
1262 | 1257 |
1263 void ListValue::AppendStrings(const std::vector<string16>& in_values) { | 1258 void ListValue::AppendStrings(const std::vector<string16>& in_values) { |
1264 for (std::vector<string16>::const_iterator it = in_values.begin(); | 1259 list_->reserve(list_->size() + in_values.size()); |
1265 it != in_values.end(); ++it) { | 1260 for (const auto& in_value : in_values) |
1266 AppendString(*it); | 1261 list_->emplace_back(in_value); |
1267 } | |
1268 } | 1262 } |
1269 | 1263 |
1270 bool ListValue::AppendIfNotPresent(std::unique_ptr<Value> in_value) { | 1264 bool ListValue::AppendIfNotPresent(std::unique_ptr<Value> in_value) { |
1271 DCHECK(in_value); | 1265 DCHECK(in_value); |
1272 if (std::find(list_->begin(), list_->end(), *in_value) != list_->end()) | 1266 if (std::find(list_->begin(), list_->end(), *in_value) != list_->end()) |
1273 return false; | 1267 return false; |
1274 | 1268 |
1275 list_->push_back(std::move(*in_value)); | 1269 list_->push_back(std::move(*in_value)); |
1276 return true; | 1270 return true; |
1277 } | 1271 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1315 } | 1309 } |
1316 | 1310 |
1317 std::ostream& operator<<(std::ostream& out, const Value::Type& type) { | 1311 std::ostream& operator<<(std::ostream& out, const Value::Type& type) { |
1318 if (static_cast<int>(type) < 0 || | 1312 if (static_cast<int>(type) < 0 || |
1319 static_cast<size_t>(type) >= arraysize(kTypeNames)) | 1313 static_cast<size_t>(type) >= arraysize(kTypeNames)) |
1320 return out << "Invalid Type (index = " << static_cast<int>(type) << ")"; | 1314 return out << "Invalid Type (index = " << static_cast<int>(type) << ")"; |
1321 return out << Value::GetTypeName(type); | 1315 return out << Value::GetTypeName(type); |
1322 } | 1316 } |
1323 | 1317 |
1324 } // namespace base | 1318 } // namespace base |
OLD | NEW |