Author: jsdelfino
Date: Tue Dec 11 03:51:03 2012
New Revision: 1419985
URL: http://svn.apache.org/viewvc?rev=1419985&view=rev
Log:
Port kernel to C++11 and refactor some of the core modules. Convert functors to lambdas, and add C++ const, noexcept and inline annotations to get more efficient generated code.
Added:
tuscany/sca-cpp/trunk/etc/callgrind (contents, props changed)
- copied, changed from r1361918, tuscany/sca-cpp/trunk/modules/Makefile.am
tuscany/sca-cpp/trunk/modules/scheme/element-xml.cpp
- copied, changed from r1361918, tuscany/sca-cpp/trunk/modules/scheme/value-xml.cpp
tuscany/sca-cpp/trunk/modules/scheme/scheme-test.hpp
tuscany/sca-cpp/trunk/modules/scheme/test.scm
tuscany/sca-cpp/trunk/modules/scheme/xml-element.cpp
- copied, changed from r1361918, tuscany/sca-cpp/trunk/modules/scheme/xml-value.cpp
tuscany/sca-cpp/trunk/modules/xml/
tuscany/sca-cpp/trunk/modules/xml/Makefile.am
- copied, changed from r1361918, tuscany/sca-cpp/trunk/modules/Makefile.am
tuscany/sca-cpp/trunk/modules/xml/test.xml
tuscany/sca-cpp/trunk/modules/xml/xml-test.cpp
tuscany/sca-cpp/trunk/modules/xml/xml-test.hpp
tuscany/sca-cpp/trunk/modules/xml/xml.hpp
- copied, changed from r1361918, tuscany/sca-cpp/trunk/kernel/xml.hpp
tuscany/sca-cpp/trunk/modules/xml/xsd-test.cpp
- copied, changed from r1361918, tuscany/sca-cpp/trunk/kernel/xsd-test.cpp
Removed:
tuscany/sca-cpp/trunk/kernel/xml-test.cpp
tuscany/sca-cpp/trunk/kernel/xml.hpp
tuscany/sca-cpp/trunk/kernel/xsd-test.cpp
tuscany/sca-cpp/trunk/modules/scheme/value-xml.cpp
tuscany/sca-cpp/trunk/modules/scheme/xml-value.cpp
Modified:
tuscany/sca-cpp/trunk/.gitignore
tuscany/sca-cpp/trunk/etc/memgrind
tuscany/sca-cpp/trunk/kernel/Makefile.am
tuscany/sca-cpp/trunk/kernel/config.hpp
tuscany/sca-cpp/trunk/kernel/dynlib-test.cpp
tuscany/sca-cpp/trunk/kernel/dynlib.hpp
tuscany/sca-cpp/trunk/kernel/element.hpp
tuscany/sca-cpp/trunk/kernel/fstream.hpp
tuscany/sca-cpp/trunk/kernel/function.hpp
tuscany/sca-cpp/trunk/kernel/gc.hpp
tuscany/sca-cpp/trunk/kernel/hash-test.cpp
tuscany/sca-cpp/trunk/kernel/hash.hpp
tuscany/sca-cpp/trunk/kernel/kernel-test.cpp
tuscany/sca-cpp/trunk/kernel/lambda-test.cpp
tuscany/sca-cpp/trunk/kernel/list.hpp
tuscany/sca-cpp/trunk/kernel/mem-test.cpp
tuscany/sca-cpp/trunk/kernel/monad.hpp
tuscany/sca-cpp/trunk/kernel/parallel-test.cpp
tuscany/sca-cpp/trunk/kernel/parallel.hpp
tuscany/sca-cpp/trunk/kernel/perf.hpp
tuscany/sca-cpp/trunk/kernel/sstream.hpp
tuscany/sca-cpp/trunk/kernel/stream.hpp
tuscany/sca-cpp/trunk/kernel/string-test.cpp
tuscany/sca-cpp/trunk/kernel/string.hpp
tuscany/sca-cpp/trunk/kernel/tree.hpp
tuscany/sca-cpp/trunk/kernel/value.hpp
tuscany/sca-cpp/trunk/modules/Makefile.am
tuscany/sca-cpp/trunk/modules/atom/atom-test.cpp
tuscany/sca-cpp/trunk/modules/atom/atom.hpp
tuscany/sca-cpp/trunk/modules/rss/rss-test.cpp
tuscany/sca-cpp/trunk/modules/rss/rss.hpp
tuscany/sca-cpp/trunk/modules/scdl/scdl-test.cpp
tuscany/sca-cpp/trunk/modules/scdl/scdl.hpp
tuscany/sca-cpp/trunk/modules/scheme/Makefile.am
tuscany/sca-cpp/trunk/modules/scheme/driver.hpp
tuscany/sca-cpp/trunk/modules/scheme/element-value.cpp
tuscany/sca-cpp/trunk/modules/scheme/environment.hpp
tuscany/sca-cpp/trunk/modules/scheme/eval.hpp
tuscany/sca-cpp/trunk/modules/scheme/io.hpp
tuscany/sca-cpp/trunk/modules/scheme/primitive.hpp
tuscany/sca-cpp/trunk/modules/scheme/scheme-shell.cpp
tuscany/sca-cpp/trunk/modules/scheme/scheme-test.cpp
tuscany/sca-cpp/trunk/modules/scheme/value-element.cpp
Modified: tuscany/sca-cpp/trunk/.gitignore
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/.gitignore?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/.gitignore (original)
+++ tuscany/sca-cpp/trunk/.gitignore Tue Dec 11 03:51:03 2012
@@ -61,8 +61,8 @@ cache-manifest.cmf
*-min.html
*-min.js
*-min.css
+*.b64
intro*.png
-intro*.b64
depcomp
install-sh
ltmain.sh
@@ -138,8 +138,8 @@ js-shell
file-test
test-start*
test-stop*
-xml-value
-value-xml
+xml-element
+element-xml
json-value
value-json
element-value
Copied: tuscany/sca-cpp/trunk/etc/callgrind (from r1361918, tuscany/sca-cpp/trunk/modules/Makefile.am)
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/etc/callgrind?p2=tuscany/sca-cpp/trunk/etc/callgrind&p1=tuscany/sca-cpp/trunk/modules/Makefile.am&r1=1361918&r2=1419985&rev=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/modules/Makefile.am (original)
+++ tuscany/sca-cpp/trunk/etc/callgrind Tue Dec 11 03:51:03 2012
@@ -1,3 +1,5 @@
+#!/bin/sh
+
# 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
@@ -15,5 +17,9 @@
# specific language governing permissions and limitations
# under the License.
-SUBDIRS = scheme atom rss js json scdl http server python opencl java openid oauth wsgi
+# Run valgrind to record call history, generate a call graph and help analyze
+# cache and branch prediction
+here=`echo "import os; print os.path.realpath('$0')" | python`; here=`dirname $here`
+
+valgrind --tool=callgrind $* 2>&1 | tee callgrind.log
Propchange: tuscany/sca-cpp/trunk/etc/callgrind
------------------------------------------------------------------------------
svn:executable = *
Modified: tuscany/sca-cpp/trunk/etc/memgrind
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/etc/memgrind?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/etc/memgrind (original)
+++ tuscany/sca-cpp/trunk/etc/memgrind Tue Dec 11 03:51:03 2012
@@ -20,5 +20,5 @@
# Run valgrind to analyze memory usage and track memory leaks
here=`echo "import os; print os.path.realpath('$0')" | python`; here=`dirname $here`
-valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=50 --track-fds=yes --trace-children=yes --suppressions=$here/../../Python-2.6.6/Misc/valgrind-python.supp --suppressions=$here/valgrind-apr.supp $* 2>&1 | tee memgrind.log
+valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=50 --track-fds=yes --trace-children=yes --suppressions=$here/../../Python-2.7.3/Misc/valgrind-python.supp --suppressions=$here/valgrind-apr.supp $* 2>&1 | tee memgrind.log
Modified: tuscany/sca-cpp/trunk/kernel/Makefile.am
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/Makefile.am?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/Makefile.am (original)
+++ tuscany/sca-cpp/trunk/kernel/Makefile.am Tue Dec 11 03:51:03 2012
@@ -37,14 +37,8 @@ mem_test_SOURCES = mem-test.cpp
parallel_test_SOURCES = parallel-test.cpp
-xml_test_SOURCES = xml-test.cpp
-xml_test_LDFLAGS = -lxml2
-
-xsd_test_SOURCES = xsd-test.cpp
-xsd_test_LDFLAGS = -lxml2
-
hash_test_SOURCES = hash-test.cpp
-noinst_PROGRAMS = string-test kernel-test lambda-test hash-test mem-test parallel-test xml-test xsd-test
-TESTS = string-test kernel-test lambda-test hash-test mem-test parallel-test xml-test
+noinst_PROGRAMS = string-test kernel-test lambda-test hash-test mem-test parallel-test
+TESTS = string-test kernel-test lambda-test hash-test mem-test parallel-test
Modified: tuscany/sca-cpp/trunk/kernel/config.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/config.hpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/config.hpp (original)
+++ tuscany/sca-cpp/trunk/kernel/config.hpp Tue Dec 11 03:51:03 2012
@@ -93,12 +93,12 @@ namespace tuscany
#ifdef WANT_MAINTAINER_COUNTERS
-bool debug_inc(long int& c) {
+const bool debug_inc(long int& c) {
c++;
return true;
}
-bool debug_dec(long int& c) {
+const bool debug_dec(long int& c) {
c--;
return true;
}
Modified: tuscany/sca-cpp/trunk/kernel/dynlib-test.cpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/dynlib-test.cpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/dynlib-test.cpp (original)
+++ tuscany/sca-cpp/trunk/kernel/dynlib-test.cpp Tue Dec 11 03:51:03 2012
@@ -28,7 +28,7 @@
namespace tuscany {
namespace test {
- const int cppsquare(int x) {
+ const int cppsquare(const int x) {
return x * x;
}
@@ -41,8 +41,8 @@ extern "C" {
return tuscany::test::cppsquare(x);
}
- const tuscany::lambda<int(const int)> csquarel() {
- return tuscany::lambda<int(const int)>(tuscany::test::cppsquare);
+ const tuscany::lambda<const int(const int)> csquarel() {
+ return tuscany::lambda<const int(const int)>(tuscany::test::cppsquare);
}
}
Modified: tuscany/sca-cpp/trunk/kernel/dynlib.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/dynlib.hpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/dynlib.hpp (original)
+++ tuscany/sca-cpp/trunk/kernel/dynlib.hpp Tue Dec 11 03:51:03 2012
@@ -48,27 +48,18 @@ const string dynlibExt(".so");
*/
class lib {
public:
- lib() : h(NULL), owner(false) {
+ inline lib() : h(NULL), owner(false) {
}
- lib(const string& name) : name(name), h(dlopen(c_str(name), RTLD_NOW)), owner(true) {
- if (h == NULL)
- h = mkfailure<void*>(string("Could not load library: ") + name + ": " + dlerror());
+ inline lib(const string& name) : name(name), h(open(name)), owner(true) {
}
- lib(const lib& l) : name(l.name), h(l.h), owner(false) {
+ inline lib(const lib& l) : name(l.name), h(l.h), owner(false) {
}
- const lib& operator=(const lib& l) {
- if(this == &l)
- return *this;
- name = l.name;
- h = l.h;
- owner = false;
- return *this;
- }
+ lib& operator=(const lib& l) = delete;
- ~lib() {
+ inline ~lib() {
if (!owner)
return;
if (!hasContent(h) || content(h) == NULL)
@@ -77,17 +68,22 @@ public:
}
private:
+ const failable<void*> open(const string& name) {
+ void * h = dlopen(c_str(name), RTLD_NOW);
+ return h != NULL? failable<void*>(h) : mkfailure<void*>(string("Could not load library: ") + name + ": " + dlerror());
+ }
+
template<typename S> friend const failable<lambda<S> > dynlambda(const string& name, const lib& l);
- string name;
- failable<void*> h;
- bool owner;
+ const string name;
+ const failable<void*> h;
+ const bool owner;
};
/**
* Find a lambda function in a dynamic library.
*/
-template<typename S> const failable<lambda<S> > dynlambda(const string& name, const lib& l) {
+template<typename S> inline const failable<lambda<S> > dynlambda(const string& name, const lib& l) {
if (!hasContent(l.h))
return mkfailure<lambda<S>>(l.h);
const void* s = dlsym(content(l.h), c_str(name));
Modified: tuscany/sca-cpp/trunk/kernel/element.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/element.hpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/element.hpp (original)
+++ tuscany/sca-cpp/trunk/kernel/element.hpp Tue Dec 11 03:51:03 2012
@@ -42,7 +42,7 @@ const string atsign("@");
/**
* Returns true if a value is an element.
*/
-bool isElement(const value& v) {
+inline const bool isElement(const value& v) {
if (!isList(v) || isNil(v) || element != car<value>(v))
return false;
return true;
@@ -51,7 +51,7 @@ bool isElement(const value& v) {
/**
* Returns true if a value is an attribute.
*/
-bool isAttribute(const value& v) {
+inline const bool isAttribute(const value& v) {
if (!isList(v) || isNil(v) || attribute != car<value>(v))
return false;
return true;
@@ -60,42 +60,42 @@ bool isAttribute(const value& v) {
/**
* Returns the name of an attribute.
*/
-const value attributeName(const list<value>& l) {
+inline const value attributeName(const list<value>& l) {
return cadr(l);
}
/**
* Returns the value of an attribute.
*/
-const value attributeValue(const list<value>& l) {
+inline const value attributeValue(const list<value>& l) {
return caddr(l);
}
/**
* Returns the name of an element.
*/
-const value elementName(const list<value>& l) {
+inline const value elementName(const list<value>& l) {
return cadr(l);
}
/**
* Returns true if an element has children.
*/
-const bool elementHasChildren(const list<value>& l) {
+inline const bool elementHasChildren(const list<value>& l) {
return !isNil(cddr(l));
}
/**
* Returns the children of an element.
*/
-const list<value> elementChildren(const list<value>& l) {
+inline const list<value> elementChildren(const list<value>& l) {
return cddr(l);
}
/**
* Returns true if an element has a value.
*/
-const bool elementHasValue(const list<value>& l) {
+inline const bool elementHasValue(const list<value>& l) {
const list<value> r = reverse(l);
if (isSymbol(car(r)))
return false;
@@ -107,26 +107,26 @@ const bool elementHasValue(const list<va
/**
* Returns the value of an element.
*/
-const value elementValue(const list<value>& l) {
+inline const value elementValue(const list<value>& l) {
return car(reverse(l));
}
/**
* Convert an element to a value.
*/
-const bool elementToValueIsList(const value& v) {
+inline const bool elementToValueIsList(const value& v) {
if (!isList(v))
return false;
const list<value> l = v;
return (isNil(l) || !isSymbol(car(l)));
}
-const value elementToValue(const value& t) {
+inline const value elementToValue(const value& t) {
const list<value> elementsToValues(const list<value>& e);
// Convert an attribute
if (isTaggedList(t, attribute))
- return mklist<value>(c_str(atsign + attributeName(t)), attributeValue(t));
+ return mklist<value>(c_str(atsign + (string)attributeName(t)), attributeValue(t));
// Convert an element
if (isTaggedList(t, element)) {
@@ -155,7 +155,7 @@ const value elementToValue(const value&
/**
* Convert a list of elements to a list of values.
*/
-const bool elementToValueIsSymbol(const value& v) {
+inline const bool elementToValueIsSymbol(const value& v) {
if (!isList(v))
return false;
const list<value> l = v;
@@ -166,7 +166,7 @@ const bool elementToValueIsSymbol(const
return true;
}
-const list<value> elementToValueGroupValues(const value& v, const list<value>& l) {
+inline const list<value> elementToValueGroupValues(const value& v, const list<value>& l) {
if (isNil(l) || !elementToValueIsSymbol(v) || !elementToValueIsSymbol(car(l)))
return cons(v, l);
if (car<value>(car(l)) != car<value>(v))
@@ -180,7 +180,7 @@ const list<value> elementToValueGroupVal
}
-const list<value> elementsToValues(const list<value>& e) {
+inline const list<value> elementsToValues(const list<value>& e) {
if (isNil(e))
return e;
return elementToValueGroupValues(elementToValue(car(e)), elementsToValues(cdr(e)));
@@ -189,13 +189,13 @@ const list<value> elementsToValues(const
/**
* Convert a value to an element.
*/
-const value valueToElement(const value& t) {
+inline const value valueToElement(const value& t) {
const list<value> valuesToElements(const list<value>& l);
// Convert a name value pair
if (isList(t) && !isNil((list<value>)t) && isSymbol(car<value>(t))) {
const value n = car<value>(t);
- const value v = isNil(cdr<value>(t))? value() : cadr<value>(t);
+ const value v = isNil(cdr<value>(t))? nilValue : cadr<value>(t);
// Convert a single value to an attribute or an element
if (!isList(v)) {
@@ -221,7 +221,7 @@ const value valueToElement(const value&
/**
* Convert a list of values to a list of elements.
*/
-const list<value> valuesToElements(const list<value>& l) {
+inline const list<value> valuesToElements(const list<value>& l) {
if (isNil(l))
return l;
return cons<value>(valueToElement(car(l)), valuesToElements(cdr(l)));
@@ -231,72 +231,51 @@ const list<value> valuesToElements(const
* Returns a selector lambda function which can be used to filter
* elements against the given element pattern.
*/
-struct selectorLambda {
- const list<value> select;
- selectorLambda(const list<value>& s) : select(s) {
- }
- const bool evalSelect(const list<value>& s, const list<value> v) const {
- if (isNil(s))
- return true;
- if (isNil(v))
- return false;
- if (car(s) != car(v))
- return false;
- return evalSelect(cdr(s), cdr(v));
- }
- const bool operator()(const value& v) const {
+inline const lambda<const bool(const value&)> selector(const list<value>& select) {
+ return [select](const value& v) -> const bool {
+ const lambda<const bool(const list<value>&, const list<value>&)> evalSelect = [&evalSelect](const list<value>& s, const list<value>& v) -> const bool {
+ if (isNil(s))
+ return true;
+ if (isNil(v))
+ return false;
+ if (car(s) != car(v))
+ return false;
+ return evalSelect(cdr(s), cdr(v));
+ };
if (!isList(v))
return false;
return evalSelect(select, v);
- }
-};
-
-const lambda<bool(const value&)> selector(const list<value> s) {
- return selectorLambda(s);
+ };
}
/**
* Returns the value of the attribute with the given name.
*/
-struct filterAttribute {
- const value name;
- filterAttribute(const value& n) : name(n) {
- }
- const bool operator()(const value& v) const {
- return isAttribute(v) && attributeName((list<value>)v) == name;
- }
-};
-
-const value attributeValue(const value& name, const value& l) {
- const list<value> f = filter<value>(filterAttribute(name), list<value>(l));
+inline const value attributeValue(const value& name, const value& l) {
+ const list<value> f = filter<value>([name](const value& v) {
+ return isAttribute(v) && attributeName((list<value>)v) == name;
+ }, list<value>(l));
if (isNil(f))
- return value();
+ return nilValue;
return caddr<value>(car(f));
}
/**
* Returns child elements with the given name.
*/
-struct filterElement {
- const value name;
- filterElement(const value& n) : name(n) {
- }
- const bool operator()(const value& v) const {
- return isElement(v) && elementName((list<value>)v) == name;
- }
-};
-
-const value elementChildren(const value& name, const value& l) {
- return filter<value>(filterElement(name), list<value>(l));
+inline const value elementChildren(const value& name, const value& l) {
+ return filter<value>([name](const value& v) {
+ return isElement(v) && elementName((list<value>)v) == name;
+ }, list<value>(l));
}
/**
* Return the child element with the given name.
*/
-const value elementChild(const value& name, const value& l) {
+inline const value elementChild(const value& name, const value& l) {
const list<value> f = elementChildren(name, l);
if (isNil(f))
- return value();
+ return nilValue;
return car(f);
}
Modified: tuscany/sca-cpp/trunk/kernel/fstream.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/fstream.hpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/fstream.hpp (original)
+++ tuscany/sca-cpp/trunk/kernel/fstream.hpp Tue Dec 11 03:51:03 2012
@@ -69,24 +69,18 @@ namespace tuscany {
*/
class ofstream : public ostream {
public:
- ofstream(const string& path) : file(fopen(c_str(path), "wb")), owner(true) {
+ inline ofstream(const string& path) : file(fopen(c_str(path), "wb")), owner(true) {
}
- ofstream(FILE* file) : file(file), owner(false) {
+ inline ofstream(FILE* const file) : file(file), owner(false) {
}
- ofstream(const ofstream& os) : file(os.file), owner(false) {
+ inline ofstream(const ofstream& os) : file(os.file), owner(false) {
}
- const ofstream& operator=(const ofstream& os) {
- if(this == &os)
- return *this;
- file = os.file;
- owner = false;
- return *this;
- }
+ ofstream& operator=(const ofstream& os) = delete;
- ~ofstream() {
+ inline ~ofstream() {
if (!owner)
return;
if (file == NULL)
@@ -94,11 +88,11 @@ public:
fclose(file);
}
- const bool fail() {
+ inline const bool fail() {
return file == NULL;
}
- ofstream& vprintf(const char* fmt, ...) {
+ inline ofstream& vprintf(const char* const fmt, ...) {
va_list args;
va_start (args, fmt);
vfprintf (file, fmt, args);
@@ -106,19 +100,24 @@ public:
return *this;
}
- ofstream& write(const string& s) {
+ inline ofstream& write(const string& s) {
fwrite(c_str(s), 1, length(s), file);
return *this;
}
- ofstream& flush() {
+ inline ofstream& write(const char c) {
+ fwrite(&c, 1, 1, file);
+ return *this;
+ }
+
+ inline ofstream& flush() {
fflush(file);
return *this;
}
private:
- FILE* file;
- bool owner;
+ FILE* const file;
+ const bool owner;
};
/*
@@ -126,24 +125,18 @@ private:
*/
class ifstream : public istream {
public:
- ifstream(const string& path) : file(fopen(c_str(path), "rb")), owner(true) {
+ inline ifstream(const string& path) : file(fopen(c_str(path), "rb")), owner(true) {
}
- ifstream(FILE* file) : file(file), owner(false) {
+ inline ifstream(FILE* const file) : file(file), owner(false) {
}
- ifstream(const ifstream& is) : file(is.file), owner(false) {
+ inline ifstream(const ifstream& is) : file(is.file), owner(false) {
}
- const ifstream& operator=(const ifstream& is) {
- if(this == &is)
- return *this;
- file = is.file;
- owner = false;
- return *this;
- }
+ ifstream& operator=(const ifstream& is) = delete;
- ~ifstream() {
+ inline ~ifstream() {
if (!owner)
return;
if (file == NULL)
@@ -151,23 +144,23 @@ public:
fclose(file);
}
- const size_t read(void* buf, size_t size) {
+ inline const size_t read(void* const buf, const size_t size) {
return fread(buf, 1, size, file);
}
- const bool eof() {
+ inline const bool eof() {
return feof(file);
}
- const bool fail() {
+ inline const bool fail() {
return file == NULL;
}
- const int get() {
+ inline const int get() {
return fgetc(file);
}
- const int peek() {
+ inline const int peek() {
int c = fgetc(file);
if (c == -1)
return c;
@@ -176,8 +169,8 @@ public:
}
private:
- FILE* file;
- bool owner;
+ FILE* const file;
+ const bool owner;
};
/**
@@ -198,24 +191,24 @@ ifstream cin(stdin);
*/
class loghstream : public ostream {
public:
- loghstream(const int level) : level(level), len(0) {
+ inline loghstream(const int level) : level(level), len(0) {
}
- ~loghstream() {
+ inline ~loghstream() {
}
- ostream& vprintf(const char* fmt, ...) {
+ inline ostream& vprintf(const char* const fmt, ...) {
va_list args;
- va_start (args, fmt);
+ va_start(args, fmt);
const int l = vsnprintf(buf + len, (sizeof(buf) - 1) - len, fmt, args);
- va_end (args);
+ va_end(args);
len += l;
if (len > (int)(sizeof(buf) - 1))
len = sizeof(buf) - 1;
return *this;
}
- ostream& write(const string& s) {
+ inline ostream& write(const string& s) {
if (s != "\n")
return this->vprintf("%s", c_str(s));
buf[len] = '\0';
@@ -224,14 +217,23 @@ public:
return *this;
}
- ostream& flush() {
+ inline ostream& write(const char c) {
+ if (c != '\n')
+ return this->vprintf("%c", c);
+ buf[len] = '\0';
+ ap_log_error(NULL, 0, -1, level, 0, ap_server_conf, "%s", buf);
+ len = 0;
+ return *this;
+ }
+
+ inline ostream& flush() {
return *this;
}
private:
const int level;
int len;
- char buf[2049];
+ char buf[513];
};
/**
@@ -259,11 +261,11 @@ loghstream cdebug(APLOG_DEBUG);
/**
* Format the current time.
*/
-const string logTime() {
+inline const string logTime() {
struct timeval tv;
gettimeofday(&tv, NULL);
const time_t t = tv.tv_sec;
- const tm* lt = localtime(&t);
+ const tm* const lt = localtime(&t);
char ft[32];
strftime(ft, 20, "%a %b %d %H:%M:%S", lt);
sprintf(ft + 19, ".%06lu ", (unsigned long)tv.tv_usec);
@@ -276,16 +278,16 @@ const string logTime() {
*/
class logfstream : public ostream {
public:
- logfstream(FILE* file, const string& type) : file(file), type(type), head(false) {
+ inline logfstream(FILE* const file, const string& type) : file(file), type(type), head(false) {
}
- logfstream(const logfstream& os) : file(os.file), type(os.type), head(os.head) {
+ inline logfstream(const logfstream& os) : file(os.file), type(os.type), head(os.head) {
}
- ~logfstream() {
+ inline ~logfstream() {
}
- ostream& vprintf(const char* fmt, ...) {
+ inline ostream& vprintf(const char* const fmt, ...) {
whead();
va_list args;
va_start (args, fmt);
@@ -294,7 +296,7 @@ public:
return *this;
}
- ostream& write(const string& s) {
+ inline ostream& write(const string& s) {
whead();
fwrite(c_str(s), 1, length(s), file);
if (s == "\n")
@@ -302,17 +304,25 @@ public:
return *this;
}
- ostream& flush() {
+ inline ostream& write(const char c) {
+ whead();
+ fwrite(&c, 1, 1, file);
+ if (c == '\n')
+ head = false;
+ return *this;
+ }
+
+ inline ostream& flush() {
fflush(file);
return *this;
}
private:
- FILE* file;
+ FILE* const file;
const string type;
bool head;
- const unsigned long tid() const {
+ inline const unsigned long tid() const {
#ifdef WANT_THREADS
return (unsigned long)pthread_self();
#else
@@ -320,7 +330,7 @@ private:
#endif
}
- ostream& whead() {
+ inline ostream& whead() {
if (head)
return *this;
head = true;
@@ -348,7 +358,7 @@ logfstream cdebug(stderr, "debug");
bool debug_isLoggingSet = false;
bool debug_isLoggingEnv = false;
-const bool debug_isLogging() {
+inline const bool debug_isLogging() {
if (debug_isLoggingSet)
return debug_isLoggingEnv;
debug_isLoggingEnv = getenv("TUSCANY_DEBUG_LOG") != NULL;
@@ -366,8 +376,8 @@ const bool debug_isLogging() {
/**
* Log a debug message.
*/
-const bool debugLog(const string& msg) {
- gc_scoped_pool p;
+inline const bool debugLog(const string& msg) {
+ const gc_scoped_pool p;
cdebug << msg << endl;
return true;
}
@@ -375,8 +385,8 @@ const bool debugLog(const string& msg) {
/**
* Log a debug message and a value.
*/
-template<typename V> const bool debugLog(const V& v, const string& msg) {
- gc_scoped_pool p;
+template<typename V> inline const bool debugLog(const V& v, const string& msg) {
+ const gc_scoped_pool p;
cdebug << msg << ": " << v << endl;
return true;
}
@@ -384,8 +394,8 @@ template<typename V> const bool debugLog
/**
* Log a debug message and two values.
*/
-template<typename V, typename W> const bool debugLog(const V& v, const W& w, const string& msg) {
- gc_scoped_pool p;
+template<typename V, typename W> inline const bool debugLog(const V& v, const W& w, const string& msg) {
+ const gc_scoped_pool p;
cdebug << msg << ": " << v << " : " << w << endl;
return true;
}
Modified: tuscany/sca-cpp/trunk/kernel/function.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/function.hpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/function.hpp (original)
+++ tuscany/sca-cpp/trunk/kernel/function.hpp Tue Dec 11 03:51:03 2012
@@ -23,7 +23,9 @@
#define tuscany_function_hpp
/**
- * Lambda function type.
+ * Lambda function type that enforces pass by ref and constant parameters,
+ * allocates memory only as needed and using an APR memory pool, is about
+ * 2 times faster than std::function and between 1/4 and 1/6 of its size.
*/
#include <utility>
@@ -46,16 +48,16 @@ long int countELambdas = 0;
long int countCLambdas = 0;
long int countFLambdas = 0;
-bool resetLambdaCounters() {
+inline const bool resetLambdaCounters() {
countLambdas = countELambdas = countCLambdas = countFLambdas = countProxies = countFProxies = countCProxies = 0;
return true;
}
-bool checkLambdaCounters() {
+inline const bool checkLambdaCounters() {
return countLambdas == 0;
}
-bool printLambdaCounters() {
+inline const bool printLambdaCounters() {
cout << "countLambdas " << countLambdas << endl;
cout << "countELambdas " << countELambdas << endl;
cout << "countFLambdas " << countFLambdas << endl;
@@ -80,38 +82,32 @@ bool printLambdaCounters() {
template<typename R, typename... P> class Callable {
public:
- Callable() {
+ inline Callable() noexcept {
}
- virtual const size_t size() const = 0;
-
- virtual const R operator()(P... p) const = 0;
+ virtual const R operator()(const P&&... p) const noexcept = 0;
- virtual ~Callable() {
+ inline virtual ~Callable() noexcept {
}
- template<typename F> class Proxy: public Callable {
+ template<typename F> class Proxy: public Callable<R, P...> {
public:
- Proxy(const F& f) : function(f) {
+ inline Proxy(const F& f) noexcept : function(f) {
debug_inc(countProxies);
debug_inc(countFProxies);
}
- Proxy(const Proxy& p) : function(p.function) {
+ inline Proxy(const Proxy& p) noexcept : function(p.function) {
debug_inc(countProxies);
debug_inc(countCProxies);
}
- ~Proxy() {
+ inline ~Proxy() noexcept {
debug_dec(countProxies);
}
- virtual const R operator() (P... p) const {
- return function(std::forward<P>(p)...);
- }
-
- virtual const size_t size() const {
- return sizeof(function);
+ virtual const R operator() (const P&&... p) const noexcept {
+ return function(std::forward<const P&&>(p)...);
}
private:
@@ -123,67 +119,68 @@ template<typename S> class lambda;
template<typename R, typename... P> class lambda<R(P...)> {
public:
- lambda() : callable(0) {
+ inline lambda() noexcept : callable(NULL) {
debug_inc(countLambdas);
debug_inc(countELambdas);
}
- template<typename F> lambda(const F f) {
+ template<typename F> inline lambda(const F f) noexcept : callable(mkproxy<F>(f)) {
debug_inc(countLambdas);
debug_inc(countFLambdas);
+ }
- typedef typename CallableType::template Proxy<F> ProxyType;
- callable = gc_ptr<CallableType>(new (gc_new<ProxyType>()) ProxyType(f));
+ template<typename F> inline lambda(const gc_mutable_ref<F>& r) noexcept : callable(mkproxy<F>(*(F*)r)) {
+ debug_inc(countLambdas);
+ debug_inc(countFLambdas);
}
- lambda(const lambda& l) {
+ inline lambda(const lambda& l) noexcept : callable(l.callable) {
debug_inc(countLambdas);
debug_inc(countCLambdas);
- callable = l.callable;
}
- const lambda& operator=(const lambda& l) {
- if (this == &l)
- return *this;
- callable = l.callable;
- return *this;
- }
+ lambda& operator=(const lambda& l) = delete;
- ~lambda() {
+ inline ~lambda() noexcept {
debug_dec(countLambdas);
}
- const bool operator==(const lambda& l) const {
+ inline const bool operator==(const lambda& l) const noexcept {
if (this == &l)
return true;
- return callable == l.callable;
+ return false;
}
- const bool operator!=(const lambda& l) const {
+ inline const bool operator!=(const lambda& l) const noexcept {
return !this->operator==(l);
}
- const R operator()(P... p) const {
- return (*callable)(std::forward<P>(p)...);
+ inline const R operator()(const P&... p) const noexcept {
+ return (*callable)(std::forward<const P&&>(p)...);
}
template<typename S> friend ostream& operator<<(ostream&, const lambda<S>&);
- template<typename S> friend const bool isNil(const lambda<S>& l);
+ template<typename S> friend const bool isNil(const lambda<S>& l) noexcept;
private:
typedef Callable<R,P...> CallableType;
- gc_ptr<CallableType> callable;
+ const gc_ptr<CallableType> callable;
+
+ template<typename F> inline gc_ptr<CallableType> mkproxy(const F& f) noexcept {
+ typedef typename CallableType::template Proxy<F> ProxyType;
+ return gc_ptr<CallableType>(new (gc_new<ProxyType>()) ProxyType(f));
+ }
};
-template<typename S> ostream& operator<<(ostream& out, const lambda<S>& l) {
+template<typename S> inline ostream& operator<<(ostream& out, const lambda<S>& l) {
return out << "lambda::" << l.callable;
}
/**
* Return true if a lambda is nil.
*/
-template<typename S> const bool isNil(const lambda<S>& l) {
- return ((void*)l.callable) == 0;
+template<typename S> inline const bool isNil(const lambda<S>& l) noexcept {
+ return (const void*)l.callable == NULL;
}
/**
@@ -191,11 +188,11 @@ template<typename S> const bool isNil(co
*/
template<typename R, typename T, typename... P> class curried {
public:
- curried(const lambda<R(T, P...)>& f, const T& v): v(v), f(f) {
+ inline curried(const lambda<R(T, P...)>& f, const T& v) noexcept: v(v), f(f) {
}
- const R operator()(P... p) const {
- return f(v, std::forward<P>(p)...);
+ inline const R operator()(const P&... p) const noexcept {
+ return f(v, std::forward<const P&>(p)...);
}
private:
@@ -203,36 +200,29 @@ private:
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) {
+template<typename R, typename T, typename... P> inline const lambda<const R(P...)> curry(const lambda<R(T, P...)>& f, const T& t) noexcept {
return 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) {
+template<typename R, typename T, typename U, typename... P> inline const lambda<R(P...)> curry(const lambda<R(T, U, P...)>& f, const T& t, const U& u) noexcept {
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, V, P...)>& f, const T& t, const U& u, const V& v) {
+template<typename R, typename T, typename U, typename V, typename... P> inline const lambda<R(P...)> curry(const lambda<R(T, U, V, P...)>& f, const T& t, const U& u, const V& v) noexcept {
return curry(curry(curry(f, t), u), v);
}
/**
* A lambda function that returns the given value.
*/
-template<typename T> class returnResult {
-public:
- returnResult(const T& v) :
- v(v) {
- }
- const T operator()() const {
- return v;
- }
-private:
- const T v;
-};
-
-template<typename T> const lambda<T()> result(const T& v) {
- return returnResult<T> (v);
+template<typename T> inline const lambda<const T()> result(const T& v) {
+ return [v]()->T { return v; };
}
+/**
+ * Commonly used lambda types.
+ */
+typedef lambda<const bool()> blambda;
+
}
#endif /* tuscany_function_hpp */
Modified: tuscany/sca-cpp/trunk/kernel/gc.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/gc.hpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/gc.hpp (original)
+++ tuscany/sca-cpp/trunk/kernel/gc.hpp Tue Dec 11 03:51:03 2012
@@ -49,7 +49,7 @@ namespace tuscany
/**
* Force a core dump on assertion violation.
*/
-bool assertOrFail(const bool expr) {
+inline const bool assertOrFail(const bool expr) {
if (!expr)
abort();
return true;
@@ -64,63 +64,135 @@ bool assertOrFail(const bool expr) {
/**
* Pointer to a value.
*/
+#ifdef WANT_RAW_PTR
+
+template<typename T> using gc_ptr = T*;
+
+#else
+
template<typename T> class gc_ptr {
public:
- gc_ptr(T* ptr = NULL) throw() : ptr(ptr) {
+ inline gc_ptr(T* const ptr = NULL) noexcept : ptr(ptr) {
+ }
+
+ inline ~gc_ptr() noexcept {
+ }
+
+ inline gc_ptr(const gc_ptr& r) noexcept : ptr(r.ptr) {
+ }
+
+ gc_ptr& operator=(const gc_ptr& r) = delete;
+
+ inline const bool operator==(const gc_ptr& r) const noexcept {
+ if (this == &r)
+ return true;
+ return ptr == r.ptr;
+ }
+
+ inline const bool operator==(const T* const p) const noexcept {
+ return ptr == p;
+ }
+
+ inline const bool operator!=(const gc_ptr& r) const noexcept {
+ return !this->operator==(r);
+ }
+
+ inline const bool operator!=(const T* const p) const noexcept {
+ return !this->operator==(p);
+ }
+
+ inline T& operator*() const noexcept {
+ return *ptr;
+ }
+
+ inline T* const operator->() const noexcept {
+ return ptr;
+ }
+
+ inline operator T* const () const noexcept {
+ return ptr;
+ }
+
+private:
+ T* const ptr;
+};
+
+#endif
+
+/**
+ * Mutable pointer to an immutable value.
+ */
+#ifdef WANT_RAW_PTR
+
+template<typename T> using gc_mutable_ptr = T*;
+
+#else
+
+template<typename T> class gc_mutable_ptr {
+public:
+ inline gc_mutable_ptr(T* const ptr = NULL) noexcept : ptr(ptr) {
}
- ~gc_ptr() throw() {
+ inline ~gc_mutable_ptr() noexcept {
}
- gc_ptr(const gc_ptr& r) throw() : ptr(r.ptr) {
+ inline gc_mutable_ptr(const gc_mutable_ptr& r) noexcept : ptr(r.ptr) {
}
- gc_ptr& operator=(const gc_ptr& r) throw() {
- if(this == &r)
+ inline gc_mutable_ptr& operator=(T* const p) noexcept {
+ ptr = p;
+ return *this;
+ }
+
+ inline gc_mutable_ptr& operator=(const gc_mutable_ptr& r) noexcept {
+ if (this == &r)
return *this;
ptr = r.ptr;
return *this;
}
- const bool operator==(const gc_ptr& r) const throw() {
+ inline const bool operator==(const gc_mutable_ptr& r) const noexcept {
if (this == &r)
return true;
return ptr == r.ptr;
}
- const bool operator==(T* p) const throw() {
+ inline const bool operator==(T* const p) const noexcept {
return ptr == p;
}
- const bool operator!=(const gc_ptr& r) const throw() {
+ inline const bool operator!=(const gc_mutable_ptr& r) const noexcept {
return !this->operator==(r);
}
- const bool operator!=(T* p) const throw() {
+ inline const bool operator!=(T* const p) const noexcept {
return !this->operator==(p);
}
- T& operator*() const throw() {
+ inline T& operator*() const noexcept {
return *ptr;
}
- T* operator->() const throw() {
+ inline T* const operator->() const noexcept {
return ptr;
}
- operator T*() const throw() {
+ inline operator T* const () const noexcept {
return ptr;
}
+private:
T* ptr;
};
+#endif
+
/**
* Initialize APR.
*/
class gc_apr_context_t {
public:
- gc_apr_context_t() {
+ inline gc_apr_context_t() {
apr_initialize();
}
} gc_apr_context;
@@ -130,36 +202,31 @@ public:
*/
class gc_pool {
public:
- gc_pool() : apr_pool(NULL) {
+ inline gc_pool() noexcept : apr_pool(NULL) {
}
- gc_pool(apr_pool_t* p) : apr_pool(p) {
+ inline gc_pool(apr_pool_t* const p) noexcept : apr_pool(p) {
}
- gc_pool(const gc_pool& pool) : apr_pool(pool.apr_pool) {
+ inline gc_pool(const gc_pool& pool) noexcept : apr_pool(pool.apr_pool) {
}
- gc_pool& operator=(const gc_pool& pool) {
- if (this == &pool)
- return *this;
- apr_pool = pool.apr_pool;
- return *this;
- }
+ gc_pool& operator=(const gc_pool& pool) = delete;
private:
- friend apr_pool_t* pool(const gc_pool& pool);
+ friend apr_pool_t* pool(const gc_pool& pool) noexcept;
friend class gc_global_pool_t;
friend class gc_child_pool;
friend class gc_local_pool;
friend class gc_scoped_pool;
- apr_pool_t* apr_pool;
+ apr_pool_t* const apr_pool;
};
/**
* Return the APR pool used by a gc_pool.
*/
-apr_pool_t* pool(const gc_pool& pool) {
+inline apr_pool_t* pool(const gc_pool& pool) noexcept {
return pool.apr_pool;
}
@@ -168,35 +235,51 @@ apr_pool_t* pool(const gc_pool& pool) {
*/
#ifdef WANT_THREADS
+#ifdef __clang__
+
class gc_pool_stack_t {
public:
- gc_pool_stack_t() {
- int rc = pthread_key_create(&key, NULL);
- assertOrFail(rc == 0);
+ inline gc_pool_stack_t() noexcept : key(mkkey()) {
}
- operator apr_pool_t*() const {
- return static_cast<apr_pool_t*>(pthread_getspecific(key));
+ inline operator apr_pool_t*() const noexcept {
+ return (apr_pool_t*)pthread_getspecific(key);
}
- const gc_pool_stack_t& operator=(apr_pool_t* p) {
+ inline const gc_pool_stack_t& operator=(apr_pool_t* p) noexcept {
pthread_setspecific(key, p);
return *this;
}
private:
pthread_key_t key;
+
+ pthread_key_t mkkey() {
+ pthread_key_t k;
+ int rc = pthread_key_create(&k, NULL);
+ assertOrFail(rc == 0);
+ return k;
+ }
+
} gc_pool_stack;
#else
+
+__thread apr_pool_t* gc_pool_stack = NULL;
+
+#endif
+
+#else
+
apr_pool_t* gc_pool_stack = NULL;
+
#endif
/**
* Push a pool onto the stack.
*/
-apr_pool_t* gc_push_pool(apr_pool_t* pool) {
- apr_pool_t* p = gc_pool_stack;
+inline apr_pool_t* const gc_push_pool(apr_pool_t* pool) noexcept {
+ apr_pool_t* const p = gc_pool_stack;
gc_pool_stack = pool;
return p;
}
@@ -204,8 +287,8 @@ apr_pool_t* gc_push_pool(apr_pool_t* poo
/**
* Pop a pool from the stack.
*/
-apr_pool_t* gc_pop_pool(apr_pool_t* pool) {
- apr_pool_t* p = gc_pool_stack;
+inline apr_pool_t* const gc_pop_pool(apr_pool_t* pool) noexcept {
+ apr_pool_t* const p = gc_pool_stack;
gc_pool_stack = pool;
return p;
}
@@ -213,16 +296,17 @@ apr_pool_t* gc_pop_pool(apr_pool_t* pool
/**
* Return the current memory pool.
*/
-apr_pool_t* gc_current_pool() {
- apr_pool_t* p = gc_pool_stack;
+inline apr_pool_t* const gc_current_pool() noexcept {
+ apr_pool_t* const p = gc_pool_stack;
if (p != NULL)
return p;
// Create a parent pool for the current thread
- apr_pool_create(&p, NULL);
- assertOrFail(p != NULL);
- gc_push_pool(p);
- return p;
+ apr_pool_t* pp;
+ apr_pool_create(&pp, NULL);
+ assertOrFail(pp != NULL);
+ gc_push_pool(pp);
+ return pp;
}
/**
@@ -231,25 +315,23 @@ apr_pool_t* gc_current_pool() {
class gc_child_pool : public gc_pool {
public:
- gc_child_pool() : gc_pool(NULL), owner(true) {
- apr_pool_create(&apr_pool, gc_current_pool());
- assertOrFail(apr_pool != NULL);
+ inline gc_child_pool() noexcept : gc_pool(mkpool()), owner(true) {
}
- gc_child_pool(const gc_child_pool& p) : gc_pool(p.apr_pool), owner(false) {
- }
-
- const gc_child_pool& operator=(const gc_child_pool& p) {
- if(this == &p)
- return *this;
- apr_pool = p.apr_pool;
- owner = false;
- return *this;
+ inline gc_child_pool(const gc_child_pool& p) noexcept : gc_pool(p.apr_pool), owner(false) {
}
+ gc_child_pool& operator=(const gc_child_pool& p) = delete;
private:
- bool owner;
+ const bool owner;
+
+ apr_pool_t* const mkpool() {
+ apr_pool_t* p;
+ apr_pool_create(&p, gc_current_pool());
+ assertOrFail(p != NULL);
+ return p;
+ }
};
/**
@@ -258,29 +340,28 @@ private:
class gc_local_pool : public gc_pool {
public:
- gc_local_pool() : gc_pool(NULL), owner(true) {
- apr_pool_create(&apr_pool, gc_current_pool());
- assertOrFail(apr_pool != NULL);
+ inline gc_local_pool() noexcept : gc_pool(mkpool()), owner(true) {
}
- ~gc_local_pool() {
+ inline ~gc_local_pool() noexcept {
if (owner)
apr_pool_destroy(apr_pool);
}
- gc_local_pool(const gc_local_pool& p) : gc_pool(p.apr_pool), owner(false) {
+ inline gc_local_pool(const gc_local_pool& p) noexcept : gc_pool(p.apr_pool), owner(false) {
}
- const gc_local_pool& operator=(const gc_local_pool& p) {
- if(this == &p)
- return *this;
- apr_pool = p.apr_pool;
- owner = false;
- return *this;
- }
+ gc_local_pool& operator=(const gc_local_pool& p) = delete;
private:
- bool owner;
+ const bool owner;
+
+ apr_pool_t* const mkpool() {
+ apr_pool_t* p;
+ apr_pool_create(&p, gc_current_pool());
+ assertOrFail(p != NULL);
+ return p;
+ }
};
/**
@@ -290,66 +371,64 @@ private:
class gc_scoped_pool : public gc_pool {
public:
- gc_scoped_pool() : gc_pool(NULL), prev(gc_current_pool()), owner(true) {
- apr_pool_create(&apr_pool, prev);
- assertOrFail(apr_pool != NULL);
+ inline gc_scoped_pool() noexcept : gc_pool(mkpool()), prev(gc_current_pool()), owner(true) {
gc_push_pool(apr_pool);
}
- gc_scoped_pool(apr_pool_t* p) : gc_pool(p), prev(gc_current_pool()), owner(false) {
+ inline gc_scoped_pool(apr_pool_t* p) noexcept : gc_pool(p), prev(gc_current_pool()), owner(false) {
gc_push_pool(apr_pool);
}
- ~gc_scoped_pool() {
+ inline ~gc_scoped_pool() noexcept {
if (owner)
apr_pool_destroy(apr_pool);
gc_pop_pool(prev);
}
- gc_scoped_pool(const gc_scoped_pool& p) : gc_pool(p.apr_pool), prev(p.prev), owner(false) {
+ inline gc_scoped_pool(const gc_scoped_pool& p) noexcept : gc_pool(p.apr_pool), prev(p.prev), owner(false) {
}
- const gc_scoped_pool& operator=(const gc_scoped_pool& p) {
- if(this == &p)
- return *this;
- apr_pool = p.apr_pool;
- prev = p.prev;
- owner = false;
- return *this;
- }
+ gc_scoped_pool& operator=(const gc_scoped_pool& p) = delete;
private:
- apr_pool_t* prev;
- bool owner;
+ apr_pool_t* const prev;
+ const bool owner;
+
+ apr_pool_t* const mkpool() {
+ apr_pool_t* p;
+ apr_pool_create(&p, gc_current_pool());
+ assertOrFail(p != NULL);
+ return p;
+ }
};
/**
* Allocates a pointer to an object allocated from a memory pool and
* register a cleanup callback for it.
*/
-template<typename T> apr_status_t gc_pool_cleanup(void* v) {
+template<typename T> inline apr_status_t gc_pool_cleanup(void* v) {
T* t = (T*)v;
t->~T();
return APR_SUCCESS;
}
-template<typename T> T* gc_new(apr_pool_t* p) {
+template<typename T> inline T* const gc_new(apr_pool_t* const p) noexcept {
void* gc_new_ptr = apr_palloc(p, sizeof(T));
assertOrFail(gc_new_ptr != NULL);
apr_pool_cleanup_register(p, gc_new_ptr, gc_pool_cleanup<T>, apr_pool_cleanup_null) ;
return (T*)(gc_new_ptr);
}
-template<typename T> T* gc_new(const gc_pool& p) {
+template<typename T> inline T* const gc_new(const gc_pool& p) noexcept {
return gc_new<T>(pool(p));
}
-template<typename T> T* gc_new() {
+template<typename T> inline T* const gc_new() noexcept {
return gc_new<T>(gc_current_pool());
}
-template<typename T> apr_status_t gc_pool_acleanup(void* v) {
- size_t* m = static_cast<size_t*>(v);
+template<typename T> inline apr_status_t gc_pool_acleanup(void* v) {
+ size_t* m = (size_t*)v;
size_t n = *m;
T* t = (T*)(m + 1);
for (size_t i = 0; i < n; i++, t++)
@@ -357,40 +436,101 @@ template<typename T> apr_status_t gc_poo
return APR_SUCCESS;
}
-template<typename T> T* gc_anew(apr_pool_t* p, size_t n) {
- size_t* gc_anew_ptr = static_cast<size_t*>(apr_palloc(p, sizeof(size_t) + sizeof(T) * n));
+template<typename T> inline T* const gc_anew(apr_pool_t* const p, const size_t n) noexcept {
+ size_t* const gc_anew_ptr = (size_t*)apr_palloc(p, sizeof(size_t) + sizeof(T) * n);
assertOrFail(gc_anew_ptr != NULL);
*gc_anew_ptr = n;
apr_pool_cleanup_register(p, gc_anew_ptr, gc_pool_acleanup<T>, apr_pool_cleanup_null) ;
return (T*)(gc_anew_ptr + 1);
}
-template<typename T> T* gc_anew(const gc_pool& p, size_t n) {
+template<typename T> inline T* const gc_anew(const gc_pool& p, const size_t n) noexcept {
return gc_anew<T>(pool(p), n);
}
-template<typename T> T* gc_anew(size_t n) {
+template<typename T> inline T* const gc_anew(const size_t n) noexcept {
return gc_anew<T>(gc_current_pool(), n);
}
/**
* Allocate an array of chars.
*/
-char* gc_cnew(apr_pool_t* p, size_t n) {
- char* gc_cnew_ptr = static_cast<char*>(apr_palloc(p, n));
+inline char* const gc_cnew(apr_pool_t* const p, const size_t n) noexcept {
+ char* const gc_cnew_ptr = (char*)apr_palloc(p, n);
assertOrFail(gc_cnew_ptr != NULL);
return gc_cnew_ptr;
}
-char* gc_cnew(size_t n) {
+inline char* const gc_cnew(const size_t n) noexcept {
return gc_cnew(gc_current_pool(), n);
}
/**
+ * Mutable reference to an immutable value.
+ */
+template<typename T> class gc_mutable_ref {
+public:
+ inline gc_mutable_ref() noexcept : ptr(new (gc_new<T>()) T()) {
+ }
+
+ inline ~gc_mutable_ref() noexcept {
+ }
+
+ inline gc_mutable_ref(const gc_mutable_ref& r) noexcept : ptr(r.ptr) {
+ }
+
+ inline gc_mutable_ref(const T& v) noexcept : ptr(new (gc_new<T>()) T(v)) {
+ }
+
+ inline gc_mutable_ref& operator=(const gc_mutable_ref& r) noexcept {
+ if (this == &r)
+ return *this;
+ ptr = r.ptr;
+ return *this;
+ }
+
+ inline gc_mutable_ref& operator=(const T& v) noexcept {
+ ptr = new (gc_new<T>()) T(v);
+ return *this;
+ }
+
+ inline const bool operator==(const gc_mutable_ref& r) const noexcept {
+ if (this == &r)
+ return true;
+ if (ptr == r.ptr)
+ return true;
+ return *ptr == *r.ptr;
+ }
+
+ inline const bool operator==(const T& v) const noexcept {
+ return *ptr == v;
+ }
+
+ inline const bool operator!=(const gc_mutable_ref& r) const noexcept {
+ return !this->operator==(r);
+ }
+
+ inline const bool operator!=(const T& v) const noexcept {
+ return !this->operator==(v);
+ }
+
+ inline operator T&() const noexcept {
+ return *ptr;
+ }
+
+ inline operator T* const () const noexcept {
+ return ptr;
+ }
+
+private:
+ T* ptr;
+};
+
+/**
* Pool based equivalent of the standard malloc function.
*/
-void* gc_pool_malloc(size_t n) {
- size_t* ptr = static_cast<size_t*>(apr_palloc(gc_current_pool(), sizeof(size_t) + n));
+inline void* gc_pool_malloc(size_t n) {
+ size_t* ptr = (size_t*)apr_palloc(gc_current_pool(), sizeof(size_t) + n);
assertOrFail(ptr != NULL);
*ptr = n;
return ptr + 1;
@@ -399,9 +539,9 @@ void* gc_pool_malloc(size_t n) {
/**
* Pool based equivalent of the standard realloc function.
*/
-void* gc_pool_realloc(void* ptr, size_t n) {
- size_t size = *(static_cast<size_t*>(ptr) - 1);
- size_t* rptr = static_cast<size_t*>(apr_palloc(gc_current_pool(), sizeof(size_t) + n));
+inline void* gc_pool_realloc(void* ptr, size_t n) {
+ size_t size = *(((size_t*)ptr) - 1);
+ size_t* rptr = (size_t*)apr_palloc(gc_current_pool(), sizeof(size_t) + n);
assertOrFail(rptr != NULL);
*rptr = n;
memcpy(rptr + 1, ptr, size < n? size : n);
@@ -411,15 +551,15 @@ void* gc_pool_realloc(void* ptr, size_t
/**
* Pool based equivalent of the standard free function.
*/
-void gc_pool_free(unused void* ptr) {
+inline void gc_pool_free(unused void* ptr) {
// Memory allocated from a pool is freed when the pool is freed
}
/**
* Pool based equivalent of the standard strdup function.
*/
-char* gc_pool_strdup(const char* str) {
- char* dptr = static_cast<char*>(gc_pool_malloc(strlen(str) + 1));
+inline char* gc_pool_strdup(const char* str) {
+ char* dptr = (char*)gc_pool_malloc(strlen(str) + 1);
assertOrFail(dptr != NULL);
strcpy(dptr, str);
return dptr;
@@ -434,9 +574,9 @@ char* gc_pool_strdup(const char* str) {
/**
* Mmap based equivalent of the standard malloc function.
*/
-void* gc_mmap_malloc(size_t n, unused const void* caller) {
+inline void* gc_mmap_malloc(size_t n, unused const void* caller) {
//printf("gc_mmap_malloc %d", n);
- size_t* ptr = static_cast<size_t*>(mmap(NULL, sizeof(size_t) + n, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0));
+ size_t* ptr = (size_t*)mmap(NULL, sizeof(size_t) + n, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
assertOrFail(ptr != NULL);
*ptr = n;
//printf(" %p\n", ptr + 1);
@@ -446,12 +586,12 @@ void* gc_mmap_malloc(size_t n, unused co
/**
* Mmap based equivalent of the standard realloc function.
*/
-void* gc_mmap_realloc(void* ptr, size_t n, const void* caller) {
+inline void* gc_mmap_realloc(void* ptr, size_t n, const void* caller) {
if (ptr == NULL)
return gc_mmap_malloc(n, caller);;
//printf("gc_mmap_realloc %p %d", ptr, n);
- size_t size = *(static_cast<size_t*>(ptr) - 1);
- size_t* rptr = static_cast<size_t*>(mremap(static_cast<size_t*>(ptr) - 1, sizeof(size_t) + size, sizeof(size_t) + n, MREMAP_MAYMOVE, NULL));
+ size_t size = *(((size_t*)ptr) - 1);
+ size_t* rptr = (size_t*)mremap(((size_t*)ptr) - 1, sizeof(size_t) + size, sizeof(size_t) + n, MREMAP_MAYMOVE, NULL);
assertOrFail(rptr != NULL);
*rptr = n;
//printf(" %p\n", rptr + 1);
@@ -461,18 +601,18 @@ void* gc_mmap_realloc(void* ptr, size_t
/**
* Mmap based equivalent of the standard free function.
*/
-void gc_mmap_free(void* ptr, unused const void* caller) {
+inline void gc_mmap_free(void* ptr, unused const void* caller) {
//printf("gc_mmap_free %p\n", ptr);
if (ptr == NULL)
return;
- size_t size = *(static_cast<size_t*>(ptr) - 1);
- munmap(static_cast<size_t*>(ptr) - 1, sizeof(size_t) + size);
+ size_t size = *(((size_t*)ptr) - 1);
+ munmap(((size_t*)ptr) - 1, sizeof(size_t) + size);
}
/**
* Mmap based equivalent of the standard memalign function.
*/
-void* gc_mmap_memalign(unused size_t alignment, size_t n, unused const void* caller) {
+inline void* gc_mmap_memalign(unused size_t alignment, size_t n, unused const void* caller) {
//printf("gc_mmap_memalign %d %d\n", alignment, n);
return gc_mmap_malloc(n, caller);
}
@@ -480,7 +620,7 @@ void* gc_mmap_memalign(unused size_t ali
/**
* Install the mmap based memory allocation functions.
*/
-void gc_mmap_init_hook(void) {
+inline void gc_mmap_init_hook(void) {
__malloc_hook = gc_mmap_malloc;
__realloc_hook = gc_mmap_realloc;
__free_hook = gc_mmap_free;
Modified: tuscany/sca-cpp/trunk/kernel/hash-test.cpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/hash-test.cpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/hash-test.cpp (original)
+++ tuscany/sca-cpp/trunk/kernel/hash-test.cpp Tue Dec 11 03:51:03 2012
@@ -30,89 +30,64 @@
namespace tuscany {
-bool testCrc32hash() {
+const bool testCrc32hash() {
const string key("This is a test key");
- unsigned int h = crc32hash(c_str(key), length(key));
+ const unsigned int h = crc32hash(c_str(key), length(key));
assert(h != 0);
return true;
}
-bool testTimes33hash() {
+const bool testTimes33hash() {
const string key("This is a test key");
- unsigned int h = times33hash(c_str(key), length(key));
+ const unsigned int h = times33hash(c_str(key), length(key));
assert(h != 0);
return true;
}
-bool testMurmurhash() {
+const bool testMurmurhash() {
const string key("This is a test key");
- unsigned int h = murmurhash(c_str(key), length(key));
+ const unsigned int h = murmurhash(c_str(key), length(key));
assert(h != 0);
return true;
}
-bool testPortablemurmurhash() {
+const bool testPortablemurmurhash() {
const string key("This is a test key");
- unsigned int h = portablemurmurhash(c_str(key), length(key));
+ const unsigned int h = portablemurmurhash(c_str(key), length(key));
assert(h != 0);
return true;
}
-struct crc32hashTest {
- const string key;
- crc32hashTest(const string& key) : key(key) {
- }
- bool operator()() const {
- unsigned int h = crc32hash(c_str(key), length(key));
+const bool testHashPerf() {
+ const string key("This is a test key");
+ const int count = 100000;
+
+ const blambda crc32 = [key]() -> const bool {
+ const unsigned int h = crc32hash(c_str(key), length(key));
assert(h != 0);
return true;
- }
-};
+ };
+ cout << "crc32hash test " << time(crc32, 5, count) << " ms" << endl;
-struct times33hashTest {
- const string key;
- times33hashTest(const string& key) : key(key) {
- }
- bool operator()() const {
- unsigned int h = times33hash(c_str(key), length(key));
+ const blambda times33 = [key]() -> const bool {
+ const unsigned int h = times33hash(c_str(key), length(key));
assert(h != 0);
return true;
- }
-};
+ };
+ cout << "times33hash test " << time(times33, 5, count) << " ms" << endl;
-struct murmurhashTest {
- const string key;
- murmurhashTest(const string& key) : key(key) {
- }
- bool operator()() const {
- unsigned int h = murmurhash(c_str(key), length(key));
+ const blambda murmur = [key]() -> const bool {
+ const unsigned int h = murmurhash(c_str(key), length(key));
assert(h != 0);
return true;
- }
-};
+ };
+ cout << "murmurhash test " << time(murmur, 5, count) << " ms" << endl;
-struct portablemurmurhashTest {
- const string key;
- portablemurmurhashTest(const string& key) : key(key) {
- }
- bool operator()() const {
- unsigned int h = portablemurmurhash(c_str(key), length(key));
+ const blambda portablemurmur = [key]() -> const bool {
+ const unsigned int h = portablemurmurhash(c_str(key), length(key));
assert(h != 0);
return true;
- }
-};
-
-bool testHashPerf() {
- const string key("This is a test key");
- const int count = 100000;
-
- const lambda<bool()> crc32 = crc32hashTest(key);
- cout << "crc32hash test " << time(crc32, 5, count) << " ms" << endl;
- const lambda<bool()> times33 = times33hashTest(key);
- cout << "times33hash test " << time(times33, 5, count) << " ms" << endl;
- const lambda<bool()> murmur = murmurhashTest(key);
- cout << "murmurhash test " << time(murmur, 5, count) << " ms" << endl;
- const lambda<bool()> portablemurmur = portablemurmurhashTest(key);
+ };
cout << "portable murmurhash test " << time(portablemurmur, 5, count) << " ms" << endl;
return true;
@@ -121,7 +96,7 @@ bool testHashPerf() {
}
int main() {
- tuscany::gc_scoped_pool p;
+ const tuscany::gc_scoped_pool p;
tuscany::cout << "Testing..." << tuscany::endl;
tuscany::testCrc32hash();
Modified: tuscany/sca-cpp/trunk/kernel/hash.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/hash.hpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/hash.hpp (original)
+++ tuscany/sca-cpp/trunk/kernel/hash.hpp Tue Dec 11 03:51:03 2012
@@ -43,7 +43,7 @@ namespace tuscany
* tree at contrib/ltree/crc32.[ch] and from FreeBSD at
* src/usr.bin/cksum/crc32.c.
*/
-const unsigned int crc32hash(const char* data, const size_t len) {
+inline const unsigned int crc32hash(const char* const data, const size_t len) {
return (unsigned int)apr_memcache_hash_default(NULL, data, len);
}
@@ -89,7 +89,7 @@ const unsigned int crc32hash(const char*
*
* -- Ralf S. Engelschall <rse@engelschall.com>
*/
-const unsigned int times33hash(const char* data, const size_t len) {
+inline const unsigned int times33hash(const char* const data, const size_t len) {
apr_ssize_t l = len;
return apr_hashfunc_default(data, &l);
}
@@ -107,7 +107,7 @@ const unsigned int times33hash(const cha
* Passes Bob Jenkin's frog.c torture-test. No collisions possible for 4 byte
* keys, no small 1 to 7 bit differentials.
*/
-const unsigned int murmurhash(const char* key, const size_t klen) {
+inline const unsigned int murmurhash(const char* const key, const size_t klen) {
unsigned int len = (unsigned int)klen;
const unsigned int seed = 0;
@@ -153,7 +153,7 @@ const unsigned int murmurhash(const char
* An endian and alignment neutral, but half the speed, version of
* the murmur hash.
*/
-const unsigned int portablemurmurhash(const char* key, const size_t klen) {
+inline const unsigned int portablemurmurhash(const char* const key, const size_t klen) {
unsigned int len = (unsigned int)klen;
const unsigned int seed = 0;
@@ -199,7 +199,7 @@ const unsigned int portablemurmurhash(co
return h;
}
-const unsigned int hashselect(const unsigned int hash, const unsigned int max) {
+inline const unsigned int hashselect(const unsigned int hash, const unsigned int max) {
return hash % max;
}
Modified: tuscany/sca-cpp/trunk/kernel/kernel-test.cpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/kernel-test.cpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/kernel-test.cpp (original)
+++ tuscany/sca-cpp/trunk/kernel/kernel-test.cpp Tue Dec 11 03:51:03 2012
@@ -23,6 +23,7 @@
* Test kernel functions.
*/
+#include <functional>
#include <assert.h>
#include "string.hpp"
#include "sstream.hpp"
@@ -36,58 +37,324 @@
namespace tuscany {
-struct inc {
- int i;
- inc(int i) :
+const bool ptrPerf() {
+ int x = 1;
+ for (int i = 0; i < 10000; i++) {
+ int* const y = &x;
+ int z = *y + *y;
+ z = z + 1;
+ }
+ return true;
+}
+
+const bool gcptrPerf() {
+ int x = 1;
+ for (int i = 0; i < 10000; i++) {
+ const gc_ptr<int> y = &x;
+ int z = *y + *y;
+ z = z + 1;
+ }
+ return true;
+}
+
+const bool testPtrPerf() {
+ {
+ const gc_scoped_pool gcp;
+ const blambda pp = ptrPerf;
+ cout << "Pointer test " << (time(pp, 5, 10000) / 10000) << " ms" << endl;
+ }
+ {
+ const gc_scoped_pool gcp;
+ const blambda gpp = gcptrPerf;
+ cout << "GC Pointer test " << (time(gpp, 5, 10000) / 10000) << " ms" << endl;
+ }
+ return true;
+}
+
+class incFunctor {
+public:
+ incFunctor(const int i) :
i(i) {
}
const int operator()(const int x) const {
return x + i;
}
+private:
+ const int i;
};
const int square(const int x) {
return x * x;
}
-int mapLambda(lambda<int(const int)> f, int v) {
+const int mapLambda(lambda<const int(const int)> f, int v) {
return f(v);
}
-bool testLambda() {
- const lambda<int(const int)> sq(square);
+const lambda<const int(const int)> mksquare() {
+ return square;
+}
+
+const bool testSizes() {
+ const gc_ptr<int> p(NULL);
+ cout << "sizeof gc_ptr " << sizeof(p) << endl;
+ const lambda<const int(const int)> sq(square);
+ cout << "sizeof C function lambda " << sizeof(sq) << endl;
+ const lambda<const int(const int)> incl(incFunctor(10));
+ cout << "sizeof functor lambda " << sizeof(incl) << endl;
+ const std::function<int(const int)> sqf(square);
+ cout << "sizeof C function std::function " << sizeof(sqf) << endl;
+ const std::function<int(const int)> incf(incFunctor(10));
+ cout << "sizeof functor std::function " << sizeof(incf) << endl;
+ const string s("abcd");
+ cout << "sizeof string " << sizeof(s) << endl;
+ const string v(s);
+ cout << "sizeof val " << sizeof(v) << endl;
+ const list<int> li = cons<int>(2, mklist<int>(3, 4));
+ cout << "sizeof list<int> " << sizeof(li) << endl;
+ const list<string> ls = cons<string>("a", mklist<string>("b", "c"));
+ cout << "sizeof list<string> " << sizeof(ls) << endl;
+ const list<value> lv = cons<value>("a", mklist<value>("b", "c"));
+ cout << "sizeof list<value> " << sizeof(lv) << endl;
+ return true;
+}
+
+const bool testLambda() {
+ const lambda<const int(const int)> sq(square);
assert(sq(2) == 4);
+
+ const lambda<const lambda<const int(const int)>()> mksq(mksquare);
+ assert(mksq()(2) == 4);
+
assert(mapLambda(sq, 2) == 4);
assert(mapLambda(square, 2) == 4);
- const lambda<int(const int)> incf(inc(10));
+ const lambda<const int(const int)> incf(incFunctor(10));
assert(incf(1) == 11);
assert(mapLambda(incf, 1) == 11);
- assert(mapLambda(inc(10), 1) == 11);
+ assert(mapLambda(incFunctor(10), 1) == 11);
- lambda<int(const int)> l;
- l = incf;
- assert(l(1) == 11);
- l = square;
- assert(l(2) == 4);
+ const int base = 10;
+ auto incl11 = [base](const int i) { return base + i; };
+ const lambda<const int(const int)> incl(incl11);
+ assert(incl(1) == 11);
+ assert(mapLambda(incl, 1) == 11);
+ assert(mapLambda(incl11, 1) == 11);
+
+ const lambda<const int(const int)> il(incf);
+ assert(il(1) == 11);
+ const lambda<const int(const int)> sl(square);
+ assert(sl(2) == 4);
return true;
}
-bool testLambdaGC() {
+int constructCopiable = 0;
+int copyCopiable = 0;
+
+class Copiable {
+public:
+ Copiable() {
+ assert(false);
+ }
+
+ Copiable(const string& s) : s(s) {
+ constructCopiable++;
+ }
+
+ Copiable(const Copiable& c) : s(c.s) {
+ copyCopiable++;
+ }
+
+ Copiable& operator=(const Copiable& c) = delete;
+
+ ~Copiable() {
+ }
+
+ void doit() const {
+ }
+
+private:
+ const string s;
+};
+
+bool testLambdaRef(const Copiable& c) {
+ [&c] { c.doit(); }();
+ return true;
+}
+
+bool testLambdaCopy(const Copiable& c) {
+ [c] { c.doit(); }();
+ return true;
+}
+
+const bool testCopiable() {
+ const Copiable ac = Copiable("assigned");
+ ac.doit();
+ assert(constructCopiable = 1);
+ assert(copyCopiable = 1);
+
+ const Copiable ac2 = []{ return Copiable("returned"); }();
+ ac2.doit();
+ assert(constructCopiable = 2);
+ assert(copyCopiable = 2);
+
+ const Copiable rc = Copiable("captured by ref");
+ testLambdaRef(rc);
+ assert(constructCopiable = 3);
+ assert(copyCopiable = 3);
+
+ const Copiable cc = Copiable("captured by value");
+ testLambdaCopy(cc);
+ assert(constructCopiable = 4);
+ assert(copyCopiable = 5);
+
+ return true;
+}
+
+const bool testMutable() {
+ {
+ gc_mutable_ref<string> s = string("aaa");
+ s = "bbb";
+ assert(s == "bbb");
+ }
+ {
+ gc_mutable_ref<string> s;
+ assert(s == "");
+ s = "bbb";
+ assert(s == "bbb");
+ }
+ {
+ gc_mutable_ref<value> v;
+ assert(isNil((value)v));
+ v = 1;
+ assert(v == 1);
+ }
+ return true;
+}
+
+const bool testLambdaGC() {
resetLambdaCounters();
{
- gc_scoped_pool gc;
+ const gc_scoped_pool gcp;
testLambda();
}
assert(checkLambdaCounters());
return true;
}
-int countElements = 0;
+class sint {
+public:
+ sint(const int i) : i(i) {
+ }
+ const int i;
+ char b[4];
+};
-struct Element {
- int i;
+const int mult(const sint& a, const sint& b) {
+ return a.i * b.i;
+}
+
+const bool testCurry() {
+ const lambda<const int(const sint)> mult2 = curry((lambda<const int(const sint, const sint)>)mult, sint(2));
+ assert(6 == mult2(sint(3)));
+ return true;
+}
+const bool curryPerf() {
+ const sint a(2);
+ const sint b(3);
+ const lambda<const int(const sint)> mult2 = curry((lambda<const int(const sint, const sint)>)mult, a);
+ for(int i = 0; i < 1000; i++)
+ mult2(b);
+ return true;
+}
+
+class multFunctor {
+public:
+ multFunctor(const sint& a) : a(a) {
+ }
+ const int operator()(const sint& b) const {
+ return a.i * b.i;
+ }
+private:
+ const sint a;
+};
+
+const bool testFunctor() {
+ const lambda<const int(const sint)> mult2 = multFunctor(sint(2));
+ assert(6 == mult2(sint(3)));
+ return true;
+}
+
+const bool functorPerf() {
+ const sint a(2);
+ const sint b(3);
+ const lambda<const int(const sint)> mult2 = lambda<const int(const sint)>(multFunctor(a));
+ for(int i = 0; i < 1000; i++)
+ mult2(b);
+ return true;
+}
+
+const bool test11Lambda() {
+ const sint a(2);
+ assert(6 == [a](const sint& b) { return a.i * b.i; } (sint(3)));
+ return true;
+}
+
+const lambda<const sint(const sint)> multlambda11(const sint& a) {
+ return [a](const sint& b) { return a.i * b.i; };
+}
+
+const bool lambda11Perf() {
+ const sint a(2);
+ const sint b(3);
+ const lambda<const sint(const sint)> mult2 = multlambda11(a);
+ for(int i = 0; i < 1000; i++)
+ mult2(b);
+ return true;
+}
+
+const std::function<sint(const sint&)> multfunction11(const sint& a) {
+ return [a](const sint& b) { return a.i * b.i; };
+}
+
+const bool function11Perf() {
+ const sint a(2);
+ const sint b(3);
+ const std::function<sint(const sint&)> mult2 = multfunction11(a);
+ for(int i = 0; i < 1000; i++)
+ mult2(b);
+ return true;
+}
+
+const bool testFuncPerf() {
+ {
+ const gc_scoped_pool gcp;
+ const blambda cp = curryPerf;
+ cout << "Curry test " << (time(cp, 5, 1000) / 1000) << " ms" << endl;
+ }
+ {
+ const gc_scoped_pool gcp;
+ const blambda fp = functorPerf;
+ cout << "Functor test " << (time(fp, 5, 1000) / 1000) << " ms" << endl;
+ }
+ {
+ const gc_scoped_pool gcp;
+ const blambda lp = lambda11Perf;
+ cout << "Lambda11 test " << (time(lp, 5, 1000) / 1000) << " ms" << endl;
+ }
+ {
+ const gc_scoped_pool gcp;
+ const blambda lp = function11Perf;
+ cout << "Function11 test " << (time(lp, 5, 1000) / 1000) << " ms" << endl;
+ }
+ return true;
+}
+
+int countElements = 0;
+
+class Element {
+public:
Element() : i(0) {
countElements++;
}
@@ -107,13 +374,16 @@ struct Element {
const bool operator==(const Element& o) const {
return o.i == i;
}
+
+ int i;
};
+
ostream& operator<<(ostream& out, const Element& v) {
out << v.i ;
return out;
}
-bool testCons() {
+const bool testCons() {
assert(car(cons(2, mklist(3))) == 2);
assert(car(cdr(cons(2, mklist(3)))) == 3);
assert(isNil(cdr(cdr(cons(2, mklist(3))))));
@@ -122,12 +392,12 @@ bool testCons() {
return true;
}
-bool testListGC() {
+const bool testListGC() {
resetLambdaCounters();
resetListCounters();
countElements = 0;
{
- gc_scoped_pool gc;
+ const gc_scoped_pool gcp;
testCons();
}
assert(checkLambdaCounters());
@@ -136,7 +406,41 @@ bool testListGC() {
return true;
}
-bool testOut() {
+template<typename T> const list<T> listPerf(const T& v, const long int i) {
+ if (i == 0)
+ return list<T>();
+ return cons<T>(v, listPerf<T>(v, i -1));
+}
+
+const bool testListPerf() {
+ {
+ const gc_scoped_pool gcp;
+ const blambda lp = []() -> const bool {
+ listPerf<int>(0, 1000);
+ return true;
+ };
+ cout << "List<int> test " << (time(lp, 5, 1000) / 1000) << " ms" << endl;
+ }
+ {
+ const gc_scoped_pool gcp;
+ const blambda lp = []() -> const bool {
+ listPerf<string>(string("x"), 1000);
+ return true;
+ };
+ cout << "List<string> test " << (time(lp, 5, 1000) / 1000) << " ms" << endl;
+ }
+ {
+ const gc_scoped_pool gcp;
+ const blambda lp = []() -> const bool {
+ listPerf<string>(value("x"), 1000);
+ return true;
+ };
+ cout << "List<value> test " << (time(lp, 5, 1000) / 1000) << " ms" << endl;
+ }
+ return true;
+}
+
+const bool testOut() {
ostringstream os1;
os1 << list<int> ();
assert(str(os1) == "()");
@@ -147,7 +451,7 @@ bool testOut() {
return true;
}
-bool testEquals() {
+const bool testEquals() {
assert(list<int>() == list<int>());
assert(mklist(1, 2) == mklist(1, 2));
assert(list<int>() != mklist(1, 2));
@@ -156,14 +460,14 @@ bool testEquals() {
return true;
}
-bool testLength() {
+const bool testLength() {
assert(0 == length(list<int>()));
assert(1 == length(mklist(1)));
assert(2 == length(cons(1, mklist(2))));
return true;
}
-bool testAppend() {
+const bool testAppend() {
assert(car(append(mklist(1), mklist(2))) == 1);
assert(car(cdr(append(mklist(1), mklist(2)))) == 2);
assert(car(cdr(cdr(append(mklist(1), mklist(2, 3))))) == 3);
@@ -173,21 +477,23 @@ bool testAppend() {
return true;
}
-struct Complex {
- int x;
- int y;
- Complex() {
+class Complex {
+public:
+ Complex() : x(0), y(0) {
}
- Complex(int x, int y) :
- x(x), y(y) {
+ Complex(const int x, const int y) : x(x), y(y) {
}
+
+ const int x;
+ const int y;
};
+
ostream& operator<<(ostream& out, const Complex& v) {
out << "[" << v.x << ":" << v.y << "]";
return out;
}
-bool testComplex() {
+const bool testComplex() {
const list<Complex> p = mklist(Complex(1, 2), Complex(3, 4));
assert(car(p).x == 1);
assert(car(cdr(p)).x == 3);
@@ -195,7 +501,7 @@ bool testComplex() {
return true;
}
-bool testMap() {
+const bool testMap() {
assert(isNil(map<int, int>(square, list<int>())));
const list<int> m = map<int, int>(square, mklist(2, 3));
@@ -209,26 +515,26 @@ const int add(const int x, const int y)
return x + y;
}
-bool testReduce() {
- const lambda<int(const int, const int)> r(add);
+const bool testReduce() {
+ const lambda<const int(const int, const int)> r(add);
assert(reduce(r, 0, mklist(1, 2, 3)) == 6);
return true;
}
-bool isPositive(const int x) {
+const bool isPositive(const int x) {
if(x >= 0)
return true;
else
return false;
}
-bool testFilter() {
+const bool testFilter() {
assert(car(filter<int>(isPositive, mklist(1, -1, 2, -2))) == 1);
assert(cadr(filter<int>(isPositive, mklist(1, -1, 2, -2))) == 2);
return true;
}
-bool testMember() {
+const bool testMember() {
assert(isNil(member(4, mklist(1, 2, 3))));
assert(car(member(1, mklist(1, 2, 3))) == 1);
assert(car(member(2, mklist(1, 2, 3))) == 2);
@@ -236,14 +542,14 @@ bool testMember() {
return true;
}
-bool testReverse() {
+const bool testReverse() {
assert(isNil(reverse(list<int>())));
assert(car(reverse(mklist(1, 2, 3))) == 3);
assert(cadr(reverse(mklist(1, 2, 3))) == 2);
return true;
}
-bool testListRef() {
+const bool testListRef() {
assert(listRef(mklist(1), 0) == 1);
assert(listRef(mklist(1, 2, 3), 0) == 1);
assert(listRef(mklist(1, 2, 3), 1) == 2);
@@ -251,7 +557,7 @@ bool testListRef() {
return true;
}
-bool testAssoc() {
+const bool testAssoc() {
const list<list<string> > l = mklist(mklist<string>("x", "X"), mklist<string>("a", "A"), mklist<string>("y", "Y"), mklist<string>("a", "AA"));
assert(assoc<string>("a", l) == mklist<string>("a", "A"));
assert(isNil(assoc<string>("z", l)));
@@ -261,10 +567,13 @@ bool testAssoc() {
const list<value> v = mklist<value>(mklist<value>("x", "X"), mklist<value>("a", "A"), mklist<value>("y", "Y"), mklist<value>("a", "AA"));
assert(assoc<value>("a", v) == mklist<value>("a", "A"));
+
+ const list<value> v2 = mklist<value>(mklist<value>("x", "X"), "a", mklist<value>("a", "A"), mklist<value>("y", "Y"), mklist<value>("a", "AA"));
+ assert(assoc<value>("a", v2) == mklist<value>("a", "A"));
return true;
}
-bool testZip() {
+const bool testZip() {
const list<string> k = mklist<string>("x", "a", "y", "a");
const list<string> v = mklist<string>("X", "A", "Y", "AA");
const list<list<string> > z = mklist(k, v);
@@ -274,7 +583,7 @@ bool testZip() {
return true;
}
-bool testTokenize() {
+const bool testTokenize() {
assert(tokenize("/", "") == list<string>());
assert(tokenize("/", "aaa") == mklist<string>("aaa"));
assert(tokenize("/", "aaa/bbb/ccc/ddd") == mklist<string>("aaa", "bbb", "ccc", "ddd"));
@@ -292,11 +601,11 @@ bool testTokenize() {
return true;
}
-double testSeqMap(double x) {
+const double testSeqMap(const double x) {
return x;
}
-double testSeqReduce(unused double v, double accum) {
+double testSeqReduce(unused const double v, const double accum) {
return accum + 1.0;
}
@@ -316,16 +625,16 @@ bool testSeq() {
return true;
}
-value valueSquare(list<value> x) {
+const value valueSquare(const list<value>& x) {
return (int)car(x) * (int)car(x);
}
-bool testValue() {
- assert(value(true) == value(true));
+const bool testValue() {
+ assert(value(true) == trueValue);
assert(value(1) == value(1));
assert(value("abcd") == value("abcd"));
- lambda<value(const list<value>&)> vl(valueSquare);
- assert(value(vl) == value(vl));
+ lvvlambda vl(valueSquare);
+ //assert(value(vl) == value(vl));
assert(value(mklist<value>(1, 2)) == value(mklist<value>(1, 2)));
const list<value> v = mklist<value>(mklist<value>("x", "X"), mklist<value>("a", "A"), mklist<value>("y", "Y"));
@@ -339,12 +648,12 @@ bool testValue() {
return true;
}
-bool testValueGC() {
+const bool testValueGC() {
resetLambdaCounters();
resetListCounters();
resetValueCounters();
{
- gc_scoped_pool gc;
+ const gc_scoped_pool gcp;
testValue();
}
assert(checkValueCounters());
@@ -353,8 +662,8 @@ bool testValueGC() {
return true;
}
-bool testTree() {
- const list<value> t = mktree<value>("a", list<value>(), list<value>());
+const bool testTree() {
+ const list<value> t = mktree<value>("a", nilListValue, nilListValue);
const list<value> ct = constree<value>("d", constree<value>("f", constree<value>("c", constree<value>("e", constree<value>("b", t)))));
const list<value> mt = mktree(mklist<value>("d", "f", "c", "e", "b", "a"));
assert(mt == ct);
@@ -371,8 +680,8 @@ const list<value> lta(const string& x) {
return mklist<value>(c_str(x), c_str(x + x));
}
-bool testTreeAssoc() {
- const list<value> t = mktree<value>(lta("a"), list<value>(), list<value>());
+const bool testTreeAssoc() {
+ const list<value> t = mktree<value>(lta("a"), nilListValue, nilListValue);
const list<value> at = constree<value>(lta("d"), constree<value>(lta("f"), constree<value>(lta("c"), constree<value>(lta("e"), constree<value>(lta("b"), t)))));
const list<value> l = flatten<value>(at);
assert(length(l) == 6);
@@ -387,58 +696,46 @@ bool testTreeAssoc() {
return true;
}
-double fib_aux(double n, double a, double b) {
+const double fib_aux(const double n, const double a, const double b) {
if(n == 0.0)
return a;
return fib_aux(n - 1.0, b, a + b);
}
-double fib(double n) {
+const double fib(const double n) {
return fib_aux(n, 0.0, 1.0);
}
-struct fibMapPerf {
- const bool operator()() const {
- list<double> s = seq(0.0, 999.0);
- list<double> r = map<double, double>(fib, s);
- assert(1000 == length(r));
- return true;
- }
-};
-
-struct nestedFibMapPerf {
- const lambda<double(const double)> fib;
- nestedFibMapPerf(const lambda<double(const double)>& fib) : fib(fib) {
- }
- const bool operator()() const {
- list<double> s = seq(0.0, 999.0);
- list<double> r = map<double, double>(fib, s);
- assert(1000 == length(r));
- return true;
- }
-};
+const bool fibMapPerf() {
+ const list<double> s = seq(0.0, 999.0);
+ const list<double> r = map<double, double>(fib, s);
+ assert(1000 == length(r));
+ return true;
+}
-bool testCppPerf() {
+const bool testCppPerf() {
{
- const lambda<bool()> fml = fibMapPerf();
+ const gc_scoped_pool gcp;
+ const blambda fml = fibMapPerf;
cout << "Fibonacci map test " << (time(fml, 1, 1) / 1000) << " ms" << endl;
}
{
- struct nested {
- static double fib(double n) {
- struct nested {
- static double fib_aux(double n, double a, double b) {
- if(n == 0.0)
- return a;
- return fib_aux(n - 1.0, b, a + b);
- }
- };
- return nested::fib_aux(n, 0.0, 1.0);
- }
+ const lambda<const double(const double n)> fib = [](const double n) -> const double {
+ const lambda<const double(const double, const double, const double)> fib_aux = [&fib_aux](double n, double a, double b) -> const double {
+ if(n == 0.0)
+ return a;
+ return fib_aux(n - 1.0, b, a + b);
+ };
+ return fib_aux(n, 0.0, 1.0);
};
- const lambda<bool()> nfml = nestedFibMapPerf(lambda<double(const double)>(nested::fib));
+ const blambda nfml = [fib]() -> const bool {
+ const list<double> s = seq(0.0, 999.0);
+ const list<double> r = map<double, double>(fib, s);
+ assert(1000 == length(r));
+ return true;
+ };
cout << "Nested Fibonacci map test " << (time(nfml, 1, 1) / 1000) << " ms" << endl;
}
return true;
@@ -456,7 +753,7 @@ const id<int> idH(const int v) {
return idF(v) >> idG;
}
-bool testIdMonad() {
+const bool testIdMonad() {
const id<int> m(2);
assert(m >> idF == idF(2));
assert(m >> unit<int>() == m);
@@ -476,7 +773,7 @@ const maybe<int> maybeH(const int v) {
return maybeF(v) >> maybeG;
}
-bool testMaybeMonad() {
+const bool testMaybeMonad() {
const maybe<int> m(2);
assert(m >> maybeF == maybeF(2));
assert((m >> just<int>()) == m);
@@ -498,7 +795,7 @@ const failable<int> failableH(const int
return failableF(v) >> failableG;
}
-bool testFailableMonad() {
+const bool testFailableMonad() {
const failable<int> m(2);
assert(m >> failableF == failableF(2));
assert((m >> success<int, string, int>()) == m);
@@ -515,23 +812,16 @@ bool testFailableMonad() {
assert(rcode(vooops) == 500);
const value v = value(vooops);
- assert(car<value>(v) == value());
+ assert(car<value>(v) == nilValue);
assert(cadr<value>(v) == string("test"));
assert(caddr<value>(v) == value((double)500));
return true;
}
-struct tickInc {
- const double v;
- tickInc(const double v) : v(v) {
- }
- const scp<int, double> operator()(int s) const {
- return scp<int, double>(s + 1, v);
- }
-};
-
const state<int, double> tick(const double v) {
- return transformer<int, double>(tickInc(v));
+ return transformer<int, double>([v](const int s) {
+ return scp<int, double>(s + 1, v);
+ });
}
const state<int, double> stateF(const double v) {
@@ -546,8 +836,8 @@ const state<int, double> stateH(const do
return stateF(v) >> stateG;
}
-bool testStateMonad() {
- const lambda<state<int, double>(const double)> r(result<int, double>);
+const bool testStateMonad() {
+ const lambda<const state<int, double>(const double)> r(result<int, double>);
state<int, double> m = result<int, double>(2.0);
assert((m >> stateF)(0) == stateF(2.0)(0));
@@ -558,30 +848,40 @@ bool testStateMonad() {
return true;
}
-bool testDynLib() {
+const bool testDynLib() {
const lib dl(string("./libdynlib-test") + dynlibExt);
- const failable<lambda<int(const int)> > sq(dynlambda<int(const int)>("csquare", dl));
+ const failable<lambda<const int(const int)> > sq(dynlambda<const int(const int)>("csquare", dl));
assert(hasContent(sq));
- lambda<int(const int)> l(content(sq));
+ lambda<const int(const int)> l(content(sq));
assert(l(2) == 4);
- const failable<lambda<lambda<int(const int)>()> > sql(dynlambda<lambda<int(const int)>()>("csquarel", dl));
+ const failable<lambda<const lambda<const int(const int)>()> > sql(dynlambda<const lambda<const int(const int)>()>("csquarel", dl));
assert(hasContent(sql));
- lambda<lambda<int(const int)>()> ll(content(sql));
+ lambda<const lambda<const int(const int)>()> ll(content(sql));
assert(ll()(3) == 9);
return true;
}
+
}
int main() {
- tuscany::gc_scoped_pool p;
+ const tuscany::gc_scoped_pool p;
tuscany::cout << "Testing..." << tuscany::endl;
+ tuscany::testSizes();
+ tuscany::testCopiable();
+ tuscany::testMutable();
+ tuscany::testPtrPerf();
tuscany::testLambda();
tuscany::testLambdaGC();
+ tuscany::testCurry();
+ tuscany::testFunctor();
+ tuscany::test11Lambda();
+ tuscany::testFuncPerf();
tuscany::testCons();
tuscany::testListGC();
+ tuscany::testListPerf();
tuscany::testOut();
tuscany::testEquals();
tuscany::testLength();
Modified: tuscany/sca-cpp/trunk/kernel/lambda-test.cpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/kernel/lambda-test.cpp?rev=1419985&r1=1419984&r2=1419985&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/kernel/lambda-test.cpp (original)
+++ tuscany/sca-cpp/trunk/kernel/lambda-test.cpp Tue Dec 11 03:51:03 2012
@@ -34,7 +34,7 @@ namespace tuscany {
#ifdef HAS_CXX0X_LAMBDAS
const lambda<const int(const int)> inc(const int i) {
- return [=](const int x)->const int {
+ return [i](const int x)->const int {
return x + i;
};
}
@@ -43,27 +43,26 @@ const int square(const int x) {
return x * x;
}
-int mapLambda(const lambda<const int(const int)> f, int v) {
+const int mapLambda(const lambda<const int(const int)> f, int v) {
return f(v);
}
-bool testLambda() {
+const bool testLambda() {
const lambda<const int(const int)> sq = square;
assert(sq(2) == 4);
assert(mapLambda(square, 2) == 4);
assert(mapLambda(sq, 2) == 4);
assert(mapLambda([](const int x)->const int { return x * x; }, 2) == 4);
- const lambda<int(int)> incf = inc(10);
+ const lambda<const int(const int)> incf = inc(10);
assert(incf(1) == 11);
assert(mapLambda(incf, 1) == 11);
assert(mapLambda(inc(10), 1) == 11);
- lambda<const int(const int)> l;
- l = incf;
- assert(l(1) == 11);
- l = square;
- assert(l(2) == 4);
+ const lambda<const int(const int)> il(incf);
+ assert(il(1) == 11);
+ const lambda<const int(const int)> sl(square);
+ assert(sl(2) == 4);
return true;
}
@@ -72,13 +71,13 @@ const double fib_aux(const double n, con
}
const bool fibMapPerf() {
- list<double> s = seq(0.0, 4999.0);
- list<double> r = map<double, double>([](const double n)->const double { return fib_aux(n, 0.0, 1.0); }, s);
+ const list<double> s = seq(0.0, 4999.0);
+ const list<double> r = map<double, double>([](const double n)->const double { return fib_aux(n, 0.0, 1.0); }, s);
assert(5000 == length(r));
return true;
}
-bool testCppPerf() {
+const bool testCppPerf() {
cout << "Fibonacci map test " << (time([]()->const bool { return fibMapPerf(); }, 1, 1) / 5000) << " ms" << endl;
return true;
}
@@ -87,7 +86,7 @@ bool testCppPerf() {
}
int main() {
- tuscany::gc_scoped_pool p;
+ const tuscany::gc_scoped_pool p;
tuscany::cout << "Testing..." << tuscany::endl;
#ifdef HAS_CXX0X_LAMBDAS
|