Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / dom / xslt / xpath / txXPathNode.h
blob8c5d6f530838192b134d8ea2481942295132a82e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef txXPathNode_h__
7 #define txXPathNode_h__
9 #include "nsIContent.h"
10 #include "mozilla/dom/Document.h"
11 #include "nsINode.h"
12 #include "nsNameSpaceManager.h"
14 using txXPathNodeType = nsINode;
16 class txXPathNode {
17 public:
18 bool operator==(const txXPathNode& aNode) const;
19 bool operator!=(const txXPathNode& aNode) const { return !(*this == aNode); }
20 ~txXPathNode();
22 private:
23 friend class txNodeSet;
24 friend class txXPathNativeNode;
25 friend class txXPathNodeUtils;
26 friend class txXPathTreeWalker;
28 txXPathNode(const txXPathNode& aNode);
30 explicit txXPathNode(mozilla::dom::Document* aDocument)
31 : mNode(aDocument), mRefCountRoot(0), mIndex(eDocument) {
32 MOZ_COUNT_CTOR(txXPathNode);
34 txXPathNode(nsINode* aNode, uint32_t aIndex, nsINode* aRoot)
35 : mNode(aNode), mRefCountRoot(aRoot ? 1 : 0), mIndex(aIndex) {
36 MOZ_COUNT_CTOR(txXPathNode);
37 if (aRoot) {
38 NS_ADDREF(aRoot);
42 static nsINode* RootOf(nsINode* aNode) { return aNode->SubtreeRoot(); }
43 nsINode* Root() const { return RootOf(mNode); }
44 nsINode* GetRootToAddRef() const { return mRefCountRoot ? Root() : nullptr; }
46 bool isDocument() const { return mIndex == eDocument; }
47 bool isContent() const { return mIndex == eContent; }
48 bool isAttribute() const { return mIndex != eDocument && mIndex != eContent; }
50 nsIContent* Content() const {
51 NS_ASSERTION(isContent() || isAttribute(), "wrong type");
52 return static_cast<nsIContent*>(mNode);
54 mozilla::dom::Document* Document() const {
55 NS_ASSERTION(isDocument(), "wrong type");
56 return static_cast<mozilla::dom::Document*>(mNode);
59 enum PositionType { eDocument = (1 << 30), eContent = eDocument - 1 };
61 nsINode* mNode;
62 uint32_t mRefCountRoot : 1;
63 uint32_t mIndex : 31;
66 class txNamespaceManager {
67 public:
68 static int32_t getNamespaceID(const nsAString& aNamespaceURI);
69 static nsresult getNamespaceURI(const int32_t aID, nsAString& aResult);
72 /* static */
73 inline int32_t txNamespaceManager::getNamespaceID(
74 const nsAString& aNamespaceURI) {
75 int32_t namespaceID = kNameSpaceID_Unknown;
76 nsNameSpaceManager::GetInstance()->RegisterNameSpace(aNamespaceURI,
77 namespaceID);
78 return namespaceID;
81 /* static */
82 inline nsresult txNamespaceManager::getNamespaceURI(const int32_t aID,
83 nsAString& aResult) {
84 return nsNameSpaceManager::GetInstance()->GetNameSpaceURI(aID, aResult);
87 inline bool txXPathNode::operator==(const txXPathNode& aNode) const {
88 return mIndex == aNode.mIndex && mNode == aNode.mNode;
91 #endif /* txXPathNode_h__ */