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

Side by Side Diff: ppapi/cpp/dev/string_wrapper_dev.cc

Issue 116963003: App APIs in Pepper: C++ APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: changes according to Sam's suggestions. Created 6 years, 11 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 | « ppapi/cpp/dev/string_wrapper_dev.h ('k') | ppapi/cpp/dev/struct_wrapper_output_traits_dev.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013 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.
4
5 #include "ppapi/cpp/dev/string_wrapper_dev.h"
6
7 #include "ppapi/cpp/logging.h"
8 #include "ppapi/cpp/var.h"
9
10 namespace pp {
11 namespace internal {
12
13 OptionalStringWrapper::OptionalStringWrapper() {
14 }
15
16 OptionalStringWrapper::OptionalStringWrapper(const std::string& value) {
17 *storage_ = Var(value).Detach();
18 }
19
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);
24 }
25
26 OptionalStringWrapper::OptionalStringWrapper(
27 const OptionalStringWrapper& other) {
28 // Add one ref.
29 *storage_ = Var(*other.storage_).Detach();
30 }
31
32 OptionalStringWrapper::~OptionalStringWrapper() {
33 unset();
34 }
35
36 OptionalStringWrapper& OptionalStringWrapper::operator=(
37 const OptionalStringWrapper& other) {
38 return operator=(*other.storage_);
39 }
40
41 OptionalStringWrapper& OptionalStringWrapper::operator=(const PP_Var& other) {
42 if (storage_.get() == &other)
43 return *this;
44
45 Var auto_release(PASS_REF, *storage_);
46 // Add one ref.
47 *storage_ = Var(other).Detach();
48 return *this;
49 }
50
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;
55 }
56
57 void OptionalStringWrapper::unset() {
58 Var auto_release(PASS_REF, *storage_);
59 *storage_ = PP_MakeUndefined();
60 }
61
62 std::string OptionalStringWrapper::get() const {
63 // TODO(yzshen): consider adding a cache.
64 Var var(*storage_);
65 if (var.is_string()) {
66 return var.AsString();
67 } else {
68 PP_NOTREACHED();
69 return std::string();
70 }
71 }
72
73 void OptionalStringWrapper::set(const std::string& value) {
74 Var auto_release(PASS_REF, *storage_);
75 *storage_ = Var(value).Detach();
76 }
77
78 PP_Var* OptionalStringWrapper::StartRawUpdate() {
79 unset();
80 return storage_.get();
81 }
82
83 void OptionalStringWrapper::EndRawUpdate() {
84 PP_DCHECK(storage_->type == PP_VARTYPE_STRING ||
85 storage_->type == PP_VARTYPE_UNDEFINED);
86 }
87
88 StringWrapper::StringWrapper() : storage_(std::string()) {
89 }
90
91 StringWrapper::StringWrapper(const std::string& value) : storage_(value) {
92 }
93
94 StringWrapper::StringWrapper(PP_Var* storage, NotOwned)
95 : storage_(storage, NOT_OWNED) {
96 if (!storage_.is_set())
97 storage_.set(std::string());
98 }
99
100 StringWrapper::StringWrapper(const StringWrapper& other)
101 : storage_(other.storage_) {
102 }
103
104 StringWrapper::~StringWrapper() {
105 }
106
107 StringWrapper& StringWrapper::operator=(const StringWrapper& other) {
108 storage_ = other.storage_;
109 return *this;
110 }
111
112 StringWrapper& StringWrapper::operator=(const PP_Var& other) {
113 PP_DCHECK(other.type == PP_VARTYPE_STRING);
114 storage_ = other;
115 return *this;
116 }
117
118 PP_Var* StringWrapper::StartRawUpdate() {
119 return storage_.StartRawUpdate();
120 }
121
122 void StringWrapper::EndRawUpdate() {
123 storage_.EndRawUpdate();
124 if (!storage_.is_set())
125 storage_.set(std::string());
126 }
127
128 } // namespace internal
129 } // namespace pp
OLDNEW
« no previous file with comments | « ppapi/cpp/dev/string_wrapper_dev.h ('k') | ppapi/cpp/dev/struct_wrapper_output_traits_dev.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698