nsRDFBinding.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 nsRDFBinding_h__
  6. #define nsRDFBinding_h__
  7. #include "nsIAtom.h"
  8. #include "nsIRDFResource.h"
  9. #include "nsISupportsImpl.h"
  10. class nsXULTemplateResultRDF;
  11. class nsBindingValues;
  12. /*
  13. * Classes related to storing bindings for RDF handling.
  14. */
  15. /*
  16. * a <binding> descriptors
  17. */
  18. class RDFBinding {
  19. public:
  20. nsCOMPtr<nsIAtom> mSubjectVariable;
  21. nsCOMPtr<nsIRDFResource> mPredicate;
  22. nsCOMPtr<nsIAtom> mTargetVariable;
  23. // indicates whether a binding is dependant on the result from a
  24. // previous binding
  25. bool mHasDependency;
  26. RDFBinding* mNext;
  27. private:
  28. friend class RDFBindingSet;
  29. RDFBinding(nsIAtom* aSubjectVariable,
  30. nsIRDFResource* aPredicate,
  31. nsIAtom* aTargetVariable)
  32. : mSubjectVariable(aSubjectVariable),
  33. mPredicate(aPredicate),
  34. mTargetVariable(aTargetVariable),
  35. mHasDependency(false),
  36. mNext(nullptr)
  37. {
  38. MOZ_COUNT_CTOR(RDFBinding);
  39. }
  40. ~RDFBinding()
  41. {
  42. MOZ_COUNT_DTOR(RDFBinding);
  43. }
  44. };
  45. /*
  46. * a collection of <binding> descriptors. This object is refcounted by
  47. * nsBindingValues objects and the query processor.
  48. */
  49. class RDFBindingSet final
  50. {
  51. private:
  52. // Private destructor, to discourage deletion outside of Release():
  53. ~RDFBindingSet();
  54. // the number of bindings
  55. int32_t mCount;
  56. // pointer to the first binding in a linked list
  57. RDFBinding* mFirst;
  58. public:
  59. RDFBindingSet()
  60. : mCount(0),
  61. mFirst(nullptr)
  62. {
  63. MOZ_COUNT_CTOR(RDFBindingSet);
  64. }
  65. NS_INLINE_DECL_REFCOUNTING(RDFBindingSet)
  66. int32_t Count() const { return mCount; }
  67. /*
  68. * Add a binding (aRef -> aPredicate -> aVar) to the set
  69. */
  70. nsresult
  71. AddBinding(nsIAtom* aVar, nsIAtom* aRef, nsIRDFResource* aPredicate);
  72. /*
  73. * Return true if the binding set contains a binding which would cause
  74. * the result to need resynchronizing for an RDF triple. The member
  75. * variable may be supplied as an optimization since bindings most
  76. * commonly use the member variable as the subject. If aMemberVariable
  77. * is set, aSubject must be the value of the member variable for the
  78. * result. The supplied binding values aBindingValues must be values
  79. * using this binding set (that is aBindingValues->GetBindingSet() == this)
  80. *
  81. * @param aSubject subject of the RDF triple
  82. * @param aPredicate predicate of the RDF triple
  83. * @param aTarget target of the RDF triple
  84. * @param aMemberVariable member variable for the query for the binding
  85. * @param aResult result to synchronize
  86. * @param aBindingValues the values for the bindings for the result
  87. */
  88. bool
  89. SyncAssignments(nsIRDFResource* aSubject,
  90. nsIRDFResource* aPredicate,
  91. nsIRDFNode* aTarget,
  92. nsIAtom* aMemberVariable,
  93. nsXULTemplateResultRDF* aResult,
  94. nsBindingValues& aBindingValues);
  95. /*
  96. * The query processor maintains a map of subjects to an array of results.
  97. * This is used such that when a new assertion is added to the RDF graph,
  98. * the results associated with the subject of that triple may be checked
  99. * to see if their bindings have changed. The AddDependencies method adds
  100. * these subject dependencies to the map.
  101. */
  102. void
  103. AddDependencies(nsIRDFResource* aSubject,
  104. nsXULTemplateResultRDF* aResult);
  105. /*
  106. * Remove the results from the dependencies map when results are deleted.
  107. */
  108. void
  109. RemoveDependencies(nsIRDFResource* aSubject,
  110. nsXULTemplateResultRDF* aResult);
  111. /*
  112. * The nsBindingValues classes stores an array of values, one for each
  113. * target symbol that could be set by the bindings in the set.
  114. * LookupTargetIndex determines the index into the array for a given
  115. * target symbol.
  116. */
  117. int32_t
  118. LookupTargetIndex(nsIAtom* aTargetVariable, RDFBinding** aBinding);
  119. };
  120. /*
  121. * A set of values of bindings. This object is used once per result.
  122. * This stores a reference to the binding set and an array of node values.
  123. * Since the binding set is used once per query and the values are
  124. * used once per result, we reduce size by only storing the value array's
  125. * length in the binding set. This is possible since the array is always
  126. * a fixed length for a particular binding set.
  127. *
  128. * XXX ndeakin We may want to revisit this later since it makes the code
  129. * more complicated.
  130. */
  131. class nsBindingValues
  132. {
  133. protected:
  134. // the binding set
  135. RefPtr<RDFBindingSet> mBindings;
  136. /*
  137. * A set of values for variable bindings. To look up a binding value,
  138. * scan through the binding set in mBindings for the right target atom.
  139. * Its index will correspond to the index in this array. The size of this
  140. * array is determined by the RDFBindingSet's Count().
  141. */
  142. nsCOMPtr<nsIRDFNode>* mValues;
  143. public:
  144. nsBindingValues()
  145. : mBindings(nullptr),
  146. mValues(nullptr)
  147. {
  148. MOZ_COUNT_CTOR(nsBindingValues);
  149. }
  150. ~nsBindingValues();
  151. /**
  152. * Clear the binding set, to be called when the nsBindingValues is deleted
  153. * or a new binding set is being set.
  154. */
  155. void ClearBindingSet();
  156. RDFBindingSet* GetBindingSet() { return mBindings; }
  157. /**
  158. * Set the binding set to use. This needs to be called once a rule matches
  159. * since it is then known which bindings will apply.
  160. */
  161. nsresult SetBindingSet(RDFBindingSet* aBindings);
  162. nsCOMPtr<nsIRDFNode>* ValuesArray() { return mValues; }
  163. /*
  164. * Retrieve the assignment for a particular variable
  165. */
  166. void
  167. GetAssignmentFor(nsXULTemplateResultRDF* aResult,
  168. nsIAtom* aVar,
  169. nsIRDFNode** aValue);
  170. /*
  171. * Remove depenedencies the bindings have on particular resources
  172. */
  173. void
  174. RemoveDependencies(nsIRDFResource* aSubject,
  175. nsXULTemplateResultRDF* aResult);
  176. };
  177. #endif // nsRDFBinding_h__