summaryrefslogtreecommitdiffstats
path: root/dom/xslt/xpath/txResultRecycler.cpp
blob: b52f91838a4d987caeeb4c3b8318e67b1e73be7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "txResultRecycler.h"
#include "txExprResult.h"
#include "txNodeSet.h"

txResultRecycler::txResultRecycler()
    : mEmptyStringResult(new StringResult(nullptr)),
      mTrueResult(new BooleanResult(true)),
      mFalseResult(new BooleanResult(false)) {}

txResultRecycler::~txResultRecycler() {
  txStackIterator stringIter(&mStringResults);
  while (stringIter.hasNext()) {
    delete static_cast<StringResult*>(stringIter.next());
  }
  txStackIterator nodesetIter(&mNodeSetResults);
  while (nodesetIter.hasNext()) {
    delete static_cast<txNodeSet*>(nodesetIter.next());
  }
  txStackIterator numberIter(&mNumberResults);
  while (numberIter.hasNext()) {
    delete static_cast<NumberResult*>(numberIter.next());
  }
}

void txResultRecycler::recycle(txAExprResult* aResult) {
  NS_ASSERTION(aResult->mRefCnt == 0, "In-use txAExprResult recycled");
  RefPtr<txResultRecycler> kungFuDeathGrip;
  aResult->mRecycler.swap(kungFuDeathGrip);

  switch (aResult->getResultType()) {
    case txAExprResult::STRING: {
      mStringResults.push(static_cast<StringResult*>(aResult));
      return;
    }
    case txAExprResult::NODESET: {
      static_cast<txNodeSet*>(aResult)->clear();
      mNodeSetResults.push(static_cast<txNodeSet*>(aResult));
      return;
    }
    case txAExprResult::NUMBER: {
      mNumberResults.push(static_cast<NumberResult*>(aResult));
      return;
    }
    default: {
      delete aResult;
    }
  }
}

nsresult txResultRecycler::getStringResult(StringResult** aResult) {
  if (mStringResults.isEmpty()) {
    *aResult = new StringResult(this);
  } else {
    *aResult = static_cast<StringResult*>(mStringResults.pop());
    (*aResult)->mValue.Truncate();
    (*aResult)->mRecycler = this;
  }
  NS_ADDREF(*aResult);

  return NS_OK;
}

nsresult txResultRecycler::getStringResult(const nsAString& aValue,
                                           txAExprResult** aResult) {
  if (mStringResults.isEmpty()) {
    *aResult = new StringResult(aValue, this);
  } else {
    StringResult* strRes = static_cast<StringResult*>(mStringResults.pop());
    strRes->mValue = aValue;
    strRes->mRecycler = this;
    *aResult = strRes;
  }
  NS_ADDREF(*aResult);

  return NS_OK;
}

void txResultRecycler::getEmptyStringResult(txAExprResult** aResult) {
  *aResult = mEmptyStringResult;
  NS_ADDREF(*aResult);
}

nsresult txResultRecycler::getNodeSet(txNodeSet** aResult) {
  if (mNodeSetResults.isEmpty()) {
    *aResult = new txNodeSet(this);
  } else {
    *aResult = static_cast<txNodeSet*>(mNodeSetResults.pop());
    (*aResult)->mRecycler = this;
  }
  NS_ADDREF(*aResult);

  return NS_OK;
}

nsresult txResultRecycler::getNodeSet(txNodeSet* aNodeSet,
                                      txNodeSet** aResult) {
  if (mNodeSetResults.isEmpty()) {
    *aResult = new txNodeSet(*aNodeSet, this);
  } else {
    *aResult = static_cast<txNodeSet*>(mNodeSetResults.pop());
    (*aResult)->append(*aNodeSet);
    (*aResult)->mRecycler = this;
  }
  NS_ADDREF(*aResult);

  return NS_OK;
}

nsresult txResultRecycler::getNodeSet(const txXPathNode& aNode,
                                      txAExprResult** aResult) {
  if (mNodeSetResults.isEmpty()) {
    *aResult = new txNodeSet(aNode, this);
  } else {
    txNodeSet* nodes = static_cast<txNodeSet*>(mNodeSetResults.pop());
    nodes->append(aNode);
    nodes->mRecycler = this;
    *aResult = nodes;
  }
  NS_ADDREF(*aResult);

  return NS_OK;
}

nsresult txResultRecycler::getNumberResult(double aValue,
                                           txAExprResult** aResult) {
  if (mNumberResults.isEmpty()) {
    *aResult = new NumberResult(aValue, this);
  } else {
    NumberResult* numRes = static_cast<NumberResult*>(mNumberResults.pop());
    numRes->value = aValue;
    numRes->mRecycler = this;
    *aResult = numRes;
  }
  NS_ADDREF(*aResult);

  return NS_OK;
}

void txResultRecycler::getBoolResult(bool aValue, txAExprResult** aResult) {
  *aResult = aValue ? mTrueResult : mFalseResult;
  NS_ADDREF(*aResult);
}

nsresult txResultRecycler::getNonSharedNodeSet(txNodeSet* aNodeSet,
                                               txNodeSet** aResult) {
  if (aNodeSet->mRefCnt > 1) {
    return getNodeSet(aNodeSet, aResult);
  }

  *aResult = aNodeSet;
  NS_ADDREF(*aResult);

  return NS_OK;
}

void txAExprResult::Release() {
  --mRefCnt;
  NS_LOG_RELEASE(this, mRefCnt, "txAExprResult");
  if (mRefCnt == 0) {
    if (mRecycler) {
      mRecycler->recycle(this);
    } else {
      delete this;
    }
  }
}