diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2009-09-27 20:04:28 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2009-09-27 20:04:28 +0000 |
commit | 215c005bde88752999978adce56b09d2a90a13b7 (patch) | |
tree | d4371936d0c26e24b610cf5f17145a7425ca37d7 /cpp/sca/runtime/core/src/tuscany | |
parent | 49ba52239afa0e6ab295add695a7443633748cfc (diff) |
Moved some sources up in the directory tree to attempt to simplify the directory structure a bit, and some minor refactoring.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@819394 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/sca/runtime/core/src/tuscany')
-rw-r--r-- | cpp/sca/runtime/core/src/tuscany/function.hpp | 232 | ||||
-rw-r--r-- | cpp/sca/runtime/core/src/tuscany/gc.hpp | 269 | ||||
-rw-r--r-- | cpp/sca/runtime/core/src/tuscany/list.hpp | 379 | ||||
-rw-r--r-- | cpp/sca/runtime/core/src/tuscany/parallel.hpp | 283 | ||||
-rw-r--r-- | cpp/sca/runtime/core/src/tuscany/value.hpp | 357 | ||||
-rw-r--r-- | cpp/sca/runtime/core/src/tuscany/xml.hpp | 385 |
6 files changed, 0 insertions, 1905 deletions
diff --git a/cpp/sca/runtime/core/src/tuscany/function.hpp b/cpp/sca/runtime/core/src/tuscany/function.hpp deleted file mode 100644 index caba5211b9..0000000000 --- a/cpp/sca/runtime/core/src/tuscany/function.hpp +++ /dev/null @@ -1,232 +0,0 @@ -/* - * 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 <iostream> -#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<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 __sync_add_and_fetch(&refCount, 1); - } - - unsigned int release() { - return __sync_sub_and_fetch(&refCount, 1); - } - - template<typename F> class Proxy: public Callable { - public: - explicit Proxy(const F& f) : function(f) { - countProxies++; - countFProxies ++; - } - - explicit 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 std::ostream& operator<<(std::ostream&, const lambda<S>&); - -private: - typedef Callable<R,P...> CallableType; - gc_counting_ptr<CallableType> callable; -}; - -template<typename S> std::ostream& operator<<(std::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 { -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)...); - } - -private: - const T v; - const lambda<R(T, P...)>f; -}; - -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> class unitReturn { -public: - explicit unitReturn(const T& v) : - v(v) { - } - const T operator()() const { - return v; - } -private: - const T v; -}; - -template<typename T> const lambda<T()> unit(const T& v) { - return lambda<T()> (unitReturn<T> (v)); -} - -} -#endif /* tuscany_function_hpp */ diff --git a/cpp/sca/runtime/core/src/tuscany/gc.hpp b/cpp/sca/runtime/core/src/tuscany/gc.hpp deleted file mode 100644 index b0ed42a474..0000000000 --- a/cpp/sca/runtime/core/src/tuscany/gc.hpp +++ /dev/null @@ -1,269 +0,0 @@ -/* - * 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_gc_hpp -#define tuscany_gc_hpp - -/** - * Garbage collected pointer. - */ - -#include <iostream> - -namespace tuscany -{ - -template<typename T> class gc_ptr { -public: - explicit gc_ptr(T* p = 0) throw() : countingRef(p == 0? 0 : new CountingRef(p)) { - } - - ~gc_ptr() throw() { - release(); - } - - gc_ptr(const gc_ptr& r) throw() : countingRef(r.countingRef) { - acquire(r.countingRef); - } - - gc_ptr& operator=(const gc_ptr& r) throw() { - if(this == &r) - return *this; - acquire(r.countingRef); - release(); - countingRef = r.countingRef; - return *this; - } - - const bool operator==(const gc_ptr& r) const throw() { - if (this == &r) - return true; - if (countingRef == NULL) - return r.countingRef == NULL; - if (r.countingRef == NULL) - return false; - return countingRef-> ptr == r.countingRef->ptr; - } - - const bool operator!=(const gc_ptr& r) const throw() { - return !this->operator ==(r); - } - - T& operator*() const throw() { - return *countingRef->ptr; - } - - T* operator->() const throw() { - return countingRef->ptr; - } - - operator T*() const throw() { - return countingRef->ptr; - } - - template<typename X> friend std::ostream& operator<<(std::ostream&, const gc_ptr<X>&); - -private: - struct CountingRef { - T* ptr; - unsigned count; - - CountingRef(T* p) throw() : - ptr(p), count(1) { - } - }* countingRef; - - void acquire(CountingRef* ref) throw() { - if(ref) - __sync_add_and_fetch(&ref->count, 1); - } - - void release() throw() { - if(countingRef) { - unsigned rc = __sync_sub_and_fetch(&countingRef->count, 1); - if(rc == 0) { - delete countingRef->ptr; - delete countingRef; - } - } - } -}; - -template<typename T> std::ostream& operator<<(std::ostream& out, const gc_ptr<T>& p) { - return out << p.countingRef->ptr; -} - -/** - * Garbage collected pointer to an array. - */ -template<typename T> class gc_aptr { -public: - explicit gc_aptr(T* p = 0) throw() : countingRef(p == 0? 0 : new CountingRef(p)) { - } - - ~gc_aptr() throw() { - release(); - } - - gc_aptr(const gc_aptr& r) throw() : countingRef(r.countingRef) { - acquire(r.countingRef); - } - - gc_aptr& operator=(const gc_aptr& r) throw() { - if(this == &r) - return *this; - acquire(r.countingRef); - release(); - countingRef = r.countingRef; - return *this; - } - - const bool operator==(const gc_aptr& r) const throw() { - if (this == &r) - return true; - if (countingRef == NULL) - return r.countingRef == NULL; - if (r.countingRef == NULL) - return false; - return countingRef-> ptr == r.countingRef->ptr; - } - - const bool operator!=(const gc_aptr& r) const throw() { - return !this->operator ==(r); - } - - T& operator*() const throw() { - return *countingRef->ptr; - } - - T* operator->() const throw() { - return countingRef->ptr; - } - - operator T*() const throw() { - return countingRef->ptr; - } - - template<typename X> friend std::ostream& operator<<(std::ostream&, const gc_aptr<X>&); - -private: - struct CountingRef { - T* ptr; - unsigned count; - - CountingRef(T* p) throw() : - ptr(p), count(1) { - } - }* countingRef; - - void acquire(CountingRef* ref) throw() { - if(ref) - __sync_add_and_fetch(&ref->count, 1); - } - - void release() throw() { - if(countingRef) { - unsigned rc = __sync_sub_and_fetch(&countingRef->count, 1); - if(rc == 0) { - delete[] countingRef->ptr; - delete countingRef; - } - } - } -}; - -template<typename T> std::ostream& operator<<(std::ostream& out, const gc_aptr<T>& p) { - return out << p.countingRef->ptr; -} - -/** - * Garbage collected pointer to a reference counting object. - */ -template<typename T> class gc_counting_ptr { -public: - explicit gc_counting_ptr(T* p = 0) throw() : ptr(p) { - acquire(p); - } - - ~gc_counting_ptr() throw() { - release(); - } - - gc_counting_ptr(const gc_counting_ptr& r) throw() : ptr(r.ptr) { - acquire(ptr); - } - - gc_counting_ptr& operator=(const gc_counting_ptr& r) throw() { - if(this == &r) - return *this; - acquire(r.ptr); - release(); - ptr = r.ptr; - return *this; - } - - const bool operator==(const gc_counting_ptr& r) const throw() { - if (this == &r) - return true; - return ptr == r.ptr; - } - - const bool operator!=(const gc_counting_ptr& r) const throw() { - return !this->operator ==(r); - } - - T& operator*() const throw() { - return *ptr; - } - - T* operator->() const throw() { - return ptr; - } - - operator T*() const throw() { - return ptr; - } - - template<typename X> friend std::ostream& operator<<(std::ostream&, const gc_counting_ptr<X>&); - -private: - T* ptr; - - void acquire(T* p) throw() { - if(p) - p->acquire(); - } - - void release() throw() { - if(ptr) { - if(ptr->release() == 0) { - delete ptr; - } - } - } -}; - -template<typename T> std::ostream& operator<<(std::ostream& out, const gc_counting_ptr<T>& p) { - return out << p.ptr; -} - -} -#endif /* tuscany_gc_hpp */ diff --git a/cpp/sca/runtime/core/src/tuscany/list.hpp b/cpp/sca/runtime/core/src/tuscany/list.hpp deleted file mode 100644 index ef493b19ca..0000000000 --- a/cpp/sca/runtime/core/src/tuscany/list.hpp +++ /dev/null @@ -1,379 +0,0 @@ -/* - * 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_list_hpp -#define tuscany_list_hpp - -/** - * Simple list functions. - */ - -#include <iostream> -#include "function.hpp" - -namespace tuscany { - -long countlists = 0; -long countIlists = 0; -long countClists = 0; -long countElists = 0; - -bool resetlistCounters() { - countlists = countIlists = countClists = countElists = 0; - return true; -} - -bool printlistCounters() { - std::cout << "countlists " << countlists << std::endl; - std::cout << "countElists " << countElists << std::endl; - std::cout << "countIlists " << countIlists << std::endl; - std::cout << "countClists " << countClists << std::endl; - return true; -} - -/** - * A car/cdr lisp-like pair, base structure to construct lists. - */ - -template<typename T> class list { -public: - - list(const T car, const lambda<list<T> ()>& cdr) : - nil(false), car(car), cdr(cdr) { - countlists++; - countIlists++; - } - - list() : - nil(true) { - countlists++; - countElists++; - } - - list(const list& p) : - nil(p.nil), car(p.car), cdr(p.cdr) { - countlists++; - countClists++; - } - - const list& operator=(const list<T>& p) { - if(this == &p) - return *this; - nil = p.nil; - car = p.car; - cdr = p.cdr; - return *this; - } - - ~list() { - countlists--; - } - - const bool operator==(const list<T>& p) const { - if(this == &p) - return true; - if(nil) - return p.nil; - if(p.nil) - return false; - if(!(car == p.car)) - return false; - if(cdr == p.cdr) - return true; - return cdr() == p.cdr(); - } - - const bool operator!=(const list<T>& p) const { - return !this->operator==(p); - } - - template<typename X> friend std::ostream& operator<<(std::ostream&, const list<X>&); - - bool nil; - T car; - lambda<list<T> ()> cdr; -}; - -/** - * Returns true if the given list is nil. - */ -template<typename T> const bool isNil(const list<T>& p) { - return p.nil; -} - -/** - * Write a list to an output stream. - */ -template<typename X> std::ostream& operator<<(std::ostream& out, const list<X>& l) { - if(l == list<X> ()) - return out << "()"; - return out << "(" << car(l) << ", " << cdr(l) << ")"; -} - -/** - * Construct a (lazy) list from a value and a lambda function that returns the cdr. - */ -template<typename T> const list<T> cons(const T& car, const lambda<list<T> ()>& cdr) { - return list<T> (car, cdr); -} - -/** - * Construct a list from a value and a cdr list. - */ -template<typename T> const list<T> cons(const T& car, const list<T>& cdr) { - return list<T> (car, unit(cdr)); -} - -/** - * Construct a list of one value. - */ -template<typename T> const list<T> makeList(const T& car) { - return list<T> (car, unit(list<T> ())); -} - -/** - * Construct a list of two values. - */ -template<typename T> const list<T> makeList(const T& a, const T& b) { - return cons(a, makeList(b)); -} - -/** - * Construct a list of three values. - */ -template<typename T> const list<T> makeList(const T& a, const T& b, const T& c) { - return cons(a, cons(b, makeList(c))); -} - -/** - * Construct a list of four values. - */ -template<typename T> const list<T> makeList(const T& a, const T& b, const T& c, const T& d) { - return cons(a, cons(b, cons(c, makeList(d)))); -} - -/** - * Returns the car of a list. - */ -template<typename T> const T car(const list<T>& p) { - return p.car; -} - -/** - * Returns the cdr of a list. - */ -template<typename T> list<T> const cdr(const list<T>& p) { - return p.cdr(); -} - -/** - * Returns the car of the cdr of a list. - */ -template<typename T> const T cadr(const list<T>& p) { - return car(cdr(p)); -} - -/** - * Returns the car of the cdr of the cdr of a list. - */ -template<typename T> const T caddr(const list<T>& p) { - return car(cdr(cdr(p))); -} - -/** - * Returns the cdr of a cdr of a list. - */ -template<typename T> const list<T> cddr(const list<T>& p) { - return cdr(cdr(p)); -} - -/** - * Returns the cdr of a cdr of the cdr of a list. - */ -template<typename T> const list<T> cdddr(const list<T>& p) { - return cdr(cdr(cdr(p))); -} - -/** - * Returns the length of a list. - */ -template<typename T> struct lengthRef { - const int operator()(const int c, const list<T>& p) { - if(isNil(p)) - return c; - return (*this)(c + 1, cdr(p)); - } -}; - -template<typename T> const int length(const list<T>& p) { - return lengthRef<T> ()(0, p); -} - -/** - * Appends a list and a lambda function returning a list. - */ -template<typename T> struct appendCdr { - const list<T> a; - const lambda<list<T> ()> fb; - appendCdr(const list<T>& a, const lambda<list<T> ()>& fb) : - a(a), fb(fb) { - } - const list<T> operator()() const { - return append(a, fb); - } -}; - -template<typename T> const list<T> append(const list<T>&a, const lambda<list<T> ()>& fb) { - if(isNil(a)) - return fb(); - - return cons(car(a), lambda<list<T> ()> (appendCdr<T> (cdr(a), fb))); -} - -/** - * Appends two lists. - */ -template<typename T> const list<T> append(const list<T>&a, const list<T>& b) { - return append(a, unit(b)); -} - -/** - * Map a lambda function on a list. - */ -template<typename T, typename R> const list<R> map(const lambda<R(T)>& f, const list<T>& p) { - if(isNil(p)) - return list<R> (); - return cons(f(car(p)), map(f, cdr(p))); -} - -/** - * Run a reduce lambda function on a list. - */ -template<typename T, typename R> struct reduceAccumulate { - const lambda<R(R, T)> f; - explicit reduceAccumulate(const lambda<R(R, T)>& f) : - f(f) { - } - R operator()(const R& acc, const list<T>& p) const { - if(isNil(p)) - return acc; - return (*this)(f(acc, car(p)), cdr(p)); - } -}; - -template<typename T, typename R> const R reduce(const lambda<R(R, T)>& f, const R& initial, const list<T>& p) { - return reduceAccumulate<T, R> (f)(initial, p); -} - -/** - * Run a filter lambda function on a list. - */ -template<typename T> const list<T> filter(const lambda<bool(T)>& f, const list<T>& p) { - if(isNil(p)) - return list<T> (); - if(f(car(p))) { - const lambda<list<T> (lambda<bool(T)> , list<T> )> ff(filter<T> ); - return cons(car(p), curry(ff, f, cdr(p))); - } - return filter(f, cdr(p)); -} - -/** - * Returns a list pointing to a member of a list. - */ -template<typename T> const list<T> member(const T& t, const list<T>& p) { - if(isNil(p)) - return list<T> (); - if(t == car(p)) - return p; - return member(t, cdr(p)); -} - -/** - * Reverse a list. - */ -template<typename T> const list<T> reverseIter(const list<T>& acc, const list<T>& p) { - if(isNil(p)) - return acc; - return reverseIter(cons(car(p), acc), cdr(p)); -} - -template<typename T> const list<T> reverse(const list<T>& p) { - return reverseIter(list<T> (), p); -} - -template<typename T> const list<T> seq(const T& start, const T& end); - -template<typename T> struct seqGenerate { - const T start; - const T end; - seqGenerate(const T& start, const T&end) : - start(start), end(end) { - } - const list<T> operator()() const { - return seq<T> (start, end); - } -}; - -/** - * Returns a sequence of values between the given bounds. - */ -template<typename T> const list<T> seq(const T& start, const T& end) { - if(start == end) - return makeList(start); - if(start < end) - return cons(start, lambda<list<T> ()> (seqGenerate<T> (start + 1, end))); - return cons(start, lambda<list<T> ()> (seqGenerate<T> (start - 1, end))); -} - -/** - * Equivalent of the list assoc function. - */ -template<typename T> const list<T> assoc(const T& k, const list<list<T> >& p) { - if(isNil(p)) - return list<T> (); - if(k == car(car(p))) - return car(p); - return assoc(k, cdr(p)); -} - -/** - * Pretty print a list. - */ -template<typename T> std::ostream& print(const list<T>& l, std::ostream& os) { - os << "("; - if (!isNil(l)) { - list<T> ml = l; - while(true) { - os << car(ml); - ml = cdr(ml); - if (isNil(ml)) - break; - os << ", "; - } - } - os << ")"; - return os; -} - -} - -#endif /* tuscany_list_hpp */ diff --git a/cpp/sca/runtime/core/src/tuscany/parallel.hpp b/cpp/sca/runtime/core/src/tuscany/parallel.hpp deleted file mode 100644 index c0d578e281..0000000000 --- a/cpp/sca/runtime/core/src/tuscany/parallel.hpp +++ /dev/null @@ -1,283 +0,0 @@ -/* - * 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_parallel_hpp -#define tuscany_parallel_hpp - -/** - * Simple parallel work execution functions. - */ - -#include <pthread.h> -#include <sys/syscall.h> -#include "function.hpp" - -namespace tuscany { - -/** - * Returns the current thread id. - */ -unsigned int threadId() { - return syscall(__NR_gettid); -} - -/** - * Represents a value which will be know in the future. - */ -template<typename T> class future { - -private: - template<typename X> class futureValue { - public: - futureValue() : - refCount(0), hasValue(false) { - pthread_mutex_init(&valueMutex, NULL); - pthread_cond_init(&valueCond, NULL); - } - - ~futureValue() { - pthread_mutex_destroy(&valueMutex); - pthread_cond_destroy(&valueCond); - } - - unsigned int acquire() { - return __sync_add_and_fetch(&refCount, 1); - } - - unsigned int release() { - return __sync_sub_and_fetch(&refCount, 1); - } - - bool set(const T& v) { - pthread_mutex_lock(&valueMutex); - if(hasValue) { - pthread_mutex_unlock(&valueMutex); - return false; - } - hasValue = true; - value = v; - pthread_mutex_unlock(&valueMutex); - pthread_cond_broadcast(&valueCond); - return true; - } - - const T get() { - pthread_mutex_lock(&valueMutex); - while(!hasValue) { - pthread_cond_wait(&valueCond, &valueMutex); - } - const T& v = value; - pthread_mutex_unlock(&valueMutex); - return v; - } - - private: - unsigned refCount; - pthread_mutex_t valueMutex; - pthread_cond_t valueCond; - bool hasValue; - X value; - }; - - gc_counting_ptr<futureValue<T> > fvalue; - - template<typename X> friend const X get(const future<X>& f); - template<typename X> friend bool set(const future<X>& f, const X& v); - -public: - future() : fvalue(new futureValue<T>()) { - //std::cout << "future() threadId " << threadId() << "\n"; - } - - ~future() { - //std::cout << "~future() threadId " << threadId() << "\n"; - } - - future(const future& f) : fvalue(f.fvalue) { - //std::cout << "future(const future& f) threadId " << threadId() << "\n"; - } - - const future& operator=(const future& f) { - //std::cout << "future::operator=(const future& f) threadId " << threadId() << "\n"; - if (&f == this) - return *this; - fvalue = f.fvalue; - return *this; - } - - const future& operator=(const T& v) const { - fvalue->set(v); - return *this; - } - - operator const T() const { - return fvalue->get(); - } - -}; - -/** - * A bounded thread safe queue. - */ -template<typename T> class queue { -public: - explicit queue(int max) : max(max), size(0), tail(0), head(0), values(new T[max]) { - pthread_mutex_init(&mutex, NULL); - pthread_cond_init(&full, NULL); - pthread_cond_init(&empty, NULL); - } - - ~queue() { - pthread_mutex_destroy(&mutex); - pthread_cond_destroy(&full); - pthread_cond_destroy(&empty); - } - -private: - const int max; - int size; - int tail; - int head; - pthread_mutex_t mutex; - pthread_cond_t full; - pthread_cond_t empty; - gc_aptr<T> values; - - template<typename X> friend const int enqueue(queue<X>& q, const X& v); - template<typename X> friend const X dequeue(queue<X>& q); -}; - -/** - * Adds an element to the tail of the queue. - */ -template<typename T> const int enqueue(queue<T>&q, const T& v) { - pthread_mutex_lock(&q.mutex); - while(q.size == q.max) - pthread_cond_wait(&q.full, &q.mutex); - q.values[q.tail] = v; - q.tail = (q.tail + 1) % q.max; - q.size++; - pthread_mutex_unlock(&q.mutex); - pthread_cond_broadcast(&q.empty); - return q.size; -} - -/** - * Returns the element at the head of the queue. - */ -template<typename T> const T dequeue(queue<T>& q) { - pthread_mutex_lock(&q.mutex); - while(q.size == 0) - pthread_cond_wait(&q.empty, &q.mutex); - const T v = q.values[q.head]; - q.head = (q.head + 1) % q.max; - q.size--; - pthread_mutex_unlock(&q.mutex); - pthread_cond_broadcast(&q.full); - return v; -} - -/** - * The worker thread function. - */ -void *workerThreadFunc(void *arg) { - queue<lambda<bool()> >* work = reinterpret_cast<queue<lambda<bool()> >*>(arg); - while(dequeue(*work)()) - ; - return NULL; -} - -/** - * Returns a list of worker threads. - */ -const list<pthread_t> makeWorkerThreads(queue<lambda<bool()> >& queue, const int count) { - if (count == 0) - return list<pthread_t>(); - pthread_t thread; - pthread_create(&thread, NULL, workerThreadFunc, &queue); - return cons(thread, makeWorkerThreads(queue, count - 1)); -} - -/** - * A worker, implemented with a work queue and a pool of threads. - */ -class worker { -public: - explicit worker(int max) : work(queue<lambda<bool()> >(max)), threads(makeWorkerThreads(work, max)) { - } - -private: - queue<lambda<bool()> > work; - const list<pthread_t> threads; - - template<typename X> friend const future<X> submit(worker& w, const lambda<X()>& func); - friend const bool shutdown(worker& w); -}; - -/** - * Function used to wrap work submitted to a worker. - */ -template<typename R> bool submitFunc(const lambda<R()>& func, const future<R>& fut) { - fut = func(); - return true; -} - -/** - * Submits work to a worker. - */ -template<typename R> const future<R> submit(worker& w, const lambda<R()>& func) { - const future<R> fut; - const lambda<bool()> f = curry(lambda<bool(lambda<R()>, future<R>)>(submitFunc<R>), func, fut); - enqueue(w.work, f); - return fut; -} - -/** - * Enqueues shutdown requests. - */ -const bool shutdownEnqueue(const list<pthread_t>& threads, queue<lambda<bool()> >& work) { - if (threads == list<pthread_t>()) - return true; - enqueue(work, unit(false)); - return shutdownEnqueue(cdr(threads), work); -} - -/** - * Waits for shut down threads to terminate. - */ -const bool shutdownJoin(const list<pthread_t>& threads) { - if (threads == list<pthread_t>()) - return true; - pthread_join(car(threads), NULL); - return shutdownJoin(cdr(threads)); -} - -/** - * Shutdown a worker. - */ -const bool shutdown(worker& w) { - shutdownEnqueue(w.threads, w.work); - shutdownJoin(w.threads); - return true; -} - -} -#endif /* tuscany_parallel_hpp */ diff --git a/cpp/sca/runtime/core/src/tuscany/value.hpp b/cpp/sca/runtime/core/src/tuscany/value.hpp deleted file mode 100644 index ca4bd06ca6..0000000000 --- a/cpp/sca/runtime/core/src/tuscany/value.hpp +++ /dev/null @@ -1,357 +0,0 @@ -/* - * 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_value_hpp -#define tuscany_value_hpp - -/** - * Generic value type. - */ - -#include <string> -#include <iostream> -#include "gc.hpp" -#include "function.hpp" -#include "list.hpp" - -namespace tuscany -{ - -long int countValues = 0; -long int countEValues = 0; -long int countCValues = 0; -long int countVValues = 0; - -bool resetValueCounters() { - countValues = countEValues = countCValues = countVValues = 0; - return true; -} - -bool printValueCounters() { - std::cout << "countValues " << countValues << std::endl; - std::cout << "countEValues " << countEValues << std::endl; - std::cout << "countCValues " << countCValues << std::endl; - std::cout << "countVValues " << countVValues << std::endl; - return true; -} - -class value; - -class value { -public: - - enum ValueType { - Undefined, Symbol, String, List, Number, Boolean, Character, Lambda - }; - - value() : - type(value::Undefined) { - countValues++; - countEValues++; - } - - value(const value& v) { - countValues++; - countCValues++; - type = v.type; - switch(type) { - case value::List: - lst() = v.lst(); - case value::Lambda: - func() = v.func(); - case value::Symbol: - str() = v.str(); - case value::String: - str() = v.str(); - case value::Number: - num() = v.num(); - case value::Boolean: - boo() = v.boo(); - case value::Character: - chr() = v.chr(); - default: - break; - } - } - - const value& operator=(const value& v) { - if(this == &v) - return *this; - type = v.type; - switch(type) { - case value::List: - lst() = v.lst(); - case value::Lambda: - func() = v.func(); - case value::Symbol: - str() = v.str(); - case value::String: - str() = v.str(); - case value::Number: - num() = v.num(); - case value::Boolean: - boo() = v.boo(); - case value::Character: - chr() = v.chr(); - default: - break; - } - return *this; - } - - virtual ~value() { - countValues--; - } - - explicit value(const lambda<value(list<value>&)>& func) : - type(value::Lambda), data(vdata(func)) { - countValues++; - countVValues++; - } - - explicit value(const std::string& str) : - type(value::String), data(vdata(unit(str))) { - countValues++; - countVValues++; - } - - explicit value(const char* str) : - type(value::Symbol), data(vdata(unit(std::string(str)))) { - countValues++; - countVValues++; - } - - explicit value(const list<value>& lst) : - type(value::List), data(vdata(unit(lst))) { - countValues++; - countVValues++; - } - - explicit value(const double num) : - type(value::Number), data(vdata(unit(num))) { - countValues++; - countVValues++; - } - - explicit value(const int num) : - type(value::Number), data(vdata(unit((double)num))) { - countValues++; - countVValues++; - } - - explicit value(const bool boo) : - type(value::Boolean), data(vdata(unit(boo))) { - countValues++; - countVValues++; - } - - explicit value(const char chr) : - type(value::Character), data(vdata(unit(chr))) { - countValues++; - countVValues++; - } - - const bool operator!=(const value& v) const { - return !this->operator==(v); - } - - const bool operator==(const value& v) const { - if(this == &v) - return true; - if(type != v.type) - return false; - switch(type) { - case value::Undefined: - return true; - case value::List: - return lst()() == v.lst()(); - case value::Lambda: - return func() == v.func(); - case value::Symbol: - return str()() == v.str()(); - case value::String: - return str()() == v.str()(); - case value::Number: - return num()() == v.num()(); - case value::Boolean: - return boo()() == v.boo()(); - case value::Character: - return chr()() == v.chr()(); - default: - return false; - } - } - - const value operator()(list<value>& args) const { - return func()(args); - } - - operator const std::string() const { - return str()(); - } - - operator const double() const { - return num()(); - } - - operator const int() const { - return num()(); - } - - operator const bool() const { - return boo()(); - } - - operator const char() const { - return chr()(); - } - - operator const list<value>() const { - return lst()(); - } - - operator const lambda<value(list<value>&)>() const { - return func(); - } - - friend std::ostream& operator<<(std::ostream&, const value&); - - ValueType type; - lambda<char()> data; - -private: - template<typename T> lambda<T>& vdata() const { - return *reinterpret_cast<lambda<T> *> (const_cast<lambda<char()> *> (&data)); - } - - template<typename T> const lambda<char()>& vdata(const T& v) const { - return *reinterpret_cast<const lambda<char()> *> (&v); - } - - lambda<double()>& num() const { - return vdata<double()> (); - } - - lambda<bool()>& boo() const { - return vdata<bool()> (); - } - - lambda<char()>& chr() const { - return vdata<char()> (); - } - - lambda<std::string()>& str() const { - return vdata<std::string()> (); - } - - lambda<list<value>()>& lst() const { - return vdata<list<value>()> (); - } - - lambda<value(list<value>&)>& func() const { - return vdata<value(list<value>&)> (); - } - -}; - -std::ostream& operator<<(std::ostream& out, const value& v) { - switch(v.type) { - case value::List: - return out << "List::" << v.lst()(); - case value::Lambda: - return out << "Lambda::" << v.func(); - case value::Symbol: - return out << "Symbol::" << v.str()(); - case value::String: - return out << "String::" << '\'' << v.str()() << '\''; - case value::Number: - return out << "Number::" << v.num()(); - case value::Boolean: - if(v.boo()()) - return out << "Boolean::" << "true"; - else - return out << "Boolean::" << "false"; - case value::Character: - return out << "Character::" << v.chr()(); - default: - return out << "Undefined"; - } -} - -const bool isNil(const value& value) { - return value.type == value::Undefined; -} - -const bool isString(const value& value) { - return value.type == value::String; -} - -const bool isSymbol(const value& value) { - return value.type == value::Symbol; -} - -const bool isList(const value& value) { - return value.type == value::List; -} - -const bool isNumber(const value& value) { - return value.type == value::Number; -} - -const bool isBoolean(const value& value) { - return value.type == value::Boolean; -} - -const bool isCharacter(const value& value) { - return value.type == value::Character; -} - -const bool isTaggedList(const value& exp, value tag) { - if(isList(exp)) - return car((list<value> )exp) == tag; - return false; -} - -/** - * Pretty print a list of values. - */ -std::ostream& print(const list<value>& l, std::ostream& os) { - os << "("; - if (!isNil(l)) { - list<value> ml = l; - while(true) { - const value v = car(ml); - if (isList(v)) - print(list<value>(v), os); - else - os << v; - ml = cdr(ml); - if (isNil(ml)) - break; - os << ", "; - } - } - os << ")"; - return os; -} - -} -#endif /* tuscany_value_hpp */ diff --git a/cpp/sca/runtime/core/src/tuscany/xml.hpp b/cpp/sca/runtime/core/src/tuscany/xml.hpp deleted file mode 100644 index b4a3b87ad1..0000000000 --- a/cpp/sca/runtime/core/src/tuscany/xml.hpp +++ /dev/null @@ -1,385 +0,0 @@ -/* - * 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_xml_hpp -#define tuscany_xml_hpp - -/** - * XML read/write functions. - */ - -#include <libxml/xmlreader.h> -#include <libxml/xmlwriter.h> -#include <libxml/xmlschemas.h> -#include <libxml/globals.h> -#include <iostream> -#include <fstream> -#include <string> -#include "list.hpp" - -namespace tuscany { - -/** - * Encapsulates a libxml2 xmlTextReader and its state. - */ -class XmlReader { -public: - enum TokenType { - None = 0, Element = 1, Attribute = 2, EndElement = 15, Identifier = 100, Text = 101, End = 103 - }; - - explicit XmlReader(xmlTextReaderPtr xml) : xml(xml), tokenType(None) { - xmlTextReaderSetParserProp(xml, XML_PARSER_DEFAULTATTRS, 1); - xmlTextReaderSetParserProp(xml, XML_PARSER_SUBST_ENTITIES, 1); - } - - ~XmlReader() { - xmlFreeTextReader(xml); - } - - /** - * Read the next token and return its type. - */ - int read() { - if (tokenType == End) - return tokenType; - if (tokenType == Element) { - isEmptyElement = xmlTextReaderIsEmptyElement(xml); - hasValue = xmlTextReaderHasValue(xml); - hasAttributes = xmlTextReaderHasAttributes(xml); - return tokenType = Identifier; - } - if (hasValue && tokenType == Identifier) - return tokenType = Text; - if (hasAttributes && (tokenType == Identifier || tokenType == Text) && xmlTextReaderMoveToFirstAttribute(xml) == 1) - return tokenType = Attribute; - if (tokenType == Attribute && xmlTextReaderMoveToNextAttribute(xml) == 1) - return tokenType = Attribute; - if (isEmptyElement && (tokenType == Identifier || tokenType == Text || tokenType == Attribute)) - return tokenType = EndElement; - if (!xmlTextReaderRead(xml)) - return tokenType = End; - return tokenType = xmlTextReaderNodeType(xml); - } - - operator xmlTextReaderPtr() const { - return xml; - } - -private: - const xmlTextReaderPtr xml; - int tokenType; - bool isEmptyElement; - bool hasValue; - bool hasAttributes; -}; - -/** - * Constants used to tag XML tokens. - */ -const value endElement("<"); -const value startElement(">"); -const value attribute("attribute"); -const value element("element"); - -/** - * Read an XML identifier. - */ -const value readIdentifier(XmlReader& reader) { - const char* name = (const char*)xmlTextReaderConstName(reader); - return value(name); -} - -/** - * Read XML text. - */ -const value readText(XmlReader& reader) { - const char *val = (const char*)xmlTextReaderConstValue(reader); - return value(std::string(val)); -} - -/** - * Read an XML attribute. - */ -const value readAttribute(XmlReader& reader) { - const char *name = (const char*)xmlTextReaderConstName(reader); - const char *val = (const char*)xmlTextReaderConstValue(reader); - return value(makeList(attribute, value(name), value(std::string(val)))); -} - -/** - * Read an XML token. - */ -const value readToken(XmlReader& reader) { - const int tokenType = reader.read(); - if (tokenType == XmlReader::End) - return value(); - if (tokenType == XmlReader::Element) - return startElement; - if (tokenType == XmlReader::Identifier) - return readIdentifier(reader); - if (tokenType == XmlReader::Attribute) - return readAttribute(reader); - if (tokenType == XmlReader::Text) - return readText(reader); - if (tokenType == XmlReader::EndElement) - return endElement; - return readToken(reader); -} - -/** - * Read a list of XML tokens. - */ -const list<value> readList(const list<value>& listSoFar, XmlReader& reader) { - const value token = readToken(reader); - if(isNil(token) || endElement == token) - return reverse(listSoFar); - if(startElement == token) - return readList(cons(value(readList(makeList(element), reader)), listSoFar), reader); - return readList(cons(token, listSoFar), reader); -} - -/** - * Read an XML document from a libxml2 XML reader. - */ -const list<value> read(XmlReader& reader) { - value nextToken = readToken(reader); - if (startElement == nextToken) - return makeList(value(readList(makeList(element), reader))); - return list<value>(); -} - -/** - * Callback function called by libxml2 to read the XML input stream. - */ -int readCallback(void *context, char* buffer, int len) { - std::istream* is = static_cast<std::istream*>(context); - is->read(buffer, len); - if (!is->eof() && (is->fail() || is->bad())) - return -1; - const int n = is->gcount(); - return n; -} - -/** - * Read an XML document from an input stream. - */ -const list<value> readXML(std::istream& is) { - xmlTextReaderPtr xml = xmlReaderForIO(readCallback, NULL, &is, NULL, NULL, XML_PARSE_NONET); - if (xml == NULL) - return list<value>(); - XmlReader reader(xml); - return read(reader); -} - -/** - * Callback function called by libxml2 to read the XML file input stream. - */ -int readFileCallback(void *context, char* buffer, int len) { - std::ifstream* is = static_cast<std::ifstream*>(context); - is->read(buffer, len); - if (is->fail() || is->bad()) - return -1; - return is->gcount(); -} - -/** - * Callback function called by libxml2 to close the XML file input stream. - */ -int readCloseFileCallback(void *context) { - std::ifstream* fis = static_cast<std::ifstream*>(context); - fis->close(); - return 0; -} - -/** - * Read an XML document from a file input stream. - */ -const list<value> readXML(std::ifstream& is) { - xmlTextReaderPtr xml = xmlReaderForIO(readFileCallback, readCloseFileCallback, &is, NULL, NULL, XML_PARSE_NONET); - if (xml == NULL) - return list<value>(); - XmlReader reader(xml); - return read(reader); -} - -/** - * Returns true if a value is an XML attribute. - */ -const bool isAttribute(const list<value>& l) { - return !isNil(l) && car(l) == attribute; -} - -/** - * Returns the name of an XML attribute. - */ -const std::string attributeName(const list<value>& l) { - return cadr(l); -} - -/** - * Returns the text value of an XML attribute. - */ -const std::string attributeText(const list<value>& l) { - return caddr(l); -} - -/** - * Returns true if a value is an XML element. - */ -const bool isElement(const list<value>& l) { - return !isNil(l) && car(l) == element; -} - -/** - * Returns the name of an XML element. - */ -const std::string elementName(const list<value>& l) { - return cadr(l); -} - -/** - * Returns true if an XML element contains text content. - */ -const bool elementHasText(const list<value>& l) { - if (isNil(cddr(l))) - return false; - return isString(caddr(l)); -} - -/** - * Returns the text content of an XML element. - */ -const std::string elementText(const list<value>& l) { - return caddr(l); -} - -/** - * Returns the children of an XML element. - */ -const list<value> elementChildren(const list<value>& l) { - return cddr(l); -} - -/** - * Default encoding used to write XML documents. - */ -const char* encoding = "UTF-8"; - -/** - * Write a list of XML element or attribute tokens. - */ -const bool writeList(const list<value>& l, const xmlTextWriterPtr xml) { - if (isNil(l)) - return true; - - // Write an attribute - const list<value> token(car(l)); - if (isAttribute(token)) { - if (xmlTextWriterWriteAttribute(xml, (const xmlChar*)attributeName(token).c_str(), (const xmlChar*)attributeText(token).c_str()) < 0) - return false; - - } else if (isElement(token)) { - - // Write an element - if (xmlTextWriterStartElement(xml, (const xmlChar*)elementName(token).c_str()) < 0) - return false; - if (elementHasText(token) && xmlTextWriterWriteString(xml, (const xmlChar*)elementText(token).c_str()) < 0) - return false; - - // Write its children - writeList(elementChildren(token), xml); - - if (xmlTextWriterEndElement(xml) < 0) - return false; - } - - // Go on - return writeList(cdr(l), xml); -} - -/** - * Write an XML document to a libxml2 XML writer. - */ -const bool write(const list<value>& l, const xmlTextWriterPtr xml) { - if (xmlTextWriterStartDocument(xml, NULL, encoding, NULL) < 0) - return false; - writeList(l, xml); - if (xmlTextWriterEndDocument(xml) < 0) - return false; - return true; -} - -/** - * Callback function called by libxml2 to write to the XML output stream. - */ -int writeCallback(void *context, const char* buffer, int len) { - std::ostream* os = static_cast<std::ostream*>(context); - os->write(buffer, len); - if (os->fail() || os->bad()) - return -1; - return len; -} - -/** - * Write an XML document to an output stream. - */ -const bool writeXML(const list<value>& l, std::ostream& os) { - xmlOutputBufferPtr out = xmlOutputBufferCreateIO(writeCallback, NULL, &os, NULL); - xmlTextWriterPtr xml = xmlNewTextWriter(out); - if (xml == NULL) - return false; - return write(l, xml); -} - -/** - * Callback function called by libxml2 to write to the XML file output stream. - */ -int writeFileCallback(void *context, const char* buffer, int len) { - std::ofstream* os = static_cast<std::ofstream*>(context); - os->write(buffer, len); - if (os->fail() || os->bad()) - return -1; - return len; -} - -/** - * Callback function called by libxml2 to close the XML file output stream. - */ -int writeCloseFileCallback(void *context) { - std::ofstream* fos = static_cast<std::ofstream*>(context); - fos->close(); - return 0; -} - -/** - * Write an XML document to a file output stream. - */ -const bool writeXML(const list<value>& l, std::ofstream& os) { - xmlOutputBufferPtr out = xmlOutputBufferCreateIO(writeFileCallback, writeCloseFileCallback, &os, NULL); - xmlTextWriterPtr xml = xmlNewTextWriter(out); - if (xml == NULL) - return false; - return write(l, xml); -} - -} -#endif /* tuscany_xml_hpp */ |