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/. */
5 
6 #ifndef txResultRecycler_h__
7 #define txResultRecycler_h__
8 
9 #include "nsCOMPtr.h"
10 #include "txStack.h"
11 
12 class txAExprResult;
13 class StringResult;
14 class txNodeSet;
15 class txXPathNode;
16 class NumberResult;
17 class BooleanResult;
18 
19 class txResultRecycler {
20  public:
21   txResultRecycler();
22   ~txResultRecycler();
23 
AddRef()24   void AddRef() {
25     ++mRefCnt;
26     NS_LOG_ADDREF(this, mRefCnt, "txResultRecycler", sizeof(*this));
27   }
Release()28   void Release() {
29     --mRefCnt;
30     NS_LOG_RELEASE(this, mRefCnt, "txResultRecycler");
31     if (mRefCnt == 0) {
32       mRefCnt = 1;  // stabilize
33       delete this;
34     }
35   }
36 
37   /**
38    * Returns an txAExprResult to this recycler for reuse.
39    * @param aResult  result to recycle
40    */
41   void recycle(txAExprResult* aResult);
42 
43   /**
44    * Functions to return results that will be fully used by the caller.
45    * Returns nullptr on out-of-memory and an inited result otherwise.
46    */
47   nsresult getStringResult(StringResult** aResult);
48   nsresult getStringResult(const nsAString& aValue, txAExprResult** aResult);
49   nsresult getNodeSet(txNodeSet** aResult);
50   nsresult getNodeSet(txNodeSet* aNodeSet, txNodeSet** aResult);
51   nsresult getNodeSet(const txXPathNode& aNode, txAExprResult** aResult);
52   nsresult getNumberResult(double aValue, txAExprResult** aResult);
53 
54   /**
55    * Functions to return a txAExprResult that is shared across several
56    * clients and must not be modified. Never returns nullptr.
57    */
58   void getEmptyStringResult(txAExprResult** aResult);
59   void getBoolResult(bool aValue, txAExprResult** aResult);
60 
61   /**
62    * Functions that return non-shared resultsobjects
63    */
64   nsresult getNonSharedNodeSet(txNodeSet* aNodeSet, txNodeSet** aResult);
65 
66  private:
67   nsAutoRefCnt mRefCnt;
68   txStack mStringResults;
69   txStack mNodeSetResults;
70   txStack mNumberResults;
71   RefPtr<StringResult> mEmptyStringResult;
72   RefPtr<BooleanResult> mTrueResult;
73   RefPtr<BooleanResult> mFalseResult;
74 };
75 
76 #endif  // txResultRecycler_h__
77