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_
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"
23 explicit DictField(const std::string& key) : key_(key), value_() {
29 const std::string& key() const { return key_; }
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 {
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())
49 internal::FromVarConverter<T> converter(value_var.pp_var());
50 value_ = converter.value();
60 class OptionalDictField {
62 explicit OptionalDictField(const std::string& key) : key_(key) {
65 ~OptionalDictField() {
68 const std::string& key() const { return key_; }
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 {
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());
100 #endif // PPAPI_CPP_EXTENSIONS_DICT_FIELD_H_