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

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: More fixes 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
« no previous file with comments | « base/trace_event/trace_event_memory_overhead.cc ('k') | base/values.cc » ('j') | 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 // 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 using const_iterator = Storage::const_iterator;
393 using iterator = Storage::iterator;
395 394
396 // Returns |value| if it is a list, nullptr otherwise. 395 // Returns |value| if it is a list, nullptr otherwise.
397 static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value); 396 static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value);
398 397
399 ListValue(); 398 ListValue();
400 ~ListValue() override; 399 ~ListValue() override;
401 400
402 // Clears the contents of this ListValue 401 // Clears the contents of this ListValue
403 void Clear(); 402 void Clear();
404 403
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 // Overridden from Value: 500 // Overridden from Value:
502 bool GetAsList(ListValue** out_value) override; 501 bool GetAsList(ListValue** out_value) override;
503 bool GetAsList(const ListValue** out_value) const override; 502 bool GetAsList(const ListValue** out_value) const override;
504 ListValue* DeepCopy() const override; 503 ListValue* DeepCopy() const override;
505 bool Equals(const Value* other) const override; 504 bool Equals(const Value* other) const override;
506 505
507 // Preferred version of DeepCopy. TODO(estade): remove DeepCopy. 506 // Preferred version of DeepCopy. TODO(estade): remove DeepCopy.
508 std::unique_ptr<ListValue> CreateDeepCopy() const; 507 std::unique_ptr<ListValue> CreateDeepCopy() const;
509 508
510 private: 509 private:
511 ValueVector list_; 510 Storage list_;
512 511
513 DISALLOW_COPY_AND_ASSIGN(ListValue); 512 DISALLOW_COPY_AND_ASSIGN(ListValue);
514 }; 513 };
515 514
516 // This interface is implemented by classes that know how to serialize 515 // This interface is implemented by classes that know how to serialize
517 // Value objects. 516 // Value objects.
518 class BASE_EXPORT ValueSerializer { 517 class BASE_EXPORT ValueSerializer {
519 public: 518 public:
520 virtual ~ValueSerializer(); 519 virtual ~ValueSerializer();
521 520
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
560 } 559 }
561 560
562 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, 561 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
563 const ListValue& value) { 562 const ListValue& value) {
564 return out << static_cast<const Value&>(value); 563 return out << static_cast<const Value&>(value);
565 } 564 }
566 565
567 } // namespace base 566 } // namespace base
568 567
569 #endif // BASE_VALUES_H_ 568 #endif // BASE_VALUES_H_
OLDNEW
« no previous file with comments | « base/trace_event/trace_event_memory_overhead.cc ('k') | base/values.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698