summaryrefslogtreecommitdiffstats
path: root/ppapi/cpp/extensions/ext_output_traits.h
blob: b49ff9ff35c972086a50bdbe8686ebeb2e51dfa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// 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_EXTENSIONS_OUTPUT_TRAITS_H_
#define PPAPI_CPP_EXTENSIONS_OUTPUT_TRAITS_H_

#include <vector>

#include "ppapi/c/pp_var.h"
#include "ppapi/cpp/extensions/from_var_converter.h"
#include "ppapi/cpp/logging.h"
#include "ppapi/cpp/pass_ref.h"
#include "ppapi/cpp/var.h"

namespace pp {
namespace ext {
namespace internal {

// Base class for all those types within the pp::ext namespace that are used
// with CompletionCallbackWithOutput. This class doesn't do anything itself,
// but it affects the behavior of CallbackOutputTraits for all its subclasses.
//
// TODO(yzshen): Within pp::ext, basic types such as std::string or double may
// be used with CompletionCallbackWithOutput as well. This approach doesn't
// work for them. One way is to refactor CallbackOutputTraits to consider not
// only the output C++ object type, but also the output parameter type that the
// C interface uses. And then we can remove this class.
class OutputObjectBase {
};

template <class T>
class VarOutputAdapterWithStorage {
 public:
  VarOutputAdapterWithStorage() : pp_var_(PP_MakeUndefined()) {
  }

  ~VarOutputAdapterWithStorage() {
    PP_DCHECK(pp_var_.type == PP_VARTYPE_UNDEFINED);
  }

  PP_Var& pp_var() { return pp_var_; }

  T& output() {
    converter_.Set(PASS_REF, pp_var_);
    pp_var_ = PP_MakeUndefined();
    return converter_.value();
  }

 private:
  PP_Var pp_var_;
  FromVarConverter<T> converter_;

  // Disallow copying and assignment.
  VarOutputAdapterWithStorage(const VarOutputAdapterWithStorage<T>&);
  VarOutputAdapterWithStorage<T>& operator=(
      const VarOutputAdapterWithStorage<T>&);
};

template <class T>
struct ExtensionsCallbackOutputTraits {
  typedef PP_Var* APIArgType;
  typedef VarOutputAdapterWithStorage<T> StorageType;

  static inline APIArgType StorageToAPIArg(StorageType& t) {
    return &t.pp_var();
  }

  // This must be called exactly once to consume the one PP_Var reference
  // assigned to us by the browser.
  static inline T& StorageToPluginArg(StorageType& t) {
    return t.output();
  }
};

// This class provides storage for a PP_Var and a vector of objects which are
// of type T. The PP_Var is used as an output parameter to recevie an array var
// from the browser. Each element in the array var is converted to a T object,
// using FromVarConverter, and stores in the vector.
template <class T>
class ArrayVarOutputAdapterWithStorage {
 public:
  ArrayVarOutputAdapterWithStorage() : pp_var_(PP_MakeUndefined()) {
  }

  ~ArrayVarOutputAdapterWithStorage() {
    PP_DCHECK(pp_var_.type == PP_VARTYPE_UNDEFINED);
  }

  PP_Var& pp_var() { return pp_var_; }

  std::vector<T>& output() {
    PP_DCHECK(output_storage_.empty());

    // TODO(yzshen): Add support for ArrayVar and read the contents from
    // |pp_var_| and put into |output_storage_|.
    Var auto_release(PASS_REF, pp_var_);
    pp_var_ = PP_MakeUndefined();

    return output_storage_;
  }

 private:
  PP_Var pp_var_;
  std::vector<T> output_storage_;

  // Disallow copying and assignment.
  ArrayVarOutputAdapterWithStorage(const ArrayVarOutputAdapterWithStorage<T>&);
  ArrayVarOutputAdapterWithStorage<T>& operator=(
      const ArrayVarOutputAdapterWithStorage<T>&);
};

template <class T>
struct ExtensionsVectorCallbackOutputTraits {
  typedef PP_Var* APIArgType;
  typedef ArrayVarOutputAdapterWithStorage<T> StorageType;

  static inline APIArgType StorageToAPIArg(StorageType& t) {
    return &t.pp_var();
  }

  // This must be called exactly once to consume the one PP_Var reference
  // assigned to us by the browser.
  static inline std::vector<T>& StorageToPluginArg(StorageType& t) {
    return t.output();
  }
};

}  // namespace internal
}  // namespace ext
}  // namespace pp

#endif  // PPAPI_CPP_EXTENSIONS_OUTPUT_TRAITS_H_