/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /* $Rev$ $Date$ */ #ifndef tuscany_function_hpp #define tuscany_function_hpp /** * Lambda function type that enforces pass by ref and constant parameters, * allocates memory only as needed and using an APR memory pool, is about * 2 times faster than std::function and between 1/4 and 1/6 of its size. */ #include #include "fstream.hpp" #include "gc.hpp" #include "config.hpp" namespace tuscany { #ifdef WANT_MAINTAINER_COUNTERS /** * Debug counters. */ long int countProxies; long int countFProxies = 0; long int countCProxies = 0; long int countLambdas = 0; long int countELambdas = 0; long int countCLambdas = 0; long int countFLambdas = 0; inline const bool resetLambdaCounters() { countLambdas = countELambdas = countCLambdas = countFLambdas = countProxies = countFProxies = countCProxies = 0; return true; } inline const bool checkLambdaCounters() { return countLambdas == 0; } inline const bool printLambdaCounters() { cout << "countLambdas " << countLambdas << endl; cout << "countELambdas " << countELambdas << endl; cout << "countFLambdas " << countFLambdas << endl; cout << "countCLambdas " << countCLambdas << endl; cout << "countProxies " << countProxies << endl; cout << "countFProxies " << countFProxies << endl; cout << "countCProxies " << countCProxies << endl; return true; } #else #define resetLambdaCounters() #define checkLambdaCounters() true #define printLambdaCounters() #endif /** * Lambda function type. */ template class Callable { public: inline Callable() noexcept { } virtual const R operator()(const P&&... p) const noexcept = 0; inline virtual ~Callable() noexcept { } template class Proxy: public Callable { public: inline Proxy(const F& f) noexcept : function(f) { debug_inc(countProxies); debug_inc(countFProxies); } inline Proxy(const Proxy& p) noexcept : function(p.function) { debug_inc(countProxies); debug_inc(countCProxies); } inline ~Proxy() noexcept { debug_dec(countProxies); } virtual const R operator() (const P&&... p) const noexcept { return function(std::forward(p)...); } private: const F function; }; }; template class lambda; template class lambda { public: inline lambda() noexcept : callable(NULL) { debug_inc(countLambdas); debug_inc(countELambdas); } template inline lambda(const F f) noexcept : callable(mkproxy(f)) { debug_inc(countLambdas); debug_inc(countFLambdas); } template inline lambda(const gc_mutable_ref& r) noexcept : callable(mkproxy(*(F*)r)) { debug_inc(countLambdas); debug_inc(countFLambdas); } inline lambda(const lambda& l) noexcept : callable(l.callable) { debug_inc(countLambdas); debug_inc(countCLambdas); } lambda& operator=(const lambda& l) = delete; inline ~lambda() noexcept { debug_dec(countLambdas); } inline const bool operator==(const lambda& l) const noexcept { if (this == &l) return true; return false; } inline const bool operator!=(const lambda& l) const noexcept { return !this->operator==(l); } inline const R operator()(const P&... p) const noexcept { return (*callable)(std::forward(p)...); } template friend ostream& operator<<(ostream&, const lambda&); template friend const bool isNil(const lambda& l) noexcept; private: typedef Callable CallableType; const gc_ptr callable; template inline gc_ptr mkproxy(const F& f) noexcept { typedef typename CallableType::template Proxy ProxyType; return gc_ptr(new (gc_new()) ProxyType(f)); } }; template inline ostream& operator<<(ostream& out, const lambda& l) { return out << "lambda::" << l.callable; } /** * Return true if a lambda is nil. */ template inline const bool isNil(const lambda& l) noexcept { return (const void*)l.callable == NULL; } /** * Curry a lambda function. */ template class curried { public: inline curried(const lambda& f, const T& v) noexcept: v(v), f(f) { } inline const R operator()(const P&... p) const noexcept { return f(v, std::forward(p)...); } private: const T v; const lambdaf; }; template inline const lambda curry(const lambda& f, const T& t) noexcept { return curried(f, t); } template inline const lambda curry(const lambda& f, const T& t, const U& u) noexcept { return curry(curry(f, t), u); } template inline const lambda curry(const lambda& f, const T& t, const U& u, const V& v) noexcept { return curry(curry(curry(f, t), u), v); } /** * A lambda function that returns the given value. */ template inline const lambda result(const T& v) { return [v]()->T { return v; }; } /** * Commonly used lambda types. */ typedef lambda blambda; } #endif /* tuscany_function_hpp */