1 // Copyright 2014 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/dev/string_wrapper_dev.h"
7 #include "ppapi/cpp/logging.h"
8 #include "ppapi/cpp/var.h"
13 OptionalStringWrapper::OptionalStringWrapper() {
16 OptionalStringWrapper::OptionalStringWrapper(const std::string
& value
) {
17 *storage_
= Var(value
).Detach();
20 OptionalStringWrapper::OptionalStringWrapper(PP_Var
* storage
, NotOwned
)
21 : storage_(storage
, NOT_OWNED
) {
22 PP_DCHECK(storage_
->type
== PP_VARTYPE_STRING
||
23 storage_
->type
== PP_VARTYPE_UNDEFINED
);
26 OptionalStringWrapper::OptionalStringWrapper(
27 const OptionalStringWrapper
& other
) {
29 *storage_
= Var(*other
.storage_
).Detach();
32 OptionalStringWrapper::~OptionalStringWrapper() {
36 OptionalStringWrapper
& OptionalStringWrapper::operator=(
37 const OptionalStringWrapper
& other
) {
38 return operator=(*other
.storage_
);
41 OptionalStringWrapper
& OptionalStringWrapper::operator=(const PP_Var
& other
) {
42 if (storage_
.get() == &other
)
45 Var
auto_release(PASS_REF
, *storage_
);
47 *storage_
= Var(other
).Detach();
51 bool OptionalStringWrapper::is_set() const {
52 PP_DCHECK(storage_
->type
== PP_VARTYPE_STRING
||
53 storage_
->type
== PP_VARTYPE_UNDEFINED
);
54 return storage_
->type
== PP_VARTYPE_STRING
;
57 void OptionalStringWrapper::unset() {
58 Var
auto_release(PASS_REF
, *storage_
);
59 *storage_
= PP_MakeUndefined();
62 std::string
OptionalStringWrapper::get() const {
63 // TODO(yzshen): consider adding a cache.
65 if (var
.is_string()) {
66 return var
.AsString();
73 void OptionalStringWrapper::set(const std::string
& value
) {
74 Var
auto_release(PASS_REF
, *storage_
);
75 *storage_
= Var(value
).Detach();
78 PP_Var
* OptionalStringWrapper::StartRawUpdate() {
80 return storage_
.get();
83 void OptionalStringWrapper::EndRawUpdate() {
84 PP_DCHECK(storage_
->type
== PP_VARTYPE_STRING
||
85 storage_
->type
== PP_VARTYPE_UNDEFINED
);
88 StringWrapper::StringWrapper() : storage_(std::string()) {
91 StringWrapper::StringWrapper(const std::string
& value
) : storage_(value
) {
94 StringWrapper::StringWrapper(PP_Var
* storage
, NotOwned
)
95 : storage_(storage
, NOT_OWNED
) {
96 if (!storage_
.is_set())
97 storage_
.set(std::string());
100 StringWrapper::StringWrapper(const StringWrapper
& other
)
101 : storage_(other
.storage_
) {
104 StringWrapper::~StringWrapper() {
107 StringWrapper
& StringWrapper::operator=(const StringWrapper
& other
) {
108 storage_
= other
.storage_
;
112 StringWrapper
& StringWrapper::operator=(const PP_Var
& other
) {
113 PP_DCHECK(other
.type
== PP_VARTYPE_STRING
);
118 PP_Var
* StringWrapper::StartRawUpdate() {
119 return storage_
.StartRawUpdate();
122 void StringWrapper::EndRawUpdate() {
123 storage_
.EndRawUpdate();
124 if (!storage_
.is_set())
125 storage_
.set(std::string());
128 } // namespace internal