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

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 David's suggestions. 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> {
dmichael (off chromium) 2013/12/19 16:34:30 optional suggestion: I think when you have more op
yzshen1 2013/12/19 17:57:58 I am thinking we could use the generic one for all
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 it.
30 // |storage| must live longer than this object. The contents pointed to by
31 // |storage| must be zero-initialized by the caller.
32 Optional(PP_Optional_Double_Dev* storage, NotOwned)
33 : storage_(storage, NOT_OWNED) {
34 }
35
36 Optional(const Optional<double>& other) { *storage_ = *other.storage_; }
37
38 Optional<double>& operator=(const Optional<double>& other) {
39 return operator=(*other.storage_);
40 }
41
42 Optional<double>& operator=(const PP_Optional_Double_Dev& other) {
43 if (storage_.get() == &other)
44 return *this;
45
46 *storage_ = other;
47 return *this;
48 }
49
50 bool is_set() const { return PP_ToBool(storage_->is_set); }
51 void unset() { storage_->is_set = PP_FALSE; }
52
53 double get() const {
54 PP_DCHECK(is_set());
55 return storage_->value;
56 }
57
58 void set(double value) {
59 storage_->value = value;
60 storage_->is_set = PP_TRUE;
61 }
62
63 const PP_Optional_Double_Dev* ToCInput() const { return storage_.get(); }
64
65 PP_Optional_Double_Dev* StartRawUpdate() { return storage_.get(); }
66 void EndRawUpdate() {}
67
68 private:
69 internal::MayOwnPtr<PP_Optional_Double_Dev> storage_;
70 };
71
72 template <>
73 class Optional<std::string> {
74 public:
75 typedef const PP_Var& CInputType;
76
77 Optional() {}
78
79 Optional(const std::string& value) : wrapper_(value) {}
80
81 // Creates an accessor to |storage| but doesn't take ownership of it.
82 // |storage| must live longer than this object. The contents pointed to by
83 // |storage| must be zero-initialized by the caller.
84 Optional(PP_Var* storage, NotOwned) : wrapper_(storage, NOT_OWNED) {}
85
86 Optional(const Optional<std::string>& other) : wrapper_(other.wrapper_) {}
87
88 Optional<std::string>& operator=(const Optional<std::string>& other) {
89 wrapper_ = other.wrapper_;
90 return *this;
91 }
92
93 Optional<std::string>& operator=(const PP_Var& other) {
94 wrapper_ = other;
95 return *this;
96 }
97
98 bool is_set() const { return wrapper_.is_set(); }
99 void unset() { wrapper_.unset(); }
100 std::string get() const { return wrapper_.get(); }
101 void set(const std::string& value) { wrapper_.set(value); }
102
103 const PP_Var& ToCInput() const { return wrapper_.ToVar(); }
104
105 PP_Var* StartRawUpdate() { return wrapper_.StartRawUpdate(); }
106 void EndRawUpdate() { wrapper_.EndRawUpdate(); }
107
108 private:
109 internal::OptionalStringWrapper wrapper_;
110 };
111
112 } // namespace pp
113
114 #endif // PPAPI_CPP_DEV_OPTIONAL_DEV_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698