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

Side by Side Diff: base/values.h

Issue 2000803003: Use std::unique_ptr for base::DictionaryValue and base::ListValue's internal store. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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
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 // This file specifies a recursive data storage class called Value intended for 5 // This file specifies a recursive data storage class called Value intended for
6 // storing settings and other persistable data. 6 // storing settings and other persistable data.
7 // 7 //
8 // A Value represents something that can be stored in JSON or passed to/from 8 // A Value represents something that can be stored in JSON or passed to/from
9 // JavaScript. As such, it is NOT a generalized variant type, since only the 9 // JavaScript. As such, it is NOT a generalized variant type, since only the
10 // types supported by JavaScript/JSON are supported. 10 // types supported by JavaScript/JSON are supported.
(...skipping 24 matching lines...) Expand all
35 35
36 namespace base { 36 namespace base {
37 37
38 class BinaryValue; 38 class BinaryValue;
39 class DictionaryValue; 39 class DictionaryValue;
40 class FundamentalValue; 40 class FundamentalValue;
41 class ListValue; 41 class ListValue;
42 class StringValue; 42 class StringValue;
43 class Value; 43 class Value;
44 44
45 typedef std::vector<Value*> ValueVector;
46 typedef std::map<std::string, Value*> ValueMap;
47
48 // The Value class is the base class for Values. A Value can be instantiated 45 // The Value class is the base class for Values. A Value can be instantiated
49 // via the Create*Value() factory methods, or by directly creating instances of 46 // via the Create*Value() factory methods, or by directly creating instances of
50 // the subclasses. 47 // the subclasses.
51 // 48 //
52 // See the file-level comment above for more information. 49 // See the file-level comment above for more information.
53 class BASE_EXPORT Value { 50 class BASE_EXPORT Value {
54 public: 51 public:
55 enum Type { 52 enum Type {
56 TYPE_NULL = 0, 53 TYPE_NULL = 0,
57 TYPE_BOOLEAN, 54 TYPE_BOOLEAN,
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 size_t size_; 200 size_t size_;
204 201
205 DISALLOW_COPY_AND_ASSIGN(BinaryValue); 202 DISALLOW_COPY_AND_ASSIGN(BinaryValue);
206 }; 203 };
207 204
208 // DictionaryValue provides a key-value dictionary with (optional) "path" 205 // DictionaryValue provides a key-value dictionary with (optional) "path"
209 // parsing for recursive access; see the comment at the top of the file. Keys 206 // parsing for recursive access; see the comment at the top of the file. Keys
210 // are |std::string|s and should be UTF-8 encoded. 207 // are |std::string|s and should be UTF-8 encoded.
211 class BASE_EXPORT DictionaryValue : public Value { 208 class BASE_EXPORT DictionaryValue : public Value {
212 public: 209 public:
210 using Storage = std::map<std::string, std::unique_ptr<Value>>;
213 // Returns |value| if it is a dictionary, nullptr otherwise. 211 // Returns |value| if it is a dictionary, nullptr otherwise.
214 static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value); 212 static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value);
215 213
216 DictionaryValue(); 214 DictionaryValue();
217 ~DictionaryValue() override; 215 ~DictionaryValue() override;
218 216
219 // Overridden from Value: 217 // Overridden from Value:
220 bool GetAsDictionary(DictionaryValue** out_value) override; 218 bool GetAsDictionary(DictionaryValue** out_value) override;
221 bool GetAsDictionary(const DictionaryValue** out_value) const override; 219 bool GetAsDictionary(const DictionaryValue** out_value) const override;
222 220
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 ~Iterator(); 363 ~Iterator();
366 364
367 bool IsAtEnd() const { return it_ == target_.dictionary_.end(); } 365 bool IsAtEnd() const { return it_ == target_.dictionary_.end(); }
368 void Advance() { ++it_; } 366 void Advance() { ++it_; }
369 367
370 const std::string& key() const { return it_->first; } 368 const std::string& key() const { return it_->first; }
371 const Value& value() const { return *it_->second; } 369 const Value& value() const { return *it_->second; }
372 370
373 private: 371 private:
374 const DictionaryValue& target_; 372 const DictionaryValue& target_;
375 ValueMap::const_iterator it_; 373 Storage::const_iterator it_;
376 }; 374 };
377 375
378 // Overridden from Value: 376 // Overridden from Value:
379 DictionaryValue* DeepCopy() const override; 377 DictionaryValue* DeepCopy() const override;
380 // Preferred version of DeepCopy. TODO(estade): remove the above. 378 // Preferred version of DeepCopy. TODO(estade): remove the above.
381 std::unique_ptr<DictionaryValue> CreateDeepCopy() const; 379 std::unique_ptr<DictionaryValue> CreateDeepCopy() const;
382 bool Equals(const Value* other) const override; 380 bool Equals(const Value* other) const override;
383 381
384 private: 382 private:
385 ValueMap dictionary_; 383 Storage dictionary_;
386 384
387 DISALLOW_COPY_AND_ASSIGN(DictionaryValue); 385 DISALLOW_COPY_AND_ASSIGN(DictionaryValue);
388 }; 386 };
389 387
390 // This type of Value represents a list of other Value values. 388 // This type of Value represents a list of other Value values.
391 class BASE_EXPORT ListValue : public Value { 389 class BASE_EXPORT ListValue : public Value {
392 public: 390 public:
393 typedef ValueVector::iterator iterator; 391 using Storage = std::vector<std::unique_ptr<Value>>;
394 typedef ValueVector::const_iterator const_iterator; 392 class iterator : public Storage::iterator {
dcheng 2016/05/20 19:49:47 There's ~160 dependencies on iterator wrapping a r
danakj 2016/05/20 20:51:38 The subclassing part is weird. It'd be better if y
dcheng 2016/05/20 22:41:55 I could do that, but it makes it a bit more painfu
393 public:
394 iterator(const Storage::iterator& it) : Storage::iterator(it) {}
395 Value* operator*() { return Storage::iterator::operator*().get(); }
396 };
397 class const_iterator : public Storage::const_iterator {
398 public:
399 const_iterator(const Storage::const_iterator& it)
400 : Storage::const_iterator(it) {}
401 const Value* operator*() {
dcheng 2016/05/20 19:49:47 I believe const_iterator just returns Value* here.
402 return Storage::const_iterator::operator*().get();
403 }
404 };
395 405
396 // Returns |value| if it is a list, nullptr otherwise. 406 // Returns |value| if it is a list, nullptr otherwise.
397 static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value); 407 static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value);
398 408
399 ListValue(); 409 ListValue();
400 ~ListValue() override; 410 ~ListValue() override;
401 411
402 // Clears the contents of this ListValue 412 // Clears the contents of this ListValue
403 void Clear(); 413 void Clear();
404 414
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 // Overridden from Value: 511 // Overridden from Value:
502 bool GetAsList(ListValue** out_value) override; 512 bool GetAsList(ListValue** out_value) override;
503 bool GetAsList(const ListValue** out_value) const override; 513 bool GetAsList(const ListValue** out_value) const override;
504 ListValue* DeepCopy() const override; 514 ListValue* DeepCopy() const override;
505 bool Equals(const Value* other) const override; 515 bool Equals(const Value* other) const override;
506 516
507 // Preferred version of DeepCopy. TODO(estade): remove DeepCopy. 517 // Preferred version of DeepCopy. TODO(estade): remove DeepCopy.
508 std::unique_ptr<ListValue> CreateDeepCopy() const; 518 std::unique_ptr<ListValue> CreateDeepCopy() const;
509 519
510 private: 520 private:
511 ValueVector list_; 521 Storage list_;
512 522
513 DISALLOW_COPY_AND_ASSIGN(ListValue); 523 DISALLOW_COPY_AND_ASSIGN(ListValue);
514 }; 524 };
515 525
516 // This interface is implemented by classes that know how to serialize 526 // This interface is implemented by classes that know how to serialize
517 // Value objects. 527 // Value objects.
518 class BASE_EXPORT ValueSerializer { 528 class BASE_EXPORT ValueSerializer {
519 public: 529 public:
520 virtual ~ValueSerializer(); 530 virtual ~ValueSerializer();
521 531
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
560 } 570 }
561 571
562 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, 572 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
563 const ListValue& value) { 573 const ListValue& value) {
564 return out << static_cast<const Value&>(value); 574 return out << static_cast<const Value&>(value);
565 } 575 }
566 576
567 } // namespace base 577 } // namespace base
568 578
569 #endif // BASE_VALUES_H_ 579 #endif // BASE_VALUES_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698