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

Side by Side Diff: base/values.cc

Issue 2804263002: IsValid (Closed)
Patch Set: Created 3 years, 8 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 unified diff | Download patch
« no previous file with comments | « base/values.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 InternalCleanup(); 190 InternalCleanup();
191 } 191 }
192 192
193 // static 193 // static
194 const char* Value::GetTypeName(Value::Type type) { 194 const char* Value::GetTypeName(Value::Type type) {
195 DCHECK_GE(static_cast<int>(type), 0); 195 DCHECK_GE(static_cast<int>(type), 0);
196 DCHECK_LT(static_cast<size_t>(type), arraysize(kTypeNames)); 196 DCHECK_LT(static_cast<size_t>(type), arraysize(kTypeNames));
197 return kTypeNames[static_cast<size_t>(type)]; 197 return kTypeNames[static_cast<size_t>(type)];
198 } 198 }
199 199
200 bool Value::IsValid() const {
201 switch (type_) {
202 case Type::NONE:
203 case Type::BOOLEAN:
204 case Type::INTEGER:
205 case Type::DOUBLE:
206 case Type::STRING:
207 case Type::BINARY:
208 return true;
209 case Type::DICTIONARY:
210 return *dict_ptr_ &&
211 std::all_of((*dict_ptr_)->begin(), (*dict_ptr_)->end(),
212 [](const DictStorage::value_type& val) {
213 return val.second && val.second->IsValid();
214 });
215 case Type::LIST:
216 return std::all_of(list_->begin(), list_->end(),
217 [](const std::unique_ptr<Value>& val) {
218 return val && val->IsValid();
219 });
220 }
221
222 NOTREACHED();
223 return false;
224 }
225
200 bool Value::GetBool() const { 226 bool Value::GetBool() const {
201 CHECK(is_bool()); 227 CHECK(is_bool());
202 return bool_value_; 228 return bool_value_;
203 } 229 }
204 230
205 int Value::GetInt() const { 231 int Value::GetInt() const {
206 CHECK(is_int()); 232 CHECK(is_int());
207 return int_value_; 233 return int_value_;
208 } 234 }
209 235
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 Set(path, new Value(in_value)); 694 Set(path, new Value(in_value));
669 } 695 }
670 696
671 void DictionaryValue::SetString(StringPiece path, const string16& in_value) { 697 void DictionaryValue::SetString(StringPiece path, const string16& in_value) {
672 Set(path, new Value(in_value)); 698 Set(path, new Value(in_value));
673 } 699 }
674 700
675 void DictionaryValue::SetWithoutPathExpansion(StringPiece key, 701 void DictionaryValue::SetWithoutPathExpansion(StringPiece key,
676 std::unique_ptr<Value> in_value) { 702 std::unique_ptr<Value> in_value) {
677 (**dict_ptr_)[key.as_string()] = std::move(in_value); 703 (**dict_ptr_)[key.as_string()] = std::move(in_value);
704 CHECK(IsValid());
678 } 705 }
679 706
680 void DictionaryValue::SetWithoutPathExpansion(StringPiece key, 707 void DictionaryValue::SetWithoutPathExpansion(StringPiece key,
681 Value* in_value) { 708 Value* in_value) {
682 SetWithoutPathExpansion(key, WrapUnique(in_value)); 709 SetWithoutPathExpansion(key, WrapUnique(in_value));
683 } 710 }
684 711
685 void DictionaryValue::SetBooleanWithoutPathExpansion(StringPiece path, 712 void DictionaryValue::SetBooleanWithoutPathExpansion(StringPiece path,
686 bool in_value) { 713 bool in_value) {
687 SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value)); 714 SetWithoutPathExpansion(path, base::MakeUnique<base::Value>(in_value));
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
1086 if (index >= list_->size()) { 1113 if (index >= list_->size()) {
1087 // Pad out any intermediate indexes with null settings 1114 // Pad out any intermediate indexes with null settings
1088 while (index > list_->size()) 1115 while (index > list_->size())
1089 Append(MakeUnique<Value>()); 1116 Append(MakeUnique<Value>());
1090 Append(std::move(in_value)); 1117 Append(std::move(in_value));
1091 } else { 1118 } else {
1092 // TODO(dcheng): remove this DCHECK once the raw pointer version is removed? 1119 // TODO(dcheng): remove this DCHECK once the raw pointer version is removed?
1093 DCHECK((*list_)[index] != in_value); 1120 DCHECK((*list_)[index] != in_value);
1094 (*list_)[index] = std::move(in_value); 1121 (*list_)[index] = std::move(in_value);
1095 } 1122 }
1123
1124 CHECK(IsValid());
1096 return true; 1125 return true;
1097 } 1126 }
1098 1127
1099 bool ListValue::Get(size_t index, const Value** out_value) const { 1128 bool ListValue::Get(size_t index, const Value** out_value) const {
1100 if (index >= list_->size()) 1129 if (index >= list_->size())
1101 return false; 1130 return false;
1102 1131
1103 if (out_value) 1132 if (out_value)
1104 *out_value = (*list_)[index].get(); 1133 *out_value = (*list_)[index].get();
1105 1134
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
1235 ListValue::iterator ListValue::Erase(iterator iter, 1264 ListValue::iterator ListValue::Erase(iterator iter,
1236 std::unique_ptr<Value>* out_value) { 1265 std::unique_ptr<Value>* out_value) {
1237 if (out_value) 1266 if (out_value)
1238 *out_value = std::move(*ListStorage::iterator(iter)); 1267 *out_value = std::move(*ListStorage::iterator(iter));
1239 1268
1240 return list_->erase(iter); 1269 return list_->erase(iter);
1241 } 1270 }
1242 1271
1243 void ListValue::Append(std::unique_ptr<Value> in_value) { 1272 void ListValue::Append(std::unique_ptr<Value> in_value) {
1244 list_->push_back(std::move(in_value)); 1273 list_->push_back(std::move(in_value));
1274 CHECK(IsValid());
1245 } 1275 }
1246 1276
1247 #if !defined(OS_LINUX) 1277 #if !defined(OS_LINUX)
1248 void ListValue::Append(Value* in_value) { 1278 void ListValue::Append(Value* in_value) {
1249 DCHECK(in_value); 1279 DCHECK(in_value);
1250 Append(WrapUnique(in_value)); 1280 Append(WrapUnique(in_value));
1251 } 1281 }
1252 #endif 1282 #endif
1253 1283
1254 void ListValue::AppendBoolean(bool in_value) { 1284 void ListValue::AppendBoolean(bool in_value) {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
1337 } 1367 }
1338 1368
1339 std::ostream& operator<<(std::ostream& out, const Value::Type& type) { 1369 std::ostream& operator<<(std::ostream& out, const Value::Type& type) {
1340 if (static_cast<int>(type) < 0 || 1370 if (static_cast<int>(type) < 0 ||
1341 static_cast<size_t>(type) >= arraysize(kTypeNames)) 1371 static_cast<size_t>(type) >= arraysize(kTypeNames))
1342 return out << "Invalid Type (index = " << static_cast<int>(type) << ")"; 1372 return out << "Invalid Type (index = " << static_cast<int>(type) << ")";
1343 return out << Value::GetTypeName(type); 1373 return out << Value::GetTypeName(type);
1344 } 1374 }
1345 1375
1346 } // namespace base 1376 } // namespace base
OLDNEW
« no previous file with comments | « base/values.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698