From: Padraig O'Sullivan Date: Tue, 4 Aug 2009 04:08:55 +0000 (-0400) Subject: Adding some new constructors for the C++ api in libmemcached that will prove X-Git-Tag: 0.34~13^2~19 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=6995b0a6e096958ff3c621117937e82e91e3602e;p=m6w6%2Flibmemcached Adding some new constructors for the C++ api in libmemcached that will prove useful to user's of the C++ api. Also, modified the copy constructor and assignment operator to work correctly. Added a new member to the Memcache class - a std::string which contains the memcached server list. --- diff --git a/libmemcached/memcached.hpp b/libmemcached/memcached.hpp index c21651bf..e437312d 100644 --- a/libmemcached/memcached.hpp +++ b/libmemcached/memcached.hpp @@ -19,6 +19,7 @@ #include +#include #include #include #include @@ -36,15 +37,47 @@ public: Memcache() : + servers_list(), memc(), + servers(NULL), result() { memcached_create(&memc); } + Memcache(const std::string &in_servers_list) + : + servers_list(in_servers_list), + memc(), + servers(NULL), + result() + { + memcached_create(&memc); + servers= memcached_servers_parse(servers_list.c_str()); + } + + Memcache(const std::string &hostname, + unsigned int port) + : + servers_list(), + memc(), + servers(NULL), + result() + { + memcached_create(&memc); + servers_list.append(hostname); + servers_list.append(":"); + std::ostringstream strsmt; + strsmt << port; + servers_list.append(strsmt.str()); + servers= memcached_servers_parse(servers_list.c_str()); + } + Memcache(memcached_st *clone) : + servers_list(), memc(), + servers(NULL), result() { memcached_clone(&memc, clone); @@ -52,15 +85,29 @@ public: Memcache(const Memcache &rhs) : + servers_list(rhs.servers_list), memc(), + servers(NULL), result() { memcached_clone(&memc, const_cast(&rhs.getImpl())); + servers= memcached_servers_parse(servers_list.c_str()); + } + + Memcache &operator=(const Memcache &rhs) + { + if (this != &rhs) + { + memcached_clone(&memc, const_cast(&rhs.getImpl())); + servers= memcached_servers_parse(servers_list.c_str()); + } + return *this; } ~Memcache() { memcached_free(&memc); + memcached_server_list_free(servers); } /** @@ -91,6 +138,53 @@ public: return memcached_strerror(NULL, rc); } + /** + * Return the string which contains the list of memcached servers being + * used. + * + * @return a std::string containing the list of memcached servers + */ + const std::string getServersList() const + { + return servers_list; + } + + /** + * Set the list of memcached servers to use. + * + * @param[in] in_servers_list list of servers + * @return true on success; false otherwise + */ + bool setServers(const std::string &in_servers_list) + { + servers_list.assign(in_servers_list); + servers= memcached_servers_parse(in_servers_list.c_str()); + return (servers == NULL); + } + + /** + * Add a server to the list of memcached servers to use. + * + * @param[in] server_name name of the server to add + * @param[in[ port port number of server to add + * @return true on success; false otherwise + */ + bool addServer(const std::string &server_name, unsigned int port) + { + memcached_return rc; + std::ostringstream strstm; + servers_list.append(","); + servers_list.append(server_name); + servers_list.append(":"); + strstm << port; + servers_list.append(strstm.str()); + servers= memcached_server_list_append(servers, + server_name.c_str(), + port, + &rc); + return (rc == MEMCACHED_SUCCESS); + } + /** * Fetches an individual value from the server. mget() must always * be called before using this method. @@ -789,7 +883,9 @@ public: private: + std::string servers_list; memcached_st memc; + memcached_server_st *servers; memcached_result_st result; };