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 StringWrapper::StringWrapper(PP_Var* storage, NotOwned) |
| 14 : storage_(storage, NOT_OWNED) { |
| 15 // TODO(yzshen): consider what if the value is not correct. |
| 16 PP_DCHECK(storage_->type == PP_VARTYPE_UNDEFINED); |
| 17 } |
| 18 |
| 19 StringWrapper::StringWrapper(const std::string& value) { |
| 20 *storage_ = Var(value).Detach(); |
| 21 } |
| 22 |
| 23 StringWrapper::StringWrapper(const StringWrapper& other) { |
| 24 // Add one ref. |
| 25 *storage_ = Var(*other.storage_).Detach(); |
| 26 } |
| 27 |
| 28 StringWrapper::~StringWrapper() { |
| 29 Var auto_release(PASS_REF, *storage_); |
| 30 *storage_ = PP_MakeUndefined(); |
| 31 } |
| 32 |
| 33 StringWrapper& StringWrapper::operator=(const StringWrapper& other) { |
| 34 return operator=(*other.storage_); |
| 35 } |
| 36 |
| 37 StringWrapper& StringWrapper::operator=(const PP_Var& other) { |
| 38 if (storage_.get() == &other) |
| 39 return *this; |
| 40 |
| 41 Var auto_release(PASS_REF, *storage_); |
| 42 // Add one ref. |
| 43 *storage_ = Var(other).Detach(); |
| 44 return *this; |
| 45 } |
| 46 |
| 47 bool StringWrapper::is_set() const { |
| 48 PP_DCHECK(storage_->type == PP_VARTYPE_STRING || |
| 49 storage_->type == PP_VARTYPE_UNDEFINED); |
| 50 return storage_->type == PP_VARTYPE_STRING; |
| 51 } |
| 52 |
| 53 void StringWrapper::unset() { |
| 54 Var auto_release(PASS_REF, *storage_); |
| 55 *storage_ = PP_MakeUndefined(); |
| 56 } |
| 57 |
| 58 std::string StringWrapper::get() const { |
| 59 Var var(*storage_); |
| 60 if (var.is_string()) { |
| 61 return var.AsString(); |
| 62 } else { |
| 63 PP_NOTREACHED(); |
| 64 return std::string(); |
| 65 } |
| 66 } |
| 67 |
| 68 void StringWrapper::set(const std::string& value) { |
| 69 Var auto_release(PASS_REF, *storage_); |
| 70 *storage_ = Var(value).Detach(); |
| 71 } |
| 72 |
| 73 } // namespace internal |
| 74 } // namespace pp |
OLD | NEW |