]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
cmdparse: add handle_bad_get(), ceph:: qualifiers, cmdmap_t
authorDan Mick <dan.mick@inktank.com>
Tue, 4 Jun 2013 04:05:12 +0000 (21:05 -0700)
committerDan Mick <dan.mick@inktank.com>
Tue, 4 Jun 2013 04:05:12 +0000 (21:05 -0700)
Signed-off-by: Dan Mick <dan.mick@inktank.com>
src/common/cmdparse.cc
src/common/cmdparse.h

index f516017842c9bf066fe5488d597f9cabec77aeba..0fd5d438aae5e89a8d8ffa978730970139def72d 100644 (file)
  *
  */
 
+#include <cxxabi.h>
 #include "common/cmdparse.h"
 #include "include/str_list.h"
 #include "json_spirit/json_spirit.h"
+#include "common/debug.h"
 
 using namespace std;
 
@@ -170,3 +172,19 @@ cmdmap_from_json(vector<string> cmd, map<string, cmd_vartype> *mapp, stringstrea
   }
 }
 
+void
+handle_bad_get(CephContext *cct, string k, const char *tname)
+{
+  ostringstream errstr;
+  int status;
+  const char *typestr = abi::__cxa_demangle(tname, 0, 0, &status);
+  if (status != 0) 
+    typestr = tname;
+  errstr << "bad boost::get: key " << k << " is not type " << typestr;
+  lderr(cct) << errstr.str() << dendl;
+
+  BackTrace bt(1);
+  ostringstream oss;
+  bt.print(oss);
+  lderr(cct) << oss << dendl;
+}
index d6920656e981f41b078b3f86918195f50023b72c..8a65d24ac78b979608f724336ea4d9a3d00a4f1f 100644 (file)
@@ -9,65 +9,46 @@
 #include <boost/variant.hpp>
 #include <vector>
 #include <stdexcept>
-#ifdef __GNUC__
-#include <cxxabi.h>
-#endif
-#include "common/dout.h"
 #include "common/Formatter.h"
 #include "common/BackTrace.h"
+#include "common/ceph_context.h"
 
 /* this is handy; can't believe it's not standard */
 #define ARRAY_SIZE(a)  (sizeof(a) / sizeof(*a))
 
 typedef boost::variant<std::string, bool, int64_t, double, std::vector<std::string> > cmd_vartype;
+typedef std::map<std::string, cmd_vartype> cmdmap_t;
+
+void dump_cmd_to_json(ceph::JSONFormatter *f, const std::string& cmd);
+void dump_cmd_and_help_to_json(ceph::JSONFormatter *f,
+                              const std::string& secname,
+                              const std::string& cmd,
+                              const std::string& helptext);
+bool cmdmap_from_json(std::vector<std::string> cmd, cmdmap_t *mapp,
+                     std::stringstream &ss);
+void handle_bad_get(CephContext *cct, std::string k, const char *name);
 
-void dump_cmd_to_json(JSONFormatter *f, const string& cmd);
-void dump_cmd_and_help_to_json(JSONFormatter *f, const string& secname,
-                              const string& cmd, const string& helptext);
-bool cmdmap_from_json(std::vector<std::string> cmd, std::map<std::string,
-                     cmd_vartype> *mapp, std::stringstream &ss);
 
 template <typename T>
 bool
-cmd_getval(CephContext *cct, std::map<std::string, cmd_vartype>& cmdmap, std::string k, T& val)
+cmd_getval(CephContext *cct, cmdmap_t& cmdmap, std::string k, T& val)
 {
-  // referencing a nonexistent key creates it, even as an rvalue;
-  // we don't want that behavior for get.
   if (cmdmap.count(k)) {
     try {
       val = boost::get<T>(cmdmap[k]);
       return true;
     } catch (boost::bad_get) {
-      std::ostringstream errstr;
-      errstr << "bad boost::get: key " << k << ", which value " << cmdmap[k].which() << ", is not type ";
-#ifdef __GNUC__
-      int status;
-      char *tname = abi::__cxa_demangle(typeid(T).name(), 0, 0, &status);
-      if (status == 0) {
-       errstr << tname;
-       free(tname);
-      } else {
-       errstr << typeid(T).name();
-      }
-#else
-      errstr << typeid(T).name();
-#endif
-      lderr(cct) << errstr.str() << dendl;
-      BackTrace bt(1);
-      ostringstream oss;
-      bt.print(oss);
-      lderr(cct) << oss << dendl;
+      handle_bad_get(cct, k, typeid(T).name());
     }
   }
-  // either not found or bad type, return false
   return false;
 }
 
 // with default
+
 template <typename T>
 void
-cmd_getval(CephContext *cct, std::map<std::string, cmd_vartype>& cmdmap, std::string k, T& val,
-       T defval)
+cmd_getval(CephContext *cct, cmdmap_t& cmdmap, std::string k, T& val, T defval)
 {
   if (!cmd_getval(cct, cmdmap, k, val))
     val = defval;
@@ -75,9 +56,8 @@ cmd_getval(CephContext *cct, std::map<std::string, cmd_vartype>& cmdmap, std::st
 
 template <typename T>
 void
-cmd_putval(CephContext *cct, std::map<std::string, cmd_vartype>& cmdmap, std::string k, T val)
+cmd_putval(CephContext *cct, cmdmap_t& cmdmap, std::string k, T val)
 {
   cmdmap[k] = val;
 }
-
 #endif