Chromoting Android: Refactor the eye point drawing code into CardboardActivityEyePoin...
[chromium-blink-merge.git] / gin / arguments.h
blobb8d1296d1388cb1ec43c27bd4d76e3914365c774
1 // Copyright 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 GIN_ARGUMENTS_H_
6 #define GIN_ARGUMENTS_H_
8 #include "base/basictypes.h"
9 #include "gin/converter.h"
10 #include "gin/gin_export.h"
12 namespace gin {
14 // Arguments is a wrapper around v8::FunctionCallbackInfo that integrates
15 // with Converter to make it easier to marshall arguments and return values
16 // between V8 and C++.
17 class GIN_EXPORT Arguments {
18 public:
19 Arguments();
20 explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info);
21 ~Arguments();
23 template<typename T>
24 bool GetHolder(T* out) {
25 return ConvertFromV8(isolate_, info_->Holder(), out);
28 template<typename T>
29 bool GetData(T* out) {
30 return ConvertFromV8(isolate_, info_->Data(), out);
33 template<typename T>
34 bool GetNext(T* out) {
35 if (next_ >= info_->Length()) {
36 insufficient_arguments_ = true;
37 return false;
39 v8::Local<v8::Value> val = (*info_)[next_++];
40 return ConvertFromV8(isolate_, val, out);
43 template<typename T>
44 bool GetRemaining(std::vector<T>* out) {
45 if (next_ >= info_->Length()) {
46 insufficient_arguments_ = true;
47 return false;
49 int remaining = info_->Length() - next_;
50 out->resize(remaining);
51 for (int i = 0; i < remaining; ++i) {
52 v8::Local<v8::Value> val = (*info_)[next_++];
53 if (!ConvertFromV8(isolate_, val, &out->at(i)))
54 return false;
56 return true;
59 bool Skip() {
60 if (next_ >= info_->Length())
61 return false;
62 next_++;
63 return true;
66 int Length() const {
67 return info_->Length();
70 template<typename T>
71 void Return(T val) {
72 v8::Local<v8::Value> v8_value;
73 if (!TryConvertToV8(isolate_, val, &v8_value))
74 return;
75 info_->GetReturnValue().Set(v8_value);
78 // Always check the return value whether the handle is empty before
79 // dereferencing the handle.
80 v8::Local<v8::Value> PeekNext() const;
82 void ThrowError() const;
83 void ThrowTypeError(const std::string& message) const;
85 v8::Isolate* isolate() const { return isolate_; }
87 // Allows the function handler to distinguish between normal invocation
88 // and object construction.
89 bool IsConstructCall() const;
91 private:
92 v8::Isolate* isolate_;
93 const v8::FunctionCallbackInfo<v8::Value>* info_;
94 int next_;
95 bool insufficient_arguments_;
98 } // namespace gin
100 #endif // GIN_ARGUMENTS_H_