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

Unified Diff: ppapi/cpp/dev/optional_dev.h

Issue 60173003: Draft: apps APIs in Pepper (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ppapi/cpp/dev/optional_dev.h
diff --git a/ppapi/cpp/dev/optional_dev.h b/ppapi/cpp/dev/optional_dev.h
new file mode 100644
index 0000000000000000000000000000000000000000..9144ff96e792609daba6d95c7754b254a22ec6c7
--- /dev/null
+++ b/ppapi/cpp/dev/optional_dev.h
@@ -0,0 +1,173 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef PPAPI_CPP_DEV_OPTIONAL_DEV_H_
+#define PPAPI_CPP_DEV_OPTIONAL_DEV_H_
+
+#include "ppapi/c/dev/pp_optional_structs_dev.h"
+#include "ppapi/cpp/dev/array_dev.h"
+#include "ppapi/cpp/dev/may_own_ptr_dev.h"
+#include "ppapi/cpp/dev/string_wrapper_dev.h"
+#include "ppapi/cpp/logging.h"
+#include "ppapi/cpp/var.h"
+
+namespace pp {
+
+template <class T>
+class Optional;
+
+template <>
+class Optional<double> {
+ public:
+ typedef const PP_Optional_Double* CType;
+
+ Optional(PP_Optional_Double* storage, NotOwned) :
+ storage_(storage, NOT_OWNED) {
+ }
+
+ Optional(const Optional<double>& other) : storage_(other.storage_) {
+ }
+
+ Optional<double>& operator=(const Optional<double>& other) {
+ storage_ = other.storage_;
+ return *this;
+ }
+
+ Optional<double>& operator=(const PP_Optional_Double& other) {
+ *storage_ = other;
+ return *this;
+ }
+
+ bool is_set() const { return PP_ToBool(storage_->is_set); }
+ void unset() { storage_->is_set = PP_FALSE; }
+ double get() const { return storage_->value; }
+ void set(double value) { storage_->value = value; }
+
+ PP_Optional_Double* StartRawUpdate() { return storage_.get(); }
+ void EndRawUpdate() {}
+
+ const PP_Optional_Double* ToCType() const {
+ return storage_.get();
+ }
+
+ private:
+ internal::MayOwnPtr<PP_Optional_Double> storage_;
+};
+
+template <>
+class Optional<Array<double> > {
+ public:
+ typedef const PP_Optional_Double_Array* CType;
+
+ Optional(PP_Optional_Double_Array* storage, NotOwned) :
+ storage_(storage, NOT_OWNED),
+ wrapper_(&storage_->value, NOT_OWNED) {
+ }
+
+ Optional(const Optional<Array<double> >& other)
+ : wrapper_(&storage_->value, NOT_OWNED) {
+ wrapper_ = other.wrapper_;
+ }
+
+ Optional<Array<double> >& operator=(const Optional<Array<double> >& other) {
+ return operator=(*other.storage_);
+ }
+
+ Optional<Array<double> >& operator=(const PP_Optional_Double_Array& other) {
+ if (storage_.get() == &other)
+ return *this;
+
+ storage_->is_set = other.is_set;
+ if (storage_->is_set)
+ wrapper_ = other.value;
+ else
+ wrapper_.Reset(0);
+
+ return *this;
+ }
+
+ bool is_set() const { return PP_ToBool(storage_->is_set); }
+ void unset() {
+ storage_->is_set = PP_FALSE;
+ wrapper_.Reset(0);
+ }
+ const Array<double>& get() const { return wrapper_; }
+ Array<double>& get() { return wrapper_; }
+
+ PP_Optional_Double_Array* StartRawUpdate() {
+ wrapper_.StartRawUpdate();
+ return storage_.get();
+ }
+
+ void EndRawUpdate() {
+ wrapper_.EndRawUpdate();
+ }
+
+ const PP_Optional_Double_Array* ToCType() const {
+ return storage_.get();
+ }
+
+ private:
+ internal::MayOwnPtr<PP_Optional_Double_Array> storage_;
+ Array<double> wrapper_;
+};
+
+template <>
+class Optional<std::string> {
+ public:
+ typedef const PP_Var& CType;
+
+ Optional(PP_Var* storage, NotOwned)
+ : storage_(storage, NOT_OWNED),
+ wrapper_(storage_.get(), NOT_OWNED) {
+ }
+
+ Optional(const Optional<std::string>& other)
+ : wrapper_(storage_.get(), NOT_OWNED) {
+ wrapper_ = other.wrapper_;
+ }
+
+ Optional<std::string>& operator=(const Optional<std::string>& other) {
+ return operator=(*other.storage_);
+ }
+
+ Optional(const std::string& value)
+ : wrapper_(storage_.get(), NOT_OWNED) {
+ wrapper_.set(value);
+ }
+
+ Optional<std::string>& operator=(const PP_Var& other) {
+ if (storage_.get() == &other)
+ return *this;
+
+ wrapper_ = other;
+ return *this;
+ }
+
+ bool is_set() const { return wrapper_.is_set(); }
+ void unset() { wrapper_.unset(); }
+ std::string get() const { return wrapper_.get(); }
+ void set(const std::string& value) { wrapper_.set(value); }
+
+ PP_Var* StartRawUpdate() {
+ wrapper_.StartRawUpdate();
+ return storage_.get();
+ }
+
+ void EndRawUpdate() {
+ wrapper_.EndRawUpdate();
+ }
+
+ const PP_Var& ToCType() const {
+ return *storage_;
+ }
+
+ private:
+ internal::MayOwnPtr<PP_Var> storage_;
+ internal::StringWrapper wrapper_;
+};
+
+} // namespace pp
+
+#endif // PPAPI_CPP_DEV_OPTIONAL_DEV_H_
« 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