Home | History | Annotate | Download | only in extensions
      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 
     26   ~DictField() {
     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>
     60 class OptionalDictField {
     61  public:
     62   explicit OptionalDictField(const std::string& key) : key_(key) {
     63   }
     64 
     65   ~OptionalDictField() {
     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_
    101