Sfoglia il codice sorgente

Add XMLFile::GetOrCreateRoot() & XMLElement::GetOrCreateChild(). Closes #1756.

Lasse Öörni 9 anni fa
parent
commit
f20475161f

+ 2 - 0
Source/Urho3D/AngelScript/ResourceAPI.cpp

@@ -454,6 +454,7 @@ static void RegisterXMLElement(asIScriptEngine* engine)
     engine->RegisterObjectBehaviour("XMLElement", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructXMLElement), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("XMLElement", "XMLElement& opAssign(const XMLElement&in)", asMETHOD(XMLElement, operator =), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLElement", "XMLElement CreateChild(const String&in)", asMETHODPR(XMLElement, CreateChild, (const String&), XMLElement), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XMLElement", "XMLElement GetOrCreateChild(const String&in)", asMETHODPR(XMLElement, GetOrCreateChild, (const String&), XMLElement), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLElement", "bool RemoveChild(const XMLElement&in)", asMETHODPR(XMLElement, RemoveChild, (const XMLElement&), bool), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLElement", "bool RemoveChild(const String&in)", asMETHODPR(XMLElement, RemoveChild, (const String&), bool), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLElement", "bool RemoveChildren(const String&in name = String())", asMETHODPR(XMLElement, RemoveChildren, (const String&), bool), asCALL_THISCALL);
@@ -569,6 +570,7 @@ static void RegisterXMLFile(asIScriptEngine* engine)
 {
     engine->RegisterObjectMethod("XMLFile", "bool FromString(const String&in)", asMETHOD(XMLFile, FromString), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLFile", "XMLElement CreateRoot(const String&in)", asMETHOD(XMLFile, CreateRoot), asCALL_THISCALL);
+    engine->RegisterObjectMethod("XMLFile", "XMLElement GetOrCreateRoot(const String&in)", asMETHOD(XMLFile, GetOrCreateRoot), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLFile", "XMLElement GetRoot(const String&in name = String())", asMETHOD(XMLFile, GetRoot), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLFile", "bool Save(File@+, const String&in) const", asFUNCTION(XMLFileSave), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("XMLFile", "String ToString(const String&in = String(\"\t\")) const", asMETHOD(XMLFile, ToString), asCALL_THISCALL);

+ 1 - 0
Source/Urho3D/LuaScript/pkgs/Resource/XMLElement.pkg

@@ -3,6 +3,7 @@ $#include "Resource/XMLElement.h"
 class XMLElement
 {
     XMLElement CreateChild(const String name);
+    XMLElement GetOrCreateChild(const String name);
     bool RemoveChild(const XMLElement& element);
     bool RemoveChild(const String name);
     bool RemoveChildren(const String name = String::EMPTY);

+ 1 - 0
Source/Urho3D/LuaScript/pkgs/Resource/XMLFile.pkg

@@ -7,6 +7,7 @@ class XMLFile : public Resource
 
     bool FromString(const String source);
     XMLElement CreateRoot(const String name = String::EMPTY);
+    XMLElement GetOrCreateRoot(const String name = String::EMPTY);
     XMLElement GetRoot(const String name = String::EMPTY);
     String ToString(const String indentation = "\t") const;
 

+ 18 - 0
Source/Urho3D/Resource/XMLElement.cpp

@@ -106,6 +106,24 @@ XMLElement XMLElement::CreateChild(const char* name)
     return XMLElement(file_, child.internal_object());
 }
 
+XMLElement XMLElement::GetOrCreateChild(const String& name)
+{
+    XMLElement child = GetChild(name);
+    if (child.NotNull())
+        return child;
+    else
+        return CreateChild(name);
+}
+
+XMLElement XMLElement::GetOrCreateChild(const char* name)
+{
+    XMLElement child = GetChild(name);
+    if (child.NotNull())
+        return child;
+    else
+        return CreateChild(name);
+}
+
 bool XMLElement::RemoveChild(const XMLElement& element)
 {
     if (!element.file_ || (!element.node_ && !element.xpathNode_) || !file_ || (!node_ && !xpathNode_))

+ 4 - 0
Source/Urho3D/Resource/XMLElement.h

@@ -66,6 +66,10 @@ public:
     XMLElement CreateChild(const String& name);
     /// Create a child element.
     XMLElement CreateChild(const char* name);
+    /// Return the first child element with name or create if does not exist.
+    XMLElement GetOrCreateChild(const String& name);
+    /// Return the first child element with name or create if does not exist.
+    XMLElement GetOrCreateChild(const char* name);
     /// Remove a child element. Return true if successful.
     bool RemoveChild(const XMLElement& element);
     /// Remove a child element by name. Return true if successful.

+ 11 - 0
Source/Urho3D/Resource/XMLFile.cpp

@@ -150,6 +150,17 @@ XMLElement XMLFile::CreateRoot(const String& name)
     return XMLElement(this, root.internal_object());
 }
 
+XMLElement XMLFile::GetOrCreateRoot(const String& name)
+{
+    XMLElement root = GetRoot(name);
+    if (root.NotNull())
+        return root;
+    root = GetRoot();
+    if (root.NotNull())
+        URHO3D_LOGWARNING("XMLFile already has root " + root.GetName() + ", deleting it and creating root " + name);
+    return CreateRoot(name);
+}
+
 bool XMLFile::FromString(const String& source)
 {
     if (source.Empty())

+ 2 - 0
Source/Urho3D/Resource/XMLFile.h

@@ -61,6 +61,8 @@ public:
     bool FromString(const String& source);
     /// Clear the document and create a root element.
     XMLElement CreateRoot(const String& name);
+    /// Get the root element if it has matching name, otherwise create it and clear the document.
+    XMLElement GetOrCreateRoot(const String& name);
 
     /// Return the root element, with optionally specified name. Return null element if not found.
     XMLElement GetRoot(const String& name = String::EMPTY);