Chromium Code Reviews| OLD | NEW |
|---|---|
| (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) { | |
|
dmichael (off chromium)
2013/12/20 22:01:09
May be worth noting that while it does *not* own t
yzshen1
2013/12/20 22:58:18
Done. I added comments for .*StringWrapper and als
| |
| 22 PP_DCHECK(storage_->type == PP_VARTYPE_UNDEFINED); | |
| 23 } | |
| 24 | |
| 25 OptionalStringWrapper::OptionalStringWrapper( | |
| 26 const OptionalStringWrapper& other) { | |
| 27 // Add one ref. | |
| 28 *storage_ = Var(*other.storage_).Detach(); | |
| 29 } | |
| 30 | |
| 31 OptionalStringWrapper::~OptionalStringWrapper() { | |
| 32 unset(); | |
| 33 } | |
| 34 | |
| 35 OptionalStringWrapper& OptionalStringWrapper::operator=( | |
| 36 const OptionalStringWrapper& other) { | |
| 37 return operator=(*other.storage_); | |
| 38 } | |
| 39 | |
| 40 OptionalStringWrapper& OptionalStringWrapper::operator=(const PP_Var& other) { | |
| 41 if (storage_.get() == &other) | |
| 42 return *this; | |
| 43 | |
| 44 Var auto_release(PASS_REF, *storage_); | |
| 45 // Add one ref. | |
| 46 *storage_ = Var(other).Detach(); | |
| 47 return *this; | |
| 48 } | |
| 49 | |
| 50 bool OptionalStringWrapper::is_set() const { | |
| 51 PP_DCHECK(storage_->type == PP_VARTYPE_STRING || | |
| 52 storage_->type == PP_VARTYPE_UNDEFINED); | |
| 53 return storage_->type == PP_VARTYPE_STRING; | |
| 54 } | |
| 55 | |
| 56 void OptionalStringWrapper::unset() { | |
| 57 Var auto_release(PASS_REF, *storage_); | |
| 58 *storage_ = PP_MakeUndefined(); | |
| 59 } | |
| 60 | |
| 61 std::string OptionalStringWrapper::get() const { | |
| 62 // TODO(yzshen): consider adding a cache. | |
| 63 Var var(*storage_); | |
| 64 if (var.is_string()) { | |
| 65 return var.AsString(); | |
| 66 } else { | |
| 67 PP_NOTREACHED(); | |
| 68 return std::string(); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 void OptionalStringWrapper::set(const std::string& value) { | |
| 73 Var auto_release(PASS_REF, *storage_); | |
| 74 *storage_ = Var(value).Detach(); | |
| 75 } | |
| 76 | |
| 77 PP_Var* OptionalStringWrapper::StartRawUpdate() { | |
| 78 unset(); | |
| 79 return storage_.get(); | |
| 80 } | |
| 81 | |
| 82 void OptionalStringWrapper::EndRawUpdate() { | |
| 83 PP_DCHECK(storage_->type == PP_VARTYPE_STRING || | |
| 84 storage_->type == PP_VARTYPE_UNDEFINED); | |
| 85 } | |
| 86 | |
| 87 StringWrapper::StringWrapper() : storage_(std::string()) { | |
| 88 } | |
| 89 | |
| 90 StringWrapper::StringWrapper(const std::string& value) : storage_(value) { | |
| 91 } | |
| 92 | |
| 93 StringWrapper::StringWrapper(PP_Var* storage, NotOwned) | |
| 94 : storage_(storage, NOT_OWNED) { | |
| 95 storage_.set(std::string()); | |
| 96 } | |
| 97 | |
| 98 StringWrapper::StringWrapper(const StringWrapper& other) | |
| 99 : storage_(other.storage_) { | |
| 100 } | |
| 101 | |
| 102 StringWrapper::~StringWrapper() { | |
| 103 } | |
| 104 | |
| 105 StringWrapper& StringWrapper::operator=(const StringWrapper& other) { | |
| 106 storage_ = other.storage_; | |
| 107 return *this; | |
| 108 } | |
| 109 | |
| 110 StringWrapper& StringWrapper::operator=(const PP_Var& other) { | |
| 111 PP_DCHECK(other.type == PP_VARTYPE_STRING); | |
| 112 storage_ = other; | |
| 113 return *this; | |
| 114 } | |
| 115 | |
| 116 PP_Var* StringWrapper::StartRawUpdate() { | |
| 117 return storage_.StartRawUpdate(); | |
| 118 } | |
| 119 | |
| 120 void StringWrapper::EndRawUpdate() { | |
| 121 storage_.EndRawUpdate(); | |
| 122 if (!storage_.is_set()) | |
| 123 storage_.set(std::string()); | |
| 124 } | |
| 125 | |
| 126 } // namespace internal | |
| 127 } // namespace pp | |
| OLD | NEW |