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

Side by Side Diff: base/values.cc

Issue 2151793002: Add base::Value::GetTypeName(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: win build Created 4 years, 5 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') | chromeos/network/onc/onc_validator.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 #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>
11 #include <ostream> 11 #include <ostream>
12 #include <utility> 12 #include <utility>
13 13
14 #include "base/json/json_writer.h" 14 #include "base/json/json_writer.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/memory/ptr_util.h" 16 #include "base/memory/ptr_util.h"
17 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
19 19
20 namespace base { 20 namespace base {
21 21
22 namespace { 22 namespace {
23 23
24 const char* const kTypeNames[] = {"null", "boolean", "integer", "double",
25 "string", "binary", "dictionary", "list"};
26 static_assert(arraysize(kTypeNames) == Value::TYPE_LIST + 1,
27 "kTypeNames Has Wrong Size");
28
24 std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node); 29 std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node);
25 30
26 // Make a deep copy of |node|, but don't include empty lists or dictionaries 31 // Make a deep copy of |node|, but don't include empty lists or dictionaries
27 // in the copy. It's possible for this function to return NULL and it 32 // in the copy. It's possible for this function to return NULL and it
28 // expects |node| to always be non-NULL. 33 // expects |node| to always be non-NULL.
29 std::unique_ptr<ListValue> CopyListWithoutEmptyChildren(const ListValue& list) { 34 std::unique_ptr<ListValue> CopyListWithoutEmptyChildren(const ListValue& list) {
30 std::unique_ptr<ListValue> copy; 35 std::unique_ptr<ListValue> copy;
31 for (const auto& entry : list) { 36 for (const auto& entry : list) {
32 std::unique_ptr<Value> child_copy = CopyWithoutEmptyChildren(*entry); 37 std::unique_ptr<Value> child_copy = CopyWithoutEmptyChildren(*entry);
33 if (child_copy) { 38 if (child_copy) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 } // namespace 75 } // namespace
71 76
72 Value::~Value() { 77 Value::~Value() {
73 } 78 }
74 79
75 // static 80 // static
76 std::unique_ptr<Value> Value::CreateNullValue() { 81 std::unique_ptr<Value> Value::CreateNullValue() {
77 return WrapUnique(new Value(TYPE_NULL)); 82 return WrapUnique(new Value(TYPE_NULL));
78 } 83 }
79 84
85 // static
86 const char* Value::GetTypeName(Value::Type type) {
87 DCHECK_GE(type, 0);
88 DCHECK_LT(static_cast<size_t>(type), arraysize(kTypeNames));
89 return kTypeNames[type];
90 }
91
80 bool Value::GetAsBinary(const BinaryValue** out_value) const { 92 bool Value::GetAsBinary(const BinaryValue** out_value) const {
81 return false; 93 return false;
82 } 94 }
83 95
84 bool Value::GetAsBoolean(bool* out_value) const { 96 bool Value::GetAsBoolean(bool* out_value) const {
85 return false; 97 return false;
86 } 98 }
87 99
88 bool Value::GetAsInteger(int* out_value) const { 100 bool Value::GetAsInteger(int* out_value) const {
89 return false; 101 return false;
(...skipping 1070 matching lines...) Expand 10 before | Expand all | Expand 10 after
1160 ValueDeserializer::~ValueDeserializer() { 1172 ValueDeserializer::~ValueDeserializer() {
1161 } 1173 }
1162 1174
1163 std::ostream& operator<<(std::ostream& out, const Value& value) { 1175 std::ostream& operator<<(std::ostream& out, const Value& value) {
1164 std::string json; 1176 std::string json;
1165 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json); 1177 JSONWriter::WriteWithOptions(value, JSONWriter::OPTIONS_PRETTY_PRINT, &json);
1166 return out << json; 1178 return out << json;
1167 } 1179 }
1168 1180
1169 } // namespace base 1181 } // namespace base
OLDNEW
« no previous file with comments | « base/values.h ('k') | chromeos/network/onc/onc_validator.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698