From e072f345a0df02cd6f437e9965debeb6afacb366 Mon Sep 17 00:00:00 2001 From: Padraig O'Sullivan Date: Sun, 26 Jul 2009 19:46:39 -0400 Subject: [PATCH] This commit contains the following changes: * added a memcache namespace * renamed C++ API header file to *.hpp extension * modified the C++ API test cases --- libmemcached/{memcached.hh => memcached.hpp} | 53 +++++++++++++++----- tests/plus.cpp | 39 +++++++------- 2 files changed, 59 insertions(+), 33 deletions(-) rename libmemcached/{memcached.hh => memcached.hpp} (93%) diff --git a/libmemcached/memcached.hh b/libmemcached/memcached.hpp similarity index 93% rename from libmemcached/memcached.hh rename to libmemcached/memcached.hpp index b6bcf516..ecb474aa 100644 --- a/libmemcached/memcached.hh +++ b/libmemcached/memcached.hpp @@ -8,7 +8,7 @@ */ /** - * @file memcached.hh + * @file memcached.hpp * @brief Libmemcached C++ interface */ @@ -21,16 +21,20 @@ #include #include +#include + +namespace memcache +{ /** * This is the core memcached library (if later, other objects * are needed, they will be created from this class). */ -class Memcached +class Memcache { public: - Memcached() + Memcache() : memc(), result() @@ -38,7 +42,7 @@ public: memcached_create(&memc); } - Memcached(memcached_st *clone) + Memcache(memcached_st *clone) : memc(), result() @@ -46,7 +50,7 @@ public: memcached_clone(&memc, clone); } - Memcached(const Memcached &rhs) + Memcache(const Memcache &rhs) : memc(), result() @@ -54,7 +58,7 @@ public: memcached_clone(&memc, const_cast(&rhs.getImpl())); } - ~Memcached() + ~Memcache() { memcached_free(&memc); } @@ -100,7 +104,7 @@ public: if (value && ret_val.empty()) { ret_val.reserve(value_length); - memcpy(&*ret_val.begin(), value, value_length); + ret_val.assign(value, value + value_length); key.assign(ret_key); return true; } @@ -123,7 +127,7 @@ public: if (value != NULL && ret_val.empty()) { ret_val.reserve(value_length); - memcpy(&ret_val[0], value, value_length); + ret_val.assign(value, value + value_length); } return ret_val; } @@ -147,7 +151,7 @@ public: if (value) { ret_val.reserve(value_length); - memcpy(&*ret_val.begin(), value, value_length); + ret_val.assign(value, value + value_length); } return ret_val; } @@ -205,7 +209,7 @@ public: } bool setAll(std::vector &keys, - std::vector< std::vector > &values, + std::vector< std::vector *> &values, time_t expiration, uint32_t flags) { @@ -215,10 +219,10 @@ public: } bool retval= true; std::vector::iterator key_it= keys.begin(); - std::vector< std::vector >::iterator val_it= values.begin(); + std::vector< std::vector *>::iterator val_it= values.begin(); while (key_it != keys.end()) { - retval= set((*key_it), (*val_it), expiration, flags); + retval= set((*key_it), *(*val_it), expiration, flags); if (retval == false) { return retval; @@ -229,6 +233,29 @@ public: return retval; } + bool setAll(std::map > key_value_map, + time_t expiration, + uint32_t flags) + { + if (key_value_map.empty()) + { + return false; + } + bool retval= true; + std::map >::iterator it= + key_value_map.begin(); + while (it != key_value_map.end()) + { + retval= set(it->first, it->second, expiration, flags); + if (retval == false) + { + return false; + } + ++it; + } + return true; + } + bool setByKey(const std::string &master_key, const std::string &key, const std::vector &value, @@ -505,4 +532,6 @@ private: memcached_result_st result; }; +} + #endif /* LIBMEMCACHEDPP_H */ diff --git a/tests/plus.cpp b/tests/plus.cpp index 28d72870..3431fd18 100644 --- a/tests/plus.cpp +++ b/tests/plus.cpp @@ -1,7 +1,7 @@ /* C++ interface test */ -#include "libmemcached/memcached.hh" +#include "libmemcached/memcached.hpp" #include #include @@ -19,6 +19,7 @@ #include using namespace std; +using namespace memcache; extern "C" { test_return basic_test(memcached_st *memc); @@ -36,23 +37,21 @@ extern "C" { static void populate_vector(vector &vec, const string &str) { vec.reserve(str.length()); - memcpy(&*vec.begin(), str.c_str(), str.length()); + vec.assign(str.begin(), str.end()); } static void copy_vec_to_string(vector &vec, string &str) { str.clear(); - char *tmp= static_cast(malloc(vec.size() * sizeof(char))); - if (!vec.empty()) + if (! vec.empty()) { - memcpy(tmp, &vec[0], vec.size()); - str.assign(tmp); + str.assign(vec.begin(), vec.end()); } } test_return basic_test(memcached_st *memc) { - Memcached foo(memc); + Memcache foo(memc); const string value_set("This is some data"); std::vector value; std::vector test_value; @@ -69,7 +68,7 @@ test_return basic_test(memcached_st *memc) test_return increment_test(memcached_st *memc) { - Memcached mcach(memc); + Memcache mcach(memc); bool rc; const string key("blah"); const string inc_value("1"); @@ -93,8 +92,6 @@ test_return increment_test(memcached_st *memc) } copy_vec_to_string(ret_value, ret_string); - printf("string is: %s\n", ret_string.c_str()); - int_inc_value= uint64_t(atol(inc_value.c_str())); int_ret_value= uint64_t(atol(ret_string.c_str())); assert(int_ret_value == int_inc_value); @@ -116,7 +113,7 @@ test_return increment_test(memcached_st *memc) test_return basic_master_key_test(memcached_st *memc) { - Memcached foo(memc); + Memcache foo(memc); const string value_set("Data for server A"); vector value; vector test_value; @@ -153,13 +150,13 @@ memcached_return callback_counter(memcached_st *, test_return mget_result_function(memcached_st *memc) { - Memcached mc(memc); + Memcache mc(memc); bool rc; string key1("fudge"); string key2("son"); string key3("food"); vector keys; - vector< vector > values; + vector< vector *> values; vector val1; vector val2; vector val3; @@ -171,9 +168,9 @@ test_return mget_result_function(memcached_st *memc) keys.push_back(key2); keys.push_back(key3); values.reserve(3); - values.push_back(val1); - values.push_back(val2); - values.push_back(val3); + values.push_back(&val1); + values.push_back(&val2); + values.push_back(&val3); unsigned int counter; memcached_execute_function callbacks[1]; @@ -196,11 +193,11 @@ test_return mget_result_function(memcached_st *memc) test_return mget_test(memcached_st *memc) { - Memcached mc(memc); + Memcache mc(memc); bool rc; memcached_return mc_rc; vector keys; - vector< vector > values; + vector< vector *> values; keys.reserve(3); keys.push_back("fudge"); keys.push_back("son"); @@ -212,9 +209,9 @@ test_return mget_test(memcached_st *memc) populate_vector(val2, "son"); populate_vector(val3, "food"); values.reserve(3); - values.push_back(val1); - values.push_back(val2); - values.push_back(val3); + values.push_back(&val1); + values.push_back(&val2); + values.push_back(&val3); uint32_t flags; string return_key; -- 2.30.2