From 6364d325fa4423fc7f4216fac2af76dbd8ca793a Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 25 Mar 2022 17:31:40 +0800 Subject: [PATCH] 1 --- a8/mutable_xobject.cc | 16 ++++++++++++---- a8/mutable_xobject.h | 5 ++++- a8/xobject.cc | 12 ++++++++++++ a8/xobject.h | 4 ++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/a8/mutable_xobject.cc b/a8/mutable_xobject.cc index 2a93d90..8926138 100644 --- a/a8/mutable_xobject.cc +++ b/a8/mutable_xobject.cc @@ -11,19 +11,27 @@ namespace a8 a8::MutableXObject* MutableXObject::NewObject() { a8::MutableXObject* obj = new a8::MutableXObject(); - obj->type_ = XOT_OBJECT; - obj->value_.object_value = new std::map>(); + obj->ConvertObject(); return obj; } a8::MutableXObject* MutableXObject::NewArray() { a8::MutableXObject* obj = new a8::MutableXObject(); - obj->type_ = XOT_ARRAY; - obj->value_.array_value = new std::vector>(); + obj->ConvertArray(); return obj; } + std::shared_ptr MutableXObject::CreateObject() + { + return std::shared_ptr(NewObject()); + } + + std::shared_ptr MutableXObject::CreateArray() + { + return std::shared_ptr(NewArray()); + } + a8::MutableXObject& MutableXObject::Push(a8::XValue val) { if (type_ != XOT_ARRAY) { diff --git a/a8/mutable_xobject.h b/a8/mutable_xobject.h index 9175361..1b184f7 100644 --- a/a8/mutable_xobject.h +++ b/a8/mutable_xobject.h @@ -6,7 +6,7 @@ namespace a8 class MutableXObject : public XObject { - private: + protected: MutableXObject(); public: @@ -14,6 +14,9 @@ namespace a8 static a8::MutableXObject* NewObject(); static a8::MutableXObject* NewArray(); + static std::shared_ptr CreateObject(); + static std::shared_ptr CreateArray(); + a8::MutableXObject& Push(a8::XValue val); a8::MutableXObject& Push(a8::MutableXObject& val); a8::MutableXObject& SetVal(const std::string& key, a8::XValue val); diff --git a/a8/xobject.cc b/a8/xobject.cc index 4926092..aaab704 100644 --- a/a8/xobject.cc +++ b/a8/xobject.cc @@ -571,4 +571,16 @@ namespace a8 } + void XObject::ConvertObject() + { + type_ = XOT_OBJECT; + value_.object_value = new std::map>(); + } + + void XObject::ConvertArray() + { + type_ = XOT_ARRAY; + value_.array_value = new std::vector>(); + } + } diff --git a/a8/xobject.h b/a8/xobject.h index 4e2d8ce..a4f308d 100644 --- a/a8/xobject.h +++ b/a8/xobject.h @@ -58,6 +58,10 @@ namespace a8 void JsonValueToXObject(Json::Value& json_val, a8::XObject& xobject); void XmlElementToXObject(tinyxml2::XMLElement* xml_ele, a8::XObject& xobject); + protected: + void ConvertObject(); + void ConvertArray(); + protected: unsigned char type_ = a8::XOT_SIMPLE; union {