Pepper_31_C++_interfaces
dict_field.h
Go to the documentation of this file.
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_EXTENSIONS_DICT_FIELD_H_
6 #define PPAPI_CPP_EXTENSIONS_DICT_FIELD_H_
7 
8 #include <string>
9 
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"
16 
17 namespace pp {
18 namespace ext {
19 
20 template <class T>
21 class DictField {
22  public:
23  explicit DictField(const std::string& key) : key_(key), value_() {
24  }
25 
27  }
28 
29  const std::string& key() const { return key_; }
30 
31  // Returns the value.
32  T& operator()() { return value_; }
33  const T& operator()() const { return value_; }
34 
35  // Adds this field to the dictionary var.
36  bool AddTo(VarDictionary* dict) const {
37  if (!dict)
38  return false;
39 
40  internal::ToVarConverter<T> converter(value_);
41  return dict->Set(Var(key_), converter.var());
42  }
43 
44  bool Populate(const VarDictionary& dict) {
45  Var value_var = dict.Get(Var(key_));
46  if (value_var.is_undefined())
47  return false;
48 
49  internal::FromVarConverter<T> converter(value_var.pp_var());
50  value_ = converter.value();
51  return true;
52  }
53 
54  private:
55  std::string key_;
56  T value_;
57 };
58 
59 template <class T>
61  public:
62  explicit OptionalDictField(const std::string& key) : key_(key) {
63  }
64 
66  }
67 
68  const std::string& key() const { return key_; }
69 
70  // Returns the value.
71  Optional<T>& operator()() { return value_; }
72  const Optional<T>& operator()() const { return value_; }
73 
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;
80 
81  internal::ToVarConverter<T> converter(*value_);
82  return dict->Set(Var(key_), converter.var());
83  }
84 
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;
90  }
91 
92  private:
93  std::string key_;
94  Optional<T> value_;
95 };
96 
97 } // namespace ext
98 } // namespace pp
99 
100 #endif // PPAPI_CPP_EXTENSIONS_DICT_FIELD_H_
bool AddTo(VarDictionary *dict) const
Definition: dict_field.h:36
const T & operator()() const
Definition: dict_field.h:33
const Optional< T > & operator()() const
Definition: dict_field.h:72
bool Populate(const VarDictionary &dict)
Definition: dict_field.h:44
DictField(const std::string &key)
Definition: dict_field.h:23
bool is_undefined() const
Definition: var.h:106
bool Set(const Var &key, const Var &value)
Var Get(const Var &key) const
const PP_Var & pp_var() const
Definition: var.h:213
const std::string & key() const
Definition: dict_field.h:68
const std::string & key() const
Definition: dict_field.h:29
bool Populate(const VarDictionary &dict)
Definition: dict_field.h:85
A generic type used for passing data types between the module and the page.
Definition: var.h:20
bool MayAddTo(VarDictionary *dict) const
Definition: dict_field.h:75
Optional< T > & operator()()
Definition: dict_field.h:71
OptionalDictField(const std::string &key)
Definition: dict_field.h:62