From 854dea8243498795842b8aea91d619b25952b9dc Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Wed, 13 Dec 2017 03:38:09 -0800 Subject: [PATCH] ceph_json: formattable fix, api changes add implicit casts Signed-off-by: Yehuda Sadeh --- src/common/ceph_json.h | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/common/ceph_json.h b/src/common/ceph_json.h index 4e203e01d313a..a3b3a8b307a86 100644 --- a/src/common/ceph_json.h +++ b/src/common/ceph_json.h @@ -474,10 +474,10 @@ struct JSONFormattable { FMT_ARRAY, FMT_OBJ, } type{FMT_NONE}; - std::string str; + + string str; vector arr; map obj; - void decode_json(JSONObj *jo) { if (jo->is_array()) { type = JSONFormattable::FMT_ARRAY; @@ -485,7 +485,7 @@ struct JSONFormattable { } else if (jo->is_object()) { type = JSONFormattable::FMT_OBJ; auto iter = jo->find_first(); - while (!iter.end()) { + for (;!iter.end(); ++iter) { JSONObj *field = *iter; decode_json_obj(obj[field->get_name()], field); } @@ -514,6 +514,7 @@ struct JSONFormattable { decode(obj, bl); DECODE_FINISH(bl); } + const std::string& val() const { return str; } @@ -530,6 +531,23 @@ struct JSONFormattable { return this->operator[](std::string(name)); } + operator std::string() const { + return str; + } + + explicit operator int() const { + return val_int(); + } + + explicit operator bool() const { + return val_bool(); + } + + template + T operator[](const std::string& name) const { + return this->operator[](name)(T()); + } + string operator ()(const char *def_val) const { return def(string(def_val)); } -- 2.39.5