summaryrefslogtreecommitdiffstats
path: root/cpp/sca/runtime/core/src/tuscany/function.hpp
diff options
context:
space:
mode:
authorjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2009-09-14 05:55:14 +0000
committerjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2009-09-14 05:55:14 +0000
commit67618c80483e01793351b20cbbbc5a56a6a8f016 (patch)
treeb86a84ec91e778fab13285ca178f0a477d140d11 /cpp/sca/runtime/core/src/tuscany/function.hpp
parent3703aa37cef6eb16f145e7417d8e763e69f6a290 (diff)
Utility functions and headers used by the sample store app, function objects, lists and strawman pointer utils. Started to implement a generic value holder object that can help exchange data with scripting components.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@814481 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/sca/runtime/core/src/tuscany/function.hpp')
-rw-r--r--cpp/sca/runtime/core/src/tuscany/function.hpp235
1 files changed, 235 insertions, 0 deletions
diff --git a/cpp/sca/runtime/core/src/tuscany/function.hpp b/cpp/sca/runtime/core/src/tuscany/function.hpp
new file mode 100644
index 0000000000..e0e8889dad
--- /dev/null
+++ b/cpp/sca/runtime/core/src/tuscany/function.hpp
@@ -0,0 +1,235 @@
+/*
+ * 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, used to represent service operations.
+ */
+
+#include <iostream>
+#include "gc.hpp"
+
+using std::ostream;
+
+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() {
+ using std::cout;
+ using std::endl;
+ 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;
+}
+
+/**
+ * Lambda function type.
+ */
+
+template<typename R, typename... P> 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 ++refCount;
+ }
+
+ unsigned int release() {
+ return --refCount;
+ }
+
+ template<typename F> class Proxy: public Callable {
+ public:
+ explicit 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>(p)...);
+ }
+
+ virtual const int size() const {
+ return sizeof(function);
+ }
+
+ private:
+ const F function;
+ };
+
+};
+
+template<typename S> class lambda;
+
+template<typename R, typename... P> class lambda<R(P...)> {
+public:
+ lambda() : callable(0) {
+ countLambdas++;
+ countELambdas++;
+ }
+
+ template<typename F> explicit lambda(const F f) : callable(0) {
+ typedef typename CallableType::template Proxy<F> ProxyType;
+
+ countLambdas++;
+ countFLambdas++;
+ callable = gc_counting_ptr<CallableType>(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>(p)...);
+ }
+
+ template<typename S> friend ostream& operator<<(ostream&, const lambda<S>&);
+
+private:
+ typedef Callable<R,P...> CallableType;
+ gc_counting_ptr<CallableType> callable;
+};
+
+template<typename S> ostream& operator<<(ostream& out, const lambda<S>& l) {
+ return out << "lambda::" << l.callable;
+}
+
+/**
+ * Creates a lambda function from a pointer to a function.
+ */
+template<typename R, typename... P> lambda<R(P...)> makeLambda(const R (* const f)(P...)) {
+ return lambda<R(P...)>(f);
+}
+
+/**
+ * Curry a lambda function.
+ */
+template<typename R, typename T, typename... P> class Curried {
+private:
+ const T v;
+ const lambda<R(T, P...)>f;
+
+public:
+ Curried(const lambda<R(T, P...)>& f, const T& v): v(v), f(f) {
+ }
+
+ const R operator()(P... p) const {
+ return f(v, std::forward<P>(p)...);
+ }
+
+};
+
+template<typename R, typename T, typename... P> const lambda<R(P...)> curry(const lambda<R(T, P...)>& f, const T& t) {
+ return (lambda<R(P...)>)Curried<R, T, P...>(f, t);
+}
+
+template<typename R, typename T, typename U, typename... P> const lambda<R(P...)> curry(const lambda<R(T, U, P...)>& f, const T& t, const U& u) {
+ return curry(curry(f, t), u);
+}
+
+template<typename R, typename T, typename U, typename V, typename... P> const lambda<R(P...)> curry(const lambda<R(T, U, P...)>& 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<typename T> struct unitReturn {
+ const T v;
+ unitReturn(const T& v) :
+ v(v) {
+ }
+ const T operator()() const {
+ return v;
+ }
+};
+
+template<typename T> const lambda<T()> unit(const T& v) {
+ return lambda<T()> (unitReturn<T> (v));
+}
+
+}
+#endif /* tuscany_function_hpp */