nsRDFQuery.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  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/. */
  5. #ifndef nsRDFQuery_h__
  6. #define nsRDFQuery_h__
  7. #include "nsISimpleEnumerator.h"
  8. #include "nsCycleCollectionParticipant.h"
  9. #include "mozilla/Attributes.h"
  10. #define NS_ITEMPLATERDFQUERY_IID \
  11. {0x8929ff60, 0x1c9c, 0x4d87, \
  12. { 0xac, 0x02, 0x09, 0x14, 0x15, 0x3b, 0x48, 0xc4 }}
  13. class nsXULTemplateQueryProcessorRDF;
  14. /**
  15. * A compiled query in the RDF query processor. This interface should not be
  16. * used directly outside of the RDF query processor.
  17. */
  18. class nsITemplateRDFQuery : public nsISupports
  19. {
  20. public:
  21. NS_DECLARE_STATIC_IID_ACCESSOR(NS_ITEMPLATERDFQUERY_IID)
  22. // return the processor the query was created from
  23. virtual nsXULTemplateQueryProcessorRDF* Processor() = 0; // not addrefed
  24. // return the member variable for the query
  25. virtual nsIAtom* GetMemberVariable() = 0; // not addrefed
  26. // return the <query> node the query was compiled from
  27. virtual void GetQueryNode(nsIDOMNode** aQueryNode) = 0;
  28. // remove any results that are cached by the query
  29. virtual void ClearCachedResults() = 0;
  30. };
  31. class nsRDFQuery final : public nsITemplateRDFQuery
  32. {
  33. ~nsRDFQuery() { Finish(); }
  34. public:
  35. explicit nsRDFQuery(nsXULTemplateQueryProcessorRDF* aProcessor)
  36. : mProcessor(aProcessor),
  37. mSimple(false),
  38. mRoot(nullptr),
  39. mCachedResults(nullptr)
  40. { }
  41. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  42. NS_DECL_CYCLE_COLLECTION_CLASS(nsRDFQuery)
  43. /**
  44. * Retrieve the root node in the rule network
  45. * @return the root node in the rule network
  46. */
  47. TestNode* GetRoot() { return mRoot; }
  48. void SetRoot(TestNode* aRoot) { mRoot = aRoot; }
  49. void GetQueryNode(nsIDOMNode** aQueryNode) override
  50. {
  51. *aQueryNode = mQueryNode;
  52. NS_IF_ADDREF(*aQueryNode);
  53. }
  54. void SetQueryNode(nsIDOMNode* aQueryNode)
  55. {
  56. mQueryNode = aQueryNode;
  57. }
  58. // an optimization is used when several queries all use the simple query
  59. // syntax. Since simple queries can only generate one possible set of
  60. // results, they only need to be calculated once and reused for every
  61. // simple query. The results may be cached in the query for this purpose.
  62. // If successful, this method takes ownership of aInstantiations.
  63. nsresult SetCachedResults(nsXULTemplateQueryProcessorRDF* aProcessor,
  64. const InstantiationSet& aInstantiations);
  65. // grab the cached results, if any, causing the caller to take ownership
  66. // of them. This also has the effect of setting the cached results in this
  67. // nsRDFQuery to null.
  68. void UseCachedResults(nsISimpleEnumerator** aResults);
  69. // clear the cached results
  70. void ClearCachedResults() override
  71. {
  72. mCachedResults = nullptr;
  73. }
  74. nsXULTemplateQueryProcessorRDF* Processor() override { return mProcessor; }
  75. nsIAtom* GetMemberVariable() override { return mMemberVariable; }
  76. bool IsSimple() { return mSimple; }
  77. void SetSimple() { mSimple = true; }
  78. // the reference and member variables for the query
  79. nsCOMPtr<nsIAtom> mRefVariable;
  80. nsCOMPtr<nsIAtom> mMemberVariable;
  81. protected:
  82. nsXULTemplateQueryProcessorRDF* mProcessor;
  83. // true if the query is a simple rule (one with a default query)
  84. bool mSimple;
  85. /**
  86. * The root node in the network for this query
  87. */
  88. TestNode *mRoot;
  89. // the <query> node
  90. nsCOMPtr<nsIDOMNode> mQueryNode;
  91. // used for simple rules since their results are all determined in one step
  92. nsCOMPtr<nsISimpleEnumerator> mCachedResults;
  93. void Finish();
  94. };
  95. NS_DEFINE_STATIC_IID_ACCESSOR(nsITemplateRDFQuery, NS_ITEMPLATERDFQUERY_IID)
  96. #endif // nsRDFQuery_h__