From 36adc76235fb0a38e7042bc751f988b71627e2a0 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Tue, 11 Dec 2012 06:13:02 +0000 Subject: Changes to get successful C++11 based build. Code cleanup, dependency upgrades, and const + inline optimizations in components, samples, and app hosting server. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1420007 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/components/filedb/Makefile.am | 6 +-- sca-cpp/trunk/components/filedb/client-test.cpp | 71 +++++++++++-------------- sca-cpp/trunk/components/filedb/file-test.cpp | 32 ++++------- sca-cpp/trunk/components/filedb/filedb.cpp | 43 ++++++--------- sca-cpp/trunk/components/filedb/filedb.hpp | 57 ++++++++------------ 5 files changed, 83 insertions(+), 126 deletions(-) (limited to 'sca-cpp/trunk/components/filedb') diff --git a/sca-cpp/trunk/components/filedb/Makefile.am b/sca-cpp/trunk/components/filedb/Makefile.am index c6589a1b7b..e15eb2002e 100644 --- a/sca-cpp/trunk/components/filedb/Makefile.am +++ b/sca-cpp/trunk/components/filedb/Makefile.am @@ -26,15 +26,15 @@ comp_LTLIBRARIES = libfiledb.la noinst_DATA = libfiledb${libsuffix} libfiledb_la_SOURCES = filedb.cpp -libfiledb_la_LDFLAGS = -lxml2 -lmozjs +libfiledb_la_LDFLAGS = -lxml2 -ljansson libfiledb${libsuffix}: ln -s .libs/libfiledb${libsuffix} file_test_SOURCES = file-test.cpp -file_test_LDFLAGS = -lxml2 -lmozjs +file_test_LDFLAGS = -lxml2 -ljansson client_test_SOURCES = client-test.cpp -client_test_LDFLAGS = -lxml2 -lcurl -lmozjs +client_test_LDFLAGS = -lxml2 -lcurl -ljansson dist_noinst_SCRIPTS = filedb-test server-test noinst_PROGRAMS = file-test client-test diff --git a/sca-cpp/trunk/components/filedb/client-test.cpp b/sca-cpp/trunk/components/filedb/client-test.cpp index e0f98d8c3b..5694d97522 100644 --- a/sca-cpp/trunk/components/filedb/client-test.cpp +++ b/sca-cpp/trunk/components/filedb/client-test.cpp @@ -38,15 +38,15 @@ namespace filedb { const string uri("http://localhost:8090/filedb"); -bool testFileDB() { - http::CURLSession cs("", "", "", "", 0); - - const list i = list() + "content" + (list() + "item" - + (list() + "name" + string("Apple")) - + (list() + "price" + string("$2.99"))); - const list a = list() + (list() + "entry" - + (list() + "title" + string("item")) - + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) +const bool testFileDB() { + const http::CURLSession cs("", "", "", "", 0); + + const list i = nilListValue + "content" + (nilListValue + "item" + + (nilListValue + "name" + string("Apple")) + + (nilListValue + "price" + string("$2.99"))); + const list a = nilListValue + (nilListValue + "entry" + + (nilListValue + "title" + string("item")) + + (nilListValue + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + i); const failable id = http::post(a, uri, cs); @@ -59,18 +59,18 @@ bool testFileDB() { assert(content(val) == a); } - const list j = list() + "content" + (list() + "item" - + (list() + "name" + string("Apple")) - + (list() + "price" + string("$3.55"))); - const list b = list() + (list() + "entry" - + (list() + "title" + string("item")) - + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + const list j = nilListValue + "content" + (nilListValue + "item" + + (nilListValue + "name" + string("Apple")) + + (nilListValue + "price" + string("$3.55"))); + const list b = nilListValue + (nilListValue + "entry" + + (nilListValue + "title" + string("item")) + + (nilListValue + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + j); { const failable r = http::put(b, uri + p, cs); assert(hasContent(r)); - assert(content(r) == value(true)); + assert(content(r) == trueValue); } { const failable val = http::get(uri + p, cs); @@ -80,7 +80,7 @@ bool testFileDB() { { const failable r = http::del(uri + p, cs); assert(hasContent(r)); - assert(content(r) == value(true)); + assert(content(r) == trueValue); } { const failable val = http::get(uri + p, cs); @@ -90,35 +90,26 @@ bool testFileDB() { return true; } -struct getLoop { - const string path; - const value entry; - http::CURLSession& cs; - getLoop(const string& path, const value& entry, http::CURLSession& cs) : path(path), entry(entry), cs(cs) { - } - const bool operator()() const { - const failable val = http::get(uri + path, cs); - assert(hasContent(val)); - assert(content(val) == entry); - return true; - } -}; - -bool testGetPerf() { - const list i = list() + "content" + (list() + "item" - + (list() + "name" + string("Apple")) - + (list() + "price" + string("$4.55"))); - const list a = list() + (list() + "entry" - + (list() + "title" + string("item")) - + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) +const bool testGetPerf() { + const list i = nilListValue + "content" + (nilListValue + "item" + + (nilListValue + "name" + string("Apple")) + + (nilListValue + "price" + string("$4.55"))); + const list a = nilListValue + (nilListValue + "entry" + + (nilListValue + "title" + string("item")) + + (nilListValue + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + i); - http::CURLSession cs("", "", "", "", 0); + const http::CURLSession cs("", "", "", "", 0); const failable id = http::post(a, uri, cs); assert(hasContent(id)); const string p = path(content(id)); - const lambda gl = getLoop(p, a, cs); + const blambda gl = [p, a, cs]() -> const bool { + const failable val = http::get(uri + p, cs); + assert(hasContent(val)); + assert(content(val) == a); + return true; + }; cout << "FileDB get test " << time(gl, 5, 200) << " ms" << endl; return true; diff --git a/sca-cpp/trunk/components/filedb/file-test.cpp b/sca-cpp/trunk/components/filedb/file-test.cpp index ff57bd79ce..5270967ccb 100644 --- a/sca-cpp/trunk/components/filedb/file-test.cpp +++ b/sca-cpp/trunk/components/filedb/file-test.cpp @@ -32,12 +32,12 @@ namespace tuscany { namespace filedb { -bool testFileDB(const string& dbname, const string& format) { - FileDB db(dbname, format); +const bool testFileDB(const string& dbname, const string& format) { + const FileDB db(dbname, format); const value k = mklist("a", "b"); - const list a = mklist(list() + "ns1:a" + (list() + "@xmlns:ns1" + string("http://aaa")) + (list() + "text" + string("Hey!"))); - const list b = mklist(list() + "ns1:b" + (list() + "@xmlns:ns1" + string("http://bbb")) + (list() + "text" + string("Hey!"))); + const list a = mklist(nilListValue + "ns1:a" + (nilListValue + "@xmlns:ns1" + string("http://aaa")) + (nilListValue + "text" + string("Hey!"))); + const list b = mklist(nilListValue + "ns1:b" + (nilListValue + "@xmlns:ns1" + string("http://bbb")) + (nilListValue + "text" + string("Hey!"))); assert(hasContent(post(k, a, db))); assert((get(k, db)) == value(a)); @@ -50,27 +50,17 @@ bool testFileDB(const string& dbname, const string& format) { return true; } -struct getLoop { - const value k; - FileDB& db; - const list c; - getLoop(const value& k, FileDB& db) : k(k), db(db), - c(mklist(list() + "ns1:c" + (list() + "@xmlns:ns1" + string("http://ccc")) + (list() + "text" + string("Hey!")))) { - } - const bool operator()() const { - assert((get(k, db)) == value(c)); - return true; - } -}; - -bool testGetPerf(const string& dbname, const string& format) { - FileDB db(dbname, format); +const bool testGetPerf(const string& dbname, const string& format) { + const FileDB db(dbname, format); const value k = mklist("c"); - const list c = mklist(list() + "ns1:c" + (list() + "@xmlns:ns1" + string("http://ccc")) + (list() + "text" + string("Hey!"))); + const list c = mklist(nilListValue + "ns1:c" + (nilListValue + "@xmlns:ns1" + string("http://ccc")) + (nilListValue + "text" + string("Hey!"))); assert(hasContent(post(k, c, db))); - const lambda gl = getLoop(k, db); + const blambda gl = [k, c, db]() -> const bool { + assert((get(k, db)) == value(c)); + return true; + }; cout << "FileDB get test " << time(gl, 5, 5000) << " ms" << endl; return true; } diff --git a/sca-cpp/trunk/components/filedb/filedb.cpp b/sca-cpp/trunk/components/filedb/filedb.cpp index 8644a78574..37cb6c5260 100644 --- a/sca-cpp/trunk/components/filedb/filedb.cpp +++ b/sca-cpp/trunk/components/filedb/filedb.cpp @@ -37,14 +37,14 @@ namespace filedb { /** * Get an item from the database. */ -const failable get(const list& params, filedb::FileDB& db) { +const failable get(const list& params, const filedb::FileDB& db) { return filedb::get(car(params), db); } /** * Post an item to the database. */ -const failable post(const list& params, filedb::FileDB& db) { +const failable post(const list& params, const filedb::FileDB& db) { const value id = append(car(params), mklist(mkuuid())); const failable val = filedb::post(id, cadr(params), db); if (!hasContent(val)) @@ -55,7 +55,7 @@ const failable post(const list& params, filedb::FileDB& db) { /** * Put an item into the database. */ -const failable put(const list& params, filedb::FileDB& db) { +const failable put(const list& params, const filedb::FileDB& db) { const failable val = filedb::put(car(params), cadr(params), db); if (!hasContent(val)) return mkfailure(val); @@ -65,7 +65,7 @@ const failable put(const list& params, filedb::FileDB& db) { /** * Delete an item from the database. */ -const failable del(const list& params, filedb::FileDB& db) { +const failable del(const list& params, const filedb::FileDB& db) { const failable val = filedb::del(car(params), db); if (!hasContent(val)) return mkfailure(val); @@ -73,14 +73,17 @@ const failable del(const list& params, filedb::FileDB& db) { } /** - * Component implementation lambda function. + * Start the component. */ -class applyfiledb { -public: - applyfiledb(filedb::FileDB& db) : db(db) { - } +const failable start(const list& params) { + // Connect to the configured database and table + const value dbname = ((lvvlambda)car(params))(nilListValue); + const value format = ((lvvlambda)cadr(params))(nilListValue); + + const filedb::FileDB& db = *(new (gc_new()) filedb::FileDB(absdbname(dbname), format)); - const value operator()(const list& params) const { + // Return the component implementation lambda function + const lvvlambda applyfiledb = [db](const list& params) -> const value { const value func(car(params)); if (func == "get") return get(cdr(params), db); @@ -91,24 +94,8 @@ public: if (func == "delete") return del(cdr(params), db); return mkfailure(); - } - -private: - filedb::FileDB& db; -}; - -/** - * Start the component. - */ -const failable start(const list& params) { - // Connect to the configured database and table - const value dbname = ((lambda&)>)car(params))(list()); - const value format = ((lambda&)>)cadr(params))(list()); - - filedb::FileDB& db = *(new (gc_new()) filedb::FileDB(absdbname(dbname), format)); - - // Return the component implementation lambda function - return value(lambda&)>(applyfiledb(db))); + }; + return value(applyfiledb); } } diff --git a/sca-cpp/trunk/components/filedb/filedb.hpp b/sca-cpp/trunk/components/filedb/filedb.hpp index 9c3017d0d8..2855cebfc6 100644 --- a/sca-cpp/trunk/components/filedb/filedb.hpp +++ b/sca-cpp/trunk/components/filedb/filedb.hpp @@ -32,8 +32,8 @@ #include "monad.hpp" #include "fstream.hpp" #include "element.hpp" -#include "xml.hpp" #include "../../modules/scheme/eval.hpp" +#include "../../modules/xml/xml.hpp" #include "../../modules/json/json.hpp" namespace tuscany { @@ -68,30 +68,22 @@ public: debug("filedb::filedb::copy"); } - const FileDB& operator=(const FileDB& c) { - debug("filedb::filedb::operator="); - if(this == &c) - return *this; - owner = false; - name = c.name; - format = c.format; - return *this; - } + FileDB& operator=(const FileDB& c) = delete; ~FileDB() { } private: - bool owner; - string name; - string format; + const bool owner; + const string name; + const string format; friend const failable write(const value& v, ostream& os, const string& format); friend const failable read(istream& is, const string& format); - friend const failable post(const value& key, const value& val, FileDB& db); - friend const failable put(const value& key, const value& val, FileDB& db); - friend const failable get(const value& key, FileDB& db); - friend const failable del(const value& key, FileDB& db); + friend const failable post(const value& key, const value& val, const FileDB& db); + friend const failable put(const value& key, const value& val, const FileDB& db); + friend const failable get(const value& key, const FileDB& db); + friend const failable del(const value& key, const FileDB& db); }; /** @@ -100,7 +92,7 @@ private: const string filename(const list& path, const string& root) { if (isNil(path)) return root; - const string name = root + "/" + (isString(car(path))? (string)car(path) : scheme::writeValue(car(path))); + const string name = root + "/" + (isString(car(path))? (string)car(path) : write(content(scheme::writeValue(car(path))))); return filename(cdr(path), name); } @@ -116,7 +108,7 @@ const string filename(const value& key, const string& root) { const failable mkdirs(const list& path, const string& root) { if (isNil(cdr(path))) return true; - const string dir = root + "/" + (isString(car(path))? (string)car(path) : scheme::writeValue(car(path))); + const string dir = root + "/" + (isString(car(path))? (string)car(path) : write(content(scheme::writeValue(car(path))))); mkdir(c_str(dir), S_IRWXU); return mkdirs(cdr(path), dir); } @@ -126,20 +118,19 @@ const failable mkdirs(const list& path, const string& root) { */ const failable write(const value& v, ostream& os, const string& format) { if (format == "scheme") { - const string vs(scheme::writeValue(v)); + const string vs(write(content(scheme::writeValue(v)))); os << vs; return true; } if (format == "xml") { - failable > s = writeXML(valuesToElements(v)); + failable > s = xml::writeElements(valuesToElements(v)); if (!hasContent(s)) return mkfailure(s); write(content(s), os); return true; } if (format == "json") { - js::JSContext jscx; - failable > s = json::writeJSON(valuesToElements(v), jscx); + failable > s = json::writeValue(v); if (!hasContent(s)) return mkfailure(s); write(content(s), os); @@ -153,27 +144,25 @@ const failable write(const value& v, ostream& os, const string& format) { */ const failable read(istream& is, const string& format) { if (format == "scheme") { - return scheme::readValue(is); + return scheme::readValue(streamList(is)); } if (format == "xml") { - const value v = elementsToValues(readXML(streamList(is))); - return v; - } - if (format == "json") { - js::JSContext jscx; - const failable > fv = json::readJSON(streamList(is), jscx); + const failable > fv = xml::readElements(streamList(is)); if (!hasContent(fv)) return mkfailure(fv); const value v = elementsToValues(content(fv)); return v; } + if (format == "json") { + return json::readValue(streamList(is)); + } return mkfailure(string("Unsupported database format: ") + format); } /** * Post a new item to the database. */ -const failable post(const value& key, const value& val, FileDB& db) { +const failable post(const value& key, const value& val, const FileDB& db) { debug(key, "filedb::post::key"); debug(val, "filedb::post::value"); debug(db.name, "filedb::post::dbname"); @@ -197,7 +186,7 @@ const failable post(const value& key, const value& val, FileDB& db) { /** * Update an item in the database. If the item doesn't exist it is added. */ -const failable put(const value& key, const value& val, FileDB& db) { +const failable put(const value& key, const value& val, const FileDB& db) { debug(key, "filedb::put::key"); debug(val, "filedb::put::value"); debug(db.name, "filedb::put::dbname"); @@ -221,7 +210,7 @@ const failable put(const value& key, const value& val, FileDB& db) { /** * Get an item from the database. */ -const failable get(const value& key, FileDB& db) { +const failable get(const value& key, const FileDB& db) { debug(key, "filedb::get::key"); debug(db.name, "filedb::get::dbname"); @@ -242,7 +231,7 @@ const failable get(const value& key, FileDB& db) { /** * Delete an item from the database */ -const failable del(const value& key, FileDB& db) { +const failable del(const value& key, const FileDB& db) { debug(key, "filedb::delete::key"); debug(db.name, "filedb::delete::dbname"); -- cgit v1.2.3