cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / ppapi / cpp / extensions / dict_field.h
blobf332014e8978a3f089eefe03e96f1e8b0e406af1
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.
5 #ifndef PPAPI_CPP_EXTENSIONS_DICT_FIELD_H_
6 #define PPAPI_CPP_EXTENSIONS_DICT_FIELD_H_
8 #include <string>
10 #include "ppapi/c/pp_bool.h"
11 #include "ppapi/cpp/extensions/from_var_converter.h"
12 #include "ppapi/cpp/extensions/optional.h"
13 #include "ppapi/cpp/extensions/to_var_converter.h"
14 #include "ppapi/cpp/var.h"
15 #include "ppapi/cpp/var_dictionary.h"
17 namespace pp {
18 namespace ext {
20 template <class T>
21 class DictField {
22 public:
23 explicit DictField(const std::string& key) : key_(key), value_() {
26 ~DictField() {
29 const std::string& key() const { return key_; }
31 // Returns the value.
32 T& operator()() { return value_; }
33 const T& operator()() const { return value_; }
35 // Adds this field to the dictionary var.
36 bool AddTo(VarDictionary* dict) const {
37 if (!dict)
38 return false;
40 internal::ToVarConverter<T> converter(value_);
41 return dict->Set(Var(key_), converter.var());
44 bool Populate(const VarDictionary& dict) {
45 Var value_var = dict.Get(Var(key_));
46 if (value_var.is_undefined())
47 return false;
49 internal::FromVarConverter<T> converter(value_var.pp_var());
50 value_ = converter.value();
51 return true;
54 private:
55 std::string key_;
56 T value_;
59 template <class T>
60 class OptionalDictField {
61 public:
62 explicit OptionalDictField(const std::string& key) : key_(key) {
65 ~OptionalDictField() {
68 const std::string& key() const { return key_; }
70 // Returns the value.
71 Optional<T>& operator()() { return value_; }
72 const Optional<T>& operator()() const { return value_; }
74 // Adds this field to the dictionary var, if |value| has been set.
75 bool MayAddTo(VarDictionary* dict) const {
76 if (!dict)
77 return false;
78 if (!value_.IsSet())
79 return true;
81 internal::ToVarConverter<T> converter(*value_);
82 return dict->Set(Var(key_), converter.var());
85 bool Populate(const VarDictionary& dict) {
86 Var value_var = dict.Get(Var(key_));
87 internal::FromVarConverter<Optional<T> > converter(value_var.pp_var());
88 value_.Swap(&converter.value());
89 return true;
92 private:
93 std::string key_;
94 Optional<T> value_;
97 } // namespace ext
98 } // namespace pp
100 #endif // PPAPI_CPP_EXTENSIONS_DICT_FIELD_H_