]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/rgw/rgw_xml.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / rgw / rgw_xml.h
index 227e1cba4c02b8815cc71f7b949736f7a8202cda..c56c8c5247d74d38ae13225ac20cd1daba6436bd 100644 (file)
@@ -1,10 +1,11 @@
 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
-// vim: ts=8 sw=2 smarttab
+// vim: ts=8 sw=2 smarttab ft=cpp
 
 #ifndef CEPH_RGW_XML_H
 #define CEPH_RGW_XML_H
 
 #include <map>
+#include <stdexcept>
 #include <string>
 #include <iosfwd>
 #include <include/types.h>
@@ -15,8 +16,8 @@ class RGWXMLParser;
 
 class XMLObjIter {
 public:
-  typedef map<std::string, XMLObj *>::iterator map_iter_t;
-  typedef map<std::string, XMLObj *>::iterator const_map_iter_t;
+  typedef std::map<std::string, XMLObj *>::iterator map_iter_t;
+  typedef std::map<std::string, XMLObj *>::iterator const_map_iter_t;
 
   XMLObjIter();
   ~XMLObjIter();
@@ -75,7 +76,7 @@ public:
   // return the first sub-tags matching the name
   XMLObj *find_first(const std::string& name);
 
-  friend ostream& operator<<(ostream &out, const XMLObj &obj);
+  friend std::ostream& operator<<(std::ostream &out, const XMLObj &obj);
   friend RGWXMLParser;
 };
 
@@ -145,10 +146,8 @@ public:
 };
 
 namespace RGWXMLDecoder {
-  struct err {
-    std::string message;
-
-    explicit err(const std::string& m) : message(m) {}
+  struct err : std::runtime_error {
+    using runtime_error::runtime_error;
   };
 
   typedef RGWXMLParser XMLParser;
@@ -166,9 +165,9 @@ namespace RGWXMLDecoder {
   void decode_xml(const char *name, T& val, T& default_val, XMLObj* obj);
 }
 
-static inline ostream& operator<<(ostream &out, RGWXMLDecoder::err& err)
+static inline std::ostream& operator<<(std::ostream &out, RGWXMLDecoder::err& err)
 {
-  return out << err.message;
+  return out << err.what();
 }
 
 template<class T>
@@ -177,7 +176,7 @@ void decode_xml_obj(T& val, XMLObj *obj)
   val.decode_xml(obj);
 }
 
-static inline void decode_xml_obj(string& val, XMLObj *obj)
+static inline void decode_xml_obj(std::string& val, XMLObj *obj)
 {
   val = obj->get_data();
 }
@@ -194,7 +193,14 @@ class utime_t;
 void decode_xml_obj(utime_t& val, XMLObj *obj);
 
 template<class T>
-void do_decode_xml_obj(list<T>& l, const string& name, XMLObj *obj)
+void decode_xml_obj(std::optional<T>& val, XMLObj *obj)
+{
+  val.emplace();
+  decode_xml_obj(*val, obj);
+}
+
+template<class T>
+void do_decode_xml_obj(std::list<T>& l, const std::string& name, XMLObj *obj)
 {
   l.clear();
 
@@ -215,7 +221,7 @@ bool RGWXMLDecoder::decode_xml(const char *name, T& val, XMLObj *obj, bool manda
   XMLObj *o = iter.get_next();
   if (!o) {
     if (mandatory) {
-      string s = "missing mandatory field " + string(name);
+      std::string s = "missing mandatory field " + std::string(name);
       throw err(s);
     }
     val = T();
@@ -224,9 +230,9 @@ bool RGWXMLDecoder::decode_xml(const char *name, T& val, XMLObj *obj, bool manda
 
   try {
     decode_xml_obj(val, o);
-  } catch (err& e) {
-    string s = string(name) + ": ";
-    s.append(e.message);
+  } catch (const err& e) {
+    std::string s = std::string(name) + ": ";
+    s.append(e.what());
     throw err(s);
   }
 
@@ -243,7 +249,7 @@ bool RGWXMLDecoder::decode_xml(const char *name, std::vector<T>& v, XMLObj *obj,
 
   if (!o) {
     if (mandatory) {
-      string s = "missing mandatory field " + string(name);
+      std::string s = "missing mandatory field " + std::string(name);
       throw err(s);
     }
     return false;
@@ -253,9 +259,9 @@ bool RGWXMLDecoder::decode_xml(const char *name, std::vector<T>& v, XMLObj *obj,
     T val;
     try {
       decode_xml_obj(val, o);
-    } catch (err& e) {
-      string s = string(name) + ": ";
-      s.append(e.message);
+    } catch (const err& e) {
+      std::string s = std::string(name) + ": ";
+      s.append(e.what());
       throw err(s);
     }
     v.push_back(val);
@@ -272,7 +278,7 @@ bool RGWXMLDecoder::decode_xml(const char *name, C& container, void (*cb)(C&, XM
   XMLObj *o = iter.get_next();
   if (!o) {
     if (mandatory) {
-      string s = "missing mandatory field " + string(name);
+      std::string s = "missing mandatory field " + std::string(name);
       throw err(s);
     }
     return false;
@@ -280,9 +286,9 @@ bool RGWXMLDecoder::decode_xml(const char *name, C& container, void (*cb)(C&, XM
 
   try {
     decode_xml_obj(container, cb, o);
-  } catch (err& e) {
-    string s = string(name) + ": ";
-    s.append(e.message);
+  } catch (const err& e) {
+    std::string s = std::string(name) + ": ";
+    s.append(e.what());
     throw err(s);
   }
 
@@ -301,10 +307,10 @@ void RGWXMLDecoder::decode_xml(const char *name, T& val, T& default_val, XMLObj
 
   try {
     decode_xml_obj(val, o);
-  } catch (err& e) {
+  } catch (const err& e) {
     val = default_val;
-    string s = string(name) + ": ";
-    s.append(e.message);
+    std::string s = std::string(name) + ": ";
+    s.append(e.what());
     throw err(s);
   }
 }
@@ -325,7 +331,7 @@ static void encode_xml(const char *name, const char *ns, const T& val, ceph::For
   f->close_section();
 }
 
-void encode_xml(const char *name, const string& val, ceph::Formatter *f);
+void encode_xml(const char *name, const std::string& val, ceph::Formatter *f);
 void encode_xml(const char *name, const char *val, ceph::Formatter *f);
 void encode_xml(const char *name, bool val, ceph::Formatter *f);
 void encode_xml(const char *name, int val, ceph::Formatter *f);
@@ -347,6 +353,23 @@ static void do_encode_xml(const char *name, const std::list<T>& l, const char *e
   f->close_section();
 }
 
+template<class T>
+static void encode_xml(const char *name, const std::vector<T>& l, ceph::Formatter *f)
+{
+  for (typename std::vector<T>::const_iterator iter = l.begin(); iter != l.end(); ++iter) {
+    encode_xml(name, *iter, f);
+  }
+}
+
+template<class T>
+static void encode_xml(const char *name, const std::optional<T>& o, ceph::Formatter *f)
+{
+  if (!o) {
+    return;
+  }
+
+  encode_xml(name, *o, f);
+}
 
 
 #endif