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

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: changes according to Sam's suggestions. Created 6 years, 12 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/may_own_ptr_dev.h ('k') | ppapi/cpp/dev/string_wrapper_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 #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 // Creates an accessor to |storage| but doesn't take ownership of the memory.
30 // |storage| must live longer than this object.
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 Optional(const char* value) : wrapper_(value) {}
81
82 // Creates an accessor to |storage| but doesn't take ownership of the memory.
83 // |storage| must live longer than this object.
84 //
85 // Although this object doesn't own the memory of |storage|, it manages the
86 // ref count and sets the var to undefined when destructed.
87 Optional(PP_Var* storage, NotOwned) : wrapper_(storage, NOT_OWNED) {}
88
89 Optional(const Optional<std::string>& other) : wrapper_(other.wrapper_) {}
90
91 Optional<std::string>& operator=(const Optional<std::string>& other) {
92 wrapper_ = other.wrapper_;
93 return *this;
94 }
95
96 Optional<std::string>& operator=(const PP_Var& other) {
97 wrapper_ = other;
98 return *this;
99 }
100
101 bool is_set() const { return wrapper_.is_set(); }
102 void unset() { wrapper_.unset(); }
103 std::string get() const { return wrapper_.get(); }
104 void set(const std::string& value) { wrapper_.set(value); }
105
106 const PP_Var& ToCInput() const { return wrapper_.ToVar(); }
107
108 PP_Var* StartRawUpdate() { return wrapper_.StartRawUpdate(); }
109 void EndRawUpdate() { wrapper_.EndRawUpdate(); }
110
111 private:
112 internal::OptionalStringWrapper wrapper_;
113 };
114
115 } // namespace pp
116
117 #endif // PPAPI_CPP_DEV_OPTIONAL_DEV_H_
OLDNEW
« no previous file with comments | « ppapi/cpp/dev/may_own_ptr_dev.h ('k') | ppapi/cpp/dev/string_wrapper_dev.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698