json-script.cpp loader.cpp logger.cpp logger.thpp math-script.cpp
netstring.cpp networkstream.cpp number.cpp number-script.cpp object.cpp
object-script.cpp objecttype.cpp primitivetype.cpp process.cpp ringbuffer.cpp scriptframe.cpp
- function.cpp function.thpp function-script.cpp functionwrapper.cpp
+ function.cpp function.thpp function-script.cpp
perfdatavalue.cpp perfdatavalue.thpp scriptglobal.cpp
scriptutils.cpp serializer.cpp socket.cpp socketevents.cpp socketevents-epoll.cpp socketevents-poll.cpp stacktrace.cpp
statsfunction.cpp stdiostream.cpp stream.cpp streamlogger.cpp streamlogger.thpp string.cpp string-script.cpp
if (!prototype) {
prototype = new Dictionary();
- prototype->Set("len", new Function("Array#len", WrapFunction(ArrayLen), {}, true));
- prototype->Set("set", new Function("Array#set", WrapFunction(ArraySet), { "index", "value" }));
- prototype->Set("get", new Function("Array#get", WrapFunction(ArrayGet), { "index" }));
- prototype->Set("add", new Function("Array#add", WrapFunction(ArrayAdd), { "value" }));
- prototype->Set("remove", new Function("Array#remove", WrapFunction(ArrayRemove), { "index" }));
- prototype->Set("contains", new Function("Array#contains", WrapFunction(ArrayContains), { "value" }, true));
- prototype->Set("clear", new Function("Array#clear", WrapFunction(ArrayClear)));
- prototype->Set("sort", new Function("Array#sort", WrapFunction(ArraySort), { "less_cmp" }, true));
- prototype->Set("shallow_clone", new Function("Array#shallow_clone", WrapFunction(ArrayShallowClone), {}, true));
- prototype->Set("join", new Function("Array#join", WrapFunction(ArrayJoin), { "separator" }, true));
- prototype->Set("reverse", new Function("Array#reverse", WrapFunction(ArrayReverse), {}, true));
- prototype->Set("map", new Function("Array#map", WrapFunction(ArrayMap), { "func" }, true));
- prototype->Set("reduce", new Function("Array#reduce", WrapFunction(ArrayReduce), { "reduce" }, true));
- prototype->Set("filter", new Function("Array#filter", WrapFunction(ArrayFilter), { "func" }, true));
- prototype->Set("any", new Function("Array#any", WrapFunction(ArrayAny), { "func" }, true));
- prototype->Set("all", new Function("Array#all", WrapFunction(ArrayAll), { "func" }, true));
- prototype->Set("unique", new Function("Array#unique", WrapFunction(ArrayUnique), {}, true));
+ prototype->Set("len", new Function("Array#len", ArrayLen, {}, true));
+ prototype->Set("set", new Function("Array#set", ArraySet, { "index", "value" }));
+ prototype->Set("get", new Function("Array#get", ArrayGet, { "index" }));
+ prototype->Set("add", new Function("Array#add", ArrayAdd, { "value" }));
+ prototype->Set("remove", new Function("Array#remove", ArrayRemove, { "index" }));
+ prototype->Set("contains", new Function("Array#contains", ArrayContains, { "value" }, true));
+ prototype->Set("clear", new Function("Array#clear", ArrayClear));
+ prototype->Set("sort", new Function("Array#sort", ArraySort, { "less_cmp" }, true));
+ prototype->Set("shallow_clone", new Function("Array#shallow_clone", ArrayShallowClone, {}, true));
+ prototype->Set("join", new Function("Array#join", ArrayJoin, { "separator" }, true));
+ prototype->Set("reverse", new Function("Array#reverse", ArrayReverse, {}, true));
+ prototype->Set("map", new Function("Array#map", ArrayMap, { "func" }, true));
+ prototype->Set("reduce", new Function("Array#reduce", ArrayReduce, { "reduce" }, true));
+ prototype->Set("filter", new Function("Array#filter", ArrayFilter, { "func" }, true));
+ prototype->Set("any", new Function("Array#any", ArrayAny, { "func" }, true));
+ prototype->Set("all", new Function("Array#all", ArrayAll, { "func" }, true));
+ prototype->Set("unique", new Function("Array#unique", ArrayUnique, {}, true));
}
return prototype;
if (!prototype) {
prototype = new Dictionary();
- prototype->Set("to_string", new Function("Boolean#to_string", WrapFunction(BooleanToString), {}, true));
+ prototype->Set("to_string", new Function("Boolean#to_string", BooleanToString, {}, true));
}
return prototype;
if (!prototype) {
prototype = new Dictionary();
- prototype->Set("modify_attribute", new Function("ConfigObject#modify_attribute", WrapFunction(ConfigObjectModifyAttribute), { "attr", "value" }, false));
- prototype->Set("restore_attribute", new Function("ConfigObject#restore_attribute", WrapFunction(ConfigObjectRestoreAttribute), { "attr", "value" }, false));
+ prototype->Set("modify_attribute", new Function("ConfigObject#modify_attribute", ConfigObjectModifyAttribute, { "attr", "value" }, false));
+ prototype->Set("restore_attribute", new Function("ConfigObject#restore_attribute", ConfigObjectRestoreAttribute, { "attr", "value" }, false));
}
return prototype;
if (!prototype) {
prototype = new Dictionary();
- prototype->Set("format", new Function("DateTime#format", WrapFunction(DateTimeFormat), { "format" }));
+ prototype->Set("format", new Function("DateTime#format", DateTimeFormat, { "format" }));
}
return prototype;
if (!prototype) {
prototype = new Dictionary();
- prototype->Set("len", new Function("Dictionary#len", WrapFunction(DictionaryLen), {}, true));
- prototype->Set("set", new Function("Dictionary#set", WrapFunction(DictionarySet), { "key", "value" }));
- prototype->Set("get", new Function("Dictionary#get", WrapFunction(DictionaryGet), { "key" }));
- prototype->Set("remove", new Function("Dictionary#remove", WrapFunction(DictionaryRemove), { "key" }));
- prototype->Set("contains", new Function("Dictionary#contains", WrapFunction(DictionaryContains), { "key" }, true));
- prototype->Set("shallow_clone", new Function("Dictionary#shallow_clone", WrapFunction(DictionaryShallowClone), {}, true));
- prototype->Set("keys", new Function("Dictionary#keys", WrapFunction(DictionaryKeys), {}, true));
- prototype->Set("values", new Function("Dictionary#values", WrapFunction(DictionaryValues), {}, true));
+ prototype->Set("len", new Function("Dictionary#len", DictionaryLen, {}, true));
+ prototype->Set("set", new Function("Dictionary#set", DictionarySet, { "key", "value" }));
+ prototype->Set("get", new Function("Dictionary#get", DictionaryGet, { "key" }));
+ prototype->Set("remove", new Function("Dictionary#remove", DictionaryRemove, { "key" }));
+ prototype->Set("contains", new Function("Dictionary#contains", DictionaryContains, { "key" }, true));
+ prototype->Set("shallow_clone", new Function("Dictionary#shallow_clone", DictionaryShallowClone, {}, true));
+ prototype->Set("keys", new Function("Dictionary#keys", DictionaryKeys, {}, true));
+ prototype->Set("values", new Function("Dictionary#values", DictionaryValues, {}, true));
}
return prototype;
if (!prototype) {
prototype = new Dictionary();
- prototype->Set("call", new Function("Function#call", WrapFunction(FunctionCall)));
- prototype->Set("callv", new Function("Function#callv", WrapFunction(FunctionCallV)));
+ prototype->Set("call", new Function("Function#call", FunctionCall));
+ prototype->Set("callv", new Function("Function#callv", FunctionCallV));
}
return prototype;
typedef std::function<Value (const std::vector<Value>& arguments)> Callback;
- Function(const String& name, const Callback& function, const std::vector<String>& args = std::vector<String>(),
- bool side_effect_free = false, bool deprecated = false);
+ template<typename F>
+ Function(const String& name, F function, const std::vector<String>& args = std::vector<String>(),
+ bool side_effect_free = false, bool deprecated = false)
+ : Function(name, WrapFunction(function), args, side_effect_free, deprecated)
+ { }
Value Invoke(const std::vector<Value>& arguments = std::vector<Value>());
Value Invoke(const Value& otherThis, const std::vector<Value>& arguments = std::vector<Value>());
private:
Callback m_Callback;
+
+ Function(const String& name, const Callback& function, const std::vector<String>& args,
+ bool side_effect_free, bool deprecated);
};
#define REGISTER_SCRIPTFUNCTION_NS(ns, name, callback, args) \
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
- Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), false); \
+ Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), false); \
ScriptGlobal::Set(#ns "." #name, sf); \
}, 10)
#define REGISTER_SCRIPTFUNCTION_NS_PREFIX(ns, name, callback, args) \
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
- Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), false); \
+ Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), false); \
ScriptGlobal::Set(#ns "." #name, sf); \
Function::Ptr dsf = new icinga::Function("Deprecated#__" #name " (deprecated)", WrapFunction(callback), String(args).Split(":"), false, true); \
ScriptGlobal::Set("Deprecated.__" #name, dsf); \
#define REGISTER_SCRIPTFUNCTION_NS_DEPRECATED(ns, name, callback, args) \
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
- Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), false); \
+ Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), false); \
ScriptGlobal::Set(#ns "." #name, sf); \
Function::Ptr dsf = new icinga::Function("Deprecated#" #name " (deprecated)", WrapFunction(callback), String(args).Split(":"), false, true); \
ScriptGlobal::Set("Deprecated." #name, dsf); \
#define REGISTER_SAFE_SCRIPTFUNCTION_NS(ns, name, callback, args) \
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
- Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), true); \
+ Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), true); \
ScriptGlobal::Set(#ns "." #name, sf); \
}, 10)
#define REGISTER_SAFE_SCRIPTFUNCTION_NS_PREFIX(ns, name, callback, args) \
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
- Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), true); \
+ Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), true); \
ScriptGlobal::Set(#ns "." #name, sf); \
Function::Ptr dsf = new icinga::Function("Deprecated#__" #name " (deprecated)", WrapFunction(callback), String(args).Split(":"), true, true); \
ScriptGlobal::Set("Deprecated.__" #name, dsf); \
#define REGISTER_SAFE_SCRIPTFUNCTION_NS_DEPRECATED(ns, name, callback, args) \
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
- Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), String(args).Split(":"), true); \
+ Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), true); \
ScriptGlobal::Set(#ns "." #name, sf); \
Function::Ptr dsf = new icinga::Function("Deprecated#" #name " (deprecated)", WrapFunction(callback), String(args).Split(":"), true, true); \
ScriptGlobal::Set("Deprecated." #name, dsf); \
+++ /dev/null
-/******************************************************************************
- * Icinga 2 *
- * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
- * *
- * This program is free software; you can redistribute it and/or *
- * modify it under the terms of the GNU General Public License *
- * as published by the Free Software Foundation; either version 2 *
- * of the License, or (at your option) any later version. *
- * *
- * This program is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with this program; if not, write to the Free Software Foundation *
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
- ******************************************************************************/
-
-#include "base/functionwrapper.hpp"
-
-using namespace icinga;
-
-Value icinga::FunctionWrapperVV(void (*function)(void), const std::vector<Value>&)
-{
- function();
-
- return Empty;
-}
-
-Value icinga::FunctionWrapperVA(void (*function)(const std::vector<Value>&), const std::vector<Value>& arguments)
-{
- function(arguments);
-
- return Empty;
-}
-
-std::function<Value (const std::vector<Value>& arguments)> icinga::WrapFunction(void (*function)(void))
-{
- return std::bind(&FunctionWrapperVV, function, _1);
-}
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
-#ifndef SCRIPTFUNCTIONWRAPPER_H
-#define SCRIPTFUNCTIONWRAPPER_H
+#ifndef FUNCTIONWRAPPER_H
+#define FUNCTIONWRAPPER_H
#include "base/i2-base.hpp"
#include "base/value.hpp"
#include <vector>
+#include <type_traits>
using namespace std::placeholders;
namespace icinga
{
-Value FunctionWrapperVV(void (*function)(void), const std::vector<Value>& arguments);
-Value FunctionWrapperVA(void (*function)(const std::vector<Value>&), const std::vector<Value>& arguments);
-
-std::function<Value (const std::vector<Value>& arguments)> I2_BASE_API WrapFunction(void (*function)(void));
-
-template<typename TR>
-Value FunctionWrapperR(TR (*function)(void), const std::vector<Value>&)
-{
- return function();
-}
-
-template<typename TR>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(void))
-{
- return std::bind(&FunctionWrapperR<TR>, function, _1);
-}
-
-template<typename T0>
-Value FunctionWrapperV(void (*function)(T0), const std::vector<Value>& arguments)
-{
- if (arguments.size() < 1)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
- else if (arguments.size() > 1)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
- function(static_cast<T0>(arguments[0]));
-
- return Empty;
-}
-
-template<typename T0>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0))
-{
- return std::bind(&FunctionWrapperV<T0>, function, _1);
-}
-
-template<typename TR, typename T0>
-Value FunctionWrapperR(TR (*function)(T0), const std::vector<Value>& arguments)
-{
- if (arguments.size() < 1)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
- else if (arguments.size() > 1)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
- return function(static_cast<T0>(arguments[0]));
-}
-
-template<typename TR, typename T0>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0))
-{
- return std::bind(&FunctionWrapperR<TR, T0>, function, _1);
-}
-
-template<typename T0, typename T1>
-Value FunctionWrapperV(void (*function)(T0, T1), const std::vector<Value>& arguments)
-{
- if (arguments.size() < 2)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
- else if (arguments.size() > 2)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
- function(static_cast<T0>(arguments[0]),
- static_cast<T1>(arguments[1]));
-
- return Empty;
-}
-
-template<typename T0, typename T1>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1))
+inline std::function<Value (const std::vector<Value>&)> WrapFunction(const std::function<Value(const std::vector<Value>&)>& function)
{
- return std::bind(&FunctionWrapperV<T0, T1>, function, _1);
+ return function;
}
-template<typename TR, typename T0, typename T1>
-Value FunctionWrapperR(TR (*function)(T0, T1), const std::vector<Value>& arguments)
+inline std::function<Value (const std::vector<Value>&)> WrapFunction(void (*function)(const std::vector<Value>&))
{
- if (arguments.size() < 2)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
- else if (arguments.size() > 2)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
- return function(static_cast<T0>(arguments[0]),
- static_cast<T1>(arguments[1]));
+ return [function](const std::vector<Value>& arguments) {
+ function(arguments);
+ return Empty;
+ };
}
-
-template<typename TR, typename T0, typename T1>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1))
+template<typename Return>
+std::function<Value (const std::vector<Value>&)> WrapFunction(Return (*function)(const std::vector<Value>&))
{
- return std::bind(&FunctionWrapperR<TR, T0, T1>, function, _1);
+ return [function](const std::vector<Value>& arguments) {
+ return static_cast<Value>(function(arguments));
+ };
}
-template<typename T0, typename T1, typename T2>
-Value FunctionWrapperV(void (*function)(T0, T1, T2), const std::vector<Value>& arguments)
-{
- if (arguments.size() < 3)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
- else if (arguments.size() > 3)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
- function(static_cast<T0>(arguments[0]),
- static_cast<T1>(arguments[1]),
- static_cast<T2>(arguments[2]));
-
- return Empty;
-}
+template <std::size_t... Indices>
+struct indices {
+ using next = indices<Indices..., sizeof...(Indices)>;
+};
-template<typename T0, typename T1, typename T2>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2))
-{
- return std::bind(&FunctionWrapperV<T0, T1, T2>, function, _1);
-}
+template <std::size_t N>
+struct build_indices {
+ using type = typename build_indices<N-1>::type::next;
+};
-template<typename TR, typename T0, typename T1, typename T2>
-Value FunctionWrapperR(TR (*function)(T0, T1, T2), const std::vector<Value>& arguments)
-{
- if (arguments.size() < 3)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
- else if (arguments.size() > 3)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
+template <>
+struct build_indices<0> {
+ using type = indices<>;
+};
- return function(static_cast<T0>(arguments[0]),
- static_cast<T1>(arguments[1]),
- static_cast<T2>(arguments[2]));
-}
+template <std::size_t N>
+using BuildIndices = typename build_indices<N>::type;
-template<typename TR, typename T0, typename T1, typename T2>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2))
+struct unpack_caller
{
- return std::bind(&FunctionWrapperR<TR, T0, T1, T2>, function, _1);
-}
+private:
+ template <typename FuncType, size_t... I>
+ auto call(FuncType f, const std::vector<Value>& args, indices<I...>) -> decltype(f(args[I]...))
+ {
+ return f(args[I]...);
+ }
-template<typename T0, typename T1, typename T2, typename T3>
-Value FunctionWrapperV(void (*function)(T0, T1, T2, T3), const std::vector<Value>& arguments)
-{
- if (arguments.size() < 4)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
- else if (arguments.size() > 4)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
+public:
+ template <typename FuncType>
+ auto operator () (FuncType f, const std::vector<Value>& args)
+ -> decltype(call(f, args, BuildIndices<boost::function_traits<typename boost::remove_pointer<FuncType>::type>::arity>{}))
+ {
+ return call(f, args, BuildIndices<boost::function_traits<typename boost::remove_pointer<FuncType>::type>::arity>{});
+ }
+};
- function(static_cast<T0>(arguments[0]),
- static_cast<T1>(arguments[1]),
- static_cast<T2>(arguments[2]),
- static_cast<T3>(arguments[3]));
+enum class enabler_t {};
- return Empty;
-}
+template<bool T>
+using EnableIf = typename std::enable_if<T, enabler_t>::type;
-template<typename T0, typename T1, typename T2, typename T3>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3))
+template<typename FuncType>
+std::function<Value (const std::vector<Value>&)> WrapFunction(FuncType function,
+ EnableIf<std::is_same<decltype(unpack_caller()(FuncType(), std::vector<Value>())), void>::value>* = 0)
{
- return std::bind(&FunctionWrapperV<T0, T1, T2, T3>, function, _1);
-}
+ return [function](const std::vector<Value>& arguments) {
+ constexpr int arity = boost::function_traits<typename boost::remove_pointer<FuncType>::type>::arity;
-template<typename TR, typename T0, typename T1, typename T2, typename T3>
-Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3), const std::vector<Value>& arguments)
-{
- if (arguments.size() < 4)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
- else if (arguments.size() > 4)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
+ if (arguments.size() < arity)
+ BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
+ else if (arguments.size() > arity)
+ BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
- return function(static_cast<T0>(arguments[0]),
- static_cast<T1>(arguments[1]),
- static_cast<T2>(arguments[2]),
- static_cast<T3>(arguments[3]));
+ unpack_caller()(function, arguments);
+ return Empty;
+ };
}
-template<typename TR, typename T0, typename T1, typename T2, typename T3>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3))
+template<typename FuncType>
+std::function<Value (const std::vector<Value>&)> WrapFunction(FuncType function,
+ EnableIf<!std::is_same<decltype(unpack_caller()(FuncType(), std::vector<Value>())), void>::value>* = 0)
{
- return std::bind(&FunctionWrapperR<TR, T0, T1, T2, T3>, function, _1);
-}
+ return [function](const std::vector<Value>& arguments) {
+ constexpr int arity = boost::function_traits<typename boost::remove_pointer<FuncType>::type>::arity;
-template<typename T0, typename T1, typename T2, typename T3, typename T4>
-Value FunctionWrapperV(void (*function)(T0, T1, T2, T3, T4), const std::vector<Value>& arguments)
-{
- if (arguments.size() < 5)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
- else if (arguments.size() > 5)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
- function(static_cast<T0>(arguments[0]),
- static_cast<T1>(arguments[1]),
- static_cast<T2>(arguments[2]),
- static_cast<T3>(arguments[3]),
- static_cast<T4>(arguments[4]));
-
- return Empty;
-}
+ if (arity > 0) {
+ if (arguments.size() < arity)
+ BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
+ else if (arguments.size() > arity)
+ BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
+ }
-template<typename T0, typename T1, typename T2, typename T3, typename T4>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3, T4))
-{
- return std::bind(&FunctionWrapperV<T0, T1, T2, T3, T4>, function, _1);
-}
-
-template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4>
-Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3, T4), const std::vector<Value>& arguments)
-{
- if (arguments.size() < 5)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
- else if (arguments.size() > 5)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
- return function(static_cast<T0>(arguments[0]),
- static_cast<T1>(arguments[1]),
- static_cast<T2>(arguments[2]),
- static_cast<T3>(arguments[3]),
- static_cast<T4>(arguments[4]));
-}
-
-template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3, T4))
-{
- return std::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4>, function, _1);
-}
-
-template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
-Value FunctionWrapperV(void (*function)(T0, T1, T2, T3, T4, T5), const std::vector<Value>& arguments)
-{
- if (arguments.size() < 6)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
- else if (arguments.size() > 6)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
- function(static_cast<T0>(arguments[0]),
- static_cast<T1>(arguments[1]),
- static_cast<T2>(arguments[2]),
- static_cast<T3>(arguments[3]),
- static_cast<T4>(arguments[4]),
- static_cast<T5>(arguments[5]));
-
- return Empty;
-}
-
-template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3, T4, T5))
-{
- return std::bind(&FunctionWrapperV<T0, T1, T2, T3, T4, T5>, function, _1);
-}
-
-template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
-Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3, T4, T5), const std::vector<Value>& arguments)
-{
- if (arguments.size() < 6)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
- else if (arguments.size() > 6)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
- return function(static_cast<T0>(arguments[0]),
- static_cast<T1>(arguments[1]),
- static_cast<T2>(arguments[2]),
- static_cast<T3>(arguments[3]),
- static_cast<T4>(arguments[4]),
- static_cast<T5>(arguments[5]));
-}
-
-template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3, T4, T5))
-{
- return std::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4, T5>, function, _1);
-}
-
-template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
-Value FunctionWrapperV(void (*function)(T0, T1, T2, T3, T4, T5, T6), const std::vector<Value>& arguments)
-{
- if (arguments.size() < 7)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
- else if (arguments.size() > 7)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
- function(static_cast<T0>(arguments[0]),
- static_cast<T1>(arguments[1]),
- static_cast<T2>(arguments[2]),
- static_cast<T3>(arguments[3]),
- static_cast<T4>(arguments[4]),
- static_cast<T5>(arguments[5]),
- static_cast<T6>(arguments[6]));
-
- return Empty;
-}
-
-template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3, T4, T5, T6))
-{
- return std::bind(&FunctionWrapperV<T0, T1, T2, T3, T4, T5, T6>, function, _1);
-}
-
-template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
-Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3, T4, T5, T6), const std::vector<Value>& arguments)
-{
- if (arguments.size() < 7)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
- else if (arguments.size() > 7)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
- return function(static_cast<T0>(arguments[0]),
- static_cast<T1>(arguments[1]),
- static_cast<T2>(arguments[2]),
- static_cast<T3>(arguments[3]),
- static_cast<T4>(arguments[4]),
- static_cast<T5>(arguments[5]),
- static_cast<T6>(arguments[6]));
-}
-
-template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3, T4, T5, T6))
-{
- return std::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4, T5, T6>, function, _1);
-}
-
-template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
-Value FunctionWrapperV(void (*function)(T0, T1, T2, T3, T4, T5, T6, T7), const std::vector<Value>& arguments)
-{
- if (arguments.size() < 8)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
- else if (arguments.size() > 8)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
- function(static_cast<T0>(arguments[0]),
- static_cast<T1>(arguments[1]),
- static_cast<T2>(arguments[2]),
- static_cast<T3>(arguments[3]),
- static_cast<T4>(arguments[4]),
- static_cast<T5>(arguments[5]),
- static_cast<T6>(arguments[6]),
- static_cast<T7>(arguments[7]));
-
- return Empty;
-}
-
-template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3, T4, T5, T6, T7))
-{
- return std::bind(&FunctionWrapperV<T0, T1, T2, T3, T4, T5, T6, T7>, function, _1);
-}
-
-template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
-Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3, T4, T5, T6, T7), const std::vector<Value>& arguments)
-{
- if (arguments.size() < 8)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
- else if (arguments.size() > 8)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
-
- return function(static_cast<T0>(arguments[0]),
- static_cast<T1>(arguments[1]),
- static_cast<T2>(arguments[2]),
- static_cast<T3>(arguments[3]),
- static_cast<T4>(arguments[4]),
- static_cast<T5>(arguments[5]),
- static_cast<T6>(arguments[6]),
- static_cast<T7>(arguments[7]));
-}
-
-template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
-std::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3, T4, T5, T6, T7))
-{
- return std::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4, T5, T6, T7>, function, _1);
-}
-
-template<typename TR>
-std::function<TR (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(const std::vector<Value>&))
-{
- return std::bind<TR>(function, _1);
-}
-
-inline std::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(const std::vector<Value>&))
-{
- return std::bind(&FunctionWrapperVA, function, _1);
+ return unpack_caller()(function, arguments);
+ };
}
}
-#endif /* SCRIPTFUNCTION_H */
+#endif /* FUNCTIONWRAPPER_H */
Dictionary::Ptr jsonObj = new Dictionary();
/* Methods */
- jsonObj->Set("encode", new Function("Json#encode", WrapFunction(JsonEncodeShim), { "value" }, true));
- jsonObj->Set("decode", new Function("Json#decode", WrapFunction(JsonDecode), { "value" }, true));
+ jsonObj->Set("encode", new Function("Json#encode", JsonEncodeShim, { "value" }, true));
+ jsonObj->Set("decode", new Function("Json#decode", JsonDecode, { "value" }, true));
ScriptGlobal::Set("Json", jsonObj);
});
mathObj->Set("SQRT2", 1.41421356237309504880);
/* Methods */
- mathObj->Set("abs", new Function("Math#abs", WrapFunction(MathAbs), { "x" }, true));
- mathObj->Set("acos", new Function("Math#acos", WrapFunction(MathAcos), { "x" }, true));
- mathObj->Set("asin", new Function("Math#asin", WrapFunction(MathAsin), { "x" }, true));
- mathObj->Set("atan", new Function("Math#atan", WrapFunction(MathAtan), { "x" }, true));
- mathObj->Set("atan2", new Function("Math#atan2", WrapFunction(MathAtan2), { "x", "y" }, true));
- mathObj->Set("ceil", new Function("Math#ceil", WrapFunction(MathCeil), { "x" }, true));
- mathObj->Set("cos", new Function("Math#cos", WrapFunction(MathCos), { "x" }, true));
- mathObj->Set("exp", new Function("Math#exp", WrapFunction(MathExp), { "x" }, true));
- mathObj->Set("floor", new Function("Math#floor", WrapFunction(MathFloor), { "x" }, true));
- mathObj->Set("log", new Function("Math#log", WrapFunction(MathLog), { "x" }, true));
- mathObj->Set("max", new Function("Math#max", WrapFunction(MathMax), {}, true));
- mathObj->Set("min", new Function("Math#min", WrapFunction(MathMin), {}, true));
- mathObj->Set("pow", new Function("Math#pow", WrapFunction(MathPow), { "x", "y" }, true));
- mathObj->Set("random", new Function("Math#random", WrapFunction(MathRandom), {}, true));
- mathObj->Set("round", new Function("Math#round", WrapFunction(MathRound), { "x" }, true));
- mathObj->Set("sin", new Function("Math#sin", WrapFunction(MathSin), { "x" }, true));
- mathObj->Set("sqrt", new Function("Math#sqrt", WrapFunction(MathSqrt), { "x" }, true));
- mathObj->Set("tan", new Function("Math#tan", WrapFunction(MathTan), { "x" }, true));
- mathObj->Set("isnan", new Function("Math#isnan", WrapFunction(MathIsnan), { "x" }, true));
- mathObj->Set("isinf", new Function("Math#isinf", WrapFunction(MathIsinf), { "x" }, true));
- mathObj->Set("sign", new Function("Math#sign", WrapFunction(MathSign), { "x" }, true));
+ mathObj->Set("abs", new Function("Math#abs", MathAbs, { "x" }, true));
+ mathObj->Set("acos", new Function("Math#acos", MathAcos, { "x" }, true));
+ mathObj->Set("asin", new Function("Math#asin", MathAsin, { "x" }, true));
+ mathObj->Set("atan", new Function("Math#atan", MathAtan, { "x" }, true));
+ mathObj->Set("atan2", new Function("Math#atan2", MathAtan2, { "x", "y" }, true));
+ mathObj->Set("ceil", new Function("Math#ceil", MathCeil, { "x" }, true));
+ mathObj->Set("cos", new Function("Math#cos", MathCos, { "x" }, true));
+ mathObj->Set("exp", new Function("Math#exp", MathExp, { "x" }, true));
+ mathObj->Set("floor", new Function("Math#floor", MathFloor, { "x" }, true));
+ mathObj->Set("log", new Function("Math#log", MathLog, { "x" }, true));
+ mathObj->Set("max", new Function("Math#max", MathMax, {}, true));
+ mathObj->Set("min", new Function("Math#min", MathMin, {}, true));
+ mathObj->Set("pow", new Function("Math#pow", MathPow, { "x", "y" }, true));
+ mathObj->Set("random", new Function("Math#random", MathRandom, {}, true));
+ mathObj->Set("round", new Function("Math#round", MathRound, { "x" }, true));
+ mathObj->Set("sin", new Function("Math#sin", MathSin, { "x" }, true));
+ mathObj->Set("sqrt", new Function("Math#sqrt", MathSqrt, { "x" }, true));
+ mathObj->Set("tan", new Function("Math#tan", MathTan, { "x" }, true));
+ mathObj->Set("isnan", new Function("Math#isnan", MathIsnan, { "x" }, true));
+ mathObj->Set("isinf", new Function("Math#isinf", MathIsinf, { "x" }, true));
+ mathObj->Set("sign", new Function("Math#sign", MathSign, { "x" }, true));
ScriptGlobal::Set("Math", mathObj);
});
if (!prototype) {
prototype = new Dictionary();
- prototype->Set("to_string", new Function("Number#to_string", WrapFunction(NumberToString), {}, true));
+ prototype->Set("to_string", new Function("Number#to_string", NumberToString, {}, true));
}
return prototype;
if (!prototype) {
prototype = new Dictionary();
- prototype->Set("to_string", new Function("Object#to_string", WrapFunction(ObjectToString), {}, true));
- prototype->Set("notify_attribute", new Function("Object#notify_attribute", WrapFunction(ObjectNotifyAttribute), { "attribute" }, false));
- prototype->Set("clone", new Function("Object#clone", WrapFunction(ObjectClone), {}, true));
+ prototype->Set("to_string", new Function("Object#to_string", ObjectToString, {}, true));
+ prototype->Set("notify_attribute", new Function("Object#notify_attribute", ObjectNotifyAttribute, { "attribute" }, false));
+ prototype->Set("clone", new Function("Object#clone", ObjectClone, {}, true));
}
return prototype;
if (!prototype) {
prototype = new Dictionary();
- prototype->Set("len", new Function("String#len", WrapFunction(StringLen), {}, true));
- prototype->Set("to_string", new Function("String#to_string", WrapFunction(StringToString), {}, true));
- prototype->Set("substr", new Function("String#substr", WrapFunction(StringSubstr), { "start", "len" }, true));
- prototype->Set("upper", new Function("String#upper", WrapFunction(StringUpper), {}, true));
- prototype->Set("lower", new Function("String#lower", WrapFunction(StringLower), {}, true));
- prototype->Set("split", new Function("String#split", WrapFunction(StringSplit), { "delims" }, true));
- prototype->Set("find", new Function("String#find", WrapFunction(StringFind), { "str", "start" }, true));
- prototype->Set("contains", new Function("String#contains", WrapFunction(StringContains), { "str" }, true));
- prototype->Set("replace", new Function("String#replace", WrapFunction(StringReplace), { "search", "replacement" }, true));
- prototype->Set("reverse", new Function("String#reverse", WrapFunction(StringReverse), {}, true));
- prototype->Set("trim", new Function("String#trim", WrapFunction(StringTrim), {}, true));
+ prototype->Set("len", new Function("String#len", StringLen, {}, true));
+ prototype->Set("to_string", new Function("String#to_string", StringToString, {}, true));
+ prototype->Set("substr", new Function("String#substr", StringSubstr, { "start", "len" }, true));
+ prototype->Set("upper", new Function("String#upper", StringUpper, {}, true));
+ prototype->Set("lower", new Function("String#lower", StringLower, {}, true));
+ prototype->Set("split", new Function("String#split", StringSplit, { "delims" }, true));
+ prototype->Set("find", new Function("String#find", StringFind, { "str", "start" }, true));
+ prototype->Set("contains", new Function("String#contains", StringContains, { "str" }, true));
+ prototype->Set("replace", new Function("String#replace", StringReplace, { "search", "replacement" }, true));
+ prototype->Set("reverse", new Function("String#reverse", StringReverse, {}, true));
+ prototype->Set("trim", new Function("String#trim", StringTrim, {}, true));
}
return prototype;
if (!prototype) {
prototype = new Dictionary();
- prototype->Set("register_attribute_handler", new Function("Type#register_attribute_handler", WrapFunction(TypeRegisterAttributeHandler), { "field", "callback" }, false));
+ prototype->Set("register_attribute_handler", new Function("Type#register_attribute_handler", TypeRegisterAttributeHandler, { "field", "callback" }, false));
}
return prototype;
}
- static inline Value NewFunction(ScriptFrame& frame, const String& name, const std::vector<String>& args,
+ static inline Value NewFunction(ScriptFrame& frame, const String& name, const std::vector<String>& argNames,
std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression)
{
- return new Function(name, std::bind(&FunctionWrapper, _1, args,
- EvaluateClosedVars(frame, closedVars), expression), args);
+ auto evaluatedClosedVars = EvaluateClosedVars(frame, closedVars);
+
+ auto wrapper = [argNames, evaluatedClosedVars, expression](const std::vector<Value>& arguments) {
+ if (arguments.size() < argNames.size())
+ BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
+
+ ScriptFrame *frame = ScriptFrame::GetCurrentFrame();
+
+ if (evaluatedClosedVars)
+ evaluatedClosedVars->CopyTo(frame->Locals);
+
+ for (std::vector<Value>::size_type i = 0; i < std::min(arguments.size(), argNames.size()); i++)
+ frame->Locals->Set(argNames[i], arguments[i]);
+
+ return expression->Evaluate(*frame);
+ };
+
+ return new Function(name, wrapper, argNames);
}
static inline Value NewApply(ScriptFrame& frame, const String& type, const String& target, const String& name, const boost::shared_ptr<Expression>& filter,
}
private:
- static inline Value FunctionWrapper(const std::vector<Value>& arguments,
- const std::vector<String>& funcargs, const Dictionary::Ptr& closedVars, const boost::shared_ptr<Expression>& expr)
- {
- if (arguments.size() < funcargs.size())
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
-
- ScriptFrame *frame = ScriptFrame::GetCurrentFrame();
-
- if (closedVars)
- closedVars->CopyTo(frame->Locals);
-
- for (std::vector<Value>::size_type i = 0; i < std::min(arguments.size(), funcargs.size()); i++)
- frame->Locals->Set(funcargs[i], arguments[i]);
-
- return expr->Evaluate(*frame);
- }
-
static inline Dictionary::Ptr EvaluateClosedVars(ScriptFrame& frame, std::map<String, Expression *> *closedVars)
{
Dictionary::Ptr locals;
if (!prototype) {
prototype = new Dictionary();
- prototype->Set("process_check_result", new Function("Checkable#process_check_result", WrapFunction(CheckableProcessCheckResult), { "cr" }, false));
+ prototype->Set("process_check_result", new Function("Checkable#process_check_result", CheckableProcessCheckResult, { "cr" }, false));
}
return prototype;
{
BOOST_TEST_MESSAGE("Preparing config objects...");
- ConfigItem::RunWithActivationContext(new Function("CreateTestObjects", WrapFunction(CreateTestObjects)));
+ ConfigItem::RunWithActivationContext(new Function("CreateTestObjects", CreateTestObjects));
}
static void CreateTestObjects(void)