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 // 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 setting and other persistable data. It includes the ability to | 6 // storing setting and other persistable data. It includes the ability to |
7 // specify (recursive) lists and dictionaries, so it's fairly expressive. | 7 // specify (recursive) lists and dictionaries, so it's fairly expressive. |
8 // However, the API is optimized for the common case, namely storing a | 8 // However, the API is optimized for the common case, namely storing a |
9 // hierarchical tree of simple values. Given a DictionaryValue root, you can | 9 // hierarchical tree of simple values. Given a DictionaryValue root, you can |
10 // easily do things like: | 10 // easily do things like: |
(...skipping 12 matching lines...) Expand all Loading... |
23 #pragma once | 23 #pragma once |
24 | 24 |
25 #include <iterator> | 25 #include <iterator> |
26 #include <map> | 26 #include <map> |
27 #include <string> | 27 #include <string> |
28 #include <vector> | 28 #include <vector> |
29 | 29 |
30 #include "base/base_export.h" | 30 #include "base/base_export.h" |
31 #include "base/basictypes.h" | 31 #include "base/basictypes.h" |
32 #include "base/compiler_specific.h" | 32 #include "base/compiler_specific.h" |
| 33 #include "base/memory/scoped_ptr.h" |
33 #include "base/string16.h" | 34 #include "base/string16.h" |
34 | 35 |
35 // This file declares "using base::Value", etc. at the bottom, so that | 36 // This file declares "using base::Value", etc. at the bottom, so that |
36 // current code can use these classes without the base namespace. In | 37 // current code can use these classes without the base namespace. In |
37 // new code, please always use base::Value, etc. or add your own | 38 // new code, please always use base::Value, etc. or add your own |
38 // "using" declaration. | 39 // "using" declaration. |
39 // http://crbug.com/88666 | 40 // http://crbug.com/88666 |
40 namespace base { | 41 namespace base { |
41 | 42 |
42 class BinaryValue; | 43 class BinaryValue; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 virtual bool Equals(const Value* other) const OVERRIDE; | 171 virtual bool Equals(const Value* other) const OVERRIDE; |
171 | 172 |
172 private: | 173 private: |
173 std::string value_; | 174 std::string value_; |
174 | 175 |
175 DISALLOW_COPY_AND_ASSIGN(StringValue); | 176 DISALLOW_COPY_AND_ASSIGN(StringValue); |
176 }; | 177 }; |
177 | 178 |
178 class BASE_EXPORT BinaryValue: public Value { | 179 class BASE_EXPORT BinaryValue: public Value { |
179 public: | 180 public: |
| 181 // Creates a BinaryValue with a null buffer and size of 0. |
| 182 BinaryValue(); |
| 183 |
| 184 // Creates a BinaryValue, taking ownership of the bytes pointed to by |
| 185 // |buffer|. |
| 186 BinaryValue(scoped_ptr<char> buffer, size_t size); |
| 187 |
180 virtual ~BinaryValue(); | 188 virtual ~BinaryValue(); |
181 | 189 |
182 // Creates a Value to represent a binary buffer. The new object takes | |
183 // ownership of the pointer passed in, if successful. | |
184 // Returns NULL if buffer is NULL. | |
185 static BinaryValue* Create(char* buffer, size_t size); | |
186 | |
187 // For situations where you want to keep ownership of your buffer, this | 190 // For situations where you want to keep ownership of your buffer, this |
188 // factory method creates a new BinaryValue by copying the contents of the | 191 // factory method creates a new BinaryValue by copying the contents of the |
189 // buffer that's passed in. | 192 // buffer that's passed in. |
190 // Returns NULL if buffer is NULL. | |
191 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size); | 193 static BinaryValue* CreateWithCopiedBuffer(const char* buffer, size_t size); |
192 | 194 |
193 size_t GetSize() const { return size_; } | 195 size_t GetSize() const { return size_; } |
194 char* GetBuffer() { return buffer_; } | 196 |
195 const char* GetBuffer() const { return buffer_; } | 197 // May return NULL. |
| 198 char* GetBuffer() { return buffer_.get(); } |
| 199 const char* GetBuffer() const { return buffer_.get(); } |
196 | 200 |
197 // Overridden from Value: | 201 // Overridden from Value: |
198 virtual BinaryValue* DeepCopy() const OVERRIDE; | 202 virtual BinaryValue* DeepCopy() const OVERRIDE; |
199 virtual bool Equals(const Value* other) const OVERRIDE; | 203 virtual bool Equals(const Value* other) const OVERRIDE; |
200 | 204 |
201 private: | 205 private: |
202 // Constructor is private so that only objects with valid buffer pointers | 206 scoped_ptr<char> buffer_; |
203 // and size values can be created. | |
204 BinaryValue(char* buffer, size_t size); | |
205 | |
206 char* buffer_; | |
207 size_t size_; | 207 size_t size_; |
208 | 208 |
209 DISALLOW_COPY_AND_ASSIGN(BinaryValue); | 209 DISALLOW_COPY_AND_ASSIGN(BinaryValue); |
210 }; | 210 }; |
211 | 211 |
212 // DictionaryValue provides a key-value dictionary with (optional) "path" | 212 // DictionaryValue provides a key-value dictionary with (optional) "path" |
213 // parsing for recursive access; see the comment at the top of the file. Keys | 213 // parsing for recursive access; see the comment at the top of the file. Keys |
214 // are |std::string|s and should be UTF-8 encoded. | 214 // are |std::string|s and should be UTF-8 encoded. |
215 class BASE_EXPORT DictionaryValue : public Value { | 215 class BASE_EXPORT DictionaryValue : public Value { |
216 public: | 216 public: |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 | 489 |
490 } // namespace base | 490 } // namespace base |
491 | 491 |
492 // http://crbug.com/88666 | 492 // http://crbug.com/88666 |
493 using base::DictionaryValue; | 493 using base::DictionaryValue; |
494 using base::ListValue; | 494 using base::ListValue; |
495 using base::StringValue; | 495 using base::StringValue; |
496 using base::Value; | 496 using base::Value; |
497 | 497 |
498 #endif // BASE_VALUES_H_ | 498 #endif // BASE_VALUES_H_ |
OLD | NEW |