You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by js...@apache.org on 2012/12/11 04:51:12 UTC
svn commit: r1419985 [2/5] - in /tuscany/sca-cpp/trunk: ./ etc/ kernel/
modules/ modules/atom/ modules/rss/ modules/scdl/ modules/scheme/
modules/xml/
Modified: tuscany/sca-cpp/trunk/kernel/list.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/list.hpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/list.hpp (original)
+++ tuscany/sca-cpp/trunk/kernel/list.hpp Tue Dec 11 03:51:03 2012
@@ -43,16 +43,16 @@ long countILists = 0;
long countCLists = 0;
long countELists = 0;
-bool resetListCounters() {
+inline const bool resetListCounters() {
countLists = countILists = countCLists = countELists = 0;
return true;
}
-bool checkListCounters() {
+inline const bool checkListCounters() {
return countLists == 0;
}
-bool printListCounters() {
+inline const bool printListCounters() {
cout << "countLists " << countLists << endl;
cout << "countELists " << countELists << endl;
cout << "countILists " << countILists << endl;
@@ -91,19 +91,19 @@ bool printListCounters() {
template<typename T> class list {
public:
- list() {
+ inline list() : car() {
debug_inc(countLists);
debug_inc(countELists);
debug_watchList();
}
- list(const T car, const lambda<list<T>()>& cdr) : car(car), cdr(cdr) {
+ inline list(const T& car, const lambda<list<T>()>& cdr) : car(car), cdr(cdr) {
debug_inc(countLists);
debug_inc(countILists);
debug_watchList();
}
- list(const list& p) : car(p.car), cdr(p.cdr) {
+ inline list(const list& p) : car(p.car), cdr(p.cdr) {
debug_inc(countLists);
debug_inc(countCLists);
#ifdef WANT_MAINTAINER_WATCH
@@ -111,22 +111,13 @@ public:
#endif
}
- const list<T>& operator=(const list<T>& p) {
- if(this == &p)
- return *this;
- car = p.car;
- cdr = p.cdr;
-#ifdef WANT_MAINTAINER_WATCH
- watch = p.watch;
-#endif
- return *this;
- }
+ list<T>& operator=(const list<T>& p) = delete;
- ~list() {
+ inline ~list() {
debug_dec(countLists);
}
- const bool operator==(const list<T>& p) const {
+ inline const bool operator==(const list<T>& p) const {
if(this == &p)
return true;
if(isNil(cdr))
@@ -140,7 +131,7 @@ public:
return cdr() == p.cdr();
}
- const bool operator<(const list<T>& p) const {
+ inline const bool operator<(const list<T>& p) const {
if(this == &p)
return false;
if (isNil(cdr))
@@ -154,7 +145,7 @@ public:
return cdr() < p.cdr();
}
- const bool operator>(const list<T>& p) const {
+ inline const bool operator>(const list<T>& p) const {
if(this == &p)
return false;
if (isNil(cdr))
@@ -168,11 +159,11 @@ public:
return cdr() > p.cdr();
}
- const bool operator!=(const list<T>& p) const {
+ inline const bool operator!=(const list<T>& p) const {
return !this->operator==(p);
}
- operator const list<list<T> >() const {
+ inline operator const list<list<T> >() const {
return (list<list<T> >)T(*this);
}
@@ -185,9 +176,10 @@ private:
template<typename X> friend const bool isNil(const list<X>& p);
template<typename X> friend const X car(const list<X>& p);
template<typename X> friend const list<X> cdr(const list<X>& p);
+ template<typename X> friend const bool setlist(list<X>& target, const list<X>& l);
- T car;
- lambda<list<T>()> cdr;
+ const T car;
+ const lambda<list<T>()> cdr;
};
#ifdef WANT_MAINTAINER_WATCH
@@ -196,7 +188,7 @@ private:
* Debug utility used to write the contents of a list to a string, easier
* to watch than the list itself in a debugger.
*/
-template<typename T> const string watchList(const list<T>& p) {
+template<typename T> inline const string watchList(const list<T>& p) {
if(isNil(p))
return "()";
odebugstream os;
@@ -209,21 +201,21 @@ template<typename T> const string watchL
/**
* Returns true if the given list is nil.
*/
-template<typename T> const bool isNil(const list<T>& p) {
+template<typename T> inline const bool isNil(const list<T>& p) {
return isNil(p.cdr);
}
/**
* Write a list to an output stream.
*/
-template<typename T> ostream& writeHelper(ostream& out, const list<T>& l) {
+template<typename T> inline ostream& writeHelper(ostream& out, const list<T>& l) {
if (isNil(l))
return out;
out << " " << car(l);
return writeHelper(out, cdr(l));
}
-template<typename T> ostream& operator<<(ostream& out, const list<T>& l) {
+template<typename T> inline ostream& operator<<(ostream& out, const list<T>& l) {
if(isNil(l))
return out << "()";
out << "(" << car(l);
@@ -234,74 +226,74 @@ template<typename T> ostream& operator<<
/**
* 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) {
+template<typename T> inline const list<T> cons(const T& car, const lambda<const 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) {
+template<typename T> inline const list<T> cons(const T& car, const list<T>& cdr) {
return list<T> (car, result(cdr));
}
/**
* Cons variations for use with the reduce and reduceRight functions.
*/
-template<typename T> const list<T> lcons(const list<T>& cdr, const T& car) {
+template<typename T> inline const list<T> lcons(const list<T>& cdr, const T& car) {
return cons<T>(car, cdr);
}
-template<typename T> const list<T> rcons(const T& car, const list<T>& cdr) {
+template<typename T> inline const list<T> rcons(const T& car, const list<T>& cdr) {
return cons<T>(car, cdr);
}
/**
* Construct a list of one value.
*/
-template<typename T> const list<T> mklist(const T& car) {
+template<typename T> inline const list<T> mklist(const T& car) {
return list<T> (car, result(list<T> ()));
}
/**
* Construct a list of two values.
*/
-template<typename T> const list<T> mklist(const T& a, const T& b) {
+template<typename T> inline const list<T> mklist(const T& a, const T& b) {
return cons(a, mklist(b));
}
/**
* Construct a list of three values.
*/
-template<typename T> const list<T> mklist(const T& a, const T& b, const T& c) {
+template<typename T> inline const list<T> mklist(const T& a, const T& b, const T& c) {
return cons(a, cons(b, mklist(c)));
}
/**
* Construct a list of four values.
*/
-template<typename T> const list<T> mklist(const T& a, const T& b, const T& c, const T& d) {
+template<typename T> inline const list<T> mklist(const T& a, const T& b, const T& c, const T& d) {
return cons(a, cons(b, cons(c, mklist(d))));
}
/**
* Construct a list of five values.
*/
-template<typename T> const list<T> mklist(const T& a, const T& b, const T& c, const T& d, const T& e) {
+template<typename T> inline const list<T> mklist(const T& a, const T& b, const T& c, const T& d, const T& e) {
return cons(a, cons(b, cons(c, cons(d, mklist(e)))));
}
/**
* Construct a list of six values.
*/
-template<typename T> const list<T> mklist(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f) {
+template<typename T> inline const list<T> mklist(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f) {
return cons(a, cons(b, cons(c, cons(d, cons(e, mklist(f))))));
}
/**
* Returns the car of a list.
*/
-template<typename T> const T car(const list<T>& p) {
+template<typename T> inline const T car(const list<T>& p) {
// Abort if trying to access the car of a nil list
assertOrFail(!isNil(p.cdr));
return p.car;
@@ -310,159 +302,144 @@ template<typename T> const T car(const l
/**
* Returns the cdr of a list.
*/
-template<typename T> const list<T> cdr(const list<T>& p) {
+template<typename T> inline const list<T> cdr(const list<T>& p) {
return p.cdr();
}
/**
* Returns the car of the cdr (the 2nd element) of a list.
*/
-template<typename T> const T cadr(const list<T>& p) {
+template<typename T> inline const T cadr(const list<T>& p) {
return car(cdr(p));
}
/**
* Returns the 3rd element of a list.
*/
-template<typename T> const T caddr(const list<T>& p) {
+template<typename T> inline const T caddr(const list<T>& p) {
return car(cdr(cdr(p)));
}
/**
* Returns the 4th element of a list.
*/
-template<typename T> const T cadddr(const list<T>& p) {
+template<typename T> inline const T cadddr(const list<T>& p) {
return car(cdr(cdr(cdr(p))));
}
/**
* Returns the 5th element of a list.
*/
-template<typename T> const T caddddr(const list<T>& p) {
+template<typename T> inline const T caddddr(const list<T>& p) {
return car(cdr(cdr(cdr(cdr(p)))));
}
/**
* Returns the 6th element of a list.
*/
-template<typename T> const T cadddddr(const list<T>& p) {
+template<typename T> inline const T cadddddr(const list<T>& p) {
return car(cdr(cdr(cdr(cdr(cdr(p))))));
}
/**
* Returns the 7th element of a list.
*/
-template<typename T> const T caddddddr(const list<T>& p) {
+template<typename T> inline const T caddddddr(const list<T>& p) {
return car(cdr(cdr(cdr(cdr(cdr(cdr(p)))))));
}
/**
* Returns the 8th element of a list.
*/
-template<typename T> const T cadddddddr(const list<T>& p) {
+template<typename T> inline const T cadddddddr(const list<T>& p) {
return car(cdr(cdr(cdr(cdr(cdr(cdr(cdr(p))))))));
}
/**
* Returns a list of elements from the 3rd to the end of a list.
*/
-template<typename T> const list<T> cddr(const list<T>& p) {
+template<typename T> inline const list<T> cddr(const list<T>& p) {
return cdr(cdr(p));
}
/**
* Returns a list of elements from the 4th to the end of a list.
*/
-template<typename T> const list<T> cdddr(const list<T>& p) {
+template<typename T> inline const list<T> cdddr(const list<T>& p) {
return cdr(cdr(cdr(p)));
}
/**
* Returns a list of elements from the 5th to the end of a list.
*/
-template<typename T> const list<T> cddddr(const list<T>& p) {
+template<typename T> inline const list<T> cddddr(const list<T>& p) {
return cdr(cdr(cdr(cdr(p))));
}
/**
* Returns a list of elements from the 6th to the end of a list.
*/
-template<typename T> const list<T> cdddddr(const list<T>& p) {
+template<typename T> inline const list<T> cdddddr(const list<T>& p) {
return cdr(cdr(cdr(cdr(cdr(p)))));
}
/**
* Returns a list of elements from the 7th to the end of a list.
*/
-template<typename T> const list<T> cddddddr(const list<T>& p) {
+template<typename T> inline const list<T> cddddddr(const list<T>& p) {
return cdr(cdr(cdr(cdr(cdr(cdr(p))))));
}
/**
* Returns a list of elements from the 8th to the end of a list.
*/
-template<typename T> const list<T> cdddddddr(const list<T>& p) {
+template<typename T> inline const list<T> cdddddddr(const list<T>& p) {
return cdr(cdr(cdr(cdr(cdr(cdr(cdr(p)))))));
}
/**
* Returns the length of a list.
*/
-template<typename T> struct lengthRef {
- const size_t operator()(const size_t c, const list<T>& p) {
+template<typename T> inline const size_t length(const list<T>& p) {
+ const lambda<size_t(const size_t, const list<T>&)> lengthRef = [&lengthRef](const size_t c, const list<T>& p) -> const size_t {
if(isNil(p))
return c;
- return (*this)(c + 1, cdr(p));
- }
-};
-
-template<typename T> const size_t length(const list<T>& p) {
- return lengthRef<T> ()(0, p);
+ return lengthRef(c + 1, cdr(p));
+ };
+ return lengthRef(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) {
+template<typename T> inline const list<T> append(const list<T>&a, const lambda<const list<T>()>& fb) {
if(isNil(a))
return fb();
-
- return cons<T>(car(a), appendCdr<T> (cdr(a), fb));
+ return cons<T>(car(a), [a, fb]() { return append(cdr(a), fb); });
}
/**
* Appends two lists.
*/
-template<typename T> const list<T> append(const list<T>&a, const list<T>& b) {
+template<typename T> inline const list<T> append(const list<T>&a, const list<T>& b) {
return append(a, result(b));
}
/**
* Append a value to a list.
*/
-template<typename T> const list<T> operator+(const list<T>& l, const T& v) {
+template<typename T> inline const list<T> operator+(const list<T>& l, const T& v) {
return append(l, mklist(v));
}
-template<typename T, typename V> const list<T> operator+(const list<T>& l, const V& v) {
+template<typename T, typename V> const list<T> inline operator+(const list<T>& l, const V& v) {
return append(l, mklist<T>(v));
}
/**
* Map a lambda function on a list.
*/
-template<typename T, typename R> const list<R> map(const lambda<R(const T)>& f, const list<T>& p) {
+template<typename T, typename R> inline const list<R> map(const lambda<const R(const T)>& f, const list<T>& p) {
if(isNil(p))
return list<R> ();
return cons(f(car(p)), map(f, cdr(p)));
@@ -471,46 +448,32 @@ template<typename T, typename R> const l
/**
* Run a reduce lambda function on a list.
*/
-template<typename T, typename R> struct reduceAccumulate {
- const lambda<R(const R&, const T&)> f;
- reduceAccumulate(const lambda<R(const R, const T)>& f) :
- f(f) {
- }
- R operator()(const R& acc, const list<T>& p) const {
+template<typename T, typename R> inline const R reduce(const lambda<const R(const R, const T)>& f, const R& initial, const list<T>& p) {
+ const lambda<const R(const R&, const list<T>&p)> reduceAccumulate = [f, &reduceAccumulate](const R& acc, const list<T>& p) -> R {
if(isNil(p))
return acc;
- return (*this)(f(acc, car(p)), cdr(p));
- }
-};
-
-template<typename T, typename R> const R reduce(const lambda<R(const R, const T)>& f, const R& initial, const list<T>& p) {
- return reduceAccumulate<T, R> (f)(initial, p);
+ return reduceAccumulate(f(acc, car(p)), cdr(p));
+ };
+ return reduceAccumulate(initial, p);
}
-template<typename T, typename R> struct reduceRightAccumulate {
- const lambda<R(const T&, const R&)> f;
- reduceRightAccumulate(const lambda<R(const T, const R)>& f) :
- f(f) {
- }
- R operator()(const list<T>& p, const R& acc) const {
+template<typename T, typename R> inline const R reduceRight(const lambda<const R(const T, const R)>& f, const R& initial, const list<T>& p) {
+ const lambda<const R(const list<T>&p, const R&)> reduceRightAccumulate = [f, &reduceRightAccumulate](const list<T>& p, const R& acc) -> R {
if(isNil(p))
return acc;
- return (*this)(cdr(p), f(car(p), acc));
- }
-};
-
-template<typename T, typename R> const R reduceRight(const lambda<R(const T, const R)>& f, const R& initial, const list<T>& p) {
- return reduceRightAccumulate<T, R> (f)(p, initial);
+ return reduceRightAccumulate(cdr(p), f(car(p), acc));
+ };
+ return reduceRightAccumulate(p, initial);
}
/**
* Run a filter lambda function on a list.
*/
-template<typename T> const list<T> filter(const lambda<bool(const T)>& f, const list<T>& p) {
+template<typename T> inline const list<T> filter(const lambda<const bool(const T)>& f, const list<T>& p) {
if(isNil(p))
return list<T> ();
if(f(car(p))) {
- const lambda<list<T>(const lambda<bool(const T)>, const list<T>)> ff(filter<T>);
+ const lambda<const list<T>(const lambda<const bool(const T)>, const list<T>)> ff(filter<T>);
return cons(car(p), curry(ff, f, cdr(p)));
}
return filter(f, cdr(p));
@@ -519,7 +482,7 @@ template<typename T> const list<T> filte
/**
* Returns a list pointing to a member of a list.
*/
-template<typename T> const list<T> member(const T& t, const list<T>& p) {
+template<typename T> inline const list<T> member(const T& t, const list<T>& p) {
if(isNil(p))
return list<T> ();
if(t == car(p))
@@ -530,44 +493,31 @@ template<typename T> const list<T> membe
/**
* Reverse a list.
*/
-template<typename T> const list<T> reverseIter(const list<T>& acc, const list<T>& p) {
+template<typename T> inline 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) {
+template<typename T> inline 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) {
+template<typename T> inline const list<T> seq(const T& start, const T& end) {
if(start == end)
return mklist(start);
if(start < end)
- return cons<T>(start, seqGenerate<T> (start + 1, end));
- return cons<T>(start, seqGenerate<T> (start - 1, end));
+ return cons<T>(start, [start, end] { return seq<T> (start + 1, end); });
+ return cons<T>(start, [start, end] { return seq<T> (start - 1, end); });
}
/**
* Returns the i-th element of a list.
*/
-template<typename T> const T listRef(const list<T>& l, const size_t i) {
+template<typename T> inline const T listRef(const list<T>& l, const size_t i) {
if (i == 0)
return car(l);
return listRef(cdr(l), i - 1);
@@ -576,7 +526,7 @@ template<typename T> const T listRef(con
/**
* Returns the first pair matching a key from a list of key value pairs.
*/
-template<typename T> const list<T> assoc(const T& k, const list<list<T> >& p) {
+template<typename T> inline const list<T> assoc(const T& k, const list<list<T> >& p) {
if(isNil(p))
return list<T> ();
if(k == car(car(p)))
@@ -587,7 +537,7 @@ template<typename T> const list<T> assoc
/**
* Returns a list of lists containing elements from two input lists.
*/
-template<typename T> const list<list<T> > zip(const list<T>& a, const list<T>& b) {
+template<typename T> inline const list<list<T> > zip(const list<T>& a, const list<T>& b) {
if (isNil(a) || isNil(b))
return list<list<T> >();
return cons<list<T> >(mklist<T>(car(a), car(b)), zip(cdr(a), cdr(b)));
@@ -596,19 +546,19 @@ template<typename T> const list<list<T>
/**
* Converts a list of key value pairs to a list containing the list of keys and the list of values.
*/
-template<typename T> const list<T> unzipKeys(const list<list<T> >& l) {
+template<typename T> inline const list<T> unzipKeys(const list<list<T> >& l) {
if (isNil(l))
return list<T>();
return cons(car(car(l)), unzipKeys(cdr(l)));
}
-template<typename T> const list<T> unzipValues(const list<list<T> >& l) {
+template<typename T> inline const list<T> unzipValues(const list<list<T> >& l) {
if (isNil(l))
return list<T>();
return cons(cadr(car(l)), unzipValues(cdr(l)));
}
-template<typename T> const list<list<T> > unzip(const list<list<T> >& l) {
+template<typename T> inline const list<list<T> > unzip(const list<list<T> >& l) {
return mklist<list<T> >(unzipKeys(l), unzipValues(l));
}
Modified: tuscany/sca-cpp/trunk/kernel/mem-test.cpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/mem-test.cpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/mem-test.cpp (original)
+++ tuscany/sca-cpp/trunk/kernel/mem-test.cpp Tue Dec 11 03:51:03 2012
@@ -43,7 +43,7 @@ public:
maxElements = countElements;
}
- Element(int i) : i(i) {
+ Element(const int i) : i(i) {
countElements++;
if (countElements > maxElements)
maxElements = countElements;
@@ -66,7 +66,7 @@ public:
private:
friend ostream& operator<<(ostream& out, const Element& v);
- int i;
+ const int i;
char c[20];
};
@@ -75,14 +75,14 @@ ostream& operator<<(ostream& out, const
return out;
}
-bool poolAlloc(Element** p, const int count) {
+const bool poolAlloc(Element** const p, const int count) {
if (count == 0)
return true;
p[count - 1] = new (gc_new<Element>()) Element();
return poolAlloc(p, count - 1);
};
-bool poolFree(Element** p, const int count) {
+bool poolFree(Element** const p, const int count) {
if (count == 0)
return true;
// Do nothing to free the element, but cycle through them just
@@ -90,21 +90,14 @@ bool poolFree(Element** p, const int cou
return poolFree(p, count - 1);
};
-struct poolAllocPerf {
- const int n;
- Element** p;
- poolAllocPerf(const int n) : n(n), p(new Element*[n]) {
- }
- const bool operator()() const {
- gc_scoped_pool gc;
- poolAlloc(p, n);
- return true;
- }
-};
-
-bool testPoolAllocPerf() {
+const bool testPoolAllocPerf() {
const int count = 10000;
- const lambda<bool()> pl = poolAllocPerf(count);
+ Element** const elements = new Element*[count];
+ const blambda pl = [elements, count]() -> const bool {
+ const gc_scoped_pool gc;
+ poolAlloc(elements, count);
+ return true;
+ };
maxElements = 0;
cout << "Memory pool alloc test " << (time(pl, 1, 1) / count) << " ms" << endl;
assert(countElements == 0);
@@ -112,35 +105,28 @@ bool testPoolAllocPerf() {
return true;
}
-bool stdAlloc(Element** p, const int count) {
+const bool stdAlloc(Element** const p, const int count) {
if (count == 0)
return true;
p[count - 1] = new Element();
return stdAlloc(p, count - 1);
};
-bool stdFree(Element** p, const int count) {
+const bool stdFree(Element** const p, const int count) {
if (count == 0)
return true;
delete p[count -1];
return stdFree(p, count - 1);
};
-struct stdAllocPerf {
- const int n;
- Element** p;
- stdAllocPerf(const int n) : n(n), p(new Element*[n]) {
- }
- const bool operator()() const {
- stdAlloc(p, n);
- stdFree(p, n);
- return true;
- }
-};
-
-bool testStdAllocPerf() {
+const bool testStdAllocPerf() {
const int count = 10000;
- const lambda<bool()> sl = stdAllocPerf(count);
+ Element** const elements = new Element*[count];
+ const blambda sl = [elements, count]() -> const bool {
+ stdAlloc(elements, count);
+ stdFree(elements, count);
+ return true;
+ };
maxElements = 0;
cout << "Memory standard alloc test " << (time(sl, 1, 1) / count) << " ms" << endl;
assert(countElements == 0);
@@ -151,7 +137,7 @@ bool testStdAllocPerf() {
}
int main() {
- tuscany::gc_scoped_pool p;
+ const tuscany::gc_scoped_pool p;
tuscany::cout << "Testing..." << tuscany::endl;
tuscany::testPoolAllocPerf();
Modified: tuscany/sca-cpp/trunk/kernel/monad.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/monad.hpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/monad.hpp (original)
+++ tuscany/sca-cpp/trunk/kernel/monad.hpp Tue Dec 11 03:51:03 2012
@@ -26,8 +26,11 @@
* Simple monad implementations.
*/
+#ifdef WANT_MAINTAINER_BACKTRACE
#include <execinfo.h>
#include <cxxabi.h>
+#endif
+
#include "function.hpp"
#include "string.hpp"
#include "stream.hpp"
@@ -43,21 +46,16 @@ namespace tuscany
*/
template<typename V> class id {
public:
- id(const V& v) : v(v) {
+ inline id(const V& v) : v(v) {
}
- const id<V>& operator=(const id<V>& m) {
- if(this == &m)
- return *this;
- v = m.v;
- return *this;
- }
+ id<V>& operator=(const id<V>& m) = delete;
- const bool operator!=(const id<V>& m) const {
+ inline const bool operator!=(const id<V>& m) const {
return !this->operator==(m);
}
- const bool operator==(const id<V>& m) const {
+ inline const bool operator==(const id<V>& m) const {
if (&m == this)
return true;
return v == m.v;
@@ -72,7 +70,7 @@ private:
/**
* Write an identity monad to a stream.
*/
-template<typename V> ostream& operator<<(ostream& out, const id<V>& m) {
+template<typename V> inline ostream& operator<<(ostream& out, const id<V>& m) {
out << content(m);
return out;
}
@@ -80,29 +78,29 @@ template<typename V> ostream& operator<<
/**
* Returns the content of an identity monad.
*/
-template<typename V> const V content(const id<V>& m) {
+template<typename V> inline const V content(const id<V>& m) {
return m.v;
}
/**
* Return an identity monad from a value.
*/
-template<typename V> const id<V> mkunit(const V& v) {
+template<typename V> inline const id<V> mkunit(const V& v) {
return id<V>(v);
}
-template<typename V> const lambda<id<V>(const V)> unit() {
+template<typename V> inline const lambda<const id<V>(const V)> unit() {
return mkunit<V>;
}
/**
* Bind a function to an identity monad. Pass the value in the monad to the function.
*/
-template<typename R, typename V> const id<R> operator>>(const id<V>& m, const lambda<id<R>(const V)>& f) {
+template<typename R, typename V> inline const id<R> operator>>(const id<V>& m, const lambda<const id<R>(const V)>& f) {
return f(content(m));
}
-template<typename R, typename V> const id<R> operator>>(const id<V>& m, const id<R> (* const f)(const V)) {
+template<typename R, typename V> inline const id<R> operator>>(const id<V>& m, const id<R> (* const f)(const V)) {
return f(content(m));
}
@@ -112,26 +110,22 @@ template<typename R, typename V> const i
*/
template<typename V> class maybe {
public:
- maybe(const V& v) : hasv(true), v(v) {
+ inline maybe(const V& v) : hasv(true), v(v) {
}
- maybe() : hasv(false) {
+ inline maybe() : hasv(false), v() {
}
- const maybe<V>& operator=(const maybe<V>& m) {
- if(this == &m)
- return *this;
- hasv = m.hasv;
- if (hasv)
- v = m.v;
- return *this;
+ inline maybe(const maybe<V>& m) : hasv(m.hasv), v(m.v) {
}
- const bool operator!=(const maybe<V>& m) const {
+ maybe<V>& operator=(const maybe<V>& m) = delete;
+
+ inline const bool operator!=(const maybe<V>& m) const {
return !this->operator==(m);
}
- const bool operator==(const maybe<V>& m) const {
+ inline const bool operator==(const maybe<V>& m) const {
if (this == &m)
return true;
if (!hasv)
@@ -141,7 +135,7 @@ public:
private:
const bool hasv;
- V v;
+ const V v;
template<typename X> friend const bool hasContent(const maybe<X>& m);
template<typename X> friend const X content(const maybe<X>& m);
@@ -150,7 +144,7 @@ private:
/**
* Write a maybe monad to a stream.
*/
-template<typename V> ostream& operator<<(ostream& out, const maybe<V>& m) {
+template<typename V> inline ostream& operator<<(ostream& out, const maybe<V>& m) {
if (!hasContent(m)) {
out << "nothing";
return out;
@@ -162,25 +156,25 @@ template<typename V> ostream& operator<<
/**
* Return a maybe monad with a value in it.
*/
-template<typename V> const maybe<V> mkjust(const V& v) {
+template<typename V> inline const maybe<V> mkjust(const V& v) {
return maybe<V>(v);
}
-template<typename V> const lambda<maybe<V>(const V)> just() {
+template<typename V> inline const lambda<const maybe<V>(const V)> just() {
return mkjust<V>;
}
/**
* Returns true if a maybe monad contains a content.
*/
-template<typename V> const bool hasContent(const maybe<V>& m) {
+template<typename V> inline const bool hasContent(const maybe<V>& m) {
return m.hasv;
}
/**
* Returns the content of a maybe monad.
*/
-template<typename V> const V content(const maybe<V>& m) {
+template<typename V> inline const V content(const maybe<V>& m) {
return m.v;
}
@@ -188,13 +182,13 @@ template<typename V> const V content(con
* Bind a function to a maybe monad. Passes the value in the monad to the function
* if present, or does nothing if there's no value.
*/
-template<typename R, typename V> const maybe<R> operator>>(const maybe<V>& m, const lambda<maybe<R>(const V)>& f) {
+template<typename R, typename V> inline const maybe<R> operator>>(const maybe<V>& m, const lambda<const maybe<R>(const V)>& f) {
if (!hasContent(m))
return m;
return f(content(m));
}
-template<typename R, typename V> const maybe<R> operator>>(const maybe<V>& m, const maybe<R> (* const f)(const V)) {
+template<typename R, typename V> inline const maybe<R> operator>>(const maybe<V>& m, const maybe<R> (* const f)(const V)) {
if (!hasContent(m))
return m;
return f(content(m));
@@ -207,30 +201,22 @@ template<typename R, typename V> const m
*/
template<typename V, typename F = string, typename C = int> class failable {
public:
- failable() : hasv(false), c(-1) {
+ inline failable() : hasv(false), v(), c(-1) {
}
- failable(const V& v) : hasv(true), v(v), c(-1) {
+ inline failable(const V& v) : hasv(true), v(v), c(-1) {
}
- failable(const failable<V, F, C>& m) : hasv(m.hasv), v(m.v), f(m.f), c(m.c) {
+ inline failable(const failable<V, F, C>& m) : hasv(m.hasv), v(m.v), f(m.f), c(m.c) {
}
- const failable<V, F, C>& operator=(const failable<V, F, C>& m) {
- if (&m == this)
- return *this;
- hasv = m.hasv;
- v = m.v;
- f = m.f;
- c = m.c;
- return *this;
- }
+ failable<V, F, C>& operator=(const failable<V, F, C>& m) = delete;
- const bool operator!=(const failable<V, F, C>& m) const {
+ inline const bool operator!=(const failable<V, F, C>& m) const {
return !this->operator==(m);
}
- const bool operator==(const failable<V, F, C>& m) const {
+ inline const bool operator==(const failable<V, F, C>& m) const {
if (this == &m)
return true;
if (!hasv)
@@ -239,7 +225,7 @@ public:
}
private:
- failable(const bool hasv, const F& f, const C& c) : hasv(hasv), f(f), c(c) {
+ inline failable(const bool hasv, const F& f, const C& c) : hasv(hasv), v(), f(f), c(c) {
}
template<typename A, typename B, typename R> friend const bool hasContent(const failable<A, B, R>& m);
@@ -250,16 +236,16 @@ private:
template<typename A, typename B> friend const failable<A, B> mkfailure(const B& f, const int c, const bool log);
template<typename A> friend const failable<A> mkfailure();
- bool hasv;
- V v;
- F f;
- C c;
+ const bool hasv;
+ const V v;
+ const F f;
+ const C c;
};
/**
* Write a failable monad to a stream.
*/
-template<typename V, typename F, typename C> ostream& operator<<(ostream& out, const failable<V, F, C>& m) {
+template<typename V, typename F, typename C> inline ostream& operator<<(ostream& out, const failable<V, F, C>& m) {
if (!hasContent(m)) {
out << reason(m) << " : " << rcode(m);
return out;
@@ -271,20 +257,22 @@ template<typename V, typename F, typenam
/**
* Returns a failable monad with a success value in it.
*/
-template<typename V, typename F, typename C> const failable<V, F, C> mksuccess(const V& v) {
+template<typename V, typename F, typename C> inline const failable<V, F, C> mksuccess(const V& v) {
return failable<V, F, C>(v);
}
-template<typename V, typename F, typename C> const lambda<failable<V, F, C>(const V)> success() {
+template<typename V, typename F, typename C> inline const lambda<const failable<V, F, C>(const V)> success() {
return mksuccess<V, F, C>;
}
+#ifdef WANT_MAINTAINER_BACKTRACE
+
/**
* Demangle a C++ function name.
*/
-const string demangleFrame(const char* fun) {
+inline const string demangleFrame(const char* fun) {
int status;
- char* name = abi::__cxa_demangle(fun, 0, 0, &status);
+ char* const name = abi::__cxa_demangle(fun, 0, 0, &status);
if (name == NULL)
return fun;
const string s = name;
@@ -295,12 +283,12 @@ const string demangleFrame(const char* f
/**
* Format a backtrace frame.
*/
-const char* formatFrameFile(const char* file) {
- const char* s = strrchr(file, '/');
+inline const char* const formatFrameFile(const char* const file) {
+ const char* const s = strrchr(file, '/');
return s == NULL? file : s + 1;
}
-const string formatFrame(const char* symbol) {
+inline const string formatFrame(const char* const symbol) {
#ifdef __clang__
// Mac OS X CLang/LLVM stack frame format
// 0 kernel-test 0x000000010d440179 _ZN7tuscany9mkfailureINS_5valueENS_6stringEiEEKNS_8failableIT_T0_T1_EERKS5_RKS6_b + 265
@@ -349,7 +337,7 @@ const string formatFrame(const char* sym
/**
* Log backtrace frames.
*/
-const bool logFrames(char** symbols, const int frames, const bool log) {
+inline const bool logFrames(char** const symbols, const int frames, const bool log) {
if (frames == 0)
return true;
#ifdef WANT_MAINTAINER_LOG
@@ -364,26 +352,30 @@ const bool logFrames(char** symbols, con
/**
* Log a backtrace.
*/
-const bool logBacktrace(void** callstack, const int frames, const bool log) {
+inline const bool logBacktrace(void** const callstack, const int frames, const bool log) {
char** symbols = backtrace_symbols(callstack, frames);
logFrames(symbols, frames, log);
free(symbols);
return true;
}
+#endif
+
/**
* Returns a failable monad with a failure in it.
*/
-template<typename V, typename F, typename C> const failable<V, F, C> mkfailure(const F& f, const C& c, const bool log = true) {
+template<typename V, typename F, typename C> inline const failable<V, F, C> mkfailure(const F& f, const C& c, const bool log = true) {
#ifdef WANT_MAINTAINER_LOG
if (!log) {
// Log the failure
debug(f, "failable::mkfailure");
+#ifdef WANT_MAINTAINER_BACKTRACE
// Log the call stack
void* callstack[16];
const int frames = backtrace(callstack, 16);
logBacktrace(callstack, frames, log);
+#endif
}
#endif
if (log) {
@@ -393,63 +385,65 @@ template<typename V, typename F, typenam
// Log the failure
cfailure << "failable::mkfailure: " << f << " : " << c << endl;
+#ifdef WANT_MAINTAINER_BACKTRACE
// Print the call stack
void* callstack[16];
const int frames = backtrace(callstack, 16);
logBacktrace(callstack, frames, log);
+#endif
}
}
return failable<V, F, C>(false, f, c);
}
-template<typename V, typename F> const failable<V, F> mkfailure(const F& f, const int c = -1, const bool log = true) {
+template<typename V, typename F> inline const failable<V, F> mkfailure(const F& f, const int c = -1, const bool log = true) {
return mkfailure<V, F, int>(f, c, log);
}
-template<typename V> const failable<V> mkfailure(const char* f, const int c = -1, const bool log = true) {
+template<typename V> inline const failable<V> mkfailure(const char* f, const int c = -1, const bool log = true) {
return mkfailure<V, string, int>(string(f), c, log);
}
-template<typename V> const failable<V> mkfailure() {
+template<typename V> inline const failable<V> mkfailure() {
return failable<V, string>(false, string(), -1);
}
-template<typename V, typename F, typename C> const lambda<failable<V, F, C>(const V)> failure() {
+template<typename V, typename F, typename C> inline const lambda<const failable<V, F, C>(const V)> failure() {
return mkfailure<V, F, C>;
}
/**
* Convert a failable of a given type to a failable of another type.
*/
-template<typename V, typename F, typename C, typename X> const failable<V, F, C> mkfailure(const failable<X, F, C>& f, const bool log = true) {
+template<typename V, typename F, typename C, typename X> inline const failable<V, F, C> mkfailure(const failable<X, F, C>& f, const bool log = false) {
return mkfailure<V, F, C>(reason(f), rcode(f), log);
}
/**
* Returns true if the monad contains a content.
*/
-template<typename V, typename F, typename C> const bool hasContent(const failable<V, F, C>& m) {
+template<typename V, typename F, typename C> inline const bool hasContent(const failable<V, F, C>& m) {
return m.hasv;
}
/**
* Returns the content of a failable monad.
*/
-template<typename V, typename F, typename C> const V content(const failable<V, F, C>& m) {
+template<typename V, typename F, typename C> inline const V content(const failable<V, F, C>& m) {
return m.v;
}
/**
* Returns the reason for failure of a failable monad.
*/
-template<typename V, typename F, typename C> const F reason(const failable<V, F, C>& m) {
+template<typename V, typename F, typename C> inline const F reason(const failable<V, F, C>& m) {
return m.f;
}
/**
* Returns the reason code for failure of a failable monad.
*/
-template<typename V, typename F, typename C> const C rcode(const failable<V, F, C>& m) {
+template<typename V, typename F, typename C> inline const C rcode(const failable<V, F, C>& m) {
return m.c;
}
@@ -458,14 +452,14 @@ template<typename V, typename F, typenam
* if present, or does nothing if there's no value and a failure instead.
*/
template<typename R, typename FR, typename XR, typename V, typename FV, typename XV>
-const failable<R, FR, XR> operator>>(const failable<V, FV, XV>& m, const lambda<failable<R, FR, XR>(const V)>& f) {
+inline const failable<R, FR, XR> operator>>(const failable<V, FV, XV>& m, const lambda<const failable<R, FR, XR>(const V)>& f) {
if (!hasContent(m))
return m;
return f(content(m));
}
template<typename R, typename FR, typename XR, typename V, typename FV, typename XV>
-const failable<R, FR, XR> operator>>(const failable<V, FV, XV>& m, const failable<R, FR, XR> (* const f)(const V)) {
+inline const failable<R, FR, XR> operator>>(const failable<V, FV, XV>& m, const failable<R, FR, XR> (* const f)(const V)) {
if (!hasContent(m))
return m;
return f(content(m));
@@ -476,30 +470,27 @@ const failable<R, FR, XR> operator>>(con
*/
template<typename S, typename V> class scp {
public:
- scp(const S& s, const V& v) : s(s), v(v) {
+ inline scp(const S& s, const V& v) : s(s), v(v) {
}
- operator const S() const {
+ inline operator const S() const {
return s;
}
- operator const V() const {
+ inline operator const V() const {
return v;
}
- const scp<S, V>& operator=(const scp<S, V>& p) {
- if(this == &p)
- return *this;
- s = p.s;
- v = p.v;
- return *this;
+ inline scp(const scp<S, V>& p) : s(p.s), v(p.v) {
}
- const bool operator!=(const scp<S, V>& p) const {
+ scp<S, V>& operator=(const scp<S, V>& p) = delete;
+
+ inline const bool operator!=(const scp<S, V>& p) const {
return !this->operator==(p);
}
- const bool operator==(const scp<S, V>& p) const {
+ inline const bool operator==(const scp<S, V>& p) const {
if (this == &p)
return true;
return s == p.s && v == p.v;
@@ -516,14 +507,14 @@ private:
/**
* Returns the state of a state-content pair.
*/
-template<typename S, typename V> const S scpstate(const scp<S, V>& m) {
+template<typename S, typename V> inline const S scpstate(const scp<S, V>& m) {
return m.s;
}
/**
* Returns the content of a state-content pair.
*/
-template<typename S, typename V> const S content(const scp<S, V>& m) {
+template<typename S, typename V> inline const S content(const scp<S, V>& m) {
return m.v;
}
@@ -532,38 +523,36 @@ template<typename S, typename V> const S
*/
template<typename S, typename V> class state {
public:
- state(const lambda<scp<S, V>(const S)>& f) : f(f) {
+ inline state(const lambda<const scp<S, V>(const S)>& f) : f(f) {
}
- const scp<S, V> operator()(const S& s) const {
+ inline const scp<S, V> operator()(const S& s) const {
return f(s);
}
- const state<S, V>& operator=(const state<S, V>& m) {
- if(this == &m)
- return *this;
- f = m.f;
- return *this;
+ inline state(const state<S, V>& m) : f(m.f) {
}
- const bool operator!=(const state<S, V>& m) const {
+ state<S, V>& operator=(const state<S, V>& m) = delete;
+
+ inline const bool operator!=(const state<S, V>& m) const {
return !this->operator==(m);
}
- const bool operator==(const state<S, V>& m) const {
+ inline const bool operator==(const state<S, V>& m) const {
if (this == &m)
return true;
return f == m.f;
}
private:
- const lambda<scp<S, V>(const S)> f;
+ const lambda<const scp<S, V>(const S)> f;
};
/**
* Write a state monad to a stream.
*/
-template<typename S, typename V> ostream& operator<<(ostream& out, const state<S, V>& m) {
+template<typename S, typename V> inline ostream& operator<<(ostream& out, const state<S, V>& m) {
const S s = m;
const V v = m;
out << '(' << s << ' ' << v << ')';
@@ -573,17 +562,10 @@ template<typename S, typename V> ostream
/**
* Return a state monad carrying a result content.
*/
-template<typename S, typename V> struct returnState {
- const V v;
- returnState(const V& v) : v(v) {
- }
- const scp<S, V> operator()(const S& s) const {
+template<typename S, typename V> inline const state<S, V> result(const V& v) {
+ return state<S, V>([v](const S& s) -> const scp<S, V> {
return scp<S, V>(s, v);
- }
-};
-
-template<typename S, typename V> const state<S, V> result(const V& v) {
- return state<S, V>(returnState<S, V>(v));
+ });
}
/**
@@ -591,7 +573,7 @@ template<typename S, typename V> const s
* A transformer function takes a state and returns an scp pair carrying a content and a
* new (transformed) state.
*/
-template<typename S, typename V> const state<S, V> transformer(const lambda<scp<S, V>(const S)>& f) {
+template<typename S, typename V> inline const state<S, V> transformer(const lambda<const scp<S, V>(const S)>& f) {
return state<S, V>(f);
}
@@ -599,28 +581,19 @@ template<typename S, typename V> const s
* Bind a function to a state monad. The function takes a content and returns a state
* monad carrying a return content.
*/
-template<typename S, typename A, typename B> struct stateBind {
- const state<S, A> st;
- const lambda<state<S, B>(const A)>f;
-
- stateBind(const state<S, A>& st, const lambda<state<S, B>(const A)>& f) : st(st), f(f) {
- }
-
- const scp<S, B> operator()(const S& is) const {
+template<typename S, typename A, typename B>
+inline const state<S, B> operator>>(const state<S, A>& st, const lambda<const state<S, B>(const A)>& f) {
+ const lambda<const scp<S, B>(const S&)> stateBind = [st, f](const S& is) -> const scp<S, B> {
const scp<S, A> iscp = st(is);
const state<S, B> m = f((A)iscp);
return m((S)iscp);
- }
-};
-
-template<typename S, typename A, typename B>
-const state<S, B> operator>>(const state<S, A>& st, const lambda<state<S, B>(const A)>& f) {
- return state<S, B>(stateBind<S, A , B>(st, f));
+ };
+ return state<S, B>(stateBind);
}
template<typename S, typename A, typename B>
-const state<S, B> operator>>(const state<S, A>& st, const state<S, B> (* const f)(const A)) {
- return state<S, B>(stateBind<S, A , B>(st, f));
+inline const state<S, B> operator>>(const state<S, A>& st, const state<S, B> (* const f)(const A)) {
+ return st >> lambda<const state<S, B>(const A)>(f);
}
}
Modified: tuscany/sca-cpp/trunk/kernel/parallel-test.cpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/parallel-test.cpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/parallel-test.cpp (original)
+++ tuscany/sca-cpp/trunk/kernel/parallel-test.cpp Tue Dec 11 03:51:03 2012
@@ -35,84 +35,58 @@ namespace tuscany {
int inci = 0;
-struct incPerf {
- incPerf() {
- }
- const bool operator()() const {
- inci = inci + 1;
- return true;
- }
-};
-
const gc_ptr<int> tlsic() {
- gc_ptr<int> i = new (gc_new<int>()) int();
+ const gc_ptr<int> i = new (gc_new<int>()) int();
*i = 0;
return i;
}
const perthread_ptr<int> tlsi(tlsic);
-struct tlsPerf {
- tlsPerf() {
- }
- const bool operator()() const {
- *tlsi = *tlsi + 1;
- return true;
- }
-};
-
#ifdef WANT_THREADS
int addi = 0;
-
-struct addAndFetchPerf {
- addAndFetchPerf() {
- }
- const bool operator()() const {
- __sync_add_and_fetch(&addi, 1);
- return true;
- }
-};
-
int muxi = 0;
-struct mutexPerf {
- pthread_mutex_t* mutex;
- mutexPerf(pthread_mutex_t* mutex) : mutex(mutex) {
- }
- const bool operator()() const {
- pthread_mutex_lock(mutex);
- muxi = muxi + 1;
- pthread_mutex_unlock(mutex);
- return true;
- }
-};
-
#endif
-bool testAtomicPerf() {
+const bool testAtomicPerf() {
const int count = 100000;
{
- const lambda<bool()> l = incPerf();
+ const blambda l = []() -> const bool {
+ inci = inci + 1;
+ return true;
+ };
cout << "Non-atomic inc test " << time(l, 1000, count) << " ms" << endl;
assert(inci == count + 1000);
}
#ifdef WANT_THREADS
{
- const lambda<bool()> l = addAndFetchPerf();
+ const blambda l = []() -> const bool {
+ __sync_add_and_fetch(&addi, 1);
+ return true;
+ };
cout << "Atomic inc test " << time(l, 1000, count) << " ms" << endl;
assert(addi == count + 1000);
}
{
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, NULL);
- const lambda<bool()> l = mutexPerf(&mutex);
+ const blambda l = [&mutex]() -> const bool {
+ pthread_mutex_lock(&mutex);
+ muxi = muxi + 1;
+ pthread_mutex_unlock(&mutex);
+ return true;
+ };
cout << "Locked inc test " << time(l, 1000, count) << " ms" << endl;
assert(muxi == count + 1000);
pthread_mutex_destroy(&mutex);
}
#endif
{
- const lambda<bool()> l = tlsPerf();
+ const blambda l = []() -> const bool {
+ *tlsi = *tlsi + 1;
+ return true;
+ };
cout << "Thread local inc test " << time(l, 1000, count) << " ms" << endl;
assert(*tlsi == count + 1000);
}
@@ -130,7 +104,7 @@ const int mtsquare(const int x) {
const list<future<int> > submitSquares(worker& w, const int max, const int i) {
if (i == max)
return list<future<int> >();
- const lambda<int()> func = curry(lambda<int(const int)> (mtsquare), i);
+ const lambda<const int()> func = curry(lambda<const int(const int)> (mtsquare), i);
return cons(submit(w, func), submitSquares(w, max, i + 1));
}
@@ -143,7 +117,7 @@ bool checkSquareResults(const list<futur
}
const gc_ptr<unsigned long> tlsvc() {
- gc_ptr<unsigned long> i = new (gc_new<unsigned long>()) unsigned long();
+ const gc_ptr<unsigned long> i = new (gc_new<unsigned long>()) unsigned long();
*i = 0l;
return i;
}
@@ -181,11 +155,11 @@ const bool unblockWorkers(wqueue<bool>&
const list<future<long int> > submitTLSSets(worker& w, wqueue<bool>& wq, wqueue<bool>& xq, const int max, const int i) {
if (i == max)
return list<future<long int> >();
- const lambda<long int()> func = curry(lambda<long int(gc_ptr<wqueue<bool>>, gc_ptr<wqueue<bool>>)>(tlsset), (gc_ptr<wqueue<bool>>)&wq, (gc_ptr<wqueue<bool>>)&xq);
+ const lambda<const long int()> func = curry(lambda<const long int(gc_ptr<wqueue<bool>>, gc_ptr<wqueue<bool>>)>(tlsset), (gc_ptr<wqueue<bool>>)&wq, (gc_ptr<wqueue<bool>>)&xq);
return cons(submit(w, func), submitTLSSets(w, wq, xq, max, i + 1));
}
-bool checkTLSSets(const list<future<long int> > s) {
+const bool checkTLSSets(const list<future<long int> > s) {
if (isNil(s))
return true;
assert(car(s) == 0);
@@ -195,22 +169,22 @@ bool checkTLSSets(const list<future<long
const list<future<bool> > submitTLSChecks(worker& w, wqueue<bool>& wq, wqueue<bool>& xq, const int max, const int i) {
if (i == max)
return list<future<bool> >();
- const lambda<bool()> func = curry(lambda<bool(gc_ptr<wqueue<bool>>, gc_ptr<wqueue<bool>>)>(tlscheck), (gc_ptr<wqueue<bool>>)&wq, (gc_ptr<wqueue<bool>>)&xq);
+ const blambda func = curry(lambda<const bool(gc_ptr<wqueue<bool>>, gc_ptr<wqueue<bool>>)>(tlscheck), (gc_ptr<wqueue<bool>>)&wq, (gc_ptr<wqueue<bool>>)&xq);
return cons(submit(w, func), submitTLSChecks(w, wq, xq, max, i + 1));
}
-bool checkTLSResults(const list<future<bool> > r) {
+const bool checkTLSResults(const list<future<bool> > r) {
if (isNil(r))
return true;
assert(car(r) == true);
return checkTLSResults(cdr(r));
}
-bool testWorker() {
+const bool testWorker() {
const int max = 100;
worker w(max);
{
- const lambda<int()> func = curry(lambda<int(const int)> (mtsquare), 2);
+ const lambda<const int()> func = curry(lambda<const int(const int)> (mtsquare), 2);
assert(submit(w, func) == 4);
}
{
@@ -245,7 +219,7 @@ bool testWorker() {
}
int main() {
- tuscany::gc_scoped_pool p;
+ const tuscany::gc_scoped_pool p;
tuscany::cout << "Testing..." << tuscany::endl;
tuscany::testAtomicPerf();
Modified: tuscany/sca-cpp/trunk/kernel/parallel.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/parallel.hpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/parallel.hpp (original)
+++ tuscany/sca-cpp/trunk/kernel/parallel.hpp Tue Dec 11 03:51:03 2012
@@ -39,7 +39,7 @@ namespace tuscany {
/**
* Returns the current process id.
*/
-unsigned long processId() {
+inline const unsigned long processId() noexcept {
return (unsigned long)getpid();
}
@@ -48,7 +48,7 @@ unsigned long processId() {
/**
* Returns the current thread id.
*/
-unsigned long threadId() {
+inline const unsigned long threadId() noexcept{
return (unsigned long)pthread_self();
}
@@ -60,20 +60,20 @@ template<typename T> class future {
private:
template<typename X> class futureValue {
public:
- futureValue() : hasValue(false) {
+ inline futureValue() noexcept : hasValue(false) {
pthread_mutex_init(&valueMutex, NULL);
pthread_cond_init(&valueCond, NULL);
}
- futureValue(const futureValue& fv) : valueMutex(fv.valueMutex), valueCond(fv.valueCond), hasValue(fv.hasValue), value(fv.value) {
+ inline futureValue(const futureValue& fv) noexcept : valueMutex(fv.valueMutex), valueCond(fv.valueCond), hasValue(fv.hasValue), value(fv.value) {
}
- ~futureValue() {
+ inline ~futureValue() noexcept {
//pthread_mutex_destroy(&valueMutex);
//pthread_cond_destroy(&valueCond);
}
- bool set(const T& v) {
+ inline const bool set(const T& v) noexcept {
pthread_mutex_lock(&valueMutex);
if(hasValue) {
pthread_mutex_unlock(&valueMutex);
@@ -86,7 +86,7 @@ private:
return true;
}
- const T get() {
+ inline const T get() noexcept {
pthread_mutex_lock(&valueMutex);
while(!hasValue) {
pthread_cond_wait(&valueCond, &valueMutex);
@@ -100,37 +100,32 @@ private:
pthread_mutex_t valueMutex;
pthread_cond_t valueCond;
bool hasValue;
- X value;
+ gc_mutable_ref<X> value;
};
- gc_ptr<futureValue<T> > fvalue;
+ const gc_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);
+ template<typename X> friend const X get(const future<X>& f) noexcept;
+ template<typename X> friend bool set(const future<X>& f, const X& v) noexcept;
public:
- future() : fvalue(new (gc_new<futureValue<T> >()) futureValue<T>()) {
+ inline future() noexcept : fvalue(new (gc_new<futureValue<T> >()) futureValue<T>()) {
}
- ~future() {
+ inline ~future() noexcept {
}
- future(const future& f) : fvalue(f.fvalue) {
+ inline future(const future& f) noexcept : fvalue(f.fvalue) {
}
- const future& operator=(const future& f) {
- if (&f == this)
- return *this;
- fvalue = f.fvalue;
- return *this;
- }
+ const future& operator=(const future& f) = delete;
- const future& operator=(const T& v) const {
+ inline const future& operator=(const T& v) const noexcept {
fvalue->set(v);
return *this;
}
- operator const T() const {
+ inline operator const T() const noexcept {
return fvalue->get();
}
};
@@ -140,16 +135,16 @@ public:
*/
template<typename T> class wqueue {
public:
- wqueue(size_t max) : max(max), size(0), tail(0), head(0), values(new (gc_anew<T>(max)) T[max]) {
+ inline wqueue(size_t max) noexcept : max(max), size(0), tail(0), head(0), values(new (gc_anew<gc_mutable_ref<T> >(max)) gc_mutable_ref<T>[max]) {
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&full, NULL);
pthread_cond_init(&empty, NULL);
}
- wqueue(const wqueue& wq) : max(wq.max), size(wq.size), tail(wq.tail), head(wq.head), mutex(wq.mutex), full(wq.full), empty(wq.empty), values(wq.values) {
+ inline wqueue(const wqueue& wq) noexcept : max(wq.max), size(wq.size), tail(wq.tail), head(wq.head), mutex(wq.mutex), full(wq.full), empty(wq.empty), values(wq.values) {
}
- ~wqueue() {
+ inline ~wqueue() {
//pthread_mutex_destroy(&mutex);
//pthread_cond_destroy(&full);
//pthread_cond_destroy(&empty);
@@ -163,16 +158,16 @@ private:
pthread_mutex_t mutex;
pthread_cond_t full;
pthread_cond_t empty;
- gc_ptr<T> values;
+ gc_ptr<gc_mutable_ref<T> > values;
- template<typename X> friend const size_t enqueue(wqueue<X>& q, const X& v);
- template<typename X> friend const X dequeue(wqueue<X>& q);
+ template<typename X> friend const size_t enqueue(wqueue<X>& q, const X& v) noexcept;
+ template<typename X> friend const X dequeue(wqueue<X>& q) noexcept;
};
/**
* Adds an element to the tail of the queue.
*/
-template<typename T> const size_t enqueue(wqueue<T>&q, const T& v) {
+template<typename T> inline const size_t enqueue(wqueue<T>&q, const T& v) noexcept {
pthread_mutex_lock(&q.mutex);
while(q.size == q.max)
pthread_cond_wait(&q.full, &q.mutex);
@@ -187,7 +182,7 @@ template<typename T> const size_t enqueu
/**
* Returns the element at the head of the queue.
*/
-template<typename T> const T dequeue(wqueue<T>& q) {
+template<typename T> inline const T dequeue(wqueue<T>& q) noexcept {
pthread_mutex_lock(&q.mutex);
while(q.size == 0)
pthread_cond_wait(&q.empty, &q.mutex);
@@ -202,13 +197,13 @@ template<typename T> const T dequeue(wqu
/**
* The worker thread function.
*/
-void *workerThreadFunc(void *arg) {
+inline void* workerThreadFunc(void* arg) noexcept {
int ost;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &ost);
int ot;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &ot);
- wqueue<lambda<bool()> >* work = reinterpret_cast<wqueue<lambda<bool()> >*>(arg);
+ wqueue<blambda >* work = reinterpret_cast<wqueue<blambda >*>(arg);
while(dequeue(*work)())
;
return NULL;
@@ -217,7 +212,7 @@ void *workerThreadFunc(void *arg) {
/**
* Returns a list of worker threads.
*/
-const list<pthread_t> workerThreads(wqueue<lambda<bool()> >& wqueue, const size_t count) {
+inline const list<pthread_t> workerThreads(wqueue<blambda >& wqueue, const size_t count) noexcept {
if (count == 0)
return list<pthread_t>();
pthread_t thread;
@@ -235,32 +230,32 @@ private:
// copies of the queue and thread pool when a worker is copied
class sharedWorker {
public:
- sharedWorker(size_t max) : work(wqueue<lambda<bool()> >(max)), threads(workerThreads(work, max)) {
+ inline sharedWorker(size_t max) noexcept : work(wqueue<blambda >(max)), threads(workerThreads(work, max)) {
}
- wqueue<lambda<bool()> > work;
+ wqueue<blambda > work;
const list<pthread_t> threads;
};
public:
- worker(size_t max) : w(*(new (gc_new<sharedWorker>()) sharedWorker(max))) {
+ inline worker(size_t max) noexcept : w(*(new (gc_new<sharedWorker>()) sharedWorker(max))) {
}
- worker(const worker& wk) : w(wk.w) {
+ inline worker(const worker& wk) noexcept : w(wk.w) {
}
private:
sharedWorker& w;
- template<typename X> friend const future<X> submit(worker& w, const lambda<X()>& func);
- friend const bool shutdown(worker& w);
- friend const bool cancel(worker& w);
+ template<typename X> friend const future<X> submit(const worker& w, const lambda<const X()>& func) noexcept;
+ friend const bool shutdown(const worker& w) noexcept;
+ friend const bool cancel(const worker& w) noexcept;
};
/**
* Function used to wrap work submitted to a worker.
*/
-template<typename R> bool submitFunc(const lambda<R()>& func, const future<R>& fut) {
+template<typename R> inline const bool submitFunc(const lambda<const R()>& func, const future<R>& fut) noexcept {
fut = func();
return true;
}
@@ -268,9 +263,9 @@ template<typename R> bool submitFunc(con
/**
* Submits work to a worker.
*/
-template<typename R> const future<R> submit(worker& w, const lambda<R()>& func) {
+template<typename R> inline const future<R> submit(const worker& w, const lambda<const R()>& func) noexcept {
const future<R> fut;
- const lambda<bool()> f = curry(lambda<bool(const lambda<R()>, future<R>)>(submitFunc<R>), func, fut);
+ const blambda f = curry(lambda<const bool(const lambda<const R()>, future<R>)>(submitFunc<R>), func, fut);
enqueue(w.w.work, f);
return fut;
}
@@ -278,7 +273,7 @@ template<typename R> const future<R> sub
/**
* Enqueues shutdown requests.
*/
-const bool shutdownEnqueue(const list<pthread_t>& threads, wqueue<lambda<bool()> >& work) {
+inline const bool shutdownEnqueue(const list<pthread_t>& threads, wqueue<blambda>& work) noexcept {
if (isNil(threads))
return true;
enqueue(work, result(false));
@@ -288,7 +283,7 @@ const bool shutdownEnqueue(const list<pt
/**
* Waits for shut down threads to terminate.
*/
-const bool shutdownJoin(const list<pthread_t>& threads) {
+inline const bool shutdownJoin(const list<pthread_t>& threads) noexcept {
if (isNil(threads))
return true;
pthread_join(car(threads), NULL);
@@ -298,7 +293,7 @@ const bool shutdownJoin(const list<pthre
/**
* Shutdown a worker.
*/
-const bool shutdown(worker& w) {
+inline const bool shutdown(const worker& w) noexcept {
shutdownEnqueue(w.w.threads, w.w.work);
shutdownJoin(w.w.threads);
return true;
@@ -307,14 +302,14 @@ const bool shutdown(worker& w) {
/**
* Cancel a worker.
*/
-const bool cancel(const list<pthread_t>& threads) {
+inline const bool cancel(const list<pthread_t>& threads) noexcept {
if (isNil(threads))
return true;
pthread_cancel(car(threads));
return cancel(cdr(threads));
}
-const bool cancel(worker& w) {
+inline const bool cancel(const worker& w) noexcept {
cancel(w.w.threads);
return true;
}
@@ -324,7 +319,7 @@ const bool cancel(worker& w) {
/**
* Returns the current thread id.
*/
-unsigned long threadId() {
+inline const unsigned long threadId() noexcept {
return 0;
}
@@ -335,114 +330,106 @@ unsigned long threadId() {
*/
template<typename T> class perthread_ptr {
public:
- perthread_ptr() : key(createkey()), owner(true), cl(lambda<gc_ptr<T>()>()), managed(false) {
+ inline perthread_ptr() noexcept : key(createkey()), owner(true), cl(lambda<const gc_ptr<T>()>()), managed(false) {
}
- perthread_ptr(const lambda<gc_ptr<T>()>& cl) : key(createkey()), owner(true), cl(cl), managed(true) {
+ inline perthread_ptr(const lambda<const gc_ptr<T>()>& cl) noexcept : key(createkey()), owner(true), cl(cl), managed(true) {
}
- ~perthread_ptr() {
+ inline ~perthread_ptr() noexcept {
if (owner)
deletekey(key);
}
- perthread_ptr(const perthread_ptr& c) : key(c.key), owner(false), cl(c.cl), managed(c.managed) {
+ inline perthread_ptr(const perthread_ptr& c) noexcept : key(c.key), owner(false), cl(c.cl), managed(c.managed) {
}
- perthread_ptr& operator=(const perthread_ptr& r) throw() {
- if(this == &r)
- return *this;
- key = r.key;
- owner = false;
- cl = r.cl;
- managed = r.managed;
- return *this;
- }
+ perthread_ptr& operator=(const perthread_ptr& r) = delete;
- const perthread_ptr& operator=(const gc_ptr<T>& v) {
+ inline const perthread_ptr& operator=(const gc_ptr<T>& v) const noexcept {
set(v);
return *this;
}
- const perthread_ptr& operator=(T* v) {
+ inline const perthread_ptr& operator=(T* const v) const noexcept {
set(v);
return *this;
}
- const bool operator==(const gc_ptr<T>& r) const throw() {
+ inline const bool operator==(const gc_ptr<T>& r) const noexcept {
return get() == r;
}
- const bool operator==(T* p) const throw() {
+ inline const bool operator==(const T* const p) const noexcept {
return get() == p;
}
- const bool operator!=(const gc_ptr<T>& r) const throw() {
+ inline const bool operator!=(const gc_ptr<T>& r) const noexcept {
return !this->operator==(r);
}
- const bool operator!=(T* p) const throw() {
+ inline const bool operator!=(const T* const p) const noexcept {
return !this->operator==(p);
}
- T& operator*() const throw() {
+ inline T& operator*() const noexcept {
return *get();
}
- T* operator->() const throw() {
+ inline T* const operator->() const noexcept {
return get();
}
- operator gc_ptr<T>() const {
+ inline operator gc_ptr<T>() const {
return get();
}
- operator T*() const {
+ inline operator T* const () const {
return get();
}
private:
#ifdef WANT_THREADS
- pthread_key_t createkey() {
+ inline const pthread_key_t createkey() noexcept {
pthread_key_t k;
pthread_key_create(&k, NULL);
return k;
}
- bool deletekey(pthread_key_t k) {
+ inline const bool deletekey(pthread_key_t k) noexcept {
pthread_key_delete(k);
return true;
}
- bool set(const gc_ptr<T>& v) {
+ inline const bool set(const gc_ptr<T>& v) const noexcept {
pthread_setspecific(key, (T*)v);
return true;
}
- gc_ptr<T> get() const {
- const gc_ptr<T> v = static_cast<T*>(pthread_getspecific(key));
+ inline const gc_ptr<T> get() const noexcept {
+ const gc_ptr<T> v = (T*)(pthread_getspecific(key));
if (v != NULL || !managed)
return v;
const gc_ptr<T> nv = cl();
- pthread_setspecific(key, nv);
+ pthread_setspecific(key, (T*)nv);
return nv;
}
#else
- gc_ptr<gc_ptr<T> > createkey() {
+ inline const gc_ptr<gc_ptr<T> > createkey() noexcept {
return new (gc_new<gc_ptr<T> >()) gc_ptr<T>();
}
- bool deletekey(unused gc_ptr<gc_ptr<T> > k) {
+ inline const bool deletekey(unused gc_ptr<gc_ptr<T> > k) noexcept {
return true;
}
- bool set(const gc_ptr<T>& v) {
+ inline const bool set(const gc_ptr<T>& v) const noexcept {
*key = v;
return true;
}
- gc_ptr<T> get() const {
+ inline const gc_ptr<T> get() const noexcept {
if (*key != NULL || !managed)
return *key;
*key = cl();
@@ -452,14 +439,14 @@ private:
#endif
#ifdef WANT_THREADS
- pthread_key_t key;
+ const pthread_key_t key;
#else
- gc_ptr<gc_ptr<T> >key;
+ const gc_ptr<gc_ptr<T> >key;
#endif
- bool owner;
- lambda<const gc_ptr<T>()> cl;
- bool managed;
+ const bool owner;
+ const lambda<const gc_ptr<T>()> cl;
+ const bool managed;
};
}
Modified: tuscany/sca-cpp/trunk/kernel/perf.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/perf.hpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/perf.hpp (original)
+++ tuscany/sca-cpp/trunk/kernel/perf.hpp Tue Dec 11 03:51:03 2012
@@ -37,21 +37,15 @@ namespace tuscany
/**
* Measure the time required to perform a function in msec.
*/
-struct timeLambda {
- const lambda<bool()> f;
- timeLambda(const lambda<bool()>& f) : f(f) {
- }
- bool operator()(const long count) const {
+inline const double time(const blambda& f, const long warmup, const long count) {
+ struct timeval start;
+ struct timeval end;
+
+ const lambda<const bool(const long)> tl = [f](const long count) -> const bool {
for (long i = 0; i < count; i++)
f();
return true;
- }
-};
-
-const double time(const lambda<bool()>& f, const long warmup, const long count) {
- const lambda<bool(long)> tl = timeLambda(f);
- struct timeval start;
- struct timeval end;
+ };
tl(warmup);
gettimeofday(&start, NULL);
@@ -62,13 +56,13 @@ const double time(const lambda<bool()>&
return (double)t / (double)count;
}
-const unsigned long timems() {
+inline const unsigned long timems() {
struct timeval t;
gettimeofday(&t, NULL);
return (unsigned long)(t.tv_sec * 1000 + t.tv_usec / 1000);
}
-const unsigned long timens() {
+inline const unsigned long timens() {
struct timeval t;
gettimeofday(&t, NULL);
return (unsigned long)(t.tv_sec * 1000000000 + t.tv_usec * 1000);
Modified: tuscany/sca-cpp/trunk/kernel/sstream.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/sstream.hpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/sstream.hpp (original)
+++ tuscany/sca-cpp/trunk/kernel/sstream.hpp Tue Dec 11 03:51:03 2012
@@ -38,14 +38,14 @@ namespace tuscany {
/**
* Instrumentable memcpy.
*/
-void* stream_memcpy(void* t, const void* s, const size_t n) {
+inline void* stream_memcpy(void* t, const void* s, const size_t n) {
return memcpy(t, s, n);
}
/**
* Write a list of strings into a buffer.
*/
-const bool writeList(const list<string>& l, char* buf) {
+const bool writeList(const list<string>& l, char* const buf) {
if (isNil(l))
return true;
const string c = car(l);
@@ -59,63 +59,108 @@ const bool writeList(const list<string>&
*/
class ostringstream : public ostream {
public:
- ostringstream() : len(0) {
+ inline ostringstream() : len(0), accumbuf(NULL), accumlen(0), buf() {
}
- ~ostringstream() {
+ inline ~ostringstream() {
}
- ostringstream(const ostringstream& os) {
- len = os.len;
- buf = os.buf;
+ inline ostringstream(const ostringstream& os) : len(os.len), accumbuf(os.accumbuf), accumlen(os.accumlen), buf(os.buf) {
}
- ostringstream& vprintf(const char* fmt, ...) {
+ inline ostringstream& vprintf(const char* const fmt, ...) {
va_list args;
- string s;
va_start (args, fmt);
- s.len = vsnprintf(NULL, 0, fmt, args);
- s.buf = gc_cnew(s.len + 1);
+ const size_t l = vsnprintf(NULL, 0, fmt, args);
+ if (l <= 256) {
+ char c[256];
+ va_start (args, fmt);
+ vsnprintf(c, l + 1, fmt, args);
+ va_end (args);
+ accumulate(c, l);
+ return *this;
+ }
+ spill();
+ char* const sbuf = gc_cnew(l + 1);
va_start (args, fmt);
- vsnprintf(s.buf, s.len + 1, fmt, args);
- buf = cons(s, buf);
- len += s.len;
+ vsnprintf(sbuf, l + 1, fmt, args);
va_end (args);
+ buf = cons(string(sbuf, l, false), (const list<string>)buf);
+ len += l;
return *this;
}
- ostringstream& write(const string& s) {
- buf = cons(s, buf);
+ inline ostringstream& write(const string& s) {
+ if (s.len <= 256) {
+ accumulate(s.buf, s.len);
+ return *this;
+ }
+ spill();
+ buf = cons(s, (const list<string>)buf);
len += s.len;
return *this;
}
- ostringstream& flush() {
+ inline ostringstream& write(const char c) {
+ accumulate(&c, 1);
+ return *this;
+ }
+
+ inline ostringstream& flush() {
+ spill();
return *this;
}
private:
- const string str() {
- if (isNil(buf))
- return string();
- string s;
- s.len = len;
- s.buf = gc_cnew(s.len + 1);
- writeList(buf, s.buf + len);
- s.buf[s.len] = '\0';
- return s;
+ inline const string str() {
+ spill();
+ if (isNil((const list<string>)buf))
+ return emptyString;
+ char* const sbuf = gc_cnew(len + 1);
+ writeList(buf, sbuf + len);
+ sbuf[len] = '\0';
+ return string(sbuf, len, false);
+ }
+
+ inline const bool accumulate(const char* const c, const size_t l) {
+ if (accumbuf == NULL)
+ accumbuf = gc_cnew(65);
+ for(size_t i = 0; i < l; i++) {
+ accumbuf[accumlen] = c[i];
+ accumlen++;
+ if (accumlen == 64) {
+ accumbuf[accumlen] = '\0';
+ buf = cons(string(accumbuf, accumlen, false), (const list<string>)buf);
+ accumbuf = i < l? gc_cnew(65) : NULL;
+ accumlen = 0;
+ }
+ }
+ accumbuf[accumlen] = '\0';
+ len += l;
+ return true;
+ }
+
+ inline const bool spill() {
+ if (accumbuf == NULL)
+ return true;
+ buf = cons(string(accumbuf, accumlen), (const list<string>)buf);
+ accumbuf = NULL;
+ accumlen = 0;
+ return true;
}
friend const string str(ostringstream& os);
size_t len;
- list<string> buf;
+ char* accumbuf;
+ size_t accumlen;
+ gc_mutable_ref<list<string> > buf;
};
/**
* Return a string representation of a stream.
*/
-const string str(ostringstream& os) {
+inline const string str(ostringstream& os) {
return os.str();
}
@@ -124,23 +169,16 @@ const string str(ostringstream& os) {
*/
class istringstream : public istream {
public:
- istringstream(const string& s) {
- cur = 0;
- const size_t slen = length(s);
- len = slen;
- buf = c_str(s);
- }
+ inline istringstream(const string& s) : len(length(s)), cur(0), buf(c_str(s)) {
+ }
- ~istringstream() {
+ inline ~istringstream() {
}
- istringstream(const istringstream& is) {
- len = is.len;
- cur = is.cur;
- buf = is.buf;
+ inline istringstream(const istringstream& is) : len(is.len), cur(is.cur), buf(is.buf) {
}
- const size_t read(void* b, size_t size) {
+ inline const size_t read(void* const b, const size_t size) {
const size_t n = len - cur;
if (n == 0)
return 0;
@@ -154,30 +192,28 @@ public:
return n;
}
- const bool eof() {
+ inline const bool eof() {
return cur == len;
}
- const bool fail() {
+ inline const bool fail() {
return false;
}
- const int get() {
- if (eof())
+ inline const int get() {
+ if (cur == len)
return -1;
- const int c = buf[cur];
- cur += 1;
- return c;
+ return buf[cur++];
}
- const int peek() {
- if (eof())
+ inline const int peek() {
+ if (cur == len)
return -1;
return buf[cur];
}
private:
- size_t len;
+ const size_t len;
size_t cur;
const char* buf;
};
@@ -185,62 +221,52 @@ private:
/**
* Tokenize a string into a list of strings.
*/
-const list<string> tokenize(const char* sep, const string& str) {
- struct nested {
- static const list<string> tokenize(const char* sep, const size_t slen, const string& str, const size_t start) {
- if (start >= length(str))
- return list<string>();
- const size_t i = find(str, sep, start);
- if (i == length(str))
- return mklist(string(substr(str, start)));
- return cons(string(substr(str, start, i - start)), tokenize(sep, slen, str, i + slen));
- }
+inline const list<string> tokenize(const char* const sep, const string& str) {
+ const lambda<const list<string>(const char* const, const size_t, const string&, const size_t)> tokenize = [&tokenize](const char* const sep, const size_t slen, const string& str, const size_t start) -> const list<string> {
+ if (start >= length(str))
+ return list<string>();
+ const size_t i = find(str, sep, start);
+ if (i == length(str))
+ return mklist(string(substr(str, start)));
+ return cons(string(substr(str, start, i - start)), tokenize(sep, slen, str, i + slen));
};
- return nested::tokenize(sep, strlen(sep), str, 0);
+ return tokenize(sep, strlen(sep), str, 0);
}
/**
* Join a list of strings into a single string.
*/
-const string join(const char* sep, const list<string>& l) {
- struct nested {
- static ostringstream& join(const char* sep, const list<string>& l, ostringstream& os) {
- if (isNil(l))
- return os;
- os << car(l);
- if (!isNil(cdr(l)))
- os << sep;
- return join(sep, cdr(l), os);
- }
- };
+inline const string join(const char* const sep, const list<string>& l) {
ostringstream os;
- return str(nested::join(sep, l, os));
+ const lambda<ostringstream&(const char* const, const list<string>&, ostringstream&)> join = [&join](const char* const sep, const list<string>& l, ostringstream& os) -> ostringstream& {
+ if (isNil(l))
+ return os;
+ os << car(l);
+ if (!isNil(cdr(l)))
+ os << sep;
+ return join(sep, cdr(l), os);
+ };
+ return str(join(sep, l, os));
}
/**
* Returns a lazy list view of an input stream.
*/
-struct ilistRead{
- istream &is;
- ilistRead(istream& is) : is(is) {
- }
- const list<string> operator()() {
+inline const list<string> streamList(istream& is) {
+ const lambda<const list<string>()> ilistRead = [&is, &ilistRead]() -> const list<string> {
char buffer[1024];
const size_t n = read(is, buffer, sizeof(buffer));
if (n ==0)
return list<string>();
- return cons(string(buffer, n), (*this)());
- }
-};
-
-const list<string> streamList(istream& is) {
- return ilistRead(is)();
+ return cons(string(buffer, n), ilistRead());
+ };
+ return ilistRead();
}
/**
* Fragment the first element of a list of strings to fit the given max length.
*/
-const list<string> fragment(list<string> l, size_t max) {
+inline const list<string> fragment(const list<string>& l, const size_t max) {
const string s = car(l);
if (length(s) <= max)
return l;
@@ -250,13 +276,26 @@ const list<string> fragment(list<string>
/**
* Write a list of strings to an output stream.
*/
-ostream& write(const list<string>& l, ostream& os) {
+inline ostream& write(const list<string>& l, ostream& os) {
if(isNil(l))
return os;
os << car(l);
return write(cdr(l), os);
}
+/**
+ * Convert a list of strings to a string.
+ */
+inline const string write(const list<string>& l) {
+ if (isNil(l))
+ return emptyString;
+ if (isNil(cdr(l)))
+ return car(l);
+ ostringstream os;
+ write(l, os);
+ return str(os);
+}
+
}
#endif /* tuscany_sstream_hpp */
Modified: tuscany/sca-cpp/trunk/kernel/stream.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/stream.hpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/stream.hpp (original)
+++ tuscany/sca-cpp/trunk/kernel/stream.hpp Tue Dec 11 03:51:03 2012
@@ -38,66 +38,67 @@ namespace tuscany {
*/
class ostream {
public:
- virtual ostream& vprintf(const char* fmt, ...) = 0;
+ virtual ostream& vprintf(const char* const fmt, ...) = 0;
virtual ostream& write(const string& s) = 0;
+ virtual ostream& write(const char c) = 0;
virtual ostream& flush() = 0;
};
/**
* Flush a stream.
*/
-ostream& flush(ostream& os) {
+inline ostream& flush(ostream& os) {
return os.flush();
}
/**
* Write simple values to a stream.
*/
-ostream& operator<<(ostream& os, const char* v) {
+inline ostream& operator<<(ostream& os, const char* const v) {
return os.vprintf("%s", v);
}
-ostream& operator<<(ostream& os, const unsigned char* v) {
+inline ostream& operator<<(ostream& os, const unsigned char* const v) {
return os.vprintf("%s", v);
}
-ostream& operator<<(ostream& os, const char v) {
- return os.vprintf("%c", v);
+inline ostream& operator<<(ostream& os, const char v) {
+ return os.write(v);
}
-ostream& operator<<(ostream& os, const int v) {
+inline ostream& operator<<(ostream& os, const int v) {
return os.vprintf("%d", v);
}
-ostream& operator<<(ostream& os, const unsigned int v) {
+inline ostream& operator<<(ostream& os, const unsigned int v) {
return os.vprintf("%u", v);
}
-ostream& operator<<(ostream& os, const long int v) {
+inline ostream& operator<<(ostream& os, const long int v) {
return os.vprintf("%ld", v);
}
-ostream& operator<<(ostream& os, const long unsigned int v) {
+inline ostream& operator<<(ostream& os, const long unsigned int v) {
return os.vprintf("%lu", v);
}
-ostream& operator<<(ostream& os, const double v) {
+inline ostream& operator<<(ostream& os, const double v) {
return os.vprintf("%.10g", v);
}
-ostream& operator<<(ostream& os, const void* v) {
+inline ostream& operator<<(ostream& os, const void* const v) {
return os.vprintf("%p", v);
}
-ostream& operator<<(ostream& os, const string& v) {
+inline ostream& operator<<(ostream& os, const string& v) {
return os.write(v);
}
class stream_endl {
} endl;
-ostream& operator<<(ostream& os, unused const stream_endl e) {
- os.write("\n");
+inline ostream& operator<<(ostream& os, unused const stream_endl e) {
+ os.write('\n');
return os.flush();
}
@@ -106,7 +107,7 @@ ostream& operator<<(ostream& os, unused
*/
class istream {
public:
- virtual const size_t read(void* buf, size_t size) = 0;
+ virtual const size_t read(void* const buf, const size_t size) = 0;
virtual const bool eof() = 0;
virtual const bool fail() = 0;
virtual const int get() = 0;
@@ -116,40 +117,61 @@ public:
/**
* Read from an input stream.
*/
-const size_t read(istream& is, void * buf, size_t size) {
+inline const size_t read(istream& is, void* const buf, const size_t size) {
return is.read(buf, size);
}
/**
* Return true if the end of an input stream has been reached.
*/
-const bool eof(istream& is) {
+inline const bool eof(istream& is) {
return is.eof();
}
/**
* Return true if an input stream can't be accessed.
*/
-const bool fail(istream& is) {
+inline const bool fail(istream& is) {
return is.fail();
}
/**
* Read a character from a stream.
*/
-const int get(istream& is) {
+inline const int get(istream& is) {
return is.get();
}
/**
* Peek a character from a stream.
*/
-const int peek(istream& is) {
+inline const int peek(istream& is) {
return is.peek();
}
-template<typename T> ostream& operator<<(ostream& out, const gc_ptr<T>& p) {
- return out << p.ptr;
+#ifndef WANT_RAW_PTR
+
+/**
+ * Write a pointer.
+ */
+template<typename T> inline ostream& operator<<(ostream& out, const gc_ptr<T>& p) {
+ return out << (T*)p;
+}
+
+/**
+ * Write a mutable pointer.
+ */
+template<typename T> inline ostream& operator<<(ostream& out, const gc_mutable_ptr<T>& p) {
+ return out << (T*)p;
+}
+
+#endif
+
+/**
+ * Write a mutable reference.
+ */
+template<typename T> inline ostream& operator<<(ostream& out, const gc_mutable_ref<T>& r) {
+ return out << (T)r;
}
#ifdef WANT_MAINTAINER_LOG
@@ -159,35 +181,40 @@ template<typename T> ostream& operator<<
*/
class odebugstream : public ostream {
public:
- odebugstream() {
+ inline odebugstream() : buf() {
}
- odebugstream& vprintf(const char* fmt, ...) {
+ inline odebugstream& vprintf(const char* const fmt, ...) {
va_list args;
string s;
va_start (args, fmt);
- s.len = vsnprintf(NULL, 0, fmt, args);
- s.buf = gc_cnew(s.len + 1);
+ const size_t slen = vsnprintf(NULL, 0, fmt, args);
+ char* const sbuf = gc_cnew(slen + 1);
va_start (args, fmt);
- vsnprintf(s.buf, s.len + 1, fmt, args);
- buf = buf + s;
+ vsnprintf(sbuf, slen + 1, fmt, args);
+ buf = buf + string(sbuf, slen, false);
va_end (args);
return *this;
}
- odebugstream& write(const string& s) {
+ inline odebugstream& write(const string& s) {
buf = buf + s;
return *this;
}
- odebugstream& flush() {
+ inline odebugstream& write(const char c) {
+ buf = buf + c;
+ return *this;
+ }
+
+ inline odebugstream& flush() {
return *this;
}
private:
friend const string str(odebugstream& os);
- string buf;
+ gc_mutable_ref<string> buf;
};
const string str(odebugstream& os) {
Modified: tuscany/sca-cpp/trunk/kernel/string-test.cpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/string-test.cpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/string-test.cpp (original)
+++ tuscany/sca-cpp/trunk/kernel/string-test.cpp Tue Dec 11 03:51:03 2012
@@ -32,15 +32,15 @@
namespace tuscany {
-bool testCopies() {
+const bool testCopies() {
resetStringCopyCounters();
- string x("abcd");
+ const string x("abcd");
assert(checkStringCopyCounters(1));
resetStringCopyCounters();
- string y = string("abcd");
+ const string y = string("abcd");
assert(checkStringCopyCounters(1));
resetStringCopyCounters();
- unused string z = y;
+ unused const string z = y;
assert(checkStringCopyCounters(0));
resetStringCopyCounters();
const list<string> pl = list<string>() + "abcd" + "efgh";
@@ -51,7 +51,7 @@ bool testCopies() {
return true;
}
-bool testString() {
+const bool testString() {
const string s("abcd");
assert(length(s) == 4);
assert(!strcmp(c_str(s), "abcd"));
@@ -89,12 +89,19 @@ bool testString() {
return true;
}
-bool testStream() {
+const bool testStream() {
ostringstream os;
os << "ab" << "cd";
- cout << str(os) << endl;
assert(str(os) == "abcd");
+ ostringstream bos;
+ bos << "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
+ bos << "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ cout << str(bos) << endl;
+ assert(str(bos) ==
+ "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ");
+
ostringstream cs;
cs << "\'";
assert(str(cs) == "\'");
@@ -113,69 +120,65 @@ bool testStream() {
return true;
}
-std::string stdAdd(std::string& x, std::string& y) {
+const std::string stdAdd(const std::string& x, const std::string& y) {
return x + y;
}
-string add(string& x, string& y) {
+const string add(const string& x, const string& y) {
return x + y;
}
char charBuffer[16385];
-struct addStrings{
- const size_t size;
- addStrings(const size_t size) : size(size) {
- }
- bool operator()() const {
- const size_t sz = size / 4;
- string x(charBuffer, sz);
- string y(charBuffer, sz);
- assert(length(add(x, y)) == sz * 2);
- return true;
- }
-};
-
-struct addStdStrings{
- const size_t size;
- addStdStrings(const size_t size) : size(size) {
- }
- bool operator()() const {
- const size_t sz = size / 4;
- std::string x(charBuffer, sz);
- std::string y(charBuffer, sz);
- assert(stdAdd(x, y).length() == (unsigned int)(sz * 2));
- return true;
- }
-};
-
-bool testStringPerf() {
+const bool testStringPerf() {
memset(charBuffer, 'A', 16384);
charBuffer[16384] = '\0';
const int count = 10000;
{
- const lambda<bool()> a16 = addStrings(16);
+ const lambda<const blambda(const size_t size)> addStrings = [](const size_t size) -> const blambda {
+ const blambda l = [size]() -> const bool {
+ const size_t sz = size / 4;
+ const string x(charBuffer, sz);
+ const string y(charBuffer, sz);
+ assert(length(add(x, y)) == sz * 2);
+ return true;
+ };
+ return l;
+ };
+
+ const blambda a16 = addStrings(16);
cout << "string test " << time(a16, 5, count) << " ms" << endl;
- const lambda<bool()> a32 =addStrings(32);
+ const blambda a32 =addStrings(32);
cout << "string test " << time(a32, 5, count) << " ms" << endl;
- const lambda<bool()> a256 =addStrings(256);
+ const blambda a256 =addStrings(256);
cout << "string test " << time(a256, 5, count) << " ms" << endl;
- const lambda<bool()> a1024 =addStrings(1024);
+ const blambda a1024 =addStrings(1024);
cout << "string test " << time(a1024, 5, count) << " ms" << endl;
- const lambda<bool()> a4096 =addStrings(4096);
+ const blambda a4096 =addStrings(4096);
cout << "string test " << time(a4096, 5, count) << " ms" << endl;
}
{
- const lambda<bool()> a16 =addStdStrings(16);
+ const lambda<const blambda(const size_t size)> addStdStrings = [](const size_t size) -> const blambda {
+ const blambda l = [size]() -> const bool {
+ const size_t sz = size / 4;
+ const std::string x(charBuffer, sz);
+ const std::string y(charBuffer, sz);
+ assert(stdAdd(x, y).length() == (unsigned int)(sz * 2));
+ return true;
+ };
+ return l;
+ };
+
+ const blambda a16 =addStdStrings(16);
cout << "Std string test " << time(a16, 5, count) << " ms" << endl;
- const lambda<bool()> a32 =addStdStrings(32);
+ const blambda a32 =addStdStrings(32);
cout << "Std string test " << time(a32, 5, count) << " ms" << endl;
- const lambda<bool()> a256 =addStdStrings(256);
+ const blambda a256 =addStdStrings(256);
cout << "Std string test " << time(a256, 5, count) << " ms" << endl;
- const lambda<bool()> a1024 =addStdStrings(1024);
+ const blambda a1024 =addStdStrings(1024);
cout << "Std string test " << time(a1024, 5, count) << " ms" << endl;
- const lambda<bool()> a4096 =addStdStrings(4096);
+ const blambda a4096 =addStdStrings(4096);
cout << "Std string test " << time(a4096, 5, count) << " ms" << endl;
}
@@ -185,7 +188,7 @@ bool testStringPerf() {
}
int main() {
- tuscany::gc_scoped_pool p;
+ const tuscany::gc_scoped_pool p;
tuscany::cout << "Testing..." << tuscany::endl;
tuscany::testCopies();