electron/native_mate/constructor.h.pump

130 lines
3.4 KiB
Text
Raw Normal View History

2014-08-10 08:59:50 +00:00
$$ This is a pump file for generating file templates. Pump is a python
$$ script that is part of the Google Test suite of utilities. Description
$$ can be found here:
$$
$$ http://code.google.com/p/googletest/wiki/PumpManual
$$
$var MAX_ARITY = 6
// Copyright 2014 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.chromium file.
#ifndef NATIVE_MATE_WRAPPABLE_CLASS_H_
#define NATIVE_MATE_WRAPPABLE_CLASS_H_
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "native_mate/wrappable.h"
#include "native_mate/function_template.h"
namespace mate {
class Wrappable;
namespace internal {
// This set of templates invokes a base::Callback by converting the Arguments
// into native types. It relies on the function_template.h to provide helper
// templates.
$range ARITY 0..MAX_ARITY
$for ARITY [[
$range ARG 1..ARITY
$if ARITY == 0 [[
]] $else [[
template<$for ARG , [[typename P$(ARG)]]>
]]
inline Wrappable* InvokeFactory(
Arguments* args,
const base::Callback<Wrappable*($for ARG , [[P$(ARG)]])>& callback) {
$if ARITY != 0 [[
$for ARG [[ typename CallbackParamTraits<P$(ARG)>::LocalType a$(ARG);
]]
if ($for ARG ||
[[!GetNextArgument(args, 0, $if ARG == 1 [[true]] $else [[false]], &a$(ARG))]])
return NULL;
]]
return callback.Run($for ARG , [[a$(ARG)]]);
};
]]
} // namespace internal
template<typename Sig>
class Constructor {
public:
typedef base::Callback<Sig> WrappableFactoryFunction;
Constructor(const base::StringPiece& name) : name_(name) {}
virtual ~Constructor() {
MATE_PERSISTENT_RESET(constructor_);
}
v8::Handle<v8::FunctionTemplate> GetFunctionTemplate(
v8::Isolate* isolate, const WrappableFactoryFunction& factory) {
if (constructor_.IsEmpty()) {
v8::Local<v8::FunctionTemplate> constructor = CreateFunctionTemplate(
isolate, base::Bind(&Constructor::New, factory));
constructor->InstanceTemplate()->SetInternalFieldCount(1);
constructor->SetClassName(StringToV8(isolate, name_));
MATE_PERSISTENT_ASSIGN(v8::FunctionTemplate, isolate, constructor_,
constructor);
}
return MATE_PERSISTENT_TO_LOCAL(
v8::FunctionTemplate, isolate, constructor_);
}
private:
static MATE_METHOD_RETURN_TYPE New(const WrappableFactoryFunction& factory,
v8::Isolate* isolate, Arguments* args) {
2015-01-03 02:04:46 +00:00
if (!args->IsConstructCall()) {
args->ThrowError("Requires constructor call");
MATE_METHOD_RETURN_UNDEFINED();
}
2014-08-10 08:59:50 +00:00
Wrappable* object = internal::InvokeFactory(args, factory);
if (object)
object->Wrap(isolate, args->GetThis());
else
args->ThrowError();
MATE_METHOD_RETURN_UNDEFINED();
}
base::StringPiece name_;
v8::Persistent<v8::FunctionTemplate> constructor_;
DISALLOW_COPY_AND_ASSIGN(Constructor);
};
template<typename T>
Wrappable* NewOperatorFactory() {
return new T;
}
template<typename T, typename Sig>
v8::Local<v8::Function> CreateConstructor(
v8::Isolate* isolate,
const base::StringPiece& name,
const base::Callback<Sig>& callback) {
v8::Local<v8::FunctionTemplate> constructor =
Constructor<Sig>(name).GetFunctionTemplate(isolate, callback);
T::BuildPrototype(isolate, constructor->PrototypeTemplate());
return constructor->GetFunction();
}
} // namespace mate
#endif // NATIVE_MATE_WRAPPABLE_CLASS_H_