Refactor android test results logging.
[chromium-blink-merge.git] / ppapi / cpp / var.cc
blob7fc67f5b273e754e4b296a030e781c3bb56345e8
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
3 // found in the LICENSE file.
5 #include "ppapi/cpp/var.h"
7 #include <stdio.h>
8 #include <string.h>
10 #include <algorithm>
12 #include "ppapi/c/pp_var.h"
13 #include "ppapi/c/ppb_var.h"
14 #include "ppapi/cpp/instance.h"
15 #include "ppapi/cpp/logging.h"
16 #include "ppapi/cpp/module.h"
17 #include "ppapi/cpp/module_impl.h"
19 // Define equivalent to snprintf on Windows.
20 #if defined(_MSC_VER)
21 # define snprintf sprintf_s
22 #endif
24 namespace pp {
26 namespace {
28 template <> const char* interface_name<PPB_Var_1_1>() {
29 return PPB_VAR_INTERFACE_1_1;
31 template <> const char* interface_name<PPB_Var_1_0>() {
32 return PPB_VAR_INTERFACE_1_0;
35 // Technically you can call AddRef and Release on any Var, but it may involve
36 // cross-process calls depending on the plugin. This is an optimization so we
37 // only do refcounting on the necessary objects.
38 inline bool NeedsRefcounting(const PP_Var& var) {
39 return var.type > PP_VARTYPE_DOUBLE;
42 // This helper function detects whether PPB_Var version 1.1 is available. If so,
43 // it uses it to create a PP_Var for the given string. Otherwise it falls back
44 // to PPB_Var version 1.0.
45 PP_Var VarFromUtf8Helper(const char* utf8_str, uint32_t len) {
46 if (has_interface<PPB_Var_1_1>()) {
47 return get_interface<PPB_Var_1_1>()->VarFromUtf8(utf8_str, len);
48 } else if (has_interface<PPB_Var_1_0>()) {
49 return get_interface<PPB_Var_1_0>()->VarFromUtf8(Module::Get()->pp_module(),
50 utf8_str,
51 len);
52 } else {
53 return PP_MakeNull();
57 } // namespace
59 Var::Var() {
60 memset(&var_, 0, sizeof(var_));
61 var_.type = PP_VARTYPE_UNDEFINED;
62 is_managed_ = true;
65 Var::Var(Null) {
66 memset(&var_, 0, sizeof(var_));
67 var_.type = PP_VARTYPE_NULL;
68 is_managed_ = true;
71 Var::Var(bool b) {
72 var_.type = PP_VARTYPE_BOOL;
73 var_.padding = 0;
74 var_.value.as_bool = PP_FromBool(b);
75 is_managed_ = true;
78 Var::Var(int32_t i) {
79 var_.type = PP_VARTYPE_INT32;
80 var_.padding = 0;
81 var_.value.as_int = i;
82 is_managed_ = true;
85 Var::Var(double d) {
86 var_.type = PP_VARTYPE_DOUBLE;
87 var_.padding = 0;
88 var_.value.as_double = d;
89 is_managed_ = true;
92 Var::Var(const char* utf8_str) {
93 uint32_t len = utf8_str ? static_cast<uint32_t>(strlen(utf8_str)) : 0;
94 var_ = VarFromUtf8Helper(utf8_str, len);
95 is_managed_ = true;
98 Var::Var(const std::string& utf8_str) {
99 var_ = VarFromUtf8Helper(utf8_str.c_str(),
100 static_cast<uint32_t>(utf8_str.size()));
101 is_managed_ = true;
104 Var::Var(const Var& other) {
105 var_ = other.var_;
106 is_managed_ = true;
107 if (NeedsRefcounting(var_)) {
108 if (has_interface<PPB_Var_1_0>())
109 get_interface<PPB_Var_1_0>()->AddRef(var_);
110 else
111 var_.type = PP_VARTYPE_NULL;
115 Var::~Var() {
116 if (NeedsRefcounting(var_) &&
117 is_managed_ &&
118 has_interface<PPB_Var_1_0>())
119 get_interface<PPB_Var_1_0>()->Release(var_);
122 Var& Var::operator=(const Var& other) {
123 // Early return for self-assignment. Note however, that two distinct vars
124 // can refer to the same object, so we still need to be careful about the
125 // refcounting below.
126 if (this == &other)
127 return *this;
129 // Be careful to keep the ref alive for cases where we're assigning an
130 // object to itself by addrefing the new one before releasing the old one.
131 bool old_is_managed = is_managed_;
132 is_managed_ = true;
133 if (NeedsRefcounting(other.var_)) {
134 // Assume we already has_interface<PPB_Var_1_0> for refcounted vars or else
135 // we couldn't have created them in the first place.
136 get_interface<PPB_Var_1_0>()->AddRef(other.var_);
138 if (NeedsRefcounting(var_) && old_is_managed)
139 get_interface<PPB_Var_1_0>()->Release(var_);
141 var_ = other.var_;
142 return *this;
145 bool Var::operator==(const Var& other) const {
146 if (var_.type != other.var_.type)
147 return false;
148 switch (var_.type) {
149 case PP_VARTYPE_UNDEFINED:
150 case PP_VARTYPE_NULL:
151 return true;
152 case PP_VARTYPE_BOOL:
153 return AsBool() == other.AsBool();
154 case PP_VARTYPE_INT32:
155 return AsInt() == other.AsInt();
156 case PP_VARTYPE_DOUBLE:
157 return AsDouble() == other.AsDouble();
158 case PP_VARTYPE_STRING:
159 if (var_.value.as_id == other.var_.value.as_id)
160 return true;
161 return AsString() == other.AsString();
162 case PP_VARTYPE_OBJECT:
163 case PP_VARTYPE_ARRAY:
164 case PP_VARTYPE_ARRAY_BUFFER:
165 case PP_VARTYPE_DICTIONARY:
166 default: // Objects, arrays, dictionaries.
167 return var_.value.as_id == other.var_.value.as_id;
171 bool Var::AsBool() const {
172 if (!is_bool()) {
173 PP_NOTREACHED();
174 return false;
176 return PP_ToBool(var_.value.as_bool);
179 int32_t Var::AsInt() const {
180 if (is_int())
181 return var_.value.as_int;
182 if (is_double())
183 return static_cast<int>(var_.value.as_double);
184 PP_NOTREACHED();
185 return 0;
188 double Var::AsDouble() const {
189 if (is_double())
190 return var_.value.as_double;
191 if (is_int())
192 return static_cast<double>(var_.value.as_int);
193 PP_NOTREACHED();
194 return 0.0;
197 std::string Var::AsString() const {
198 if (!is_string()) {
199 PP_NOTREACHED();
200 return std::string();
203 if (!has_interface<PPB_Var_1_0>())
204 return std::string();
205 uint32_t len;
206 const char* str = get_interface<PPB_Var_1_0>()->VarToUtf8(var_, &len);
207 return std::string(str, len);
210 std::string Var::DebugString() const {
211 char buf[256];
212 if (is_undefined()) {
213 snprintf(buf, sizeof(buf), "Var(UNDEFINED)");
214 } else if (is_null()) {
215 snprintf(buf, sizeof(buf), "Var(NULL)");
216 } else if (is_bool()) {
217 snprintf(buf, sizeof(buf), AsBool() ? "Var(true)" : "Var(false)");
218 } else if (is_int()) {
219 snprintf(buf, sizeof(buf), "Var(%d)", static_cast<int>(AsInt()));
220 } else if (is_double()) {
221 snprintf(buf, sizeof(buf), "Var(%f)", AsDouble());
222 } else if (is_string()) {
223 char format[] = "Var<'%s'>";
224 size_t decoration = sizeof(format) - 2; // The %s is removed.
225 size_t available = sizeof(buf) - decoration;
226 std::string str = AsString();
227 if (str.length() > available) {
228 str.resize(available - 3); // Reserve space for ellipsis.
229 str.append("...");
231 snprintf(buf, sizeof(buf), format, str.c_str());
232 } else if (is_array_buffer()) {
233 snprintf(buf, sizeof(buf), "Var(ARRAY_BUFFER)");
234 } else if (is_object()) {
235 snprintf(buf, sizeof(buf), "Var(OBJECT)");
237 return buf;
240 } // namespace pp