michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsTemplateMatch_h__ michael@0: #define nsTemplateMatch_h__ michael@0: michael@0: #include "nsIContent.h" michael@0: #include "nsIXULTemplateQueryProcessor.h" michael@0: #include "nsIXULTemplateResult.h" michael@0: #include "nsRuleNetwork.h" michael@0: michael@0: /** michael@0: * A match object, where each match object is associated with one result. michael@0: * There will be one match list for each unique id generated. However, since michael@0: * there are multiple querysets and each may generate results with the same michael@0: * id, they are all chained together in a linked list, ordered in the same michael@0: * order as the respective elements they were generated from. michael@0: * A match can be identified by the container and id. The id is retrievable michael@0: * from the result. michael@0: * michael@0: * Only one match per container and id pair is active at a time, but which michael@0: * match is active may change as new results are added or removed. When a michael@0: * match is active, content is generated for that match. michael@0: * michael@0: * Matches are stored and owned by the mMatchToMap hash in the template michael@0: * builder. michael@0: */ michael@0: michael@0: class nsTemplateRule; michael@0: class nsTemplateQuerySet; michael@0: michael@0: class nsTemplateMatch { michael@0: private: michael@0: // Hide so that only Create() and Destroy() can be used to michael@0: // allocate and deallocate from the heap michael@0: void* operator new(size_t) CPP_THROW_NEW { MOZ_ASSERT(0); return nullptr; } michael@0: void operator delete(void*, size_t) { MOZ_ASSERT(0); } michael@0: michael@0: public: michael@0: nsTemplateMatch(uint16_t aQuerySetPriority, michael@0: nsIXULTemplateResult* aResult, michael@0: nsIContent* aContainer) michael@0: : mRuleIndex(-1), michael@0: mQuerySetPriority(aQuerySetPriority), michael@0: mContainer(aContainer), michael@0: mResult(aResult), michael@0: mNext(nullptr) michael@0: { michael@0: MOZ_COUNT_CTOR(nsTemplateMatch); michael@0: } michael@0: michael@0: ~nsTemplateMatch() michael@0: { michael@0: MOZ_COUNT_DTOR(nsTemplateMatch); michael@0: } michael@0: michael@0: static nsTemplateMatch* michael@0: Create(uint16_t aQuerySetPriority, michael@0: nsIXULTemplateResult* aResult, michael@0: nsIContent* aContainer) { michael@0: return ::new nsTemplateMatch(aQuerySetPriority, aResult, aContainer); michael@0: } michael@0: michael@0: static void Destroy(nsTemplateMatch*& aMatch, bool aRemoveResult); michael@0: michael@0: // return true if the the match is active, and has generated output michael@0: bool IsActive() { michael@0: return mRuleIndex >= 0; michael@0: } michael@0: michael@0: // indicate that a rule is no longer active, used when a query with a michael@0: // lower priority has overriden the match michael@0: void SetInactive() { michael@0: mRuleIndex = -1; michael@0: } michael@0: michael@0: // return matching rule index michael@0: int16_t RuleIndex() { michael@0: return mRuleIndex; michael@0: } michael@0: michael@0: // return priority of query set michael@0: uint16_t QuerySetPriority() { michael@0: return mQuerySetPriority; michael@0: } michael@0: michael@0: // return container, not addrefed. May be null. michael@0: nsIContent* GetContainer() { michael@0: return mContainer; michael@0: } michael@0: michael@0: nsresult RuleMatched(nsTemplateQuerySet* aQuerySet, michael@0: nsTemplateRule* aRule, michael@0: int16_t aRuleIndex, michael@0: nsIXULTemplateResult* aResult); michael@0: michael@0: private: michael@0: michael@0: /** michael@0: * The index of the rule that matched, or -1 if the match is not active. michael@0: */ michael@0: int16_t mRuleIndex; michael@0: michael@0: /** michael@0: * The priority of the queryset for this rule michael@0: */ michael@0: uint16_t mQuerySetPriority; michael@0: michael@0: /** michael@0: * The container the content generated for the match is inside. michael@0: */ michael@0: nsCOMPtr mContainer; michael@0: michael@0: public: michael@0: michael@0: /** michael@0: * The result associated with this match michael@0: */ michael@0: nsCOMPtr mResult; michael@0: michael@0: /** michael@0: * Matches are stored in a linked list, in priority order. This first michael@0: * match that has a rule set (mRule) is the active match and generates michael@0: * content. The next match is owned by the builder, which will delete michael@0: * template matches when needed. michael@0: */ michael@0: nsTemplateMatch *mNext; michael@0: michael@0: private: michael@0: michael@0: nsTemplateMatch(const nsTemplateMatch& aMatch); // not to be implemented michael@0: void operator=(const nsTemplateMatch& aMatch); // not to be implemented michael@0: }; michael@0: michael@0: #endif // nsTemplateMatch_h__ michael@0: