electron/native_mate/constructor.h.pump
2016-04-25 10:17:39 +09:00

139 lines
3.7 KiB
Text

$$ 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 WrappableBase;
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 WrappableBase* InvokeFactory(
Arguments* args,
const base::Callback<WrappableBase*($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 nullptr;
]]
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::Local<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) {
if (!args->IsConstructCall()) {
args->ThrowError("Requires constructor call");
MATE_METHOD_RETURN_UNDEFINED();
}
WrappableBase* object;
{
// Don't continue if the constructor throws an exception.
v8::TryCatch try_catch;
object = internal::InvokeFactory(args, factory);
if (try_catch.HasCaught()) {
try_catch.ReThrow();
MATE_METHOD_RETURN_UNDEFINED();
}
}
if (object)
object->InitWith(isolate, args->GetThis());
else
args->ThrowError();
MATE_METHOD_RETURN_UNDEFINED();
}
base::StringPiece name_;
v8::Global<v8::FunctionTemplate> constructor_;
DISALLOW_COPY_AND_ASSIGN(Constructor);
};
template<typename T>
WrappableBase* 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_