2021-03-04 19:30:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2021 Google LLC.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SKSL_DSL_FUNCTION
|
|
|
|
#define SKSL_DSL_FUNCTION
|
|
|
|
|
|
|
|
#include "include/sksl/DSLBlock.h"
|
2021-04-09 16:25:03 +00:00
|
|
|
#include "include/sksl/DSLExpression.h"
|
2021-03-04 19:30:25 +00:00
|
|
|
#include "include/sksl/DSLType.h"
|
2021-04-09 16:25:03 +00:00
|
|
|
#include "include/sksl/DSLVar.h"
|
2021-05-06 14:47:06 +00:00
|
|
|
#include "include/sksl/DSLWrapper.h"
|
2021-03-04 19:30:25 +00:00
|
|
|
|
|
|
|
namespace SkSL {
|
|
|
|
|
|
|
|
class Block;
|
2021-04-09 16:25:03 +00:00
|
|
|
class FunctionDeclaration;
|
2021-03-04 19:30:25 +00:00
|
|
|
class Variable;
|
|
|
|
|
|
|
|
namespace dsl {
|
|
|
|
|
|
|
|
class DSLType;
|
|
|
|
|
|
|
|
class DSLFunction {
|
|
|
|
public:
|
|
|
|
template<class... Parameters>
|
2021-05-06 14:47:06 +00:00
|
|
|
DSLFunction(const DSLType& returnType, const char* name, Parameters&... parameters) {
|
|
|
|
SkTArray<DSLVar*> parameterArray;
|
|
|
|
parameterArray.reserve_back(sizeof...(parameters));
|
2021-03-04 19:30:25 +00:00
|
|
|
|
|
|
|
// in C++17, we could just do:
|
|
|
|
// (parameterArray.push_back(¶meters), ...);
|
|
|
|
int unused[] = {0, (static_cast<void>(parameterArray.push_back(¶meters)), 0)...};
|
|
|
|
static_cast<void>(unused);
|
|
|
|
this->init(returnType, name, std::move(parameterArray));
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:47:06 +00:00
|
|
|
DSLFunction(const DSLType& returnType, const char* name, SkTArray<DSLVar*> parameters) {
|
|
|
|
this->init(returnType, name, std::move(parameters));
|
|
|
|
}
|
|
|
|
|
2021-03-04 19:30:25 +00:00
|
|
|
virtual ~DSLFunction() = default;
|
|
|
|
|
|
|
|
template<class... Stmt>
|
|
|
|
void define(Stmt... stmts) {
|
|
|
|
DSLBlock block = DSLBlock(DSLStatement(std::move(stmts))...);
|
|
|
|
this->define(std::move(block));
|
|
|
|
}
|
|
|
|
|
|
|
|
void define(DSLBlock block);
|
|
|
|
|
2021-05-06 14:47:06 +00:00
|
|
|
/**
|
|
|
|
* Invokes the function with the given arguments.
|
|
|
|
*/
|
2021-03-04 19:30:25 +00:00
|
|
|
template<class... Args>
|
2021-04-09 16:25:03 +00:00
|
|
|
DSLExpression operator()(Args&&... args) {
|
2021-05-06 14:47:06 +00:00
|
|
|
SkTArray<DSLWrapper<DSLExpression>> argArray;
|
2021-03-04 19:30:25 +00:00
|
|
|
argArray.reserve_back(sizeof...(args));
|
2021-04-09 16:25:03 +00:00
|
|
|
this->collectArgs(argArray, std::forward<Args>(args)...);
|
2021-03-04 19:30:25 +00:00
|
|
|
return this->call(std::move(argArray));
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:47:06 +00:00
|
|
|
/**
|
|
|
|
* Invokes the function with the given arguments.
|
|
|
|
*/
|
|
|
|
DSLExpression call(SkTArray<DSLWrapper<DSLExpression>> args);
|
|
|
|
|
2021-03-04 19:30:25 +00:00
|
|
|
private:
|
2021-05-06 14:47:06 +00:00
|
|
|
void collectArgs(SkTArray<DSLWrapper<DSLExpression>>& args) {}
|
2021-04-09 16:25:03 +00:00
|
|
|
|
|
|
|
template<class... RemainingArgs>
|
2021-05-06 14:47:06 +00:00
|
|
|
void collectArgs(SkTArray<DSLWrapper<DSLExpression>>& args, DSLVar& var,
|
|
|
|
RemainingArgs&&... remaining) {
|
|
|
|
args.push_back(DSLWrapper<DSLExpression>(var));
|
2021-04-09 16:25:03 +00:00
|
|
|
collectArgs(args, std::forward<RemainingArgs>(remaining)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class... RemainingArgs>
|
2021-05-06 14:47:06 +00:00
|
|
|
void collectArgs(SkTArray<DSLWrapper<DSLExpression>>& args, DSLExpression expr,
|
2021-04-09 16:25:03 +00:00
|
|
|
RemainingArgs&&... remaining) {
|
2021-05-06 14:47:06 +00:00
|
|
|
args.push_back(DSLWrapper<DSLExpression>(std::move(expr)));
|
2021-04-09 16:25:03 +00:00
|
|
|
collectArgs(args, std::forward<RemainingArgs>(remaining)...);
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:47:06 +00:00
|
|
|
void init(const DSLType& returnType, const char* name, SkTArray<DSLVar*> params);
|
2021-03-04 19:30:25 +00:00
|
|
|
|
2021-05-04 18:30:02 +00:00
|
|
|
const SkSL::FunctionDeclaration* fDecl = nullptr;
|
2021-03-04 19:30:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dsl
|
|
|
|
|
|
|
|
} // namespace SkSL
|
|
|
|
|
|
|
|
#endif
|