Update docs directives.
[awesomized/libmemcached] / tests / cpp_example.cc
index 6feb26ab5e0b00baa3e7f25bfe8447dc0323e6fb..f84af7596e25759e9e55847508208793cb1cba37 100644 (file)
@@ -1,10 +1,13 @@
 /*
  * An example file showing the usage of the C++ libmemcached interface.
  */
+#include <config.h>
 
 #include <vector>
 #include <string>
 #include <iostream>
+#include <algorithm>
+#include <map>
 
 #include <string.h>
 
 using namespace std;
 using namespace memcache;
 
+class DeletePtrs
+{
+public:
+  template<typename T>
+  inline void operator()(const T *ptr) const
+  {
+    delete ptr;
+  }
+};
+
 class MyCache
 {
 public:
@@ -47,9 +60,12 @@ public:
 
   Memcache *getCache()
   {
-    /* pick a random element from the vector of clients */
-    Memcache *first= clients[0];
-    return first;
+    /* 
+     * pick a random element from the vector of clients. Obviously, this is
+     * not very random but suffices as an example!
+     */
+    uint32_t index= rand() % num_of_clients;
+    return clients[index];
   } 
 
 private:
@@ -73,6 +89,7 @@ private:
 
   ~MyCache()
   {
+    for_each(clients.begin(), clients.end(), DeletePtrs());
     clients.clear();
   }
 
@@ -152,28 +169,27 @@ void setProduct(const string &key, const Product &product)
 
 int main()
 {
-  Product pad(1, 5.0);
-  const string key("padraig");
-  cout << "Going to set an object in the cache..." << endl;
-  setProduct(key, pad);
-  cout << "Now retrieve that key..." << endl;
-  Product test= getProduct(key);
-  double price= test.getPrice();
-  cout << "Price of retrieve object: " << price << endl;
-  Product next(2, 10.0);
-  vector<Product> products;
-  products.push_back(pad);
-  products.push_back(next);
-  cout << "going to set a vector of products..." << endl;
-  setAllProducts(products);
-  cout << "now retrieve those products..." << endl;
-  vector<Product> got= getAllProducts();
-  cout << "size of retrieved vector: " << got.size() << endl;
-  vector<Product>::iterator iter= got.begin();
-  while (iter != got.end())
+  Memcache first_client("127.0.0.1:19191");
+  map< string, map<string, string> > my_stats;
+  first_client.getStats(my_stats);
+  
+  /*
+   * Iterate through the retrieved stats.
+   */
+  map< string, map<string, string> >::iterator it=
+    my_stats.begin();
+  while (it != my_stats.end())
   {
-    cout << "product " << (*iter).getId() << " costs " << (*iter).getPrice() << endl;
-    ++iter;
+    cout << "working with server: " << (*it).first << endl;
+    map<string, string> serv_stats= (*it).second;
+    map<string, string>::iterator iter= serv_stats.begin();
+    while (iter != serv_stats.end())
+    {
+      cout << (*iter).first << ":" << (*iter).second << endl;
+      ++iter;
+    }
+    ++it;
   }
-  return 0;
+
+  return EXIT_SUCCESS;
 }