From: Padraig O'Sullivan Date: Fri, 10 Jul 2009 04:19:15 +0000 (-0400) Subject: Making some modifications to the C++ interface. Mainly going from const char X-Git-Tag: 0.32~17^2~10 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=71e7ddb8865ac660dfa6c1f5c11204aa90ba075f;p=m6w6%2Flibmemcached Making some modifications to the C++ interface. Mainly going from const char * to const std::string&. Also updated the C++ client test source file. --- diff --git a/libmemcached/memcached.hh b/libmemcached/memcached.hh index 2f2d519e..55b09945 100644 --- a/libmemcached/memcached.hh +++ b/libmemcached/memcached.hh @@ -2,6 +2,8 @@ #include #include +#include + class Memcached { memcached_st memc; @@ -9,41 +11,73 @@ class Memcached public: - Memcached() : memc(), result() + Memcached() + : + memc(), + result() { memcached_create(&memc); } - Memcached(memcached_st *clone) : memc(), result() + Memcached(memcached_st *clone) + : + memc(), + result() { memcached_clone(&memc, clone); } - char *fetch (char *key, size_t *key_length, size_t *value_length) + + ~Memcached() + { + memcached_free(&memc); + } + + std::string fetch(const std::string& key, size_t *key_length, size_t *value_length) { uint32_t flags; memcached_return rc; + std::string ret_val; - return memcached_fetch(&memc, key, key_length, - value_length, &flags, &rc); + char *value= memcached_fetch(&memc, const_cast(key.c_str()), key_length, + value_length, &flags, &rc); + if (value) + { + ret_val.assign(value); + } + return ret_val; } - char *get(const char *key, size_t *value_length) + + std::string get(const std::string& key, size_t *value_length) { uint32_t flags; memcached_return rc; + std::string ret_val; - return memcached_get(&memc, key, strlen(key), - value_length, &flags, &rc); + char *value= memcached_get(&memc, key.c_str(), key.length(), + value_length, &flags, &rc); + if (value) + { + ret_val.assign(value); + } + return ret_val; } - char *get_by_key(const char *master_key, const char *key, + std::string get_by_key(const std::string& master_key, + const std::string& key, size_t *value_length) { uint32_t flags; memcached_return rc; + std::string ret_val; - return memcached_get_by_key(&memc, master_key, strlen(master_key), - key, strlen(key), - value_length, &flags, &rc); + char *value= memcached_get_by_key(&memc, master_key.c_str(), master_key.length(), + key.c_str(), key.length(), + value_length, &flags, &rc); + if (value) + { + ret_val.assign(value); + } + return ret_val; } memcached_return mget(char **keys, size_t *key_length, @@ -53,28 +87,32 @@ public: return memcached_mget(&memc, keys, key_length, number_of_keys); } - memcached_return set(const char *key, const char *value, size_t value_length) + memcached_return set(const std::string& key, const char *value, size_t value_length) { - return memcached_set(&memc, key, strlen(key), + return memcached_set(&memc, key.c_str(), key.length(), value, value_length, time_t(0), uint32_t(0)); } - memcached_return set_by_key(const char *master_key, const char *key, + memcached_return set_by_key(const std::string& master_key, + const std::string& key, const char *value, size_t value_length) { - return memcached_set_by_key(&memc, master_key, strlen(master_key), - key, strlen(key), - value, value_length, - time_t(0), - uint32_t(0) ); + return memcached_set_by_key(&memc, master_key.c_str(), + master_key.length(), + key.c_str(), key.length(), + value, value_length, + time_t(0), + uint32_t(0)); } + memcached_return - increment(const char *key, unsigned int offset, uint64_t *value) + increment(const std::string& key, unsigned int offset, uint64_t *value) { - return memcached_increment(&memc, key, strlen(key), + return memcached_increment(&memc, key.c_str(), key.length(), offset, value); } + memcached_return decrement(const char *key, unsigned int offset, uint64_t *value) { @@ -87,6 +125,7 @@ public: { return memcached_add(&memc, key, strlen(key), value, value_length, 0, 0); } + memcached_return add_by_key(const char *master_key, const char *key, const char *value, size_t value_length) { @@ -103,6 +142,7 @@ public: value, value_length, 0, 0); } + memcached_return replace_by_key(const char *master_key, const char *key, const char *value, size_t value_length) { @@ -117,6 +157,7 @@ public: return memcached_prepend(&memc, key, strlen(key), value, value_length, 0, 0); } + memcached_return prepend_by_key(const char *master_key, const char *key, const char *value, size_t value_length) { @@ -133,6 +174,7 @@ public: return memcached_append(&memc, key, strlen(key), value, value_length, 0, 0); } + memcached_return append_by_key(const char *master_key, const char *key, const char *value, size_t value_length) { @@ -141,12 +183,14 @@ public: key, strlen(key), value, value_length, 0, 0); } + memcached_return cas(const char *key, const char *value, size_t value_length, uint64_t cas_arg) { return memcached_cas(&memc, key, strlen(key), value, value_length, 0, 0, cas_arg); } + memcached_return cas_by_key(const char *master_key, const char *key, const char *value, size_t value_length, uint64_t cas_arg) @@ -157,19 +201,17 @@ public: value, value_length, 0, 0, cas_arg); } + // using 'remove' vs. 'delete' since 'delete' is a keyword memcached_return remove(const char *key) { return memcached_delete (&memc, key, strlen(key), 0); } + memcached_return delete_by_key(const char *master_key, const char *key) { return memcached_delete_by_key(&memc, master_key, strlen(master_key), key, strlen(key), 0); } - ~Memcached() - { - memcached_free(&memc); - } }; diff --git a/tests/plus.cpp b/tests/plus.cpp index d3674d6c..1b7be39d 100644 --- a/tests/plus.cpp +++ b/tests/plus.cpp @@ -16,6 +16,10 @@ #include "test.h" +#include + +using namespace std; + extern "C" { test_return basic_test(memcached_st *memc); uint8_t increment_test(memcached_st *memc); @@ -28,13 +32,13 @@ test_return basic_test(memcached_st *memc) { Memcached foo(memc); const char *value_set= "This is some data"; - char *value; + string value; size_t value_length; foo.set("mine", value_set, strlen(value_set)); value= foo.get("mine", &value_length); - assert((memcmp(value, value_set, value_length) == 0)); + assert((memcmp(value.c_str(), value_set, value_length) == 0)); return TEST_SUCCESS; } @@ -43,18 +47,18 @@ uint8_t increment_test(memcached_st *memc) { Memcached mcach(memc); memcached_return rc; - const char *key= "inctest"; + const string key("inctest"); const char *inc_value= "1"; - char *ret_value; + string ret_value; uint64_t int_inc_value; uint64_t int_ret_value; size_t value_length; mcach.set(key, inc_value, strlen(inc_value)); ret_value= mcach.get(key, &value_length); - printf("\nretvalue %s\n",ret_value); + printf("\nretvalue %s\n",ret_value.c_str()); int_inc_value= uint64_t(atol(inc_value)); - int_ret_value= uint64_t(atol(ret_value)); + int_ret_value= uint64_t(atol(ret_value.c_str())); assert(int_ret_value == int_inc_value); rc= mcach.increment(key, 1, &int_ret_value); @@ -76,19 +80,19 @@ test_return basic_master_key_test(memcached_st *memc) { Memcached foo(memc); const char *value_set= "Data for server A"; - const char *master_key_a= "server-a"; - const char *master_key_b= "server-b"; - const char *key= "xyz"; - char *value; + const string master_key_a("server-a"); + const string master_key_b("server-b"); + const string key("xyz"); + string value; size_t value_length; foo.set_by_key(master_key_a, key, value_set, strlen(value_set)); value= foo.get_by_key(master_key_a, key, &value_length); - assert((memcmp(value, value_set, value_length) == 0)); + assert((memcmp(value.c_str(), value_set, value_length) == 0)); value= foo.get_by_key(master_key_b, key, &value_length); - assert((memcmp(value, value_set, value_length) == 0)); + assert((memcmp(value.c_str(), value_set, value_length) == 0)); return TEST_SUCCESS; }