xref: /aosp_15_r20/external/pdfium/core/fxcrt/xml/cfx_xmldocument.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2018 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CORE_FXCRT_XML_CFX_XMLDOCUMENT_H_
6 #define CORE_FXCRT_XML_CFX_XMLDOCUMENT_H_
7 
8 #include <memory>
9 #include <utility>
10 #include <vector>
11 
12 #include "core/fxcrt/unowned_ptr.h"
13 
14 class CFX_XMLElement;
15 class CFX_XMLNode;
16 
17 class CFX_XMLDocument {
18  public:
19   CFX_XMLDocument();
20   ~CFX_XMLDocument();
21 
GetRoot()22   CFX_XMLElement* GetRoot() const { return root_; }
23 
24   template <typename T, typename... Args>
CreateNode(Args &&...args)25   T* CreateNode(Args&&... args) {
26     nodes_.push_back(std::make_unique<T>(std::forward<Args>(args)...));
27     return static_cast<T*>(nodes_.back().get());
28   }
29 
30   // Transfers ownership of entries in |nodes_| from |other| to |this|.
31   // This is used in CJX_Node::loadXML to transfer ownership of the newly
32   // created nodes to the top-level XML doc for the PDF, after parsing an XML
33   // blob.
34   void AppendNodesFrom(CFX_XMLDocument* other);
35 
36  private:
37   std::vector<std::unique_ptr<CFX_XMLNode>> nodes_;
38   UnownedPtr<CFX_XMLElement> root_;
39 };
40 
41 #endif  // CORE_FXCRT_XML_CFX_XMLDOCUMENT_H_
42