From: Padraig O'Sullivan Date: Sat, 11 Jul 2009 17:38:57 +0000 (-0400) Subject: Updating the mget function in the C++ interface to take a std::vector of X-Git-Tag: 0.32~17^2~3 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=8d2e59120724923d1ebe75112d73ef03181e0e67;p=awesomized%2Flibmemcached Updating the mget function in the C++ interface to take a std::vector of keys as input instead of an array of char *. --- diff --git a/libmemcached/memcached.hh b/libmemcached/memcached.hh index d777e139..08aa8442 100644 --- a/libmemcached/memcached.hh +++ b/libmemcached/memcached.hh @@ -1,6 +1,7 @@ #include #include +#include class Memcached { @@ -75,12 +76,41 @@ public: return ret_val; } - bool mget(char **keys, size_t *key_length, - unsigned int number_of_keys) + bool mget(std::vector &keys) { + /* + * Construct an array which will contain the length + * of each of the strings in the input vector. Also, to + * interface with the memcached C API, we need to convert + * the vector of std::string's to a vector of char *. + */ + size_t *key_len= static_cast(malloc(keys.size() * sizeof(size_t))); + if (key_len == NULL) + { + return false; + } + std::vector real_keys; + std::vector::iterator it= keys.begin(); + int i= 0; + while (it != keys.end()) + { + real_keys.push_back(const_cast((*it).c_str())); + key_len[i++]= (*it).length(); + ++it; + } - memcached_return rc= memcached_mget(&memc, keys, key_length, number_of_keys); - return (rc == MEMCACHED_SUCCESS); + /* + * If the std::vector of keys is empty then we cannot + * call memcached_mget as we will get undefined behavior. + */ + if (!real_keys.empty()) + { + memcached_return rc= memcached_mget(&memc, &real_keys[0], key_len, + static_cast(real_keys.size())); + return (rc == MEMCACHED_SUCCESS); + } + + return false; } bool set(const std::string &key, const std::string &value)