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

Side by Side Diff: ppapi/cpp/dev/optional_dev.h

Issue 116963003: App APIs in Pepper: C++ APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years 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
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 #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/c/pp_bool.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 <typename T>
18 class Optional;
19
20 template <>
21 class Optional<double> {
22 public:
23 typedef const PP_Optional_Double_Dev* CInputType;
24
25 Optional() {}
26
27 Optional(double value) { set(value); }
28
29 // It doesn't take ownership of |storage|, therefore |storage| must live
30 // longer than this object. |storage| must be zero-initialized.
31 Optional(PP_Optional_Double_Dev* storage, NotOwned)
32 : storage_(storage, NOT_OWNED) {
33 }
34
35 Optional(const Optional<double>& other) { *storage_ = *other.storage_; }
36
37 Optional<double>& operator=(const Optional<double>& other) {
38 return operator=(*other.storage_);
39 }
40
41 Optional<double>& operator=(const PP_Optional_Double_Dev& other) {
42 if (storage_.get() == &other)
43 return *this;
44
45 *storage_ = other;
46 return *this;
47 }
48
49 bool is_set() const { return PP_ToBool(storage_->is_set); }
50 void unset() { storage_->is_set = PP_FALSE; }
51
52 double get() const {
53 PP_DCHECK(is_set());
54 return storage_->value;
55 }
56
57 void set(double value) {
58 storage_->value = value;
59 storage_->is_set = PP_TRUE;
60 }
61
62 const PP_Optional_Double_Dev* ToCInput() const { return storage_.get(); }
63
64 PP_Optional_Double_Dev* StartRawUpdate() { return storage_.get(); }
65 void EndRawUpdate() {}
66
67 private:
68 internal::MayOwnPtr<PP_Optional_Double_Dev> storage_;
69 };
70
71 template <>
72 class Optional<std::string> {
73 public:
74 typedef const PP_Var& CInputType;
75
76 Optional() {}
77
78 Optional(const std::string& value) : wrapper_(value) {}
79
80 // It doesn't take ownership of |storage|, therefore |storage| must live
81 // longer than this object. |storage| must be zero-initialized.
82 Optional(PP_Var* storage, NotOwned) : wrapper_(storage, NOT_OWNED) {}
83
84 Optional(const Optional<std::string>& other) : wrapper_(other.wrapper_) {}
85
86 Optional<std::string>& operator=(const Optional<std::string>& other) {
87 wrapper_ = other.wrapper_;
88 return *this;
89 }
90
91 Optional<std::string>& operator=(const PP_Var& other) {
92 wrapper_ = other;
93 return *this;
94 }
95
96 bool is_set() const { return wrapper_.is_set(); }
97 void unset() { wrapper_.unset(); }
98 std::string get() const { return wrapper_.get(); }
99 void set(const std::string& value) { wrapper_.set(value); }
100
101 const PP_Var& ToCInput() const { return wrapper_.ToVar(); }
102
103 PP_Var* StartRawUpdate() { return wrapper_.StartRawUpdate(); }
104 void EndRawUpdate() { wrapper_.EndRawUpdate(); }
105
106 private:
107 internal::OptionalStringWrapper wrapper_;
108 };
109
110 } // namespace pp
111
112 #endif // PPAPI_CPP_DEV_OPTIONAL_DEV_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698