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 #ifndef PPAPI_CPP_DEV_OPTIONAL_DEV_H_ |
| 6 #define PPAPI_CPP_DEV_OPTIONAL_DEV_H_ |
| 7 |
| 8 #include "ppapi/c/dev/pp_optional_structs_dev.h" |
| 9 #include "ppapi/cpp/dev/array_dev.h" |
| 10 #include "ppapi/cpp/dev/may_own_ptr_dev.h" |
| 11 #include "ppapi/cpp/dev/string_wrapper_dev.h" |
| 12 #include "ppapi/cpp/logging.h" |
| 13 #include "ppapi/cpp/var.h" |
| 14 |
| 15 namespace pp { |
| 16 |
| 17 template <class T> |
| 18 class Optional; |
| 19 |
| 20 template <> |
| 21 class Optional<double> { |
| 22 public: |
| 23 typedef const PP_Optional_Double* CType; |
| 24 |
| 25 Optional(PP_Optional_Double* storage, NotOwned) : |
| 26 storage_(storage, NOT_OWNED) { |
| 27 } |
| 28 |
| 29 Optional(const Optional<double>& other) : storage_(other.storage_) { |
| 30 } |
| 31 |
| 32 Optional<double>& operator=(const Optional<double>& other) { |
| 33 storage_ = other.storage_; |
| 34 return *this; |
| 35 } |
| 36 |
| 37 Optional<double>& operator=(const PP_Optional_Double& other) { |
| 38 *storage_ = other; |
| 39 return *this; |
| 40 } |
| 41 |
| 42 bool is_set() const { return PP_ToBool(storage_->is_set); } |
| 43 void unset() { storage_->is_set = PP_FALSE; } |
| 44 double get() const { return storage_->value; } |
| 45 void set(double value) { storage_->value = value; } |
| 46 |
| 47 PP_Optional_Double* StartRawUpdate() { return storage_.get(); } |
| 48 void EndRawUpdate() {} |
| 49 |
| 50 const PP_Optional_Double* ToCType() const { |
| 51 return storage_.get(); |
| 52 } |
| 53 |
| 54 private: |
| 55 internal::MayOwnPtr<PP_Optional_Double> storage_; |
| 56 }; |
| 57 |
| 58 template <> |
| 59 class Optional<Array<double> > { |
| 60 public: |
| 61 typedef const PP_Optional_Double_Array* CType; |
| 62 |
| 63 Optional(PP_Optional_Double_Array* storage, NotOwned) : |
| 64 storage_(storage, NOT_OWNED), |
| 65 wrapper_(&storage_->value, NOT_OWNED) { |
| 66 } |
| 67 |
| 68 Optional(const Optional<Array<double> >& other) |
| 69 : wrapper_(&storage_->value, NOT_OWNED) { |
| 70 wrapper_ = other.wrapper_; |
| 71 } |
| 72 |
| 73 Optional<Array<double> >& operator=(const Optional<Array<double> >& other) { |
| 74 return operator=(*other.storage_); |
| 75 } |
| 76 |
| 77 Optional<Array<double> >& operator=(const PP_Optional_Double_Array& other) { |
| 78 if (storage_.get() == &other) |
| 79 return *this; |
| 80 |
| 81 storage_->is_set = other.is_set; |
| 82 if (storage_->is_set) |
| 83 wrapper_ = other.value; |
| 84 else |
| 85 wrapper_.Reset(0); |
| 86 |
| 87 return *this; |
| 88 } |
| 89 |
| 90 bool is_set() const { return PP_ToBool(storage_->is_set); } |
| 91 void unset() { |
| 92 storage_->is_set = PP_FALSE; |
| 93 wrapper_.Reset(0); |
| 94 } |
| 95 const Array<double>& get() const { return wrapper_; } |
| 96 Array<double>& get() { return wrapper_; } |
| 97 |
| 98 PP_Optional_Double_Array* StartRawUpdate() { |
| 99 wrapper_.StartRawUpdate(); |
| 100 return storage_.get(); |
| 101 } |
| 102 |
| 103 void EndRawUpdate() { |
| 104 wrapper_.EndRawUpdate(); |
| 105 } |
| 106 |
| 107 const PP_Optional_Double_Array* ToCType() const { |
| 108 return storage_.get(); |
| 109 } |
| 110 |
| 111 private: |
| 112 internal::MayOwnPtr<PP_Optional_Double_Array> storage_; |
| 113 Array<double> wrapper_; |
| 114 }; |
| 115 |
| 116 template <> |
| 117 class Optional<std::string> { |
| 118 public: |
| 119 typedef const PP_Var& CType; |
| 120 |
| 121 Optional(PP_Var* storage, NotOwned) |
| 122 : storage_(storage, NOT_OWNED), |
| 123 wrapper_(storage_.get(), NOT_OWNED) { |
| 124 } |
| 125 |
| 126 Optional(const Optional<std::string>& other) |
| 127 : wrapper_(storage_.get(), NOT_OWNED) { |
| 128 wrapper_ = other.wrapper_; |
| 129 } |
| 130 |
| 131 Optional<std::string>& operator=(const Optional<std::string>& other) { |
| 132 return operator=(*other.storage_); |
| 133 } |
| 134 |
| 135 Optional(const std::string& value) |
| 136 : wrapper_(storage_.get(), NOT_OWNED) { |
| 137 wrapper_.set(value); |
| 138 } |
| 139 |
| 140 Optional<std::string>& operator=(const PP_Var& other) { |
| 141 if (storage_.get() == &other) |
| 142 return *this; |
| 143 |
| 144 wrapper_ = other; |
| 145 return *this; |
| 146 } |
| 147 |
| 148 bool is_set() const { return wrapper_.is_set(); } |
| 149 void unset() { wrapper_.unset(); } |
| 150 std::string get() const { return wrapper_.get(); } |
| 151 void set(const std::string& value) { wrapper_.set(value); } |
| 152 |
| 153 PP_Var* StartRawUpdate() { |
| 154 wrapper_.StartRawUpdate(); |
| 155 return storage_.get(); |
| 156 } |
| 157 |
| 158 void EndRawUpdate() { |
| 159 wrapper_.EndRawUpdate(); |
| 160 } |
| 161 |
| 162 const PP_Var& ToCType() const { |
| 163 return *storage_; |
| 164 } |
| 165 |
| 166 private: |
| 167 internal::MayOwnPtr<PP_Var> storage_; |
| 168 internal::StringWrapper wrapper_; |
| 169 }; |
| 170 |
| 171 } // namespace pp |
| 172 |
| 173 #endif // PPAPI_CPP_DEV_OPTIONAL_DEV_H_ |
OLD | NEW |