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/cache/memcache.hpp | 78 ++++++++++++++++------------- 1 file changed, 42 insertions(+), 36 deletions(-) (limited to 'sca-cpp/trunk/components/cache/memcache.hpp') diff --git a/sca-cpp/trunk/components/cache/memcache.hpp b/sca-cpp/trunk/components/cache/memcache.hpp index f18405b2ec..00ee9c6291 100644 --- a/sca-cpp/trunk/components/cache/memcache.hpp +++ b/sca-cpp/trunk/components/cache/memcache.hpp @@ -48,43 +48,28 @@ namespace memcache { */ class MemCached { public: - MemCached() : owner(false) { + MemCached() : owner(false), mc(NULL) { } - MemCached(const string host, const int port) : p(), owner(true) { - debug(host, "memcache::memcached::host"); - debug(port, "memcache::memcached::port"); - apr_memcache_create(pool(p), 1, 0, &mc); - addServer(host, port); + MemCached(const string& host, const int port) : p(), owner(true), mc(mkmc(host, port)) { } - MemCached(const list& servers) : p(), owner(true) { - debug(servers, "memcache::memcached::servers"); - apr_memcache_create(pool(p), (apr_uint16_t)length(servers), 0, &mc); - addServers(servers); + MemCached(const list& servers) : p(), owner(true), mc(mkmc(servers)) { } MemCached(const MemCached& c) : p(c.p), owner(false), mc(c.mc) { debug("memcache::memcached::copy"); } - const MemCached& operator=(const MemCached& c) { - debug("memcache::memcached::operator="); - if(this == &c) - return *this; - p = c.p; - owner = false; - mc = c.mc; - return *this; - } + MemCached& operator=(const MemCached& c) = delete; ~MemCached() { } private: - gc_child_pool p; - bool owner; - apr_memcache_t* mc; + const gc_child_pool p; + const bool owner; + apr_memcache_t* const mc; friend const failable post(const value& key, const value& val, const MemCached& cache); friend const failable put(const value& key, const value& val, const MemCached& cache); @@ -94,7 +79,7 @@ private: /** * Add servers to the memcached context. */ - const failable addServer(const string& host, const int port) { + const failable addServer(apr_memcache_t* const m, const string& host, const int port) { apr_memcache_server_t *server; const apr_status_t sc = apr_memcache_server_create(pool(p), c_str(host), (apr_port_t)port, 1, 1, 1, 600, &server); if (sc != APR_SUCCESS) { @@ -102,27 +87,48 @@ private: os << "Couldn't connect to memcached server: " << host << ":" << port; return mkfailure(str(os)); } - const apr_status_t as = apr_memcache_add_server(mc, server); + const apr_status_t as = apr_memcache_add_server(m, server); if (as != APR_SUCCESS) return mkfailure("Couldn't add memcached server"); return true; } - const failable addServers(const list& servers) { + const failable addServers(apr_memcache_t* const m, const list& servers) { if (isNil(servers)) return true; const list toks = tokenize(":", car(servers)); - const failable r = addServer(car(toks), isNil(cdr(toks))? 11211 : atoi(c_str(cadr(toks)))); + const failable r = addServer(m, car(toks), isNil(cdr(toks))? 11211 : atoi(c_str(cadr(toks)))); if (!hasContent(r)) return r; - return addServers(cdr(servers)); + return addServers(m, cdr(servers)); + } + + /** + * Create memcached handle. + */ + apr_memcache_t* const mkmc(const string& host, const int port) { + debug(host, "memcache::memcached::host"); + debug(port, "memcache::memcached::port"); + apr_memcache_t* m; + apr_memcache_create(pool(p), 1, 0, &m); + addServer(m, host, port); + return m; } + + apr_memcache_t* const mkmc(const list& servers) { + debug(servers, "memcache::memcached::servers"); + apr_memcache_t* m; + apr_memcache_create(pool(p), (apr_uint16_t)length(servers), 0, &m); + addServers(m, servers); + return m; + } + }; /** * Replace spaces by tabs (as spaces are not allowed in memcached keys). */ -const char* nospaces(const char* s) { +const char* const nospaces(const char* const s) { char* c = const_cast(s); for (; *c; c++) if (*c == ' ') @@ -137,8 +143,8 @@ const failable post(const value& key, const value& val, const MemCached& c debug(key, "memcache::post::key"); debug(val, "memcache::post::value"); - const string ks(scheme::writeValue(key)); - const string vs(scheme::writeValue(val)); + const string ks(write(content(scheme::writeValue(key)))); + const string vs(write(content(scheme::writeValue(val)))); const apr_status_t rc = apr_memcache_add(cache.mc, nospaces(c_str(ks)), const_cast(c_str(vs)), length(vs), 0, 27); if (rc != APR_SUCCESS) { ostringstream os; @@ -157,8 +163,8 @@ const failable put(const value& key, const value& val, const MemCached& ca debug(key, "memcache::put::key"); debug(val, "memcache::put::value"); - const string ks(scheme::writeValue(key)); - const string vs(scheme::writeValue(val)); + const string ks(write(content(scheme::writeValue(key)))); + const string vs(write(content(scheme::writeValue(val)))); const apr_status_t rc = apr_memcache_set(cache.mc, nospaces(c_str(ks)), const_cast(c_str(vs)), length(vs), 0, 27); if (rc != APR_SUCCESS) { ostringstream os; @@ -176,17 +182,17 @@ const failable put(const value& key, const value& val, const MemCached& ca const failable get(const value& key, const MemCached& cache) { debug(key, "memcache::get::key"); - const string ks(scheme::writeValue(key)); + const string ks(write(content(scheme::writeValue(key)))); char *data; apr_size_t size; - gc_local_pool lp; + const gc_local_pool lp; const apr_status_t rc = apr_memcache_getp(cache.mc, pool(lp), nospaces(c_str(ks)), &data, &size, NULL); if (rc != APR_SUCCESS) { ostringstream os; os << "Couldn't get memcached entry: " << key; return mkfailure(str(os), 404, false); } - const value val(scheme::readValue(string(data, size))); + const value val(content(scheme::readValue(string(data, size)))); debug(val, "memcache::get::result"); return val; @@ -198,7 +204,7 @@ const failable get(const value& key, const MemCached& cache) { const failable del(const value& key, const MemCached& cache) { debug(key, "memcache::delete::key"); - const string ks(scheme::writeValue(key)); + const string ks(write(content(scheme::writeValue(key)))); const apr_status_t rc = apr_memcache_delete(cache.mc, nospaces(c_str(ks)), 0); if (rc != APR_SUCCESS) { ostringstream os; -- cgit v1.2.3