/* * 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. */ #include #include "gc.hpp" namespace tuscany { /** * 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; bool resetLambdaCounters() { countLambdas = countELambdas = countCLambdas = countFLambdas = countProxies = countFProxies = countCProxies = 0; return true; } bool printLambdaCounters() { std::cout << "countLambdas " << countLambdas << std::endl; std::cout << "countELambdas " << countELambdas << std::endl; std::cout << "countFLambdas " << countFLambdas << std::endl; std::cout << "countCLambdas " << countCLambdas << std::endl; std::cout << "countProxies " << countProxies << std::endl; std::cout << "countFProxies " << countFProxies << std::endl; std::cout << "countCProxies " << countCProxies << std::endl; return true; } /** * Lambda function type. */ template class Callable { public: unsigned int refCount; Callable() : refCount(0) { } virtual const int size() const = 0; virtual const R operator()(P... p) const = 0; virtual ~Callable() { } unsigned int acquire() { return __sync_add_and_fetch(&refCount, 1); } unsigned int release() { return __sync_sub_and_fetch(&refCount, 1); } template class Proxy: public Callable { public: Proxy(const F& f) : function(f) { countProxies++; countFProxies ++; } Proxy(const Proxy& p) : function(p.function) { countProxies++; countCProxies ++; } ~Proxy() { countProxies--; } virtual const R operator() (P... p) const { return function(std::forward

(p)...); } virtual const int size() const { return sizeof(function); } private: const F function; }; }; template class lambda; template class lambda { public: lambda() : callable(0) { countLambdas++; countELambdas++; } template lambda(const F f) : callable(0) { typedef typename CallableType::template Proxy ProxyType; countLambdas++; countFLambdas++; callable = gc_counting_ptr(new ProxyType(f)); } lambda(const lambda& l) { countLambdas++; countCLambdas++; callable = l.callable; } const lambda& operator=(const lambda& l) { if (this == &l) return *this; callable = l.callable; return *this; } ~lambda() { countLambdas--; } const bool operator==(const lambda& l) const { if (this == &l) return true; return callable == l.callable; } const bool operator!=(const lambda& l) const { return !this->operator==(l); } const R operator()(P... p) const { return (*callable)(std::forward

(p)...); } template friend std::ostream& operator<<(std::ostream&, const lambda&); private: typedef Callable CallableType; gc_counting_ptr callable; }; template std::ostream& operator<<(std::ostream& out, const lambda& l) { return out << "lambda::" << l.callable; } /** * Creates a lambda function from a pointer to a function. */ template lambda makeLambda(const R (* const f)(P...)) { return lambda(f); } /** * Curry a lambda function. */ template class curried { public: curried(const lambda& f, const T& v): v(v), f(f) { } const R operator()(P... p) const { return f(v, std::forward

(p)...); } private: const T v; const lambdaf; }; template const lambda curry(const lambda& f, const T& t) { return (lambda)curried(f, t); } template const lambda curry(const lambda& f, const T& t, const U& u) { return curry(curry(f, t), u); } template const lambda curry(const lambda& f, const T& t, const U& u, const V& v) { return curry(curry(curry(f, t), u), v); } /** * A lambda function that returns the given value. */ template class unitReturn { public: unitReturn(const T& v) : v(v) { } const T operator()() const { return v; } private: const T v; }; template const lambda unit(const T& v) { return lambda (unitReturn (v)); } } #endif /* tuscany_function_hpp */