From ae0b7c0063db6236be2d7cf01ddbf2159f77c98c Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Tue, 11 Dec 2012 03:51:03 +0000 Subject: 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. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1419985 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/kernel/element.hpp | 103 ++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 62 deletions(-) (limited to 'sca-cpp/trunk/kernel/element.hpp') diff --git a/sca-cpp/trunk/kernel/element.hpp b/sca-cpp/trunk/kernel/element.hpp index 27b5af8691..9ae1e632cb 100644 --- a/sca-cpp/trunk/kernel/element.hpp +++ b/sca-cpp/trunk/kernel/element.hpp @@ -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(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(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& l) { +inline const value attributeName(const list& l) { return cadr(l); } /** * Returns the value of an attribute. */ -const value attributeValue(const list& l) { +inline const value attributeValue(const list& l) { return caddr(l); } /** * Returns the name of an element. */ -const value elementName(const list& l) { +inline const value elementName(const list& l) { return cadr(l); } /** * Returns true if an element has children. */ -const bool elementHasChildren(const list& l) { +inline const bool elementHasChildren(const list& l) { return !isNil(cddr(l)); } /** * Returns the children of an element. */ -const list elementChildren(const list& l) { +inline const list elementChildren(const list& l) { return cddr(l); } /** * Returns true if an element has a value. */ -const bool elementHasValue(const list& l) { +inline const bool elementHasValue(const list& l) { const list r = reverse(l); if (isSymbol(car(r))) return false; @@ -107,26 +107,26 @@ const bool elementHasValue(const list& l) { /** * Returns the value of an element. */ -const value elementValue(const list& l) { +inline const value elementValue(const list& 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 l = v; return (isNil(l) || !isSymbol(car(l))); } -const value elementToValue(const value& t) { +inline const value elementToValue(const value& t) { const list elementsToValues(const list& e); // Convert an attribute if (isTaggedList(t, attribute)) - return mklist(c_str(atsign + attributeName(t)), attributeValue(t)); + return mklist(c_str(atsign + (string)attributeName(t)), attributeValue(t)); // Convert an element if (isTaggedList(t, element)) { @@ -155,7 +155,7 @@ const value elementToValue(const value& t) { /** * 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 l = v; @@ -166,7 +166,7 @@ const bool elementToValueIsSymbol(const value& v) { return true; } -const list elementToValueGroupValues(const value& v, const list& l) { +inline const list elementToValueGroupValues(const value& v, const list& l) { if (isNil(l) || !elementToValueIsSymbol(v) || !elementToValueIsSymbol(car(l))) return cons(v, l); if (car(car(l)) != car(v)) @@ -180,7 +180,7 @@ const list elementToValueGroupValues(const value& v, const list& l } -const list elementsToValues(const list& e) { +inline const list elementsToValues(const list& e) { if (isNil(e)) return e; return elementToValueGroupValues(elementToValue(car(e)), elementsToValues(cdr(e))); @@ -189,13 +189,13 @@ const list elementsToValues(const list& e) { /** * Convert a value to an element. */ -const value valueToElement(const value& t) { +inline const value valueToElement(const value& t) { const list valuesToElements(const list& l); // Convert a name value pair if (isList(t) && !isNil((list)t) && isSymbol(car(t))) { const value n = car(t); - const value v = isNil(cdr(t))? value() : cadr(t); + const value v = isNil(cdr(t))? nilValue : cadr(t); // Convert a single value to an attribute or an element if (!isList(v)) { @@ -221,7 +221,7 @@ const value valueToElement(const value& t) { /** * Convert a list of values to a list of elements. */ -const list valuesToElements(const list& l) { +inline const list valuesToElements(const list& l) { if (isNil(l)) return l; return cons(valueToElement(car(l)), valuesToElements(cdr(l))); @@ -231,72 +231,51 @@ const list valuesToElements(const list& l) { * Returns a selector lambda function which can be used to filter * elements against the given element pattern. */ -struct selectorLambda { - const list select; - selectorLambda(const list& s) : select(s) { - } - const bool evalSelect(const list& s, const list 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 selector(const list& select) { + return [select](const value& v) -> const bool { + const lambda&, const list&)> evalSelect = [&evalSelect](const list& s, const list& 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 selector(const list 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)v) == name; - } -}; - -const value attributeValue(const value& name, const value& l) { - const list f = filter(filterAttribute(name), list(l)); +inline const value attributeValue(const value& name, const value& l) { + const list f = filter([name](const value& v) { + return isAttribute(v) && attributeName((list)v) == name; + }, list(l)); if (isNil(f)) - return value(); + return nilValue; return caddr(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)v) == name; - } -}; - -const value elementChildren(const value& name, const value& l) { - return filter(filterElement(name), list(l)); +inline const value elementChildren(const value& name, const value& l) { + return filter([name](const value& v) { + return isElement(v) && elementName((list)v) == name; + }, list(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 f = elementChildren(name, l); if (isNil(f)) - return value(); + return nilValue; return car(f); } -- cgit v1.2.3